blob: cdd59c1cdbc0f752653ac6467f4e801a5f43b5c2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkDraw.h"
11#include "SkBlitter.h"
12#include "SkBounder.h"
13#include "SkCanvas.h"
14#include "SkColorPriv.h"
15#include "SkDevice.h"
bungeman@google.com2211b622012-01-13 15:02:58 +000016#include "SkFixed.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkMaskFilter.h"
18#include "SkPaint.h"
19#include "SkPathEffect.h"
reed@google.com045e62d2011-10-24 12:19:46 +000020#include "SkRasterClip.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkRasterizer.h"
22#include "SkScan.h"
23#include "SkShader.h"
24#include "SkStroke.h"
25#include "SkTemplatesPriv.h"
reed@google.com32e5d972011-07-27 18:25:57 +000026#include "SkTLazy.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027#include "SkUtils.h"
28
29#include "SkAutoKern.h"
30#include "SkBitmapProcShader.h"
31#include "SkDrawProcs.h"
32
33//#define TRACE_BITMAP_DRAWS
34
reed@android.com8a1c16f2008-12-17 15:59:43 +000035#define kBlitterStorageLongCount (sizeof(SkBitmapProcShader) >> 2)
36
reed@google.comfd4236e2011-07-25 21:16:22 +000037/** Helper for allocating small blitters on the stack.
38 */
reed@google.com40c2ba22011-07-25 19:41:22 +000039class SkAutoBlitterChoose : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040public:
reed@google.com1d6ee0b2011-07-05 17:44:56 +000041 SkAutoBlitterChoose() {
42 fBlitter = NULL;
43 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 SkAutoBlitterChoose(const SkBitmap& device, const SkMatrix& matrix,
45 const SkPaint& paint) {
46 fBlitter = SkBlitter::Choose(device, matrix, paint,
47 fStorage, sizeof(fStorage));
48 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000049
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 ~SkAutoBlitterChoose();
51
52 SkBlitter* operator->() { return fBlitter; }
53 SkBlitter* get() const { return fBlitter; }
54
reed@google.com1d6ee0b2011-07-05 17:44:56 +000055 void choose(const SkBitmap& device, const SkMatrix& matrix,
56 const SkPaint& paint) {
57 SkASSERT(!fBlitter);
58 fBlitter = SkBlitter::Choose(device, matrix, paint,
59 fStorage, sizeof(fStorage));
60 }
61
reed@android.com8a1c16f2008-12-17 15:59:43 +000062private:
63 SkBlitter* fBlitter;
64 uint32_t fStorage[kBlitterStorageLongCount];
65};
66
67SkAutoBlitterChoose::~SkAutoBlitterChoose() {
68 if ((void*)fBlitter == (void*)fStorage) {
69 fBlitter->~SkBlitter();
70 } else {
71 SkDELETE(fBlitter);
72 }
73}
74
reed@google.com40c2ba22011-07-25 19:41:22 +000075/**
76 * Since we are providing the storage for the shader (to avoid the perf cost
77 * of calling new) we insist that in our destructor we can account for all
78 * owners of the shader.
79 */
80class SkAutoBitmapShaderInstall : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081public:
reed@google.com40c2ba22011-07-25 19:41:22 +000082 SkAutoBitmapShaderInstall(const SkBitmap& src, const SkPaint& paint)
83 : fPaint(paint) /* makes a copy of the paint */ {
84 fPaint.setShader(SkShader::CreateBitmapShader(src,
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
86 fStorage, sizeof(fStorage)));
reed@google.com40c2ba22011-07-25 19:41:22 +000087 // we deliberately left the shader with an owner-count of 2
88 SkASSERT(2 == fPaint.getShader()->getRefCnt());
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 }
reed@google.com82065d62011-02-07 15:30:46 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 ~SkAutoBitmapShaderInstall() {
reed@google.com40c2ba22011-07-25 19:41:22 +000092 SkShader* shader = fPaint.getShader();
93 // since we manually destroy shader, we insist that owners == 2
94 SkASSERT(2 == shader->getRefCnt());
reed@android.com8a1c16f2008-12-17 15:59:43 +000095
reed@google.com40c2ba22011-07-25 19:41:22 +000096 fPaint.setShader(NULL); // unref the shader by 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000097
reed@google.com40c2ba22011-07-25 19:41:22 +000098 // now destroy to take care of the 2nd owner-count
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 if ((void*)shader == (void*)fStorage) {
100 shader->~SkShader();
101 } else {
102 SkDELETE(shader);
103 }
104 }
reed@google.com82065d62011-02-07 15:30:46 +0000105
reed@google.com40c2ba22011-07-25 19:41:22 +0000106 // return the new paint that has the shader applied
107 const SkPaint& paintWithShader() const { return fPaint; }
108
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109private:
reed@google.com40c2ba22011-07-25 19:41:22 +0000110 SkPaint fPaint; // copy of caller's paint (which we then modify)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 uint32_t fStorage[kBlitterStorageLongCount];
112};
113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114///////////////////////////////////////////////////////////////////////////////
115
reed@android.comf2b98d62010-12-20 18:26:13 +0000116SkDraw::SkDraw() {
117 sk_bzero(this, sizeof(*this));
118}
119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120SkDraw::SkDraw(const SkDraw& src) {
121 memcpy(this, &src, sizeof(*this));
122}
123
124///////////////////////////////////////////////////////////////////////////////
125
126typedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data);
127
128static void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) {
reed@android.com4516f472009-06-29 16:25:36 +0000129 sk_bzero(pixels, bytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130}
131
132static void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {}
133
134static void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
135 sk_memset32((uint32_t*)pixels, data, bytes >> 2);
136}
137
138static void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
139 sk_memset16((uint16_t*)pixels, data, bytes >> 1);
140}
141
142static void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
143 memset(pixels, data, bytes);
144}
145
146static BitmapXferProc ChooseBitmapXferProc(const SkBitmap& bitmap,
147 const SkPaint& paint,
148 uint32_t* data) {
149 // todo: we can apply colorfilter up front if no shader, so we wouldn't
150 // need to abort this fastpath
151 if (paint.getShader() || paint.getColorFilter()) {
152 return NULL;
153 }
154
reed@android.com845fdac2009-06-23 03:01:32 +0000155 SkXfermode::Mode mode;
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +0000156 if (!SkXfermode::AsMode(paint.getXfermode(), &mode)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 return NULL;
158 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 SkColor color = paint.getColor();
reed@google.coma76de3d2011-01-13 18:30:42 +0000161
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 // collaps modes based on color...
reed@android.com845fdac2009-06-23 03:01:32 +0000163 if (SkXfermode::kSrcOver_Mode == mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 unsigned alpha = SkColorGetA(color);
165 if (0 == alpha) {
reed@android.com845fdac2009-06-23 03:01:32 +0000166 mode = SkXfermode::kDst_Mode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 } else if (0xFF == alpha) {
reed@android.com845fdac2009-06-23 03:01:32 +0000168 mode = SkXfermode::kSrc_Mode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 }
170 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000171
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 switch (mode) {
reed@android.com845fdac2009-06-23 03:01:32 +0000173 case SkXfermode::kClear_Mode:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174// SkDebugf("--- D_Clear_BitmapXferProc\n");
175 return D_Clear_BitmapXferProc; // ignore data
reed@android.com845fdac2009-06-23 03:01:32 +0000176 case SkXfermode::kDst_Mode:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177// SkDebugf("--- D_Dst_BitmapXferProc\n");
178 return D_Dst_BitmapXferProc; // ignore data
reed@android.com845fdac2009-06-23 03:01:32 +0000179 case SkXfermode::kSrc_Mode: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 /*
reed@google.coma76de3d2011-01-13 18:30:42 +0000181 should I worry about dithering for the lower depths?
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 */
183 SkPMColor pmc = SkPreMultiplyColor(color);
184 switch (bitmap.config()) {
185 case SkBitmap::kARGB_8888_Config:
186 if (data) {
187 *data = pmc;
188 }
189// SkDebugf("--- D32_Src_BitmapXferProc\n");
190 return D32_Src_BitmapXferProc;
191 case SkBitmap::kARGB_4444_Config:
192 if (data) {
193 *data = SkPixel32ToPixel4444(pmc);
194 }
195// SkDebugf("--- D16_Src_BitmapXferProc\n");
196 return D16_Src_BitmapXferProc;
197 case SkBitmap::kRGB_565_Config:
198 if (data) {
199 *data = SkPixel32ToPixel16(pmc);
200 }
201// SkDebugf("--- D16_Src_BitmapXferProc\n");
202 return D16_Src_BitmapXferProc;
203 case SkBitmap::kA8_Config:
204 if (data) {
205 *data = SkGetPackedA32(pmc);
206 }
207// SkDebugf("--- DA8_Src_BitmapXferProc\n");
208 return DA8_Src_BitmapXferProc;
209 default:
210 break;
211 }
212 break;
213 }
214 default:
215 break;
216 }
217 return NULL;
218}
219
220static void CallBitmapXferProc(const SkBitmap& bitmap, const SkIRect& rect,
221 BitmapXferProc proc, uint32_t procData) {
222 int shiftPerPixel;
223 switch (bitmap.config()) {
224 case SkBitmap::kARGB_8888_Config:
225 shiftPerPixel = 2;
226 break;
227 case SkBitmap::kARGB_4444_Config:
228 case SkBitmap::kRGB_565_Config:
229 shiftPerPixel = 1;
230 break;
231 case SkBitmap::kA8_Config:
232 shiftPerPixel = 0;
233 break;
234 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000235 SkDEBUGFAIL("Can't use xferproc on this config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 return;
237 }
238
239 uint8_t* pixels = (uint8_t*)bitmap.getPixels();
240 SkASSERT(pixels);
241 const size_t rowBytes = bitmap.rowBytes();
242 const int widthBytes = rect.width() << shiftPerPixel;
243
244 // skip down to the first scanline and X position
245 pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel);
246 for (int scans = rect.height() - 1; scans >= 0; --scans) {
247 proc(pixels, widthBytes, procData);
248 pixels += rowBytes;
249 }
250}
251
252void SkDraw::drawPaint(const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000253 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254
reed@google.com045e62d2011-10-24 12:19:46 +0000255 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 return;
257 }
258
259 SkIRect devRect;
260 devRect.set(0, 0, fBitmap->width(), fBitmap->height());
261 if (fBounder && !fBounder->doIRect(devRect)) {
262 return;
263 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000264
reed@google.com045e62d2011-10-24 12:19:46 +0000265 if (fRC->isBW()) {
266 /* If we don't have a shader (i.e. we're just a solid color) we may
267 be faster to operate directly on the device bitmap, rather than invoking
268 a blitter. Esp. true for xfermodes, which require a colorshader to be
269 present, which is just redundant work. Since we're drawing everywhere
270 in the clip, we don't have to worry about antialiasing.
271 */
272 uint32_t procData = 0; // to avoid the warning
273 BitmapXferProc proc = ChooseBitmapXferProc(*fBitmap, paint, &procData);
274 if (proc) {
275 if (D_Dst_BitmapXferProc == proc) { // nothing to do
276 return;
277 }
278
279 SkRegion::Iterator iter(fRC->bwRgn());
280 while (!iter.done()) {
281 CallBitmapXferProc(*fBitmap, iter.rect(), proc, procData);
282 iter.next();
283 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 return;
285 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 }
reed@google.com045e62d2011-10-24 12:19:46 +0000287
288 // normal case: use a blitter
289 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
290 SkScan::FillIRect(devRect, *fRC, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291}
292
293///////////////////////////////////////////////////////////////////////////////
294
295struct PtProcRec {
296 SkCanvas::PointMode fMode;
297 const SkPaint* fPaint;
298 const SkRegion* fClip;
reed@google.com045e62d2011-10-24 12:19:46 +0000299 const SkRasterClip* fRC;
reed@google.coma76de3d2011-01-13 18:30:42 +0000300
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301 // computed values
302 SkFixed fRadius;
reed@google.coma76de3d2011-01-13 18:30:42 +0000303
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304 typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count,
305 SkBlitter*);
306
307 bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix,
reed@google.com045e62d2011-10-24 12:19:46 +0000308 const SkRasterClip*);
309 Proc chooseProc(SkBlitter** blitter);
310
311private:
312 SkAAClipBlitterWrapper fWrapper;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000313};
314
315static void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
316 int count, SkBlitter* blitter) {
317 SkASSERT(rec.fClip->isRect());
318 const SkIRect& r = rec.fClip->getBounds();
reed@google.coma76de3d2011-01-13 18:30:42 +0000319
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320 for (int i = 0; i < count; i++) {
321 int x = SkScalarFloor(devPts[i].fX);
322 int y = SkScalarFloor(devPts[i].fY);
323 if (r.contains(x, y)) {
324 blitter->blitH(x, y, 1);
325 }
326 }
327}
328
329static void bw_pt_rect_16_hair_proc(const PtProcRec& rec,
330 const SkPoint devPts[], int count,
331 SkBlitter* blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +0000332 SkASSERT(rec.fRC->isRect());
333 const SkIRect& r = rec.fRC->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334 uint32_t value;
335 const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
336 SkASSERT(bitmap);
reed@google.coma76de3d2011-01-13 18:30:42 +0000337
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338 uint16_t* addr = bitmap->getAddr16(0, 0);
339 int rb = bitmap->rowBytes();
340
341 for (int i = 0; i < count; i++) {
342 int x = SkScalarFloor(devPts[i].fX);
343 int y = SkScalarFloor(devPts[i].fY);
344 if (r.contains(x, y)) {
345// *bitmap->getAddr16(x, y) = SkToU16(value);
346 ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value);
347 }
348 }
349}
350
351static void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
352 int count, SkBlitter* blitter) {
353 for (int i = 0; i < count; i++) {
354 int x = SkScalarFloor(devPts[i].fX);
355 int y = SkScalarFloor(devPts[i].fY);
356 if (rec.fClip->contains(x, y)) {
357 blitter->blitH(x, y, 1);
358 }
359 }
360}
361
362static void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
363 int count, SkBlitter* blitter) {
364 for (int i = 0; i < count; i += 2) {
reed@google.com045e62d2011-10-24 12:19:46 +0000365 SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 }
367}
368
369static void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
370 int count, SkBlitter* blitter) {
371 for (int i = 0; i < count - 1; i++) {
reed@google.com045e62d2011-10-24 12:19:46 +0000372 SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 }
374}
375
376// aa versions
377
378static void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
379 int count, SkBlitter* blitter) {
380 for (int i = 0; i < count; i += 2) {
reed@google.com045e62d2011-10-24 12:19:46 +0000381 SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000382 }
383}
384
385static void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
386 int count, SkBlitter* blitter) {
387 for (int i = 0; i < count - 1; i++) {
reed@google.com045e62d2011-10-24 12:19:46 +0000388 SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389 }
390}
391
392// square procs (strokeWidth > 0 but matrix is square-scale (sx == sy)
393
394static void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[],
395 int count, SkBlitter* blitter) {
396 const SkFixed radius = rec.fRadius;
397 for (int i = 0; i < count; i++) {
398 SkFixed x = SkScalarToFixed(devPts[i].fX);
399 SkFixed y = SkScalarToFixed(devPts[i].fY);
reed@google.coma76de3d2011-01-13 18:30:42 +0000400
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 SkXRect r;
402 r.fLeft = x - radius;
403 r.fTop = y - radius;
404 r.fRight = x + radius;
405 r.fBottom = y + radius;
reed@google.coma76de3d2011-01-13 18:30:42 +0000406
reed@google.com045e62d2011-10-24 12:19:46 +0000407 SkScan::FillXRect(r, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000408 }
409}
410
411static void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[],
412 int count, SkBlitter* blitter) {
413 const SkFixed radius = rec.fRadius;
414 for (int i = 0; i < count; i++) {
415 SkFixed x = SkScalarToFixed(devPts[i].fX);
416 SkFixed y = SkScalarToFixed(devPts[i].fY);
reed@google.coma76de3d2011-01-13 18:30:42 +0000417
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418 SkXRect r;
419 r.fLeft = x - radius;
420 r.fTop = y - radius;
421 r.fRight = x + radius;
422 r.fBottom = y + radius;
reed@google.coma76de3d2011-01-13 18:30:42 +0000423
reed@google.com045e62d2011-10-24 12:19:46 +0000424 SkScan::AntiFillXRect(r, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425 }
426}
427
reed@android.comb4f404a2009-07-10 17:02:17 +0000428// If this guy returns true, then chooseProc() must return a valid proc
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429bool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint,
reed@google.com045e62d2011-10-24 12:19:46 +0000430 const SkMatrix* matrix, const SkRasterClip* rc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000431 if (paint.getPathEffect()) {
432 return false;
433 }
434 SkScalar width = paint.getStrokeWidth();
435 if (0 == width) {
436 fMode = mode;
437 fPaint = &paint;
reed@google.com045e62d2011-10-24 12:19:46 +0000438 fClip = NULL;
439 fRC = rc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440 fRadius = SK_Fixed1 >> 1;
441 return true;
442 }
reed@android.comb4f404a2009-07-10 17:02:17 +0000443 if (paint.getStrokeCap() != SkPaint::kRound_Cap &&
444 matrix->rectStaysRect() && SkCanvas::kPoints_PointMode == mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000445 SkScalar sx = matrix->get(SkMatrix::kMScaleX);
446 SkScalar sy = matrix->get(SkMatrix::kMScaleY);
447 if (SkScalarNearlyZero(sx - sy)) {
448 if (sx < 0) {
449 sx = -sx;
450 }
451
452 fMode = mode;
453 fPaint = &paint;
reed@google.com045e62d2011-10-24 12:19:46 +0000454 fClip = NULL;
455 fRC = rc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000456 fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1;
457 return true;
458 }
459 }
460 return false;
461}
462
reed@google.com045e62d2011-10-24 12:19:46 +0000463PtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) {
reed@android.comb4f404a2009-07-10 17:02:17 +0000464 Proc proc = NULL;
reed@google.coma76de3d2011-01-13 18:30:42 +0000465
reed@google.com045e62d2011-10-24 12:19:46 +0000466 SkBlitter* blitter = *blitterPtr;
467 if (fRC->isBW()) {
468 fClip = &fRC->bwRgn();
469 } else {
470 fWrapper.init(*fRC, blitter);
471 fClip = &fWrapper.getRgn();
472 blitter = fWrapper.getBlitter();
473 *blitterPtr = blitter;
474 }
475
reed@android.com8a1c16f2008-12-17 15:59:43 +0000476 // for our arrays
477 SkASSERT(0 == SkCanvas::kPoints_PointMode);
478 SkASSERT(1 == SkCanvas::kLines_PointMode);
479 SkASSERT(2 == SkCanvas::kPolygon_PointMode);
480 SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode);
481
482 // first check for hairlines
483 if (0 == fPaint->getStrokeWidth()) {
484 if (fPaint->isAntiAlias()) {
485 static const Proc gAAProcs[] = {
486 aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc
487 };
488 proc = gAAProcs[fMode];
489 } else {
490 if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) {
491 uint32_t value;
492 const SkBitmap* bm = blitter->justAnOpaqueColor(&value);
493 if (bm && bm->config() == SkBitmap::kRGB_565_Config) {
494 proc = bw_pt_rect_16_hair_proc;
495 } else {
496 proc = bw_pt_rect_hair_proc;
497 }
498 } else {
499 static Proc gBWProcs[] = {
500 bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc
501 };
502 proc = gBWProcs[fMode];
503 }
504 }
reed@android.comb4f404a2009-07-10 17:02:17 +0000505 } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000506 SkASSERT(SkCanvas::kPoints_PointMode == fMode);
507 if (fPaint->isAntiAlias()) {
508 proc = aa_square_proc;
509 } else {
510 proc = bw_square_proc;
511 }
512 }
513 return proc;
514}
515
516static bool bounder_points(SkBounder* bounder, SkCanvas::PointMode mode,
517 size_t count, const SkPoint pts[],
518 const SkPaint& paint, const SkMatrix& matrix) {
519 SkIRect ibounds;
520 SkRect bounds;
521 SkScalar inset = paint.getStrokeWidth();
522
523 bounds.set(pts, count);
524 bounds.inset(-inset, -inset);
525 matrix.mapRect(&bounds);
526
527 bounds.roundOut(&ibounds);
528 return bounder->doIRect(ibounds);
529}
530
531// each of these costs 8-bytes of stack space, so don't make it too large
532// must be even for lines/polygon to work
533#define MAX_DEV_PTS 32
534
535void SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count,
reed@android.comf2b98d62010-12-20 18:26:13 +0000536 const SkPoint pts[], const SkPaint& paint,
537 bool forceUseDevice) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000538 // if we're in lines mode, force count to be even
539 if (SkCanvas::kLines_PointMode == mode) {
540 count &= ~(size_t)1;
541 }
542
543 if ((long)count <= 0) {
544 return;
545 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000546
reed@android.com8a1c16f2008-12-17 15:59:43 +0000547 SkASSERT(pts != NULL);
reed@android.comf2b98d62010-12-20 18:26:13 +0000548 SkDEBUGCODE(this->validate();)
reed@google.coma76de3d2011-01-13 18:30:42 +0000549
reed@android.com8a1c16f2008-12-17 15:59:43 +0000550 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +0000551 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000552 return;
553 }
554
reed@google.comfd4236e2011-07-25 21:16:22 +0000555 if (fBounder) {
556 if (!bounder_points(fBounder, mode, count, pts, paint, *fMatrix)) {
557 return;
558 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000559
reed@google.comfd4236e2011-07-25 21:16:22 +0000560 // clear the bounder and call this again, so we don't invoke the bounder
561 // later if we happen to call ourselves for drawRect, drawPath, etc.
562 SkDraw noBounder(*this);
563 noBounder.fBounder = NULL;
564 noBounder.drawPoints(mode, count, pts, paint, forceUseDevice);
565 return;
566 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000567
reed@android.com8a1c16f2008-12-17 15:59:43 +0000568 PtProcRec rec;
reed@google.com045e62d2011-10-24 12:19:46 +0000569 if (!forceUseDevice && rec.init(mode, paint, fMatrix, fRC)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000570 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
571
572 SkPoint devPts[MAX_DEV_PTS];
573 const SkMatrix* matrix = fMatrix;
574 SkBlitter* bltr = blitter.get();
reed@google.com045e62d2011-10-24 12:19:46 +0000575 PtProcRec::Proc proc = rec.chooseProc(&bltr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000576 // we have to back up subsequent passes if we're in polygon mode
577 const size_t backup = (SkCanvas::kPolygon_PointMode == mode);
reed@google.coma76de3d2011-01-13 18:30:42 +0000578
reed@android.com8a1c16f2008-12-17 15:59:43 +0000579 do {
580 size_t n = count;
581 if (n > MAX_DEV_PTS) {
582 n = MAX_DEV_PTS;
583 }
584 matrix->mapPoints(devPts, pts, n);
585 proc(rec, devPts, n, bltr);
586 pts += n - backup;
587 SkASSERT(count >= n);
588 count -= n;
589 if (count > 0) {
590 count += backup;
591 }
592 } while (count != 0);
593 } else {
594 switch (mode) {
595 case SkCanvas::kPoints_PointMode: {
596 // temporarily mark the paint as filling.
reed@google.com40c2ba22011-07-25 19:41:22 +0000597 SkPaint newPaint(paint);
598 newPaint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599
reed@google.com40c2ba22011-07-25 19:41:22 +0000600 SkScalar width = newPaint.getStrokeWidth();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000601 SkScalar radius = SkScalarHalf(width);
reed@google.coma76de3d2011-01-13 18:30:42 +0000602
reed@google.com40c2ba22011-07-25 19:41:22 +0000603 if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000604 SkPath path;
605 SkMatrix preMatrix;
reed@google.coma76de3d2011-01-13 18:30:42 +0000606
reed@android.com8a1c16f2008-12-17 15:59:43 +0000607 path.addCircle(0, 0, radius);
608 for (size_t i = 0; i < count; i++) {
609 preMatrix.setTranslate(pts[i].fX, pts[i].fY);
610 // pass true for the last point, since we can modify
611 // then path then
reed@android.comf2b98d62010-12-20 18:26:13 +0000612 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000613 fDevice->drawPath(*this, path, newPaint, &preMatrix,
reed@android.comf2b98d62010-12-20 18:26:13 +0000614 (count-1) == i);
615 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000616 this->drawPath(path, newPaint, &preMatrix,
617 (count-1) == i);
reed@android.comf2b98d62010-12-20 18:26:13 +0000618 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000619 }
620 } else {
621 SkRect r;
reed@google.coma76de3d2011-01-13 18:30:42 +0000622
reed@android.com8a1c16f2008-12-17 15:59:43 +0000623 for (size_t i = 0; i < count; i++) {
624 r.fLeft = pts[i].fX - radius;
625 r.fTop = pts[i].fY - radius;
626 r.fRight = r.fLeft + width;
627 r.fBottom = r.fTop + width;
reed@android.comf2b98d62010-12-20 18:26:13 +0000628 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000629 fDevice->drawRect(*this, r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000630 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000631 this->drawRect(r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000632 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000633 }
634 }
635 break;
636 }
637 case SkCanvas::kLines_PointMode:
638 case SkCanvas::kPolygon_PointMode: {
639 count -= 1;
640 SkPath path;
641 SkPaint p(paint);
642 p.setStyle(SkPaint::kStroke_Style);
643 size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
644 for (size_t i = 0; i < count; i += inc) {
645 path.moveTo(pts[i]);
646 path.lineTo(pts[i+1]);
reed@android.comf2b98d62010-12-20 18:26:13 +0000647 if (fDevice) {
648 fDevice->drawPath(*this, path, p, NULL, true);
649 } else {
650 this->drawPath(path, p, NULL, true);
651 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000652 path.rewind();
653 }
654 break;
655 }
656 }
657 }
658}
659
660static inline SkPoint* as_lefttop(SkRect* r) {
661 return (SkPoint*)(void*)r;
662}
663
664static inline SkPoint* as_rightbottom(SkRect* r) {
665 return ((SkPoint*)(void*)r) + 1;
666}
667
reed@google.com761fb622011-04-04 18:58:05 +0000668static bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix,
669 SkPoint* strokeSize) {
670 if (SkPaint::kMiter_Join != paint.getStrokeJoin() ||
671 paint.getStrokeMiter() < SK_ScalarSqrt2) {
672 return false;
673 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000674
reed@google.com761fb622011-04-04 18:58:05 +0000675 SkASSERT(matrix.rectStaysRect());
676 SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() };
677 matrix.mapVectors(strokeSize, &pt, 1);
reed@google.com61153382011-04-05 13:05:18 +0000678 strokeSize->fX = SkScalarAbs(strokeSize->fX);
679 strokeSize->fY = SkScalarAbs(strokeSize->fY);
reed@google.com761fb622011-04-04 18:58:05 +0000680 return true;
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000681}
682
reed@google.com62ab7ad2011-04-05 14:08:25 +0000683SkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint,
684 const SkMatrix& matrix,
685 SkPoint* strokeSize) {
686 RectType rtype;
687 const SkScalar width = paint.getStrokeWidth();
688 const bool zeroWidth = (0 == width);
689 SkPaint::Style style = paint.getStyle();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000690
reed@google.com62ab7ad2011-04-05 14:08:25 +0000691 if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) {
692 style = SkPaint::kFill_Style;
693 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000694
reed@google.com62ab7ad2011-04-05 14:08:25 +0000695 if (paint.getPathEffect() || paint.getMaskFilter() ||
696 paint.getRasterizer() || !matrix.rectStaysRect() ||
697 SkPaint::kStrokeAndFill_Style == style) {
698 rtype = kPath_RectType;
699 } else if (SkPaint::kFill_Style == style) {
700 rtype = kFill_RectType;
701 } else if (zeroWidth) {
702 rtype = kHair_RectType;
703 } else if (easy_rect_join(paint, matrix, strokeSize)) {
704 rtype = kStroke_RectType;
705 } else {
706 rtype = kPath_RectType;
707 }
708 return rtype;
709}
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000710
reed@google.com73244152012-05-11 14:35:23 +0000711static const SkPoint* rect_points(const SkRect& r) {
712 return (const SkPoint*)(void*)&r;
713}
714
715static SkPoint* rect_points(SkRect& r) {
716 return (SkPoint*)(void*)&r;
reed@google.com40c2ba22011-07-25 19:41:22 +0000717}
718
reed@android.com8a1c16f2008-12-17 15:59:43 +0000719void SkDraw::drawRect(const SkRect& rect, const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000720 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000721
722 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +0000723 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000724 return;
725 }
726
reed@google.com761fb622011-04-04 18:58:05 +0000727 SkPoint strokeSize;
reed@google.com62ab7ad2011-04-05 14:08:25 +0000728 RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize);
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000729
reed@google.com5bff8d22011-04-04 14:47:09 +0000730#ifdef SK_DISABLE_FAST_AA_STROKE_RECT
reed@google.com62ab7ad2011-04-05 14:08:25 +0000731 if (kStroke_RectType == rtype && paint.isAntiAlias()) {
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000732 rtype = kPath_RectType;
733 }
reed@google.com62ab7ad2011-04-05 14:08:25 +0000734#endif
reed@google.com045e62d2011-10-24 12:19:46 +0000735
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000736 if (kPath_RectType == rtype) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000737 SkPath tmp;
738 tmp.addRect(rect);
739 tmp.setFillType(SkPath::kWinding_FillType);
reed@android.com187d5592009-07-08 14:03:56 +0000740 this->drawPath(tmp, paint, NULL, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000741 return;
742 }
743
744 const SkMatrix& matrix = *fMatrix;
745 SkRect devRect;
746
747 // transform rect into devRect
reed@google.com73244152012-05-11 14:35:23 +0000748 matrix.mapPoints(rect_points(devRect), rect_points(rect), 2);
749 devRect.sort();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000750
751 if (fBounder && !fBounder->doRect(devRect, paint)) {
752 return;
753 }
754
755 // look for the quick exit, before we build a blitter
reed@google.com73244152012-05-11 14:35:23 +0000756 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000757 SkIRect ir;
758 devRect.roundOut(&ir);
reed@android.com55e76b22009-11-23 21:46:47 +0000759 if (paint.getStyle() != SkPaint::kFill_Style) {
760 // extra space for hairlines
761 ir.inset(-1, -1);
762 }
reed@google.com045e62d2011-10-24 12:19:46 +0000763 if (fRC->quickReject(ir))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000764 return;
765 }
766
767 SkAutoBlitterChoose blitterStorage(*fBitmap, matrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +0000768 const SkRasterClip& clip = *fRC;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000769 SkBlitter* blitter = blitterStorage.get();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770
reed@android.comb641c9f22010-03-25 14:31:46 +0000771 // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
772 // case we are also hairline (if we've gotten to here), which devolves to
773 // effectively just kFill
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000774 switch (rtype) {
775 case kFill_RectType:
776 if (paint.isAntiAlias()) {
777 SkScan::AntiFillRect(devRect, clip, blitter);
778 } else {
779 SkScan::FillRect(devRect, clip, blitter);
780 }
781 break;
782 case kStroke_RectType:
783 if (paint.isAntiAlias()) {
reed@google.com761fb622011-04-04 18:58:05 +0000784 SkScan::AntiFrameRect(devRect, strokeSize, clip, blitter);
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000785 } else {
reed@google.com761fb622011-04-04 18:58:05 +0000786 SkScan::FrameRect(devRect, strokeSize, clip, blitter);
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000787 }
788 break;
789 case kHair_RectType:
790 if (paint.isAntiAlias()) {
791 SkScan::AntiHairRect(devRect, clip, blitter);
792 } else {
793 SkScan::HairRect(devRect, clip, blitter);
794 }
795 break;
796 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000797 SkDEBUGFAIL("bad rtype");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000798 }
799}
800
801void SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const {
802 if (srcM.fBounds.isEmpty()) {
803 return;
804 }
805
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000806 const SkMask* mask = &srcM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000807
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000808 SkMask dstM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000809 if (paint.getMaskFilter() &&
810 paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, NULL)) {
811 mask = &dstM;
bungeman@google.combf2ac7e2011-09-26 19:51:33 +0000812 } else {
813 dstM.fImage = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000814 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000815 SkAutoMaskFreeImage ami(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000816
817 if (fBounder && !fBounder->doIRect(mask->fBounds)) {
818 return;
819 }
820
reed@google.com045e62d2011-10-24 12:19:46 +0000821 SkAutoBlitterChoose blitterChooser(*fBitmap, *fMatrix, paint);
822 SkBlitter* blitter = blitterChooser.get();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000823
reed@google.com045e62d2011-10-24 12:19:46 +0000824 SkAAClipBlitterWrapper wrapper;
825 const SkRegion* clipRgn;
826
827 if (fRC->isBW()) {
828 clipRgn = &fRC->bwRgn();
829 } else {
830 wrapper.init(*fRC, blitter);
831 clipRgn = &wrapper.getRgn();
832 blitter = wrapper.getBlitter();
833 }
834 blitter->blitMaskRegion(*mask, *clipRgn);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000835}
836
reed@android.comebdeeb82009-09-03 21:45:49 +0000837static SkScalar fast_len(const SkVector& vec) {
838 SkScalar x = SkScalarAbs(vec.fX);
839 SkScalar y = SkScalarAbs(vec.fY);
840 if (x < y) {
841 SkTSwap(x, y);
842 }
843 return x + SkScalarHalf(y);
844}
845
reed@google.comecadf992011-09-19 19:18:18 +0000846static bool xfermodeSupportsCoverageAsAlpha(SkXfermode* xfer) {
847 SkXfermode::Coeff dc;
848 if (!SkXfermode::AsCoeff(xfer, NULL, &dc)) {
849 return false;
850 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000851
reed@google.comecadf992011-09-19 19:18:18 +0000852 switch (dc) {
853 case SkXfermode::kOne_Coeff:
854 case SkXfermode::kISA_Coeff:
855 case SkXfermode::kISC_Coeff:
856 return true;
857 default:
858 return false;
859 }
860}
861
862bool SkDrawTreatAsHairline(const SkPaint& paint, const SkMatrix& matrix,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000863 SkScalar* coverage) {
864 SkASSERT(coverage);
reed@google.comecadf992011-09-19 19:18:18 +0000865 if (SkPaint::kStroke_Style != paint.getStyle()) {
866 return false;
867 }
868 SkScalar strokeWidth = paint.getStrokeWidth();
869 if (0 == strokeWidth) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000870 *coverage = SK_Scalar1;
reed@google.comecadf992011-09-19 19:18:18 +0000871 return true;
872 }
873
874 // if we get here, we need to try to fake a thick-stroke with a modulated
875 // hairline
876
877 if (!paint.isAntiAlias()) {
878 return false;
879 }
tomhudson@google.com8d430182011-06-06 19:11:19 +0000880 if (matrix.hasPerspective()) {
reed@android.comebdeeb82009-09-03 21:45:49 +0000881 return false;
882 }
reed@google.comecadf992011-09-19 19:18:18 +0000883
reed@android.comebdeeb82009-09-03 21:45:49 +0000884 SkVector src[2], dst[2];
reed@google.comecadf992011-09-19 19:18:18 +0000885 src[0].set(strokeWidth, 0);
886 src[1].set(0, strokeWidth);
reed@android.comebdeeb82009-09-03 21:45:49 +0000887 matrix.mapVectors(dst, src, 2);
888 SkScalar len0 = fast_len(dst[0]);
889 SkScalar len1 = fast_len(dst[1]);
agl@chromium.org652807b2010-04-27 15:47:34 +0000890 if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000891 *coverage = SkScalarAve(len0, len1);
reed@android.comebdeeb82009-09-03 21:45:49 +0000892 return true;
893 }
894 return false;
895}
896
reed@google.com32e5d972011-07-27 18:25:57 +0000897void SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000898 const SkMatrix* prePathMatrix, bool pathIsMutable) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000899 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000900
901 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +0000902 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000903 return;
904 }
905
906 SkPath* pathPtr = (SkPath*)&origSrcPath;
907 bool doFill = true;
908 SkPath tmpPath;
909 SkMatrix tmpMatrix;
910 const SkMatrix* matrix = fMatrix;
911
912 if (prePathMatrix) {
reed@google.com32e5d972011-07-27 18:25:57 +0000913 if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style ||
914 origPaint.getRasterizer()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000915 SkPath* result = pathPtr;
reed@google.coma76de3d2011-01-13 18:30:42 +0000916
reed@android.com8a1c16f2008-12-17 15:59:43 +0000917 if (!pathIsMutable) {
918 result = &tmpPath;
919 pathIsMutable = true;
920 }
921 pathPtr->transform(*prePathMatrix, result);
922 pathPtr = result;
923 } else {
924 if (!tmpMatrix.setConcat(*matrix, *prePathMatrix)) {
925 // overflow
926 return;
927 }
928 matrix = &tmpMatrix;
929 }
930 }
931 // at this point we're done with prePathMatrix
932 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.coma76de3d2011-01-13 18:30:42 +0000933
reed@google.com32e5d972011-07-27 18:25:57 +0000934 const SkPaint* paint = &origPaint;
935 SkTLazy<SkPaint> lazyPaint;
reed@google.coma76de3d2011-01-13 18:30:42 +0000936
reed@google.comecadf992011-09-19 19:18:18 +0000937 {
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000938 SkScalar coverage;
939 if (SkDrawTreatAsHairline(origPaint, *matrix, &coverage)) {
940 if (SK_Scalar1 == coverage) {
941 lazyPaint.set(origPaint);
942 lazyPaint.get()->setStrokeWidth(0);
943 paint = lazyPaint.get();
944 } else if (xfermodeSupportsCoverageAsAlpha(origPaint.getXfermode())) {
945 U8CPU newAlpha;
946#if 0
947 newAlpha = SkToU8(SkScalarRoundToInt(coverage *
948 origPaint.getAlpha()));
949#else
950 // this is the old technique, which we preserve for now so
951 // we don't change previous results (testing)
952 // the new way seems fine, its just (a tiny bit) different
953 int scale = (int)SkScalarMul(coverage, 256);
954 newAlpha = origPaint.getAlpha() * scale >> 8;
955#endif
956 lazyPaint.set(origPaint);
957 lazyPaint.get()->setStrokeWidth(0);
958 lazyPaint.get()->setAlpha(newAlpha);
959 paint = lazyPaint.get();
960 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000961 }
962 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000963
reed@google.com32e5d972011-07-27 18:25:57 +0000964 if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) {
965 doFill = paint->getFillPath(*pathPtr, &tmpPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000966 pathPtr = &tmpPath;
967 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000968
reed@google.com32e5d972011-07-27 18:25:57 +0000969 if (paint->getRasterizer()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000970 SkMask mask;
reed@google.com32e5d972011-07-27 18:25:57 +0000971 if (paint->getRasterizer()->rasterize(*pathPtr, *matrix,
reed@google.com045e62d2011-10-24 12:19:46 +0000972 &fRC->getBounds(), paint->getMaskFilter(), &mask,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000973 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
reed@google.com32e5d972011-07-27 18:25:57 +0000974 this->drawDevMask(mask, *paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000975 SkMask::FreeImage(mask.fImage);
976 }
977 return;
978 }
979
980 // avoid possibly allocating a new path in transform if we can
981 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
982
983 // transform the path into device space
984 pathPtr->transform(*matrix, devPathPtr);
985
reed@google.com32e5d972011-07-27 18:25:57 +0000986 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, *paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000987
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000988 if (paint->getMaskFilter()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000989 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000990 SkPaint::kStroke_Style;
991 if (paint->getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fRC,
992 fBounder, blitter.get(),
993 style)) {
994 return; // filterPath() called the blitter, so we're done
995 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000996 }
997
reed@google.com32e5d972011-07-27 18:25:57 +0000998 if (fBounder && !fBounder->doPath(*devPathPtr, *paint, doFill)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000999 return;
1000 }
1001
reed@google.com045e62d2011-10-24 12:19:46 +00001002 void (*proc)(const SkPath&, const SkRasterClip&, SkBlitter*);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001003 if (doFill) {
reed@google.com32e5d972011-07-27 18:25:57 +00001004 if (paint->isAntiAlias()) {
reed@google.com045e62d2011-10-24 12:19:46 +00001005 proc = SkScan::AntiFillPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001006 } else {
reed@google.com045e62d2011-10-24 12:19:46 +00001007 proc = SkScan::FillPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001008 }
1009 } else { // hairline
reed@google.com32e5d972011-07-27 18:25:57 +00001010 if (paint->isAntiAlias()) {
reed@google.com045e62d2011-10-24 12:19:46 +00001011 proc = SkScan::AntiHairPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001012 } else {
reed@google.com045e62d2011-10-24 12:19:46 +00001013 proc = SkScan::HairPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001014 }
1015 }
reed@google.com045e62d2011-10-24 12:19:46 +00001016 proc(*devPathPtr, *fRC, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001017}
1018
reed@android.com0baf1932009-06-24 12:41:42 +00001019/** For the purposes of drawing bitmaps, if a matrix is "almost" translate
1020 go ahead and treat it as if it were, so that subsequent code can go fast.
1021 */
1022static bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) {
1023 SkMatrix::TypeMask mask = matrix.getType();
1024
1025 if (mask & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
1026 return false;
1027 }
1028 if (mask & SkMatrix::kScale_Mask) {
1029 SkScalar sx = matrix[SkMatrix::kMScaleX];
1030 SkScalar sy = matrix[SkMatrix::kMScaleY];
1031 int w = bitmap.width();
1032 int h = bitmap.height();
1033 int sw = SkScalarRound(SkScalarMul(sx, SkIntToScalar(w)));
1034 int sh = SkScalarRound(SkScalarMul(sy, SkIntToScalar(h)));
1035 return sw == w && sh == h;
1036 }
1037 // if we got here, we're either kTranslate_Mask or identity
1038 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001039}
1040
1041void SkDraw::drawBitmapAsMask(const SkBitmap& bitmap,
1042 const SkPaint& paint) const {
1043 SkASSERT(bitmap.getConfig() == SkBitmap::kA8_Config);
1044
reed@google.coma76de3d2011-01-13 18:30:42 +00001045 if (just_translate(*fMatrix, bitmap)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001046 int ix = SkScalarRound(fMatrix->getTranslateX());
1047 int iy = SkScalarRound(fMatrix->getTranslateY());
1048
1049 SkMask mask;
1050 mask.fBounds.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1051 mask.fFormat = SkMask::kA8_Format;
1052 mask.fRowBytes = bitmap.rowBytes();
1053 mask.fImage = bitmap.getAddr8(0, 0);
reed@google.coma76de3d2011-01-13 18:30:42 +00001054
reed@android.com8a1c16f2008-12-17 15:59:43 +00001055 this->drawDevMask(mask, paint);
1056 } else { // need to xform the bitmap first
1057 SkRect r;
1058 SkMask mask;
reed@google.coma76de3d2011-01-13 18:30:42 +00001059
reed@android.com8a1c16f2008-12-17 15:59:43 +00001060 r.set(0, 0,
1061 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
1062 fMatrix->mapRect(&r);
1063 r.round(&mask.fBounds);
reed@google.coma76de3d2011-01-13 18:30:42 +00001064
reed@android.com8a1c16f2008-12-17 15:59:43 +00001065 // set the mask's bounds to the transformed bitmap-bounds,
1066 // clipped to the actual device
1067 {
1068 SkIRect devBounds;
1069 devBounds.set(0, 0, fBitmap->width(), fBitmap->height());
1070 // need intersect(l, t, r, b) on irect
1071 if (!mask.fBounds.intersect(devBounds)) {
1072 return;
1073 }
1074 }
reed@android.com543ed932009-04-24 12:43:40 +00001075
reed@android.com8a1c16f2008-12-17 15:59:43 +00001076 mask.fFormat = SkMask::kA8_Format;
1077 mask.fRowBytes = SkAlign4(mask.fBounds.width());
reed@android.com543ed932009-04-24 12:43:40 +00001078 size_t size = mask.computeImageSize();
1079 if (0 == size) {
1080 // the mask is too big to allocated, draw nothing
1081 return;
1082 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001083
1084 // allocate (and clear) our temp buffer to hold the transformed bitmap
reed@android.com8a1c16f2008-12-17 15:59:43 +00001085 SkAutoMalloc storage(size);
1086 mask.fImage = (uint8_t*)storage.get();
1087 memset(mask.fImage, 0, size);
reed@google.coma76de3d2011-01-13 18:30:42 +00001088
reed@android.com8a1c16f2008-12-17 15:59:43 +00001089 // now draw our bitmap(src) into mask(dst), transformed by the matrix
1090 {
1091 SkBitmap device;
1092 device.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(),
1093 mask.fBounds.height(), mask.fRowBytes);
1094 device.setPixels(mask.fImage);
reed@google.coma76de3d2011-01-13 18:30:42 +00001095
reed@android.com8a1c16f2008-12-17 15:59:43 +00001096 SkCanvas c(device);
1097 // need the unclipped top/left for the translate
1098 c.translate(-SkIntToScalar(mask.fBounds.fLeft),
1099 -SkIntToScalar(mask.fBounds.fTop));
1100 c.concat(*fMatrix);
reed@android.com3469c762009-02-24 19:03:20 +00001101
1102 // We can't call drawBitmap, or we'll infinitely recurse. Instead
reed@android.comfb12c3e2009-03-05 20:43:42 +00001103 // we manually build a shader and draw that into our new mask
reed@android.com3469c762009-02-24 19:03:20 +00001104 SkPaint tmpPaint;
1105 tmpPaint.setFlags(paint.getFlags());
reed@google.com40c2ba22011-07-25 19:41:22 +00001106 SkAutoBitmapShaderInstall install(bitmap, tmpPaint);
reed@android.com3469c762009-02-24 19:03:20 +00001107 SkRect rr;
1108 rr.set(0, 0, SkIntToScalar(bitmap.width()),
1109 SkIntToScalar(bitmap.height()));
reed@google.com40c2ba22011-07-25 19:41:22 +00001110 c.drawRect(rr, install.paintWithShader());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001111 }
1112 this->drawDevMask(mask, paint);
1113 }
1114}
1115
reed@google.com045e62d2011-10-24 12:19:46 +00001116static bool clipped_out(const SkMatrix& m, const SkRasterClip& c,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001117 const SkRect& srcR) {
1118 SkRect dstR;
1119 SkIRect devIR;
reed@google.coma76de3d2011-01-13 18:30:42 +00001120
reed@android.com8a1c16f2008-12-17 15:59:43 +00001121 m.mapRect(&dstR, srcR);
reed@google.coma76de3d2011-01-13 18:30:42 +00001122 dstR.roundOut(&devIR);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001123 return c.quickReject(devIR);
1124}
1125
reed@google.com045e62d2011-10-24 12:19:46 +00001126static bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001127 int width, int height) {
1128 SkRect r;
1129 r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
1130 return clipped_out(matrix, clip, r);
1131}
1132
reed@google.com045e62d2011-10-24 12:19:46 +00001133static bool clipHandlesSprite(const SkRasterClip& clip, int x, int y,
1134 const SkBitmap& bitmap) {
1135 return clip.isBW() ||
1136 clip.quickContains(x, y, x + bitmap.width(), y + bitmap.height());
1137}
1138
reed@android.com8a1c16f2008-12-17 15:59:43 +00001139void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
reed@google.com40c2ba22011-07-25 19:41:22 +00001140 const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001141 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001142
1143 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001144 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001145 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.comdcd0f3a2011-10-04 01:17:15 +00001146 bitmap.getConfig() == SkBitmap::kNo_Config) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001147 return;
1148 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001149
1150#ifndef SK_ALLOW_OVER_32K_BITMAPS
reed@android.com8a1c16f2008-12-17 15:59:43 +00001151 // run away on too-big bitmaps for now (exceed 16.16)
1152 if (bitmap.width() > 32767 || bitmap.height() > 32767) {
1153 return;
1154 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001155#endif
1156
reed@google.com40c2ba22011-07-25 19:41:22 +00001157 SkPaint paint(origPaint);
1158 paint.setStyle(SkPaint::kFill_Style);
reed@google.coma76de3d2011-01-13 18:30:42 +00001159
reed@android.com8a1c16f2008-12-17 15:59:43 +00001160 SkMatrix matrix;
1161 if (!matrix.setConcat(*fMatrix, prematrix)) {
1162 return;
1163 }
1164
reed@google.com045e62d2011-10-24 12:19:46 +00001165 if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001166 return;
1167 }
1168
reed@android.com218521e2010-02-09 13:06:29 +00001169 if (fBounder && just_translate(matrix, bitmap)) {
1170 SkIRect ir;
1171 int32_t ix = SkScalarRound(matrix.getTranslateX());
1172 int32_t iy = SkScalarRound(matrix.getTranslateY());
1173 ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1174 if (!fBounder->doIRect(ir)) {
1175 return;
1176 }
1177 }
1178
1179 // only lock the pixels if we passed the clip and bounder tests
reed@android.com8a1c16f2008-12-17 15:59:43 +00001180 SkAutoLockPixels alp(bitmap);
1181 // after the lock, check if we are valid
1182 if (!bitmap.readyToDraw()) {
1183 return;
1184 }
1185
reed@android.com0baf1932009-06-24 12:41:42 +00001186 if (bitmap.getConfig() != SkBitmap::kA8_Config &&
1187 just_translate(matrix, bitmap)) {
reed@google.com045e62d2011-10-24 12:19:46 +00001188 int ix = SkScalarRound(matrix.getTranslateX());
1189 int iy = SkScalarRound(matrix.getTranslateY());
1190 if (clipHandlesSprite(*fRC, ix, iy, bitmap)) {
1191 uint32_t storage[kBlitterStorageLongCount];
1192 SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1193 ix, iy, storage, sizeof(storage));
1194 if (blitter) {
1195 SkAutoTPlacementDelete<SkBlitter> ad(blitter, storage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001196
reed@google.com045e62d2011-10-24 12:19:46 +00001197 SkIRect ir;
1198 ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001199
reed@google.com045e62d2011-10-24 12:19:46 +00001200 SkScan::FillIRect(ir, *fRC, blitter);
1201 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001202 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001203 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001204 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001205
reed@android.com8a1c16f2008-12-17 15:59:43 +00001206 // now make a temp draw on the stack, and use it
1207 //
1208 SkDraw draw(*this);
1209 draw.fMatrix = &matrix;
reed@google.coma76de3d2011-01-13 18:30:42 +00001210
reed@android.com8a1c16f2008-12-17 15:59:43 +00001211 if (bitmap.getConfig() == SkBitmap::kA8_Config) {
1212 draw.drawBitmapAsMask(bitmap, paint);
1213 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +00001214 SkAutoBitmapShaderInstall install(bitmap, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215
1216 SkRect r;
1217 r.set(0, 0, SkIntToScalar(bitmap.width()),
1218 SkIntToScalar(bitmap.height()));
reed@google.coma76de3d2011-01-13 18:30:42 +00001219 // is this ok if paint has a rasterizer?
reed@google.com40c2ba22011-07-25 19:41:22 +00001220 draw.drawRect(r, install.paintWithShader());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001221 }
1222}
1223
1224void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y,
reed@google.com40c2ba22011-07-25 19:41:22 +00001225 const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001226 SkDEBUGCODE(this->validate();)
reed@google.coma76de3d2011-01-13 18:30:42 +00001227
reed@android.com8a1c16f2008-12-17 15:59:43 +00001228 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001229 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001230 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.comdcd0f3a2011-10-04 01:17:15 +00001231 bitmap.getConfig() == SkBitmap::kNo_Config) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001232 return;
1233 }
1234
1235 SkIRect bounds;
1236 bounds.set(x, y, x + bitmap.width(), y + bitmap.height());
1237
reed@google.com045e62d2011-10-24 12:19:46 +00001238 if (fRC->quickReject(bounds)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001239 return; // nothing to draw
1240 }
1241
reed@google.com40c2ba22011-07-25 19:41:22 +00001242 SkPaint paint(origPaint);
1243 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001244
reed@google.com045e62d2011-10-24 12:19:46 +00001245 if (NULL == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, bitmap)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001246 uint32_t storage[kBlitterStorageLongCount];
1247 SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1248 x, y, storage, sizeof(storage));
1249
1250 if (blitter) {
1251 SkAutoTPlacementDelete<SkBlitter> ad(blitter, storage);
1252
1253 if (fBounder && !fBounder->doIRect(bounds)) {
1254 return;
1255 }
1256
reed@google.com045e62d2011-10-24 12:19:46 +00001257 SkScan::FillIRect(bounds, *fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001258 return;
1259 }
1260 }
1261
reed@google.com40c2ba22011-07-25 19:41:22 +00001262 SkAutoBitmapShaderInstall install(bitmap, paint);
1263 const SkPaint& shaderPaint = install.paintWithShader();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001264
1265 SkMatrix matrix;
1266 SkRect r;
1267
1268 // get a scalar version of our rect
1269 r.set(bounds);
1270
1271 // tell the shader our offset
1272 matrix.setTranslate(r.fLeft, r.fTop);
reed@google.com40c2ba22011-07-25 19:41:22 +00001273 shaderPaint.getShader()->setLocalMatrix(matrix);
reed@google.coma76de3d2011-01-13 18:30:42 +00001274
reed@android.com8a1c16f2008-12-17 15:59:43 +00001275 SkDraw draw(*this);
1276 matrix.reset();
1277 draw.fMatrix = &matrix;
1278 // call ourself with a rect
reed@google.coma76de3d2011-01-13 18:30:42 +00001279 // is this OK if paint has a rasterizer?
reed@google.com40c2ba22011-07-25 19:41:22 +00001280 draw.drawRect(r, shaderPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001281}
1282
1283///////////////////////////////////////////////////////////////////////////////
1284
1285#include "SkScalerContext.h"
1286#include "SkGlyphCache.h"
reed@google.come6913762012-08-07 15:19:47 +00001287#include "SkTextToPathIter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001288#include "SkUtils.h"
1289
1290static void measure_text(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
1291 const char text[], size_t byteLength, SkVector* stopVector) {
1292 SkFixed x = 0, y = 0;
1293 const char* stop = text + byteLength;
1294
1295 SkAutoKern autokern;
reed@google.coma76de3d2011-01-13 18:30:42 +00001296
reed@android.com8a1c16f2008-12-17 15:59:43 +00001297 while (text < stop) {
1298 // don't need x, y here, since all subpixel variants will have the
1299 // same advance
1300 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1301
1302 x += autokern.adjust(glyph) + glyph.fAdvanceX;
1303 y += glyph.fAdvanceY;
1304 }
1305 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
1306
1307 SkASSERT(text == stop);
1308}
1309
1310void SkDraw::drawText_asPaths(const char text[], size_t byteLength,
1311 SkScalar x, SkScalar y,
1312 const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001313 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001314
djsollen@google.com166e6532012-03-20 14:24:38 +00001315 SkTextToPathIter iter(text, byteLength, paint, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001316
1317 SkMatrix matrix;
1318 matrix.setScale(iter.getPathScale(), iter.getPathScale());
1319 matrix.postTranslate(x, y);
1320
1321 const SkPath* iterPath;
1322 SkScalar xpos, prevXPos = 0;
1323
reed@google.com7b4531f2012-08-07 15:53:00 +00001324 while (iter.next(&iterPath, &xpos)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001325 matrix.postTranslate(xpos - prevXPos, 0);
reed@google.com7b4531f2012-08-07 15:53:00 +00001326 if (iterPath) {
1327 const SkPaint& pnt = iter.getPaint();
1328 if (fDevice) {
1329 fDevice->drawPath(*this, *iterPath, pnt, &matrix, false);
1330 } else {
1331 this->drawPath(*iterPath, pnt, &matrix, false);
1332 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001333 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001334 prevXPos = xpos;
1335 }
1336}
1337
reed@android.com8a1c16f2008-12-17 15:59:43 +00001338// disable warning : local variable used without having been initialized
reed@google.coma76de3d2011-01-13 18:30:42 +00001339#if defined _WIN32 && _MSC_VER >= 1300
reed@android.com8a1c16f2008-12-17 15:59:43 +00001340#pragma warning ( push )
1341#pragma warning ( disable : 4701 )
1342#endif
1343
1344//////////////////////////////////////////////////////////////////////////////
1345
1346static void D1G_NoBounder_RectClip(const SkDraw1Glyph& state,
reed@android.comf2b98d62010-12-20 18:26:13 +00001347 SkFixed fx, SkFixed fy,
1348 const SkGlyph& glyph) {
1349 int left = SkFixedFloor(fx);
1350 int top = SkFixedFloor(fy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001351 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001352 SkASSERT(NULL == state.fBounder);
1353 SkASSERT((NULL == state.fClip && state.fAAClip) ||
1354 (state.fClip && NULL == state.fAAClip && state.fClip->isRect()));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001355
1356 left += glyph.fLeft;
1357 top += glyph.fTop;
1358
1359 int right = left + glyph.fWidth;
1360 int bottom = top + glyph.fHeight;
1361
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001362 SkMask mask;
1363 SkIRect storage;
1364 SkIRect* bounds = &mask.fBounds;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001365
tomhudson@google.com83a44462011-10-27 15:27:51 +00001366 mask.fBounds.set(left, top, right, bottom);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001367
tomhudson@google.com83a44462011-10-27 15:27:51 +00001368 // this extra test is worth it, assuming that most of the time it succeeds
1369 // since we can avoid writing to storage
1370 if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) {
1371 if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds))
1372 return;
1373 bounds = &storage;
1374 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001375
tomhudson@google.com83a44462011-10-27 15:27:51 +00001376 uint8_t* aa = (uint8_t*)glyph.fImage;
1377 if (NULL == aa) {
1378 aa = (uint8_t*)state.fCache->findImage(glyph);
1379 if (NULL == aa) {
1380 return; // can't rasterize glyph
reed@android.com8a1c16f2008-12-17 15:59:43 +00001381 }
tomhudson@google.com83a44462011-10-27 15:27:51 +00001382 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001383
tomhudson@google.com83a44462011-10-27 15:27:51 +00001384 mask.fRowBytes = glyph.rowBytes();
1385 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1386 mask.fImage = aa;
1387 state.fBlitter->blitMask(mask, *bounds);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001388}
1389
1390static void D1G_NoBounder_RgnClip(const SkDraw1Glyph& state,
reed@android.comf2b98d62010-12-20 18:26:13 +00001391 SkFixed fx, SkFixed fy,
tomhudson@google.com83a44462011-10-27 15:27:51 +00001392 const SkGlyph& glyph) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001393 int left = SkFixedFloor(fx);
1394 int top = SkFixedFloor(fy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001395 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001396 SkASSERT(!state.fClip->isRect());
1397 SkASSERT(NULL == state.fBounder);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001398
1399 SkMask mask;
1400
1401 left += glyph.fLeft;
1402 top += glyph.fTop;
1403
1404 mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001405 SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001406
tomhudson@google.com83a44462011-10-27 15:27:51 +00001407 if (!clipper.done()) {
1408 const SkIRect& cr = clipper.rect();
1409 const uint8_t* aa = (const uint8_t*)glyph.fImage;
1410 if (NULL == aa) {
1411 aa = (uint8_t*)state.fCache->findImage(glyph);
1412 if (NULL == aa) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001413 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001414 }
tomhudson@google.com83a44462011-10-27 15:27:51 +00001415 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001416
tomhudson@google.com83a44462011-10-27 15:27:51 +00001417 mask.fRowBytes = glyph.rowBytes();
1418 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1419 mask.fImage = (uint8_t*)aa;
1420 do {
1421 state.fBlitter->blitMask(mask, cr);
1422 clipper.next();
1423 } while (!clipper.done());
1424 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001425}
1426
1427static void D1G_Bounder(const SkDraw1Glyph& state,
reed@android.comf2b98d62010-12-20 18:26:13 +00001428 SkFixed fx, SkFixed fy,
tomhudson@google.com83a44462011-10-27 15:27:51 +00001429 const SkGlyph& glyph) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001430 int left = SkFixedFloor(fx);
1431 int top = SkFixedFloor(fy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001432 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001433
reed@android.com8a1c16f2008-12-17 15:59:43 +00001434 SkMask mask;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001435
reed@android.com8a1c16f2008-12-17 15:59:43 +00001436 left += glyph.fLeft;
1437 top += glyph.fTop;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001438
reed@android.com8a1c16f2008-12-17 15:59:43 +00001439 mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
1440 SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001441
tomhudson@google.com83a44462011-10-27 15:27:51 +00001442 if (!clipper.done()) {
1443 const SkIRect& cr = clipper.rect();
1444 const uint8_t* aa = (const uint8_t*)glyph.fImage;
1445 if (NULL == aa) {
1446 aa = (uint8_t*)state.fCache->findImage(glyph);
1447 if (NULL == aa) {
1448 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001449 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001450 }
1451
reed@android.comd055c1f2010-03-01 14:54:05 +00001452 // we need to pass the origin, which we approximate with our
1453 // (unadjusted) left,top coordinates (the caller called fixedfloor)
tomhudson@google.com83a44462011-10-27 15:27:51 +00001454 if (state.fBounder->doIRectGlyph(cr,
reed@android.comd055c1f2010-03-01 14:54:05 +00001455 left - glyph.fLeft,
1456 top - glyph.fTop, glyph)) {
tomhudson@google.com83a44462011-10-27 15:27:51 +00001457 mask.fRowBytes = glyph.rowBytes();
1458 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1459 mask.fImage = (uint8_t*)aa;
1460 do {
1461 state.fBlitter->blitMask(mask, cr);
1462 clipper.next();
1463 } while (!clipper.done());
1464 }
1465 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001466}
1467
reed@google.com045e62d2011-10-24 12:19:46 +00001468static void D1G_Bounder_AAClip(const SkDraw1Glyph& state,
1469 SkFixed fx, SkFixed fy,
1470 const SkGlyph& glyph) {
1471 int left = SkFixedFloor(fx);
1472 int top = SkFixedFloor(fy);
1473 SkIRect bounds;
1474 bounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
1475
1476 if (state.fBounder->doIRectGlyph(bounds, left, top, glyph)) {
1477 D1G_NoBounder_RectClip(state, fx, fy, glyph);
1478 }
1479}
1480
reed@google.comfd4236e2011-07-25 21:16:22 +00001481static bool hasCustomD1GProc(const SkDraw& draw) {
1482 return draw.fProcs && draw.fProcs->fD1GProc;
1483}
1484
1485static bool needsRasterTextBlit(const SkDraw& draw) {
1486 return !hasCustomD1GProc(draw);
1487}
1488
reed@android.com8a1c16f2008-12-17 15:59:43 +00001489SkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter,
1490 SkGlyphCache* cache) {
1491 fDraw = draw;
bungeman@google.com2211b622012-01-13 15:02:58 +00001492 fBounder = draw->fBounder;
1493 fBlitter = blitter;
1494 fCache = cache;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001495
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001496 if (hasCustomD1GProc(*draw)) {
reed@google.com045e62d2011-10-24 12:19:46 +00001497 // todo: fix this assumption about clips w/ custom
1498 fClip = draw->fClip;
1499 fClipBounds = fClip->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001500 return draw->fProcs->fD1GProc;
1501 }
1502
reed@google.com045e62d2011-10-24 12:19:46 +00001503 if (draw->fRC->isBW()) {
1504 fAAClip = NULL;
1505 fClip = &draw->fRC->bwRgn();
1506 fClipBounds = fClip->getBounds();
1507 if (NULL == fBounder) {
1508 if (fClip->isRect()) {
1509 return D1G_NoBounder_RectClip;
1510 } else {
1511 return D1G_NoBounder_RgnClip;
1512 }
1513 } else {
1514 return D1G_Bounder;
1515 }
1516 } else { // aaclip
1517 fAAClip = &draw->fRC->aaRgn();
1518 fClip = NULL;
1519 fClipBounds = fAAClip->getBounds();
1520 if (NULL == fBounder) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001521 return D1G_NoBounder_RectClip;
1522 } else {
reed@google.com045e62d2011-10-24 12:19:46 +00001523 return D1G_Bounder_AAClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001524 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001525 }
1526}
1527
reed@android.com8a1c16f2008-12-17 15:59:43 +00001528///////////////////////////////////////////////////////////////////////////////
1529
1530void SkDraw::drawText(const char text[], size_t byteLength,
1531 SkScalar x, SkScalar y, const SkPaint& paint) const {
1532 SkASSERT(byteLength == 0 || text != NULL);
1533
reed@android.comf2b98d62010-12-20 18:26:13 +00001534 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001535
1536 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001537 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001538 return;
1539 }
1540
bsalomon@google.com36d6eda2012-10-10 16:12:14 +00001541 // SkScalarRec doesn't currently have a way of representing hairline stroke and
1542 // will fill if its frame-width is 0.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001543 if (/*paint.isLinearText() ||*/
skia.committer@gmail.comfc843592012-10-11 02:01:14 +00001544 (fMatrix->hasPerspective()) ||
bsalomon@google.com36d6eda2012-10-10 16:12:14 +00001545 (0 == paint.getStrokeWidth() && SkPaint::kStroke_Style == paint.getStyle())) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001546 this->drawText_asPaths(text, byteLength, x, y, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001547 return;
1548 }
1549
1550 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
1551
reed@android.comf2b98d62010-12-20 18:26:13 +00001552 const SkMatrix* matrix = fMatrix;
reed@android.comf2b98d62010-12-20 18:26:13 +00001553
1554 SkAutoGlyphCache autoCache(paint, matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001555 SkGlyphCache* cache = autoCache.getCache();
reed@google.coma76de3d2011-01-13 18:30:42 +00001556
reed@android.com8a1c16f2008-12-17 15:59:43 +00001557 // transform our starting point
1558 {
1559 SkPoint loc;
reed@android.comf2b98d62010-12-20 18:26:13 +00001560 matrix->mapXY(x, y, &loc);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001561 x = loc.fX;
1562 y = loc.fY;
1563 }
1564
1565 // need to measure first
1566 if (paint.getTextAlign() != SkPaint::kLeft_Align) {
1567 SkVector stop;
1568
1569 measure_text(cache, glyphCacheProc, text, byteLength, &stop);
1570
1571 SkScalar stopX = stop.fX;
1572 SkScalar stopY = stop.fY;
1573
1574 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
1575 stopX = SkScalarHalf(stopX);
1576 stopY = SkScalarHalf(stopY);
1577 }
1578 x -= stopX;
1579 y -= stopY;
1580 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001581
reed@android.com8a1c16f2008-12-17 15:59:43 +00001582 SkFixed fx = SkScalarToFixed(x);
1583 SkFixed fy = SkScalarToFixed(y);
1584 const char* stop = text + byteLength;
1585
reed@android.comf2b98d62010-12-20 18:26:13 +00001586 SkFixed fxMask = ~0;
1587 SkFixed fyMask = ~0;
bungeman@google.com2211b622012-01-13 15:02:58 +00001588 if (cache->isSubpixel()) {
reed@google.comcb6ccdd2011-08-23 21:30:47 +00001589 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*matrix);
1590 if (kX_SkAxisAlignment == baseline) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001591 fyMask = 0;
reed@google.comcb6ccdd2011-08-23 21:30:47 +00001592 } else if (kY_SkAxisAlignment == baseline) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001593 fxMask = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001594 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001595
bungeman@google.com2211b622012-01-13 15:02:58 +00001596 // apply bias here to avoid adding 1/2 the sampling frequency in the loop
1597 fx += SK_FixedHalf >> SkGlyph::kSubBits;
1598 fy += SK_FixedHalf >> SkGlyph::kSubBits;
1599 } else {
1600 fx += SK_FixedHalf;
1601 fy += SK_FixedHalf;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001602 }
1603
reed@google.com045e62d2011-10-24 12:19:46 +00001604 SkAAClipBlitter aaBlitter;
1605 SkAutoBlitterChoose blitterChooser;
1606 SkBlitter* blitter = NULL;
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001607 if (needsRasterTextBlit(*this)) {
reed@google.com045e62d2011-10-24 12:19:46 +00001608 blitterChooser.choose(*fBitmap, *matrix, paint);
1609 blitter = blitterChooser.get();
1610 if (fRC->isAA()) {
1611 aaBlitter.init(blitter, &fRC->aaRgn());
1612 blitter = &aaBlitter;
1613 }
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001614 }
1615
reed@android.com8a1c16f2008-12-17 15:59:43 +00001616 SkAutoKern autokern;
bungeman@google.com52c748b2011-08-22 21:30:43 +00001617 SkDraw1Glyph d1g;
reed@google.com045e62d2011-10-24 12:19:46 +00001618 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001619
1620 while (text < stop) {
bungeman@google.com2211b622012-01-13 15:02:58 +00001621 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001622
1623 fx += autokern.adjust(glyph);
1624
1625 if (glyph.fWidth) {
bungeman@google.com52c748b2011-08-22 21:30:43 +00001626 proc(d1g, fx, fy, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001627 }
1628 fx += glyph.fAdvanceX;
1629 fy += glyph.fAdvanceY;
1630 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001631}
1632
1633// last parameter is interpreted as SkFixed [x, y]
1634// return the fixed position, which may be rounded or not by the caller
1635// e.g. subpixel doesn't round
1636typedef void (*AlignProc)(const SkPoint&, const SkGlyph&, SkIPoint*);
1637
1638static void leftAlignProc(const SkPoint& loc, const SkGlyph& glyph,
1639 SkIPoint* dst) {
1640 dst->set(SkScalarToFixed(loc.fX), SkScalarToFixed(loc.fY));
1641}
1642
1643static void centerAlignProc(const SkPoint& loc, const SkGlyph& glyph,
1644 SkIPoint* dst) {
1645 dst->set(SkScalarToFixed(loc.fX) - (glyph.fAdvanceX >> 1),
1646 SkScalarToFixed(loc.fY) - (glyph.fAdvanceY >> 1));
1647}
1648
1649static void rightAlignProc(const SkPoint& loc, const SkGlyph& glyph,
1650 SkIPoint* dst) {
1651 dst->set(SkScalarToFixed(loc.fX) - glyph.fAdvanceX,
1652 SkScalarToFixed(loc.fY) - glyph.fAdvanceY);
1653}
1654
1655static AlignProc pick_align_proc(SkPaint::Align align) {
1656 static const AlignProc gProcs[] = {
1657 leftAlignProc, centerAlignProc, rightAlignProc
1658 };
reed@google.coma76de3d2011-01-13 18:30:42 +00001659
reed@android.com8a1c16f2008-12-17 15:59:43 +00001660 SkASSERT((unsigned)align < SK_ARRAY_COUNT(gProcs));
1661
1662 return gProcs[align];
1663}
1664
1665class TextMapState {
1666public:
1667 mutable SkPoint fLoc;
reed@google.coma76de3d2011-01-13 18:30:42 +00001668
reed@android.com8a1c16f2008-12-17 15:59:43 +00001669 TextMapState(const SkMatrix& matrix, SkScalar y)
1670 : fMatrix(matrix), fProc(matrix.getMapXYProc()), fY(y) {}
1671
1672 typedef void (*Proc)(const TextMapState&, const SkScalar pos[]);
reed@google.coma76de3d2011-01-13 18:30:42 +00001673
reed@android.com8a1c16f2008-12-17 15:59:43 +00001674 Proc pickProc(int scalarsPerPosition);
reed@google.coma76de3d2011-01-13 18:30:42 +00001675
reed@android.com8a1c16f2008-12-17 15:59:43 +00001676private:
1677 const SkMatrix& fMatrix;
1678 SkMatrix::MapXYProc fProc;
1679 SkScalar fY; // ignored by MapXYProc
1680 // these are only used by Only... procs
1681 SkScalar fScaleX, fTransX, fTransformedY;
1682
1683 static void MapXProc(const TextMapState& state, const SkScalar pos[]) {
1684 state.fProc(state.fMatrix, *pos, state.fY, &state.fLoc);
1685 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001686
reed@android.com8a1c16f2008-12-17 15:59:43 +00001687 static void MapXYProc(const TextMapState& state, const SkScalar pos[]) {
1688 state.fProc(state.fMatrix, pos[0], pos[1], &state.fLoc);
1689 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001690
reed@android.com8a1c16f2008-12-17 15:59:43 +00001691 static void MapOnlyScaleXProc(const TextMapState& state,
1692 const SkScalar pos[]) {
1693 state.fLoc.set(SkScalarMul(state.fScaleX, *pos) + state.fTransX,
1694 state.fTransformedY);
1695 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001696
reed@android.com8a1c16f2008-12-17 15:59:43 +00001697 static void MapOnlyTransXProc(const TextMapState& state,
1698 const SkScalar pos[]) {
1699 state.fLoc.set(*pos + state.fTransX, state.fTransformedY);
1700 }
1701};
1702
1703TextMapState::Proc TextMapState::pickProc(int scalarsPerPosition) {
1704 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
reed@google.coma76de3d2011-01-13 18:30:42 +00001705
reed@android.com8a1c16f2008-12-17 15:59:43 +00001706 if (1 == scalarsPerPosition) {
1707 unsigned mtype = fMatrix.getType();
1708 if (mtype & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
1709 return MapXProc;
1710 } else {
1711 fScaleX = fMatrix.getScaleX();
1712 fTransX = fMatrix.getTranslateX();
1713 fTransformedY = SkScalarMul(fY, fMatrix.getScaleY()) +
1714 fMatrix.getTranslateY();
1715 return (mtype & SkMatrix::kScale_Mask) ?
1716 MapOnlyScaleXProc : MapOnlyTransXProc;
1717 }
1718 } else {
1719 return MapXYProc;
1720 }
1721}
1722
1723//////////////////////////////////////////////////////////////////////////////
1724
1725void SkDraw::drawPosText(const char text[], size_t byteLength,
1726 const SkScalar pos[], SkScalar constY,
1727 int scalarsPerPosition, const SkPaint& paint) const {
1728 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
1738 if (/*paint.isLinearText() ||*/
tomhudson@google.com8d430182011-06-06 19:11:19 +00001739 (fMatrix->hasPerspective())) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001740 // TODO !!!!
1741// this->drawText_asPaths(text, byteLength, x, y, paint);
1742 return;
1743 }
1744
reed@android.comf2b98d62010-12-20 18:26:13 +00001745 const SkMatrix* matrix = fMatrix;
reed@google.coma76de3d2011-01-13 18:30:42 +00001746
reed@android.com8a1c16f2008-12-17 15:59:43 +00001747 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
reed@android.comf2b98d62010-12-20 18:26:13 +00001748 SkAutoGlyphCache autoCache(paint, matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001749 SkGlyphCache* cache = autoCache.getCache();
reed@google.coma76de3d2011-01-13 18:30:42 +00001750
reed@google.com045e62d2011-10-24 12:19:46 +00001751 SkAAClipBlitterWrapper wrapper;
1752 SkAutoBlitterChoose blitterChooser;
1753 SkBlitter* blitter = NULL;
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001754 if (needsRasterTextBlit(*this)) {
reed@google.com045e62d2011-10-24 12:19:46 +00001755 blitterChooser.choose(*fBitmap, *matrix, paint);
1756 blitter = blitterChooser.get();
1757 if (fRC->isAA()) {
1758 wrapper.init(*fRC, blitter);
1759 blitter = wrapper.getBlitter();
1760 }
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001761 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001762
reed@android.com8a1c16f2008-12-17 15:59:43 +00001763 const char* stop = text + byteLength;
1764 AlignProc alignProc = pick_align_proc(paint.getTextAlign());
bungeman@google.com2211b622012-01-13 15:02:58 +00001765 SkDraw1Glyph d1g;
1766 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache);
reed@android.comf2b98d62010-12-20 18:26:13 +00001767 TextMapState tms(*matrix, constY);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001768 TextMapState::Proc tmsProc = tms.pickProc(scalarsPerPosition);
1769
bungeman@google.com2211b622012-01-13 15:02:58 +00001770 if (cache->isSubpixel()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001771 // maybe we should skip the rounding if linearText is set
reed@google.comcb6ccdd2011-08-23 21:30:47 +00001772 SkAxisAlignment roundBaseline = SkComputeAxisAlignmentForHText(*matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001773
1774 if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1775 while (text < stop) {
reed@google.coma76de3d2011-01-13 18:30:42 +00001776
reed@android.com8a1c16f2008-12-17 15:59:43 +00001777 tmsProc(tms, pos);
reed@google.coma76de3d2011-01-13 18:30:42 +00001778
bungeman@google.com2211b622012-01-13 15:02:58 +00001779#ifdef SK_DRAW_POS_TEXT_IGNORE_SUBPIXEL_LEFT_ALIGN_FIX
reed@android.com8a1c16f2008-12-17 15:59:43 +00001780 SkFixed fx = SkScalarToFixed(tms.fLoc.fX);
1781 SkFixed fy = SkScalarToFixed(tms.fLoc.fY);
bungeman@google.com2211b622012-01-13 15:02:58 +00001782#else
1783 SkFixed fx = SkScalarToFixed(tms.fLoc.fX) + (SK_FixedHalf >> SkGlyph::kSubBits);
1784 SkFixed fy = SkScalarToFixed(tms.fLoc.fY) + (SK_FixedHalf >> SkGlyph::kSubBits);
1785#endif
reed@android.comf2b98d62010-12-20 18:26:13 +00001786 SkFixed fxMask = ~0;
1787 SkFixed fyMask = ~0;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001788
reed@google.comcb6ccdd2011-08-23 21:30:47 +00001789 if (kX_SkAxisAlignment == roundBaseline) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001790 fyMask = 0;
reed@google.comcb6ccdd2011-08-23 21:30:47 +00001791 } else if (kY_SkAxisAlignment == roundBaseline) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001792 fxMask = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001793 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001794
reed@android.comf2b98d62010-12-20 18:26:13 +00001795 const SkGlyph& glyph = glyphCacheProc(cache, &text,
1796 fx & fxMask, fy & fyMask);
reed@google.coma76de3d2011-01-13 18:30:42 +00001797
reed@android.com8a1c16f2008-12-17 15:59:43 +00001798 if (glyph.fWidth) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001799 proc(d1g, fx, fy, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001800 }
1801 pos += scalarsPerPosition;
1802 }
1803 } else {
1804 while (text < stop) {
bungeman@google.com9330cfe2012-01-04 14:17:00 +00001805 const char* currentText = text;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001806 const SkGlyph* glyph = &glyphCacheProc(cache, &text, 0, 0);
reed@google.coma76de3d2011-01-13 18:30:42 +00001807
reed@android.com8a1c16f2008-12-17 15:59:43 +00001808 if (glyph->fWidth) {
1809 SkDEBUGCODE(SkFixed prevAdvX = glyph->fAdvanceX;)
1810 SkDEBUGCODE(SkFixed prevAdvY = glyph->fAdvanceY;)
1811
1812 SkFixed fx, fy;
reed@android.comf2b98d62010-12-20 18:26:13 +00001813 SkFixed fxMask = ~0;
1814 SkFixed fyMask = ~0;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001815 tmsProc(tms, pos);
reed@google.coma76de3d2011-01-13 18:30:42 +00001816
reed@android.com8a1c16f2008-12-17 15:59:43 +00001817 {
1818 SkIPoint fixedLoc;
1819 alignProc(tms.fLoc, *glyph, &fixedLoc);
bungeman@google.com2211b622012-01-13 15:02:58 +00001820 fx = fixedLoc.fX + (SK_FixedHalf >> SkGlyph::kSubBits);
1821 fy = fixedLoc.fY + (SK_FixedHalf >> SkGlyph::kSubBits);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001822
reed@google.comcb6ccdd2011-08-23 21:30:47 +00001823 if (kX_SkAxisAlignment == roundBaseline) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001824 fyMask = 0;
reed@google.comcb6ccdd2011-08-23 21:30:47 +00001825 } else if (kY_SkAxisAlignment == roundBaseline) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001826 fxMask = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001827 }
1828 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001829
reed@android.com8a1c16f2008-12-17 15:59:43 +00001830 // have to call again, now that we've been "aligned"
bungeman@google.com9330cfe2012-01-04 14:17:00 +00001831 glyph = &glyphCacheProc(cache, &currentText,
1832 fx & fxMask, fy & fyMask);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001833 // the assumption is that the advance hasn't changed
1834 SkASSERT(prevAdvX == glyph->fAdvanceX);
1835 SkASSERT(prevAdvY == glyph->fAdvanceY);
reed@google.coma76de3d2011-01-13 18:30:42 +00001836
reed@android.comf2b98d62010-12-20 18:26:13 +00001837 proc(d1g, fx, fy, *glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001838 }
1839 pos += scalarsPerPosition;
1840 }
1841 }
1842 } else { // not subpixel
reed@google.comaeb07862012-04-18 18:32:04 +00001843 if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1844 while (text < stop) {
1845 // the last 2 parameters are ignored
1846 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001847
reed@google.comaeb07862012-04-18 18:32:04 +00001848 if (glyph.fWidth) {
1849 tmsProc(tms, pos);
reed@google.coma76de3d2011-01-13 18:30:42 +00001850
reed@google.comaeb07862012-04-18 18:32:04 +00001851 proc(d1g,
1852 SkScalarToFixed(tms.fLoc.fX) + SK_FixedHalf,
1853 SkScalarToFixed(tms.fLoc.fY) + SK_FixedHalf,
1854 glyph);
1855 }
1856 pos += scalarsPerPosition;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001857 }
reed@google.comaeb07862012-04-18 18:32:04 +00001858 } else {
1859 while (text < stop) {
1860 // the last 2 parameters are ignored
1861 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1862
1863 if (glyph.fWidth) {
1864 tmsProc(tms, pos);
1865
1866 SkIPoint fixedLoc;
1867 alignProc(tms.fLoc, glyph, &fixedLoc);
1868
1869 proc(d1g,
1870 fixedLoc.fX + SK_FixedHalf,
1871 fixedLoc.fY + SK_FixedHalf,
1872 glyph);
1873 }
1874 pos += scalarsPerPosition;
1875 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001876 }
1877 }
1878}
1879
1880#if defined _WIN32 && _MSC_VER >= 1300
1881#pragma warning ( pop )
1882#endif
1883
1884///////////////////////////////////////////////////////////////////////////////
1885
1886#include "SkPathMeasure.h"
1887
1888static void morphpoints(SkPoint dst[], const SkPoint src[], int count,
1889 SkPathMeasure& meas, const SkMatrix& matrix) {
1890 SkMatrix::MapXYProc proc = matrix.getMapXYProc();
1891
1892 for (int i = 0; i < count; i++) {
1893 SkPoint pos;
1894 SkVector tangent;
1895
1896 proc(matrix, src[i].fX, src[i].fY, &pos);
1897 SkScalar sx = pos.fX;
1898 SkScalar sy = pos.fY;
1899
reed@google.com8f17b0d2012-04-12 13:24:30 +00001900 if (!meas.getPosTan(sx, &pos, &tangent)) {
1901 // set to 0 if the measure failed, so that we just set dst == pos
1902 tangent.set(0, 0);
1903 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001904
1905 /* This is the old way (that explains our approach but is way too slow
1906 SkMatrix matrix;
1907 SkPoint pt;
1908
1909 pt.set(sx, sy);
1910 matrix.setSinCos(tangent.fY, tangent.fX);
1911 matrix.preTranslate(-sx, 0);
1912 matrix.postTranslate(pos.fX, pos.fY);
1913 matrix.mapPoints(&dst[i], &pt, 1);
1914 */
1915 dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy),
1916 pos.fY + SkScalarMul(tangent.fX, sy));
1917 }
1918}
1919
1920/* TODO
1921
1922 Need differentially more subdivisions when the follow-path is curvy. Not sure how to
1923 determine that, but we need it. I guess a cheap answer is let the caller tell us,
1924 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
1925*/
1926static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
1927 const SkMatrix& matrix) {
1928 SkPath::Iter iter(src, false);
1929 SkPoint srcP[4], dstP[3];
1930 SkPath::Verb verb;
1931
1932 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
1933 switch (verb) {
1934 case SkPath::kMove_Verb:
1935 morphpoints(dstP, srcP, 1, meas, matrix);
1936 dst->moveTo(dstP[0]);
1937 break;
1938 case SkPath::kLine_Verb:
1939 // turn lines into quads to look bendy
1940 srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX);
1941 srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY);
1942 morphpoints(dstP, srcP, 2, meas, matrix);
1943 dst->quadTo(dstP[0], dstP[1]);
1944 break;
1945 case SkPath::kQuad_Verb:
1946 morphpoints(dstP, &srcP[1], 2, meas, matrix);
1947 dst->quadTo(dstP[0], dstP[1]);
1948 break;
1949 case SkPath::kCubic_Verb:
1950 morphpoints(dstP, &srcP[1], 3, meas, matrix);
1951 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
1952 break;
1953 case SkPath::kClose_Verb:
1954 dst->close();
1955 break;
1956 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001957 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001958 break;
1959 }
1960 }
1961}
1962
1963void SkDraw::drawTextOnPath(const char text[], size_t byteLength,
1964 const SkPath& follow, const SkMatrix* matrix,
1965 const SkPaint& paint) const {
1966 SkASSERT(byteLength == 0 || text != NULL);
1967
1968 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001969 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001970 return;
1971 }
1972
djsollen@google.com166e6532012-03-20 14:24:38 +00001973 SkTextToPathIter iter(text, byteLength, paint, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001974 SkPathMeasure meas(follow, false);
1975 SkScalar hOffset = 0;
1976
1977 // need to measure first
1978 if (paint.getTextAlign() != SkPaint::kLeft_Align) {
1979 SkScalar pathLen = meas.getLength();
1980 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
1981 pathLen = SkScalarHalf(pathLen);
1982 }
1983 hOffset += pathLen;
1984 }
1985
1986 const SkPath* iterPath;
1987 SkScalar xpos;
1988 SkMatrix scaledMatrix;
1989 SkScalar scale = iter.getPathScale();
1990
1991 scaledMatrix.setScale(scale, scale);
reed@google.coma76de3d2011-01-13 18:30:42 +00001992
reed@google.com7b4531f2012-08-07 15:53:00 +00001993 while (iter.next(&iterPath, &xpos)) {
1994 if (iterPath) {
1995 SkPath tmp;
1996 SkMatrix m(scaledMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001997
reed@google.com7b4531f2012-08-07 15:53:00 +00001998 m.postTranslate(xpos + hOffset, 0);
1999 if (matrix) {
2000 m.postConcat(*matrix);
2001 }
2002 morphpath(&tmp, *iterPath, meas, m);
2003 if (fDevice) {
2004 fDevice->drawPath(*this, tmp, iter.getPaint(), NULL, true);
2005 } else {
2006 this->drawPath(tmp, iter.getPaint(), NULL, true);
2007 }
reed@android.comf2b98d62010-12-20 18:26:13 +00002008 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002009 }
2010}
2011
djsollen@google.com56c69772011-11-08 19:00:26 +00002012#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00002013void SkDraw::drawPosTextOnPath(const char text[], size_t byteLength,
2014 const SkPoint pos[], const SkPaint& paint,
2015 const SkPath& path, const SkMatrix* matrix) const {
2016 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00002017 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00002018 return;
2019 }
2020
2021 SkMatrix scaledMatrix;
2022 SkPathMeasure meas(path, false);
2023
2024 SkMeasureCacheProc glyphCacheProc = paint.getMeasureCacheProc(
2025 SkPaint::kForward_TextBufferDirection, true);
2026
2027 // Copied (modified) from SkTextToPathIter constructor to setup paint
2028 SkPaint tempPaint(paint);
2029
2030 tempPaint.setLinearText(true);
2031 tempPaint.setMaskFilter(NULL); // don't want this affecting our path-cache lookup
2032
2033 if (tempPaint.getPathEffect() == NULL && !(tempPaint.getStrokeWidth() > 0
2034 && tempPaint.getStyle() != SkPaint::kFill_Style)) {
2035 tempPaint.setStyle(SkPaint::kFill_Style);
2036 tempPaint.setPathEffect(NULL);
2037 }
2038 // End copied from SkTextToPathIter constructor
2039
2040 // detach cache
2041 SkGlyphCache* cache = tempPaint.detachCache(NULL);
2042
2043 // Must set scale, even if 1
2044 SkScalar scale = SK_Scalar1;
2045 scaledMatrix.setScale(scale, scale);
2046
2047 // Loop over all glyph ids
2048 for (const char* stop = text + byteLength; text < stop; pos++) {
2049
2050 const SkGlyph& glyph = glyphCacheProc(cache, &text);
2051 SkPath tmp;
2052
2053 const SkPath* glyphPath = cache->findPath(glyph);
2054 if (glyphPath == NULL) {
2055 continue;
2056 }
2057
2058 SkMatrix m(scaledMatrix);
2059 m.postTranslate(pos->fX, 0);
2060
2061 if (matrix) {
2062 m.postConcat(*matrix);
2063 }
2064
2065 morphpath(&tmp, *glyphPath, meas, m);
2066 this->drawPath(tmp, tempPaint);
2067
2068 }
2069
2070 // re-attach cache
2071 SkGlyphCache::AttachCache(cache);
2072}
2073#endif
2074
reed@android.com8a1c16f2008-12-17 15:59:43 +00002075///////////////////////////////////////////////////////////////////////////////
2076
2077struct VertState {
2078 int f0, f1, f2;
2079
2080 VertState(int vCount, const uint16_t indices[], int indexCount)
2081 : fIndices(indices) {
2082 fCurrIndex = 0;
2083 if (indices) {
2084 fCount = indexCount;
2085 } else {
2086 fCount = vCount;
2087 }
2088 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002089
2090 typedef bool (*Proc)(VertState*);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002091 Proc chooseProc(SkCanvas::VertexMode mode);
2092
2093private:
2094 int fCount;
2095 int fCurrIndex;
2096 const uint16_t* fIndices;
reed@google.coma76de3d2011-01-13 18:30:42 +00002097
reed@android.com8a1c16f2008-12-17 15:59:43 +00002098 static bool Triangles(VertState*);
2099 static bool TrianglesX(VertState*);
2100 static bool TriangleStrip(VertState*);
2101 static bool TriangleStripX(VertState*);
2102 static bool TriangleFan(VertState*);
2103 static bool TriangleFanX(VertState*);
2104};
2105
2106bool VertState::Triangles(VertState* state) {
2107 int index = state->fCurrIndex;
2108 if (index + 3 > state->fCount) {
2109 return false;
2110 }
2111 state->f0 = index + 0;
2112 state->f1 = index + 1;
2113 state->f2 = index + 2;
2114 state->fCurrIndex = index + 3;
2115 return true;
2116}
2117
2118bool VertState::TrianglesX(VertState* state) {
2119 const uint16_t* indices = state->fIndices;
2120 int index = state->fCurrIndex;
2121 if (index + 3 > state->fCount) {
2122 return false;
2123 }
2124 state->f0 = indices[index + 0];
2125 state->f1 = indices[index + 1];
2126 state->f2 = indices[index + 2];
2127 state->fCurrIndex = index + 3;
2128 return true;
2129}
2130
2131bool VertState::TriangleStrip(VertState* state) {
2132 int index = state->fCurrIndex;
2133 if (index + 3 > state->fCount) {
2134 return false;
2135 }
2136 state->f2 = index + 2;
2137 if (index & 1) {
2138 state->f0 = index + 1;
2139 state->f1 = index + 0;
2140 } else {
2141 state->f0 = index + 0;
2142 state->f1 = index + 1;
2143 }
2144 state->fCurrIndex = index + 1;
2145 return true;
2146}
2147
2148bool VertState::TriangleStripX(VertState* state) {
2149 const uint16_t* indices = state->fIndices;
2150 int index = state->fCurrIndex;
2151 if (index + 3 > state->fCount) {
2152 return false;
2153 }
2154 state->f2 = indices[index + 2];
2155 if (index & 1) {
2156 state->f0 = indices[index + 1];
2157 state->f1 = indices[index + 0];
2158 } else {
2159 state->f0 = indices[index + 0];
2160 state->f1 = indices[index + 1];
2161 }
2162 state->fCurrIndex = index + 1;
2163 return true;
2164}
2165
2166bool VertState::TriangleFan(VertState* state) {
2167 int index = state->fCurrIndex;
2168 if (index + 3 > state->fCount) {
2169 return false;
2170 }
2171 state->f0 = 0;
2172 state->f1 = index + 1;
2173 state->f2 = index + 2;
2174 state->fCurrIndex = index + 1;
2175 return true;
2176}
2177
2178bool VertState::TriangleFanX(VertState* state) {
2179 const uint16_t* indices = state->fIndices;
2180 int index = state->fCurrIndex;
2181 if (index + 3 > state->fCount) {
2182 return false;
2183 }
2184 state->f0 = indices[0];
2185 state->f1 = indices[index + 1];
2186 state->f2 = indices[index + 2];
2187 state->fCurrIndex = index + 1;
2188 return true;
2189}
2190
2191VertState::Proc VertState::chooseProc(SkCanvas::VertexMode mode) {
2192 switch (mode) {
2193 case SkCanvas::kTriangles_VertexMode:
2194 return fIndices ? TrianglesX : Triangles;
2195 case SkCanvas::kTriangleStrip_VertexMode:
2196 return fIndices ? TriangleStripX : TriangleStrip;
2197 case SkCanvas::kTriangleFan_VertexMode:
2198 return fIndices ? TriangleFanX : TriangleFan;
2199 default:
2200 return NULL;
2201 }
2202}
2203
reed@google.com045e62d2011-10-24 12:19:46 +00002204typedef void (*HairProc)(const SkPoint&, const SkPoint&, const SkRasterClip&,
reed@android.com8a1c16f2008-12-17 15:59:43 +00002205 SkBlitter*);
2206
2207static HairProc ChooseHairProc(bool doAntiAlias) {
2208 return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
2209}
2210
2211static bool texture_to_matrix(const VertState& state, const SkPoint verts[],
2212 const SkPoint texs[], SkMatrix* matrix) {
2213 SkPoint src[3], dst[3];
reed@google.coma76de3d2011-01-13 18:30:42 +00002214
reed@android.com8a1c16f2008-12-17 15:59:43 +00002215 src[0] = texs[state.f0];
2216 src[1] = texs[state.f1];
2217 src[2] = texs[state.f2];
2218 dst[0] = verts[state.f0];
2219 dst[1] = verts[state.f1];
2220 dst[2] = verts[state.f2];
2221 return matrix->setPolyToPoly(src, dst, 3);
2222}
2223
2224class SkTriColorShader : public SkShader {
2225public:
2226 SkTriColorShader() {}
2227
2228 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
reed@google.coma76de3d2011-01-13 18:30:42 +00002229
reed@android.com8a1c16f2008-12-17 15:59:43 +00002230 virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count);
reed@google.coma76de3d2011-01-13 18:30:42 +00002231
djsollen@google.comba28d032012-03-26 17:57:35 +00002232 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTriColorShader)
2233
reed@android.com8a1c16f2008-12-17 15:59:43 +00002234protected:
2235 SkTriColorShader(SkFlattenableReadBuffer& buffer) : SkShader(buffer) {}
reed@google.coma76de3d2011-01-13 18:30:42 +00002236
reed@android.com8a1c16f2008-12-17 15:59:43 +00002237private:
2238 SkMatrix fDstToUnit;
2239 SkPMColor fColors[3];
reed@google.coma76de3d2011-01-13 18:30:42 +00002240
reed@android.com8a1c16f2008-12-17 15:59:43 +00002241 typedef SkShader INHERITED;
2242};
2243
2244bool SkTriColorShader::setup(const SkPoint pts[], const SkColor colors[],
2245 int index0, int index1, int index2) {
reed@google.coma76de3d2011-01-13 18:30:42 +00002246
reed@android.com8a1c16f2008-12-17 15:59:43 +00002247 fColors[0] = SkPreMultiplyColor(colors[index0]);
2248 fColors[1] = SkPreMultiplyColor(colors[index1]);
2249 fColors[2] = SkPreMultiplyColor(colors[index2]);
reed@google.coma76de3d2011-01-13 18:30:42 +00002250
reed@android.com8a1c16f2008-12-17 15:59:43 +00002251 SkMatrix m, im;
2252 m.reset();
2253 m.set(0, pts[index1].fX - pts[index0].fX);
2254 m.set(1, pts[index2].fX - pts[index0].fX);
2255 m.set(2, pts[index0].fX);
2256 m.set(3, pts[index1].fY - pts[index0].fY);
2257 m.set(4, pts[index2].fY - pts[index0].fY);
2258 m.set(5, pts[index0].fY);
2259 if (!m.invert(&im)) {
2260 return false;
2261 }
2262 return fDstToUnit.setConcat(im, this->getTotalInverse());
2263}
2264
2265#include "SkColorPriv.h"
reed@android.com689411a2008-12-18 02:52:32 +00002266#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00002267
2268static int ScalarTo256(SkScalar v) {
2269 int scale = SkScalarToFixed(v) >> 8;
2270 if (scale < 0) {
2271 scale = 0;
2272 }
2273 if (scale > 255) {
2274 scale = 255;
2275 }
2276 return SkAlpha255To256(scale);
2277}
2278
2279void SkTriColorShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
2280 SkPoint src;
reed@google.coma76de3d2011-01-13 18:30:42 +00002281
reed@android.com8a1c16f2008-12-17 15:59:43 +00002282 for (int i = 0; i < count; i++) {
2283 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
2284 x += 1;
reed@google.coma76de3d2011-01-13 18:30:42 +00002285
reed@android.com8a1c16f2008-12-17 15:59:43 +00002286 int scale1 = ScalarTo256(src.fX);
2287 int scale2 = ScalarTo256(src.fY);
2288 int scale0 = 256 - scale1 - scale2;
2289 if (scale0 < 0) {
2290 if (scale1 > scale2) {
2291 scale2 = 256 - scale1;
2292 } else {
2293 scale1 = 256 - scale2;
2294 }
2295 scale0 = 0;
2296 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002297
reed@android.com8a1c16f2008-12-17 15:59:43 +00002298 dstC[i] = SkAlphaMulQ(fColors[0], scale0) +
2299 SkAlphaMulQ(fColors[1], scale1) +
2300 SkAlphaMulQ(fColors[2], scale2);
2301 }
2302}
2303
2304void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
2305 const SkPoint vertices[], const SkPoint textures[],
2306 const SkColor colors[], SkXfermode* xmode,
2307 const uint16_t indices[], int indexCount,
2308 const SkPaint& paint) const {
2309 SkASSERT(0 == count || NULL != vertices);
reed@google.coma76de3d2011-01-13 18:30:42 +00002310
reed@android.com8a1c16f2008-12-17 15:59:43 +00002311 // abort early if there is nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00002312 if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002313 return;
2314 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002315
reed@android.com8a1c16f2008-12-17 15:59:43 +00002316 // transform out vertices into device coordinates
2317 SkAutoSTMalloc<16, SkPoint> storage(count);
2318 SkPoint* devVerts = storage.get();
2319 fMatrix->mapPoints(devVerts, vertices, count);
reed@google.coma76de3d2011-01-13 18:30:42 +00002320
reed@android.com8a1c16f2008-12-17 15:59:43 +00002321 if (fBounder) {
2322 SkRect bounds;
2323 bounds.set(devVerts, count);
2324 if (!fBounder->doRect(bounds, paint)) {
2325 return;
2326 }
2327 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002328
reed@android.com8a1c16f2008-12-17 15:59:43 +00002329 /*
2330 We can draw the vertices in 1 of 4 ways:
2331
2332 - solid color (no shader/texture[], no colors[])
2333 - just colors (no shader/texture[], has colors[])
2334 - just texture (has shader/texture[], no colors[])
2335 - colors * texture (has shader/texture[], has colors[])
reed@google.coma76de3d2011-01-13 18:30:42 +00002336
reed@android.com8a1c16f2008-12-17 15:59:43 +00002337 Thus for texture drawing, we need both texture[] and a shader.
2338 */
2339
2340 SkTriColorShader triShader; // must be above declaration of p
2341 SkPaint p(paint);
2342
2343 SkShader* shader = p.getShader();
2344 if (NULL == shader) {
2345 // if we have no shader, we ignore the texture coordinates
2346 textures = NULL;
2347 } else if (NULL == textures) {
2348 // if we don't have texture coordinates, ignore the shader
2349 p.setShader(NULL);
2350 shader = NULL;
2351 }
2352
2353 // setup the custom shader (if needed)
2354 if (NULL != colors) {
2355 if (NULL == textures) {
2356 // just colors (no texture)
2357 p.setShader(&triShader);
2358 } else {
2359 // colors * texture
2360 SkASSERT(shader);
2361 bool releaseMode = false;
2362 if (NULL == xmode) {
reed@android.com845fdac2009-06-23 03:01:32 +00002363 xmode = SkXfermode::Create(SkXfermode::kMultiply_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002364 releaseMode = true;
2365 }
2366 SkShader* compose = SkNEW_ARGS(SkComposeShader,
2367 (&triShader, shader, xmode));
2368 p.setShader(compose)->unref();
2369 if (releaseMode) {
2370 xmode->unref();
2371 }
2372 }
2373 }
2374
2375 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
2376 // setup our state and function pointer for iterating triangles
2377 VertState state(count, indices, indexCount);
2378 VertState::Proc vertProc = state.chooseProc(vmode);
reed@google.coma76de3d2011-01-13 18:30:42 +00002379
reed@android.com8a1c16f2008-12-17 15:59:43 +00002380 if (NULL != textures || NULL != colors) {
2381 SkMatrix localM, tempM;
2382 bool hasLocalM = shader && shader->getLocalMatrix(&localM);
reed@google.coma76de3d2011-01-13 18:30:42 +00002383
reed@android.com8a1c16f2008-12-17 15:59:43 +00002384 if (NULL != colors) {
2385 if (!triShader.setContext(*fBitmap, p, *fMatrix)) {
2386 colors = NULL;
2387 }
2388 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002389
reed@android.com8a1c16f2008-12-17 15:59:43 +00002390 while (vertProc(&state)) {
2391 if (NULL != textures) {
2392 if (texture_to_matrix(state, vertices, textures, &tempM)) {
2393 if (hasLocalM) {
2394 tempM.postConcat(localM);
2395 }
2396 shader->setLocalMatrix(tempM);
2397 // need to recal setContext since we changed the local matrix
2398 if (!shader->setContext(*fBitmap, p, *fMatrix)) {
2399 continue;
2400 }
2401 }
2402 }
2403 if (NULL != colors) {
2404 if (!triShader.setup(vertices, colors,
2405 state.f0, state.f1, state.f2)) {
2406 continue;
2407 }
2408 }
reed@google.com045e62d2011-10-24 12:19:46 +00002409
2410 SkPoint tmp[] = {
2411 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
2412 };
2413 SkScan::FillTriangle(tmp, *fRC, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002414 }
2415 // now restore the shader's original local matrix
2416 if (NULL != shader) {
2417 if (hasLocalM) {
2418 shader->setLocalMatrix(localM);
2419 } else {
2420 shader->resetLocalMatrix();
2421 }
2422 }
2423 } else {
2424 // no colors[] and no texture
2425 HairProc hairProc = ChooseHairProc(paint.isAntiAlias());
reed@google.com045e62d2011-10-24 12:19:46 +00002426 const SkRasterClip& clip = *fRC;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002427 while (vertProc(&state)) {
reed@google.com045e62d2011-10-24 12:19:46 +00002428 hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get());
2429 hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get());
2430 hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002431 }
2432 }
2433}
2434
reed@google.com0a0a2362011-03-23 13:51:55 +00002435///////////////////////////////////////////////////////////////////////////////
2436///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00002437
2438#ifdef SK_DEBUG
2439
reed@android.comf2b98d62010-12-20 18:26:13 +00002440void SkDraw::validate() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002441 SkASSERT(fBitmap != NULL);
2442 SkASSERT(fMatrix != NULL);
2443 SkASSERT(fClip != NULL);
reed@google.com045e62d2011-10-24 12:19:46 +00002444 SkASSERT(fRC != NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002445
reed@google.com045e62d2011-10-24 12:19:46 +00002446 const SkIRect& cr = fRC->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002447 SkIRect br;
2448
reed@android.comf2b98d62010-12-20 18:26:13 +00002449 br.set(0, 0, fBitmap->width(), fBitmap->height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002450 SkASSERT(cr.isEmpty() || br.contains(cr));
2451}
2452
2453#endif
2454
reed@google.com0a0a2362011-03-23 13:51:55 +00002455///////////////////////////////////////////////////////////////////////////////
2456
2457SkBounder::SkBounder() {
2458 // initialize up front. This gets reset by SkCanvas before each draw call.
2459 fClip = &SkRegion::GetEmptyRegion();
2460}
reed@android.com8a1c16f2008-12-17 15:59:43 +00002461
2462bool SkBounder::doIRect(const SkIRect& r) {
2463 SkIRect rr;
2464 return rr.intersect(fClip->getBounds(), r) && this->onIRect(rr);
2465}
2466
reed@android.comd055c1f2010-03-01 14:54:05 +00002467// TODO: change the prototype to take fixed, and update the callers
2468bool SkBounder::doIRectGlyph(const SkIRect& r, int x, int y,
2469 const SkGlyph& glyph) {
reed@android.com474a12c2010-01-04 19:35:33 +00002470 SkIRect rr;
reed@android.comd055c1f2010-03-01 14:54:05 +00002471 if (!rr.intersect(fClip->getBounds(), r)) {
2472 return false;
2473 }
2474 GlyphRec rec;
2475 rec.fLSB.set(SkIntToFixed(x), SkIntToFixed(y));
2476 rec.fRSB.set(rec.fLSB.fX + glyph.fAdvanceX,
2477 rec.fLSB.fY + glyph.fAdvanceY);
2478 rec.fGlyphID = glyph.getGlyphID();
2479 rec.fFlags = 0;
2480 return this->onIRectGlyph(rr, rec);
reed@android.com474a12c2010-01-04 19:35:33 +00002481}
2482
reed@android.com8a1c16f2008-12-17 15:59:43 +00002483bool SkBounder::doHairline(const SkPoint& pt0, const SkPoint& pt1,
2484 const SkPaint& paint) {
2485 SkIRect r;
2486 SkScalar v0, v1;
2487
2488 v0 = pt0.fX;
2489 v1 = pt1.fX;
2490 if (v0 > v1) {
2491 SkTSwap<SkScalar>(v0, v1);
2492 }
2493 r.fLeft = SkScalarFloor(v0);
2494 r.fRight = SkScalarCeil(v1);
2495
2496 v0 = pt0.fY;
2497 v1 = pt1.fY;
2498 if (v0 > v1) {
2499 SkTSwap<SkScalar>(v0, v1);
2500 }
2501 r.fTop = SkScalarFloor(v0);
2502 r.fBottom = SkScalarCeil(v1);
2503
2504 if (paint.isAntiAlias()) {
2505 r.inset(-1, -1);
2506 }
2507 return this->doIRect(r);
2508}
2509
2510bool SkBounder::doRect(const SkRect& rect, const SkPaint& paint) {
2511 SkIRect r;
2512
2513 if (paint.getStyle() == SkPaint::kFill_Style) {
2514 rect.round(&r);
2515 } else {
2516 int rad = -1;
2517 rect.roundOut(&r);
2518 if (paint.isAntiAlias()) {
2519 rad = -2;
2520 }
2521 r.inset(rad, rad);
2522 }
2523 return this->doIRect(r);
2524}
2525
2526bool SkBounder::doPath(const SkPath& path, const SkPaint& paint, bool doFill) {
reed@android.comd252db02009-04-01 18:31:44 +00002527 SkIRect r;
2528 const SkRect& bounds = path.getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002529
2530 if (doFill) {
2531 bounds.round(&r);
2532 } else { // hairline
2533 bounds.roundOut(&r);
2534 }
2535
2536 if (paint.isAntiAlias()) {
2537 r.inset(-1, -1);
2538 }
2539 return this->doIRect(r);
2540}
2541
2542void SkBounder::commit() {
2543 // override in subclass
2544}
2545
2546////////////////////////////////////////////////////////////////////////////////////////////////
2547
2548#include "SkPath.h"
2549#include "SkDraw.h"
2550#include "SkRegion.h"
2551#include "SkBlitter.h"
2552
2553static bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds,
2554 SkMaskFilter* filter, const SkMatrix* filterMatrix,
2555 SkIRect* bounds) {
2556 if (devPath.isEmpty()) {
2557 return false;
2558 }
2559
reed@android.com8a1c16f2008-12-17 15:59:43 +00002560 // init our bounds from the path
2561 {
reed@android.comd252db02009-04-01 18:31:44 +00002562 SkRect pathBounds = devPath.getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002563 pathBounds.inset(-SK_ScalarHalf, -SK_ScalarHalf);
2564 pathBounds.roundOut(bounds);
2565 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002566
tomhudson@google.com6db75fc2012-03-23 14:46:48 +00002567 SkIPoint margin = SkIPoint::Make(0, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002568 if (filter) {
2569 SkASSERT(filterMatrix);
reed@google.coma76de3d2011-01-13 18:30:42 +00002570
bungeman@google.com5af16f82011-09-02 15:06:44 +00002571 SkMask srcM, dstM;
reed@google.coma76de3d2011-01-13 18:30:42 +00002572
reed@android.com8a1c16f2008-12-17 15:59:43 +00002573 srcM.fBounds = *bounds;
2574 srcM.fFormat = SkMask::kA8_Format;
2575 srcM.fImage = NULL;
2576 if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
2577 return false;
2578 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002579 }
2580
bungeman@google.com5af16f82011-09-02 15:06:44 +00002581 // (possibly) trim the bounds to reflect the clip
reed@android.com8a1c16f2008-12-17 15:59:43 +00002582 // (plus whatever slop the filter needs)
bungeman@google.com5af16f82011-09-02 15:06:44 +00002583 if (clipBounds) {
2584 SkIRect tmp = *clipBounds;
reed@android.com35555912009-03-16 18:46:55 +00002585 // Ugh. Guard against gigantic margins from wacky filters. Without this
2586 // check we can request arbitrary amounts of slop beyond our visible
2587 // clip, and bring down the renderer (at least on finite RAM machines
2588 // like handsets, etc.). Need to balance this invented value between
2589 // quality of large filters like blurs, and the corresponding memory
2590 // requests.
2591 static const int MAX_MARGIN = 128;
2592 tmp.inset(-SkMin32(margin.fX, MAX_MARGIN),
2593 -SkMin32(margin.fY, MAX_MARGIN));
bungeman@google.com5af16f82011-09-02 15:06:44 +00002594 if (!bounds->intersect(tmp)) {
2595 return false;
2596 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002597 }
2598
2599 return true;
2600}
2601
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002602static void draw_into_mask(const SkMask& mask, const SkPath& devPath,
2603 SkPaint::Style style) {
reed@google.com045e62d2011-10-24 12:19:46 +00002604 SkBitmap bm;
2605 SkDraw draw;
2606 SkRasterClip clip;
2607 SkMatrix matrix;
2608 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002609
2610 bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), mask.fBounds.height(), mask.fRowBytes);
2611 bm.setPixels(mask.fImage);
2612
reed@google.com045e62d2011-10-24 12:19:46 +00002613 clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height()));
reed@android.com8a1c16f2008-12-17 15:59:43 +00002614 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
2615 -SkIntToScalar(mask.fBounds.fTop));
2616
2617 draw.fBitmap = &bm;
reed@google.com045e62d2011-10-24 12:19:46 +00002618 draw.fRC = &clip;
2619 draw.fClip = &clip.bwRgn();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002620 draw.fMatrix = &matrix;
2621 draw.fBounder = NULL;
2622 paint.setAntiAlias(true);
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002623 paint.setStyle(style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002624 draw.drawPath(devPath, paint);
2625}
2626
2627bool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
2628 SkMaskFilter* filter, const SkMatrix* filterMatrix,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002629 SkMask* mask, SkMask::CreateMode mode,
2630 SkPaint::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002631 if (SkMask::kJustRenderImage_CreateMode != mode) {
2632 if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds))
2633 return false;
2634 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002635
reed@android.com8a1c16f2008-12-17 15:59:43 +00002636 if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
2637 mask->fFormat = SkMask::kA8_Format;
2638 mask->fRowBytes = mask->fBounds.width();
reed@android.com543ed932009-04-24 12:43:40 +00002639 size_t size = mask->computeImageSize();
2640 if (0 == size) {
2641 // we're too big to allocate the mask, abort
2642 return false;
2643 }
2644 mask->fImage = SkMask::AllocImage(size);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002645 memset(mask->fImage, 0, mask->computeImageSize());
2646 }
2647
2648 if (SkMask::kJustComputeBounds_CreateMode != mode) {
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002649 draw_into_mask(*mask, devPath, style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002650 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002651
reed@android.com8a1c16f2008-12-17 15:59:43 +00002652 return true;
2653}