2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 #define __STDC_LIMIT_MACROS
10 #include "SkBlitter.h"
12 #include "SkColorPriv.h"
14 #include "SkDeviceLooper.h"
16 #include "SkMaskFilter.h"
18 #include "SkPathEffect.h"
19 #include "SkRasterClip.h"
20 #include "SkRasterizer.h"
24 #include "SkSmallAllocator.h"
27 #include "SkTextMapStateProc.h"
30 #include "SkVertState.h"
32 #include "SkAutoKern.h"
33 #include "SkBitmapProcShader.h"
34 #include "SkDrawProcs.h"
35 #include "SkMatrixUtils.h"
37 //#define TRACE_BITMAP_DRAWS
40 /** Helper for allocating small blitters on the stack.
42 class SkAutoBlitterChoose : SkNoncopyable {
44 SkAutoBlitterChoose() {
47 SkAutoBlitterChoose(const SkBitmap& device, const SkMatrix& matrix,
48 const SkPaint& paint, bool drawCoverage = false) {
49 fBlitter = SkBlitter::Choose(device, matrix, paint, &fAllocator,
53 SkBlitter* operator->() { return fBlitter; }
54 SkBlitter* get() const { return fBlitter; }
56 void choose(const SkBitmap& device, const SkMatrix& matrix,
57 const SkPaint& paint, bool drawCoverage = false) {
59 fBlitter = SkBlitter::Choose(device, matrix, paint, &fAllocator,
64 // Owned by fAllocator, which will handle the delete.
66 SkTBlitterAllocator fAllocator;
68 #define SkAutoBlitterChoose(...) SK_REQUIRE_LOCAL_VAR(SkAutoBlitterChoose)
71 * Since we are providing the storage for the shader (to avoid the perf cost
72 * of calling new) we insist that in our destructor we can account for all
73 * owners of the shader.
75 class SkAutoBitmapShaderInstall : SkNoncopyable {
77 SkAutoBitmapShaderInstall(const SkBitmap& src, const SkPaint& paint,
78 const SkMatrix* localMatrix = NULL)
79 : fPaint(paint) /* makes a copy of the paint */ {
80 fPaint.setShader(SkCreateBitmapShader(src, SkShader::kClamp_TileMode,
81 SkShader::kClamp_TileMode,
82 localMatrix, &fAllocator));
83 // we deliberately left the shader with an owner-count of 2
84 SkASSERT(2 == fPaint.getShader()->getRefCnt());
87 ~SkAutoBitmapShaderInstall() {
88 // since fAllocator will destroy shader, we insist that owners == 2
89 SkASSERT(2 == fPaint.getShader()->getRefCnt());
91 fPaint.setShader(NULL); // unref the shader by 1
95 // return the new paint that has the shader applied
96 const SkPaint& paintWithShader() const { return fPaint; }
99 // copy of caller's paint (which we then modify)
101 // Stores the shader.
102 SkTBlitterAllocator fAllocator;
104 #define SkAutoBitmapShaderInstall(...) SK_REQUIRE_LOCAL_VAR(SkAutoBitmapShaderInstall)
106 ///////////////////////////////////////////////////////////////////////////////
109 sk_bzero(this, sizeof(*this));
112 SkDraw::SkDraw(const SkDraw& src) {
113 memcpy(this, &src, sizeof(*this));
116 bool SkDraw::computeConservativeLocalClipBounds(SkRect* localBounds) const {
117 if (fRC->isEmpty()) {
122 if (!fMatrix->invert(&inverse)) {
126 SkIRect devBounds = fRC->getBounds();
127 // outset to have slop for antialasing and hairlines
128 devBounds.outset(1, 1);
129 inverse.mapRect(localBounds, SkRect::Make(devBounds));
133 ///////////////////////////////////////////////////////////////////////////////
135 typedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data);
137 static void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) {
138 sk_bzero(pixels, bytes);
141 static void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {}
143 static void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
144 sk_memset32((uint32_t*)pixels, data, SkToInt(bytes >> 2));
147 static void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
148 sk_memset16((uint16_t*)pixels, data, SkToInt(bytes >> 1));
151 static void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
152 memset(pixels, data, bytes);
155 static BitmapXferProc ChooseBitmapXferProc(const SkBitmap& bitmap,
156 const SkPaint& paint,
158 // todo: we can apply colorfilter up front if no shader, so we wouldn't
159 // need to abort this fastpath
160 if (paint.getShader() || paint.getColorFilter()) {
164 SkXfermode::Mode mode;
165 if (!SkXfermode::AsMode(paint.getXfermode(), &mode)) {
169 SkColor color = paint.getColor();
171 // collaps modes based on color...
172 if (SkXfermode::kSrcOver_Mode == mode) {
173 unsigned alpha = SkColorGetA(color);
175 mode = SkXfermode::kDst_Mode;
176 } else if (0xFF == alpha) {
177 mode = SkXfermode::kSrc_Mode;
182 case SkXfermode::kClear_Mode:
183 // SkDebugf("--- D_Clear_BitmapXferProc\n");
184 return D_Clear_BitmapXferProc; // ignore data
185 case SkXfermode::kDst_Mode:
186 // SkDebugf("--- D_Dst_BitmapXferProc\n");
187 return D_Dst_BitmapXferProc; // ignore data
188 case SkXfermode::kSrc_Mode: {
190 should I worry about dithering for the lower depths?
192 SkPMColor pmc = SkPreMultiplyColor(color);
193 switch (bitmap.colorType()) {
194 case kN32_SkColorType:
198 // SkDebugf("--- D32_Src_BitmapXferProc\n");
199 return D32_Src_BitmapXferProc;
200 case kRGB_565_SkColorType:
202 *data = SkPixel32ToPixel16(pmc);
204 // SkDebugf("--- D16_Src_BitmapXferProc\n");
205 return D16_Src_BitmapXferProc;
206 case kAlpha_8_SkColorType:
208 *data = SkGetPackedA32(pmc);
210 // SkDebugf("--- DA8_Src_BitmapXferProc\n");
211 return DA8_Src_BitmapXferProc;
223 static void CallBitmapXferProc(const SkBitmap& bitmap, const SkIRect& rect,
224 BitmapXferProc proc, uint32_t procData) {
226 switch (bitmap.colorType()) {
227 case kN32_SkColorType:
230 case kRGB_565_SkColorType:
233 case kAlpha_8_SkColorType:
237 SkDEBUGFAIL("Can't use xferproc on this config");
241 uint8_t* pixels = (uint8_t*)bitmap.getPixels();
243 const size_t rowBytes = bitmap.rowBytes();
244 const int widthBytes = rect.width() << shiftPerPixel;
246 // skip down to the first scanline and X position
247 pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel);
248 for (int scans = rect.height() - 1; scans >= 0; --scans) {
249 proc(pixels, widthBytes, procData);
254 void SkDraw::drawPaint(const SkPaint& paint) const {
255 SkDEBUGCODE(this->validate();)
257 if (fRC->isEmpty()) {
262 devRect.set(0, 0, fBitmap->width(), fBitmap->height());
265 /* If we don't have a shader (i.e. we're just a solid color) we may
266 be faster to operate directly on the device bitmap, rather than invoking
267 a blitter. Esp. true for xfermodes, which require a colorshader to be
268 present, which is just redundant work. Since we're drawing everywhere
269 in the clip, we don't have to worry about antialiasing.
271 uint32_t procData = 0; // to avoid the warning
272 BitmapXferProc proc = ChooseBitmapXferProc(*fBitmap, paint, &procData);
274 if (D_Dst_BitmapXferProc == proc) { // nothing to do
278 SkRegion::Iterator iter(fRC->bwRgn());
279 while (!iter.done()) {
280 CallBitmapXferProc(*fBitmap, iter.rect(), proc, procData);
287 // normal case: use a blitter
288 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
289 SkScan::FillIRect(devRect, *fRC, blitter.get());
292 ///////////////////////////////////////////////////////////////////////////////
295 SkCanvas::PointMode fMode;
296 const SkPaint* fPaint;
297 const SkRegion* fClip;
298 const SkRasterClip* fRC;
303 typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count,
306 bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix,
307 const SkRasterClip*);
308 Proc chooseProc(SkBlitter** blitter);
311 SkAAClipBlitterWrapper fWrapper;
314 static void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
315 int count, SkBlitter* blitter) {
316 SkASSERT(rec.fClip->isRect());
317 const SkIRect& r = rec.fClip->getBounds();
319 for (int i = 0; i < count; i++) {
320 int x = SkScalarFloorToInt(devPts[i].fX);
321 int y = SkScalarFloorToInt(devPts[i].fY);
322 if (r.contains(x, y)) {
323 blitter->blitH(x, y, 1);
328 static void bw_pt_rect_16_hair_proc(const PtProcRec& rec,
329 const SkPoint devPts[], int count,
330 SkBlitter* blitter) {
331 SkASSERT(rec.fRC->isRect());
332 const SkIRect& r = rec.fRC->getBounds();
334 const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
337 uint16_t* addr = bitmap->getAddr16(0, 0);
338 size_t rb = bitmap->rowBytes();
340 for (int i = 0; i < count; i++) {
341 int x = SkScalarFloorToInt(devPts[i].fX);
342 int y = SkScalarFloorToInt(devPts[i].fY);
343 if (r.contains(x, y)) {
344 ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value);
349 static void bw_pt_rect_32_hair_proc(const PtProcRec& rec,
350 const SkPoint devPts[], int count,
351 SkBlitter* blitter) {
352 SkASSERT(rec.fRC->isRect());
353 const SkIRect& r = rec.fRC->getBounds();
355 const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
358 SkPMColor* addr = bitmap->getAddr32(0, 0);
359 size_t rb = bitmap->rowBytes();
361 for (int i = 0; i < count; i++) {
362 int x = SkScalarFloorToInt(devPts[i].fX);
363 int y = SkScalarFloorToInt(devPts[i].fY);
364 if (r.contains(x, y)) {
365 ((SkPMColor*)((char*)addr + y * rb))[x] = value;
370 static void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
371 int count, SkBlitter* blitter) {
372 for (int i = 0; i < count; i++) {
373 int x = SkScalarFloorToInt(devPts[i].fX);
374 int y = SkScalarFloorToInt(devPts[i].fY);
375 if (rec.fClip->contains(x, y)) {
376 blitter->blitH(x, y, 1);
381 static void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
382 int count, SkBlitter* blitter) {
383 for (int i = 0; i < count; i += 2) {
384 SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
388 static void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
389 int count, SkBlitter* blitter) {
390 for (int i = 0; i < count - 1; i++) {
391 SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
397 static void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
398 int count, SkBlitter* blitter) {
399 for (int i = 0; i < count; i += 2) {
400 SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
404 static void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
405 int count, SkBlitter* blitter) {
406 for (int i = 0; i < count - 1; i++) {
407 SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
411 // square procs (strokeWidth > 0 but matrix is square-scale (sx == sy)
413 static void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[],
414 int count, SkBlitter* blitter) {
415 const SkFixed radius = rec.fRadius;
416 for (int i = 0; i < count; i++) {
417 SkFixed x = SkScalarToFixed(devPts[i].fX);
418 SkFixed y = SkScalarToFixed(devPts[i].fY);
421 r.fLeft = x - radius;
423 r.fRight = x + radius;
424 r.fBottom = y + radius;
426 SkScan::FillXRect(r, *rec.fRC, blitter);
430 static void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[],
431 int count, SkBlitter* blitter) {
432 const SkFixed radius = rec.fRadius;
433 for (int i = 0; i < count; i++) {
434 SkFixed x = SkScalarToFixed(devPts[i].fX);
435 SkFixed y = SkScalarToFixed(devPts[i].fY);
438 r.fLeft = x - radius;
440 r.fRight = x + radius;
441 r.fBottom = y + radius;
443 SkScan::AntiFillXRect(r, *rec.fRC, blitter);
447 // If this guy returns true, then chooseProc() must return a valid proc
448 bool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint,
449 const SkMatrix* matrix, const SkRasterClip* rc) {
450 if (paint.getPathEffect()) {
453 SkScalar width = paint.getStrokeWidth();
459 fRadius = SK_FixedHalf;
462 if (paint.getStrokeCap() != SkPaint::kRound_Cap &&
463 matrix->isScaleTranslate() && SkCanvas::kPoints_PointMode == mode) {
464 SkScalar sx = matrix->get(SkMatrix::kMScaleX);
465 SkScalar sy = matrix->get(SkMatrix::kMScaleY);
466 if (SkScalarNearlyZero(sx - sy)) {
475 fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1;
482 PtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) {
485 SkBlitter* blitter = *blitterPtr;
487 fClip = &fRC->bwRgn();
489 fWrapper.init(*fRC, blitter);
490 fClip = &fWrapper.getRgn();
491 blitter = fWrapper.getBlitter();
492 *blitterPtr = blitter;
496 SkASSERT(0 == SkCanvas::kPoints_PointMode);
497 SkASSERT(1 == SkCanvas::kLines_PointMode);
498 SkASSERT(2 == SkCanvas::kPolygon_PointMode);
499 SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode);
501 if (fPaint->isAntiAlias()) {
502 if (0 == fPaint->getStrokeWidth()) {
503 static const Proc gAAProcs[] = {
504 aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc
506 proc = gAAProcs[fMode];
507 } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) {
508 SkASSERT(SkCanvas::kPoints_PointMode == fMode);
509 proc = aa_square_proc;
512 if (fRadius <= SK_FixedHalf) { // small radii and hairline
513 if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) {
515 const SkBitmap* bm = blitter->justAnOpaqueColor(&value);
516 if (bm && kRGB_565_SkColorType == bm->colorType()) {
517 proc = bw_pt_rect_16_hair_proc;
518 } else if (bm && kN32_SkColorType == bm->colorType()) {
519 proc = bw_pt_rect_32_hair_proc;
521 proc = bw_pt_rect_hair_proc;
524 static Proc gBWProcs[] = {
525 bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc
527 proc = gBWProcs[fMode];
530 proc = bw_square_proc;
536 // each of these costs 8-bytes of stack space, so don't make it too large
537 // must be even for lines/polygon to work
538 #define MAX_DEV_PTS 32
540 void SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count,
541 const SkPoint pts[], const SkPaint& paint,
542 bool forceUseDevice) const {
543 // if we're in lines mode, force count to be even
544 if (SkCanvas::kLines_PointMode == mode) {
548 if ((long)count <= 0) {
552 SkASSERT(pts != NULL);
553 SkDEBUGCODE(this->validate();)
556 if (fRC->isEmpty()) {
561 if (!forceUseDevice && rec.init(mode, paint, fMatrix, fRC)) {
562 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
564 SkPoint devPts[MAX_DEV_PTS];
565 const SkMatrix* matrix = fMatrix;
566 SkBlitter* bltr = blitter.get();
567 PtProcRec::Proc proc = rec.chooseProc(&bltr);
568 // we have to back up subsequent passes if we're in polygon mode
569 const size_t backup = (SkCanvas::kPolygon_PointMode == mode);
572 int n = SkToInt(count);
573 if (n > MAX_DEV_PTS) {
576 matrix->mapPoints(devPts, pts, n);
577 proc(rec, devPts, n, bltr);
579 SkASSERT(SkToInt(count) >= n);
584 } while (count != 0);
587 case SkCanvas::kPoints_PointMode: {
588 // temporarily mark the paint as filling.
589 SkPaint newPaint(paint);
590 newPaint.setStyle(SkPaint::kFill_Style);
592 SkScalar width = newPaint.getStrokeWidth();
593 SkScalar radius = SkScalarHalf(width);
595 if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) {
599 path.addCircle(0, 0, radius);
600 for (size_t i = 0; i < count; i++) {
601 preMatrix.setTranslate(pts[i].fX, pts[i].fY);
602 // pass true for the last point, since we can modify
604 path.setIsVolatile((count-1) == i);
606 fDevice->drawPath(*this, path, newPaint, &preMatrix,
609 this->drawPath(path, newPaint, &preMatrix,
616 for (size_t i = 0; i < count; i++) {
617 r.fLeft = pts[i].fX - radius;
618 r.fTop = pts[i].fY - radius;
619 r.fRight = r.fLeft + width;
620 r.fBottom = r.fTop + width;
622 fDevice->drawRect(*this, r, newPaint);
624 this->drawRect(r, newPaint);
630 case SkCanvas::kLines_PointMode:
631 if (2 == count && paint.getPathEffect()) {
632 // most likely a dashed line - see if it is one of the ones
634 SkStrokeRec rec(paint);
635 SkPathEffect::PointData pointData;
641 SkRect cullRect = SkRect::Make(fRC->getBounds());
643 if (paint.getPathEffect()->asPoints(&pointData, path, rec,
644 *fMatrix, &cullRect)) {
645 // 'asPoints' managed to find some fast path
648 newP.setPathEffect(NULL);
649 newP.setStyle(SkPaint::kFill_Style);
651 if (!pointData.fFirst.isEmpty()) {
653 fDevice->drawPath(*this, pointData.fFirst, newP);
655 this->drawPath(pointData.fFirst, newP);
659 if (!pointData.fLast.isEmpty()) {
661 fDevice->drawPath(*this, pointData.fLast, newP);
663 this->drawPath(pointData.fLast, newP);
667 if (pointData.fSize.fX == pointData.fSize.fY) {
668 // The rest of the dashed line can just be drawn as points
669 SkASSERT(pointData.fSize.fX == SkScalarHalf(newP.getStrokeWidth()));
671 if (SkPathEffect::PointData::kCircles_PointFlag & pointData.fFlags) {
672 newP.setStrokeCap(SkPaint::kRound_Cap);
674 newP.setStrokeCap(SkPaint::kButt_Cap);
678 fDevice->drawPoints(*this,
679 SkCanvas::kPoints_PointMode,
680 pointData.fNumPoints,
684 this->drawPoints(SkCanvas::kPoints_PointMode,
685 pointData.fNumPoints,
692 // The rest of the dashed line must be drawn as rects
693 SkASSERT(!(SkPathEffect::PointData::kCircles_PointFlag &
698 for (int i = 0; i < pointData.fNumPoints; ++i) {
699 r.set(pointData.fPoints[i].fX - pointData.fSize.fX,
700 pointData.fPoints[i].fY - pointData.fSize.fY,
701 pointData.fPoints[i].fX + pointData.fSize.fX,
702 pointData.fPoints[i].fY + pointData.fSize.fY);
704 fDevice->drawRect(*this, r, newP);
706 this->drawRect(r, newP);
714 // couldn't take fast path so fall through!
715 case SkCanvas::kPolygon_PointMode: {
719 p.setStyle(SkPaint::kStroke_Style);
720 size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
721 path.setIsVolatile(true);
722 for (size_t i = 0; i < count; i += inc) {
724 path.lineTo(pts[i+1]);
726 fDevice->drawPath(*this, path, p, NULL, true);
728 this->drawPath(path, p, NULL, true);
738 static inline SkPoint compute_stroke_size(const SkPaint& paint, const SkMatrix& matrix) {
739 SkASSERT(matrix.rectStaysRect());
740 SkASSERT(SkPaint::kFill_Style != paint.getStyle());
743 SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() };
744 matrix.mapVectors(&size, &pt, 1);
745 return SkPoint::Make(SkScalarAbs(size.fX), SkScalarAbs(size.fY));
748 static bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix,
749 SkPoint* strokeSize) {
750 if (SkPaint::kMiter_Join != paint.getStrokeJoin() ||
751 paint.getStrokeMiter() < SK_ScalarSqrt2) {
755 *strokeSize = compute_stroke_size(paint, matrix);
759 SkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint,
760 const SkMatrix& matrix,
761 SkPoint* strokeSize) {
763 const SkScalar width = paint.getStrokeWidth();
764 const bool zeroWidth = (0 == width);
765 SkPaint::Style style = paint.getStyle();
767 if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) {
768 style = SkPaint::kFill_Style;
771 if (paint.getPathEffect() || paint.getMaskFilter() ||
772 paint.getRasterizer() || !matrix.rectStaysRect() ||
773 SkPaint::kStrokeAndFill_Style == style) {
774 rtype = kPath_RectType;
775 } else if (SkPaint::kFill_Style == style) {
776 rtype = kFill_RectType;
777 } else if (zeroWidth) {
778 rtype = kHair_RectType;
779 } else if (easy_rect_join(paint, matrix, strokeSize)) {
780 rtype = kStroke_RectType;
782 rtype = kPath_RectType;
787 static const SkPoint* rect_points(const SkRect& r) {
788 return SkTCast<const SkPoint*>(&r);
791 static SkPoint* rect_points(SkRect& r) {
792 return SkTCast<SkPoint*>(&r);
795 void SkDraw::drawRect(const SkRect& prePaintRect, const SkPaint& paint,
796 const SkMatrix* paintMatrix, const SkRect* postPaintRect) const {
797 SkDEBUGCODE(this->validate();)
800 if (fRC->isEmpty()) {
804 const SkMatrix* matrix;
805 SkMatrix combinedMatrixStorage;
807 SkASSERT(postPaintRect);
808 combinedMatrixStorage.setConcat(*fMatrix, *paintMatrix);
809 matrix = &combinedMatrixStorage;
811 SkASSERT(!postPaintRect);
816 RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize);
818 if (kPath_RectType == rtype) {
821 draw.fMatrix = matrix;
824 tmp.addRect(prePaintRect);
825 tmp.setFillType(SkPath::kWinding_FillType);
826 draw.drawPath(tmp, paint, NULL, true);
831 const SkRect& paintRect = paintMatrix ? *postPaintRect : prePaintRect;
832 // skip the paintMatrix when transforming the rect by the CTM
833 fMatrix->mapPoints(rect_points(devRect), rect_points(paintRect), 2);
836 // look for the quick exit, before we build a blitter
837 SkRect bbox = devRect;
838 if (paint.getStyle() != SkPaint::kFill_Style) {
839 // extra space for hairlines
840 if (paint.getStrokeWidth() == 0) {
843 // For kStroke_RectType, strokeSize is already computed.
844 const SkPoint& ssize = (kStroke_RectType == rtype)
846 : compute_stroke_size(paint, *fMatrix);
847 bbox.outset(SkScalarHalf(ssize.x()), SkScalarHalf(ssize.y()));
851 SkIRect ir = bbox.roundOut();
852 if (fRC->quickReject(ir)) {
856 SkDeviceLooper looper(*fBitmap, *fRC, ir, paint.isAntiAlias());
857 while (looper.next()) {
859 looper.mapRect(&localDevRect, devRect);
860 SkMatrix localMatrix;
861 looper.mapMatrix(&localMatrix, *matrix);
863 SkAutoBlitterChoose blitterStorage(looper.getBitmap(), localMatrix, paint);
864 const SkRasterClip& clip = looper.getRC();
865 SkBlitter* blitter = blitterStorage.get();
867 // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
868 // case we are also hairline (if we've gotten to here), which devolves to
869 // effectively just kFill
872 if (paint.isAntiAlias()) {
873 SkScan::AntiFillRect(localDevRect, clip, blitter);
875 SkScan::FillRect(localDevRect, clip, blitter);
878 case kStroke_RectType:
879 if (paint.isAntiAlias()) {
880 SkScan::AntiFrameRect(localDevRect, strokeSize, clip, blitter);
882 SkScan::FrameRect(localDevRect, strokeSize, clip, blitter);
886 if (paint.isAntiAlias()) {
887 SkScan::AntiHairRect(localDevRect, clip, blitter);
889 SkScan::HairRect(localDevRect, clip, blitter);
893 SkDEBUGFAIL("bad rtype");
898 void SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const {
899 if (srcM.fBounds.isEmpty()) {
903 const SkMask* mask = &srcM;
906 if (paint.getMaskFilter() &&
907 paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, NULL)) {
912 SkAutoMaskFreeImage ami(dstM.fImage);
914 SkAutoBlitterChoose blitterChooser(*fBitmap, *fMatrix, paint);
915 SkBlitter* blitter = blitterChooser.get();
917 SkAAClipBlitterWrapper wrapper;
918 const SkRegion* clipRgn;
921 clipRgn = &fRC->bwRgn();
923 wrapper.init(*fRC, blitter);
924 clipRgn = &wrapper.getRgn();
925 blitter = wrapper.getBlitter();
927 blitter->blitMaskRegion(*mask, *clipRgn);
930 static SkScalar fast_len(const SkVector& vec) {
931 SkScalar x = SkScalarAbs(vec.fX);
932 SkScalar y = SkScalarAbs(vec.fY);
936 return x + SkScalarHalf(y);
939 bool SkDrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix& matrix,
940 SkScalar* coverage) {
941 SkASSERT(strokeWidth > 0);
942 // We need to try to fake a thick-stroke with a modulated hairline.
944 if (matrix.hasPerspective()) {
948 SkVector src[2], dst[2];
949 src[0].set(strokeWidth, 0);
950 src[1].set(0, strokeWidth);
951 matrix.mapVectors(dst, src, 2);
952 SkScalar len0 = fast_len(dst[0]);
953 SkScalar len1 = fast_len(dst[1]);
954 if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) {
956 *coverage = SkScalarAve(len0, len1);
963 void SkDraw::drawRRect(const SkRRect& rrect, const SkPaint& paint) const {
964 SkDEBUGCODE(this->validate());
966 if (fRC->isEmpty()) {
971 // TODO: Investigate optimizing these options. They are in the same
972 // order as SkDraw::drawPath, which handles each case. It may be
973 // that there is no way to optimize for these using the SkRRect path.
975 if (SkDrawTreatAsHairline(paint, *fMatrix, &coverage)) {
979 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
983 if (paint.getRasterizer()) {
988 if (paint.getMaskFilter()) {
989 // Transform the rrect into device space.
991 if (rrect.transform(*fMatrix, &devRRect)) {
992 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
993 if (paint.getMaskFilter()->filterRRect(devRRect, *fMatrix, *fRC, blitter.get(),
994 SkPaint::kFill_Style)) {
995 return; // filterRRect() called the blitter, so we're done
1001 // Now fall back to the default case of using a path.
1003 path.addRRect(rrect);
1004 this->drawPath(path, paint, NULL, true);
1007 static SkScalar compute_res_scale_for_stroking(const SkMatrix& matrix) {
1008 if (!matrix.hasPerspective()) {
1009 SkScalar sx = SkPoint::Length(matrix[SkMatrix::kMScaleX], matrix[SkMatrix::kMSkewY]);
1010 SkScalar sy = SkPoint::Length(matrix[SkMatrix::kMSkewX], matrix[SkMatrix::kMScaleY]);
1011 if (SkScalarsAreFinite(sx, sy)) {
1012 return SkTMax(sx, sy);
1018 void SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint,
1019 const SkMatrix* prePathMatrix, bool pathIsMutable,
1020 bool drawCoverage, SkBlitter* customBlitter) const {
1021 SkDEBUGCODE(this->validate();)
1024 if (fRC->isEmpty()) {
1028 SkPath* pathPtr = (SkPath*)&origSrcPath;
1032 const SkMatrix* matrix = fMatrix;
1033 tmpPath.setIsVolatile(true);
1035 if (prePathMatrix) {
1036 if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style ||
1037 origPaint.getRasterizer()) {
1038 SkPath* result = pathPtr;
1040 if (!pathIsMutable) {
1042 pathIsMutable = true;
1044 pathPtr->transform(*prePathMatrix, result);
1047 tmpMatrix.setConcat(*matrix, *prePathMatrix);
1048 matrix = &tmpMatrix;
1051 // at this point we're done with prePathMatrix
1052 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
1054 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1058 if (SkDrawTreatAsHairline(origPaint, *matrix, &coverage)) {
1059 if (SK_Scalar1 == coverage) {
1060 paint.writable()->setStrokeWidth(0);
1061 } else if (SkXfermode::SupportsCoverageAsAlpha(origPaint.getXfermode())) {
1064 newAlpha = SkToU8(SkScalarRoundToInt(coverage *
1065 origPaint.getAlpha()));
1067 // this is the old technique, which we preserve for now so
1068 // we don't change previous results (testing)
1069 // the new way seems fine, its just (a tiny bit) different
1070 int scale = (int)SkScalarMul(coverage, 256);
1071 newAlpha = origPaint.getAlpha() * scale >> 8;
1073 SkPaint* writablePaint = paint.writable();
1074 writablePaint->setStrokeWidth(0);
1075 writablePaint->setAlpha(newAlpha);
1080 if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) {
1082 const SkRect* cullRectPtr = NULL;
1083 if (this->computeConservativeLocalClipBounds(&cullRect)) {
1084 cullRectPtr = &cullRect;
1086 doFill = paint->getFillPath(*pathPtr, &tmpPath, cullRectPtr,
1087 compute_res_scale_for_stroking(*fMatrix));
1091 if (paint->getRasterizer()) {
1093 if (paint->getRasterizer()->rasterize(*pathPtr, *matrix,
1094 &fRC->getBounds(), paint->getMaskFilter(), &mask,
1095 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
1096 this->drawDevMask(mask, *paint);
1097 SkMask::FreeImage(mask.fImage);
1102 // avoid possibly allocating a new path in transform if we can
1103 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1105 // transform the path into device space
1106 pathPtr->transform(*matrix, devPathPtr);
1108 SkBlitter* blitter = NULL;
1109 SkAutoBlitterChoose blitterStorage;
1110 if (NULL == customBlitter) {
1111 blitterStorage.choose(*fBitmap, *fMatrix, *paint, drawCoverage);
1112 blitter = blitterStorage.get();
1114 blitter = customBlitter;
1117 if (paint->getMaskFilter()) {
1118 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1119 SkPaint::kStroke_Style;
1120 if (paint->getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fRC, blitter, style)) {
1121 return; // filterPath() called the blitter, so we're done
1125 void (*proc)(const SkPath&, const SkRasterClip&, SkBlitter*);
1127 if (paint->isAntiAlias()) {
1128 proc = SkScan::AntiFillPath;
1130 proc = SkScan::FillPath;
1132 } else { // hairline
1133 if (paint->isAntiAlias()) {
1134 proc = SkScan::AntiHairPath;
1136 proc = SkScan::HairPath;
1139 proc(*devPathPtr, *fRC, blitter);
1142 /** For the purposes of drawing bitmaps, if a matrix is "almost" translate
1143 go ahead and treat it as if it were, so that subsequent code can go fast.
1145 static bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) {
1146 unsigned bits = 0; // TODO: find a way to allow the caller to tell us to
1147 // respect filtering.
1148 return SkTreatAsSprite(matrix, bitmap.width(), bitmap.height(), bits);
1151 void SkDraw::drawBitmapAsMask(const SkBitmap& bitmap,
1152 const SkPaint& paint) const {
1153 SkASSERT(bitmap.colorType() == kAlpha_8_SkColorType);
1155 if (just_translate(*fMatrix, bitmap)) {
1156 int ix = SkScalarRoundToInt(fMatrix->getTranslateX());
1157 int iy = SkScalarRoundToInt(fMatrix->getTranslateY());
1159 SkAutoLockPixels alp(bitmap);
1160 if (!bitmap.readyToDraw()) {
1165 mask.fBounds.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1166 mask.fFormat = SkMask::kA8_Format;
1167 mask.fRowBytes = SkToU32(bitmap.rowBytes());
1168 mask.fImage = bitmap.getAddr8(0, 0);
1170 this->drawDevMask(mask, paint);
1171 } else { // need to xform the bitmap first
1176 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
1177 fMatrix->mapRect(&r);
1178 r.round(&mask.fBounds);
1180 // set the mask's bounds to the transformed bitmap-bounds,
1181 // clipped to the actual device
1184 devBounds.set(0, 0, fBitmap->width(), fBitmap->height());
1185 // need intersect(l, t, r, b) on irect
1186 if (!mask.fBounds.intersect(devBounds)) {
1191 mask.fFormat = SkMask::kA8_Format;
1192 mask.fRowBytes = SkAlign4(mask.fBounds.width());
1193 size_t size = mask.computeImageSize();
1195 // the mask is too big to allocated, draw nothing
1199 // allocate (and clear) our temp buffer to hold the transformed bitmap
1200 SkAutoMalloc storage(size);
1201 mask.fImage = (uint8_t*)storage.get();
1202 memset(mask.fImage, 0, size);
1204 // now draw our bitmap(src) into mask(dst), transformed by the matrix
1207 device.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()),
1208 mask.fImage, mask.fRowBytes);
1211 // need the unclipped top/left for the translate
1212 c.translate(-SkIntToScalar(mask.fBounds.fLeft),
1213 -SkIntToScalar(mask.fBounds.fTop));
1216 // We can't call drawBitmap, or we'll infinitely recurse. Instead
1217 // we manually build a shader and draw that into our new mask
1219 tmpPaint.setFlags(paint.getFlags());
1220 SkAutoBitmapShaderInstall install(bitmap, tmpPaint);
1222 rr.set(0, 0, SkIntToScalar(bitmap.width()),
1223 SkIntToScalar(bitmap.height()));
1224 c.drawRect(rr, install.paintWithShader());
1226 this->drawDevMask(mask, paint);
1230 static bool clipped_out(const SkMatrix& m, const SkRasterClip& c,
1231 const SkRect& srcR) {
1233 m.mapRect(&dstR, srcR);
1234 return c.quickReject(dstR.roundOut());
1237 static bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip,
1238 int width, int height) {
1240 r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
1241 return clipped_out(matrix, clip, r);
1244 static bool clipHandlesSprite(const SkRasterClip& clip, int x, int y,
1245 const SkBitmap& bitmap) {
1246 return clip.isBW() ||
1247 clip.quickContains(x, y, x + bitmap.width(), y + bitmap.height());
1250 void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
1251 const SkRect* dstBounds, const SkPaint& origPaint) const {
1252 SkDEBUGCODE(this->validate();)
1255 if (fRC->isEmpty() ||
1256 bitmap.width() == 0 || bitmap.height() == 0 ||
1257 bitmap.colorType() == kUnknown_SkColorType) {
1261 SkPaint paint(origPaint);
1262 paint.setStyle(SkPaint::kFill_Style);
1265 matrix.setConcat(*fMatrix, prematrix);
1267 if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) {
1271 if (bitmap.colorType() != kAlpha_8_SkColorType && just_translate(matrix, bitmap)) {
1273 // It is safe to call lock pixels now, since we know the matrix is
1274 // (more or less) identity.
1276 SkAutoLockPixels alp(bitmap);
1277 if (!bitmap.readyToDraw()) {
1280 int ix = SkScalarRoundToInt(matrix.getTranslateX());
1281 int iy = SkScalarRoundToInt(matrix.getTranslateY());
1282 if (clipHandlesSprite(*fRC, ix, iy, bitmap)) {
1283 SkTBlitterAllocator allocator;
1284 // blitter will be owned by the allocator.
1285 SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1286 ix, iy, &allocator);
1289 ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1291 SkScan::FillIRect(ir, *fRC, blitter);
1297 // now make a temp draw on the stack, and use it
1300 draw.fMatrix = &matrix;
1302 if (bitmap.colorType() == kAlpha_8_SkColorType) {
1303 draw.drawBitmapAsMask(bitmap, paint);
1305 SkAutoBitmapShaderInstall install(bitmap, paint);
1306 const SkPaint& paintWithShader = install.paintWithShader();
1307 const SkRect srcBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
1309 this->drawRect(srcBounds, paintWithShader, &prematrix, dstBounds);
1311 draw.drawRect(srcBounds, paintWithShader);
1316 void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y,
1317 const SkPaint& origPaint) const {
1318 SkDEBUGCODE(this->validate();)
1321 if (fRC->isEmpty() ||
1322 bitmap.width() == 0 || bitmap.height() == 0 ||
1323 bitmap.colorType() == kUnknown_SkColorType) {
1328 bounds.set(x, y, x + bitmap.width(), y + bitmap.height());
1330 if (fRC->quickReject(bounds)) {
1331 return; // nothing to draw
1334 SkPaint paint(origPaint);
1335 paint.setStyle(SkPaint::kFill_Style);
1337 if (NULL == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, bitmap)) {
1338 SkTBlitterAllocator allocator;
1339 // blitter will be owned by the allocator.
1340 SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1344 SkScan::FillIRect(bounds, *fRC, blitter);
1352 // get a scalar version of our rect
1355 // create shader with offset
1356 matrix.setTranslate(r.fLeft, r.fTop);
1357 SkAutoBitmapShaderInstall install(bitmap, paint, &matrix);
1358 const SkPaint& shaderPaint = install.paintWithShader();
1362 draw.fMatrix = &matrix;
1363 // call ourself with a rect
1364 // is this OK if paint has a rasterizer?
1365 draw.drawRect(r, shaderPaint);
1368 ///////////////////////////////////////////////////////////////////////////////
1370 #include "SkScalerContext.h"
1371 #include "SkGlyphCache.h"
1372 #include "SkTextToPathIter.h"
1373 #include "SkUtils.h"
1375 static void measure_text(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
1376 const char text[], size_t byteLength, SkVector* stopVector) {
1377 SkFixed x = 0, y = 0;
1378 const char* stop = text + byteLength;
1380 SkAutoKern autokern;
1382 while (text < stop) {
1383 // don't need x, y here, since all subpixel variants will have the
1385 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1387 x += autokern.adjust(glyph) + glyph.fAdvanceX;
1388 y += glyph.fAdvanceY;
1390 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
1392 SkASSERT(text == stop);
1395 bool SkDraw::ShouldDrawTextAsPaths(const SkPaint& paint, const SkMatrix& ctm) {
1396 // hairline glyphs are fast enough so we don't need to cache them
1397 if (SkPaint::kStroke_Style == paint.getStyle() && 0 == paint.getStrokeWidth()) {
1401 // we don't cache perspective
1402 if (ctm.hasPerspective()) {
1407 return SkPaint::TooBigToUseCache(ctm, *paint.setTextMatrix(&textM));
1410 void SkDraw::drawText_asPaths(const char text[], size_t byteLength,
1411 SkScalar x, SkScalar y,
1412 const SkPaint& paint) const {
1413 SkDEBUGCODE(this->validate();)
1415 SkTextToPathIter iter(text, byteLength, paint, true);
1418 matrix.setScale(iter.getPathScale(), iter.getPathScale());
1419 matrix.postTranslate(x, y);
1421 const SkPath* iterPath;
1422 SkScalar xpos, prevXPos = 0;
1424 while (iter.next(&iterPath, &xpos)) {
1425 matrix.postTranslate(xpos - prevXPos, 0);
1427 const SkPaint& pnt = iter.getPaint();
1429 fDevice->drawPath(*this, *iterPath, pnt, &matrix, false);
1431 this->drawPath(*iterPath, pnt, &matrix, false);
1438 // disable warning : local variable used without having been initialized
1439 #if defined _WIN32 && _MSC_VER >= 1300
1440 #pragma warning ( push )
1441 #pragma warning ( disable : 4701 )
1444 //////////////////////////////////////////////////////////////////////////////
1446 static void D1G_RectClip(const SkDraw1Glyph& state, Sk48Dot16 fx, Sk48Dot16 fy, const SkGlyph& glyph) {
1447 // Prevent glyphs from being drawn outside of or straddling the edge of device space.
1448 if ((fx >> 16) > INT_MAX - (INT16_MAX + UINT16_MAX) ||
1449 (fx >> 16) < INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/) ||
1450 (fy >> 16) > INT_MAX - (INT16_MAX + UINT16_MAX) ||
1451 (fy >> 16) < INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/))
1456 int left = Sk48Dot16FloorToInt(fx);
1457 int top = Sk48Dot16FloorToInt(fy);
1458 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1459 SkASSERT((NULL == state.fClip && state.fAAClip) ||
1460 (state.fClip && NULL == state.fAAClip && state.fClip->isRect()));
1462 left += glyph.fLeft;
1465 int right = left + glyph.fWidth;
1466 int bottom = top + glyph.fHeight;
1470 SkIRect* bounds = &mask.fBounds;
1472 mask.fBounds.set(left, top, right, bottom);
1474 // this extra test is worth it, assuming that most of the time it succeeds
1475 // since we can avoid writing to storage
1476 if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) {
1477 if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds))
1482 uint8_t* aa = (uint8_t*)glyph.fImage;
1484 aa = (uint8_t*)state.fCache->findImage(glyph);
1486 return; // can't rasterize glyph
1490 mask.fRowBytes = glyph.rowBytes();
1491 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1493 state.blitMask(mask, *bounds);
1496 static void D1G_RgnClip(const SkDraw1Glyph& state, Sk48Dot16 fx, Sk48Dot16 fy, const SkGlyph& glyph) {
1497 int left = Sk48Dot16FloorToInt(fx);
1498 int top = Sk48Dot16FloorToInt(fy);
1499 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1500 SkASSERT(!state.fClip->isRect());
1504 left += glyph.fLeft;
1507 mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
1508 SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
1510 if (!clipper.done()) {
1511 const SkIRect& cr = clipper.rect();
1512 const uint8_t* aa = (const uint8_t*)glyph.fImage;
1514 aa = (uint8_t*)state.fCache->findImage(glyph);
1520 mask.fRowBytes = glyph.rowBytes();
1521 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1522 mask.fImage = (uint8_t*)aa;
1524 state.blitMask(mask, cr);
1526 } while (!clipper.done());
1530 static bool hasCustomD1GProc(const SkDraw& draw) {
1531 return draw.fProcs && draw.fProcs->fD1GProc;
1534 static bool needsRasterTextBlit(const SkDraw& draw) {
1535 return !hasCustomD1GProc(draw);
1538 SkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter, SkGlyphCache* cache,
1539 const SkPaint& pnt) {
1545 if (cache->isSubpixel()) {
1546 fHalfSampleX = fHalfSampleY = SkFixedToScalar(SkGlyph::kSubpixelRound);
1548 fHalfSampleX = fHalfSampleY = SK_ScalarHalf;
1551 if (hasCustomD1GProc(*draw)) {
1552 // todo: fix this assumption about clips w/ custom
1553 fClip = draw->fClip;
1554 fClipBounds = fClip->getBounds();
1555 return draw->fProcs->fD1GProc;
1558 if (draw->fRC->isBW()) {
1560 fClip = &draw->fRC->bwRgn();
1561 fClipBounds = fClip->getBounds();
1562 if (fClip->isRect()) {
1563 return D1G_RectClip;
1568 fAAClip = &draw->fRC->aaRgn();
1570 fClipBounds = fAAClip->getBounds();
1571 return D1G_RectClip;
1575 void SkDraw1Glyph::blitMaskAsSprite(const SkMask& mask) const {
1576 SkASSERT(SkMask::kARGB32_Format == mask.fFormat);
1579 bm.installPixels(SkImageInfo::MakeN32Premul(mask.fBounds.width(), mask.fBounds.height()),
1580 (SkPMColor*)mask.fImage, mask.fRowBytes);
1582 fDraw->drawSprite(bm, mask.fBounds.x(), mask.fBounds.y(), *fPaint);
1585 ///////////////////////////////////////////////////////////////////////////////
1587 void SkDraw::drawText(const char text[], size_t byteLength,
1588 SkScalar x, SkScalar y, const SkPaint& paint) const {
1589 SkASSERT(byteLength == 0 || text != NULL);
1591 SkDEBUGCODE(this->validate();)
1594 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
1598 // SkScalarRec doesn't currently have a way of representing hairline stroke and
1599 // will fill if its frame-width is 0.
1600 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
1601 this->drawText_asPaths(text, byteLength, x, y, paint);
1605 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
1607 SkAutoGlyphCache autoCache(paint, &fDevice->getLeakyProperties(), fMatrix);
1608 SkGlyphCache* cache = autoCache.getCache();
1610 // transform our starting point
1613 fMatrix->mapXY(x, y, &loc);
1618 // need to measure first
1619 if (paint.getTextAlign() != SkPaint::kLeft_Align) {
1622 measure_text(cache, glyphCacheProc, text, byteLength, &stop);
1624 SkScalar stopX = stop.fX;
1625 SkScalar stopY = stop.fY;
1627 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
1628 stopX = SkScalarHalf(stopX);
1629 stopY = SkScalarHalf(stopY);
1635 const char* stop = text + byteLength;
1637 SkAAClipBlitter aaBlitter;
1638 SkAutoBlitterChoose blitterChooser;
1639 SkBlitter* blitter = NULL;
1640 if (needsRasterTextBlit(*this)) {
1641 blitterChooser.choose(*fBitmap, *fMatrix, paint);
1642 blitter = blitterChooser.get();
1644 aaBlitter.init(blitter, &fRC->aaRgn());
1645 blitter = &aaBlitter;
1649 SkAutoKern autokern;
1651 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint);
1653 SkFixed fxMask = ~0;
1654 SkFixed fyMask = ~0;
1655 if (cache->isSubpixel()) {
1656 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*fMatrix);
1657 if (kX_SkAxisAlignment == baseline) {
1659 d1g.fHalfSampleY = SK_ScalarHalf;
1660 } else if (kY_SkAxisAlignment == baseline) {
1662 d1g.fHalfSampleX = SK_ScalarHalf;
1666 Sk48Dot16 fx = SkScalarTo48Dot16(x + d1g.fHalfSampleX);
1667 Sk48Dot16 fy = SkScalarTo48Dot16(y + d1g.fHalfSampleY);
1669 while (text < stop) {
1670 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
1672 fx += autokern.adjust(glyph);
1675 proc(d1g, fx, fy, glyph);
1678 fx += glyph.fAdvanceX;
1679 fy += glyph.fAdvanceY;
1683 //////////////////////////////////////////////////////////////////////////////
1685 void SkDraw::drawPosText_asPaths(const char text[], size_t byteLength,
1686 const SkScalar pos[], int scalarsPerPosition,
1687 const SkPoint& offset, const SkPaint& origPaint) const {
1688 // setup our std paint, in hopes of getting hits in the cache
1689 SkPaint paint(origPaint);
1690 SkScalar matrixScale = paint.setupForAsPaths();
1693 matrix.setScale(matrixScale, matrixScale);
1695 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
1696 paint.setStyle(SkPaint::kFill_Style);
1697 paint.setPathEffect(NULL);
1699 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
1700 SkAutoGlyphCache autoCache(paint, NULL, NULL);
1701 SkGlyphCache* cache = autoCache.getCache();
1703 const char* stop = text + byteLength;
1704 SkTextAlignProc alignProc(paint.getTextAlign());
1705 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
1707 // Now restore the original settings, so we "draw" with whatever style/stroking.
1708 paint.setStyle(origPaint.getStyle());
1709 paint.setPathEffect(origPaint.getPathEffect());
1711 while (text < stop) {
1712 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1714 const SkPath* path = cache->findPath(glyph);
1717 tmsProc(pos, &tmsLoc);
1719 alignProc(tmsLoc, glyph, &loc);
1721 matrix[SkMatrix::kMTransX] = loc.fX;
1722 matrix[SkMatrix::kMTransY] = loc.fY;
1724 fDevice->drawPath(*this, *path, paint, &matrix, false);
1726 this->drawPath(*path, paint, &matrix, false);
1730 pos += scalarsPerPosition;
1734 void SkDraw::drawPosText(const char text[], size_t byteLength,
1735 const SkScalar pos[], int scalarsPerPosition,
1736 const SkPoint& offset, const SkPaint& paint) const {
1737 SkASSERT(byteLength == 0 || text != NULL);
1738 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1740 SkDEBUGCODE(this->validate();)
1743 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
1747 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
1748 this->drawPosText_asPaths(text, byteLength, pos, scalarsPerPosition, offset, paint);
1752 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
1753 SkAutoGlyphCache autoCache(paint, &fDevice->getLeakyProperties(), fMatrix);
1754 SkGlyphCache* cache = autoCache.getCache();
1756 SkAAClipBlitterWrapper wrapper;
1757 SkAutoBlitterChoose blitterChooser;
1758 SkBlitter* blitter = NULL;
1759 if (needsRasterTextBlit(*this)) {
1760 blitterChooser.choose(*fBitmap, *fMatrix, paint);
1761 blitter = blitterChooser.get();
1763 wrapper.init(*fRC, blitter);
1764 blitter = wrapper.getBlitter();
1768 const char* stop = text + byteLength;
1769 SkTextAlignProc alignProc(paint.getTextAlign());
1771 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint);
1772 SkTextMapStateProc tmsProc(*fMatrix, offset, scalarsPerPosition);
1774 if (cache->isSubpixel()) {
1775 // maybe we should skip the rounding if linearText is set
1776 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*fMatrix);
1778 SkFixed fxMask = ~0;
1779 SkFixed fyMask = ~0;
1780 if (kX_SkAxisAlignment == baseline) {
1782 d1g.fHalfSampleY = SK_ScalarHalf;
1783 } else if (kY_SkAxisAlignment == baseline) {
1785 d1g.fHalfSampleX = SK_ScalarHalf;
1788 if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1789 while (text < stop) {
1791 tmsProc(pos, &tmsLoc);
1793 Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + d1g.fHalfSampleX);
1794 Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + d1g.fHalfSampleY);
1796 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
1799 proc(d1g, fx, fy, glyph);
1801 pos += scalarsPerPosition;
1804 while (text < stop) {
1805 const char* currentText = text;
1806 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0);
1808 if (metricGlyph.fWidth) {
1809 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;)
1810 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;)
1812 tmsProc(pos, &tmsLoc);
1815 alignProc(tmsLoc, metricGlyph, &alignLoc);
1817 Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + d1g.fHalfSampleX);
1818 Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + d1g.fHalfSampleY);
1820 // have to call again, now that we've been "aligned"
1821 const SkGlyph& glyph = glyphCacheProc(cache, ¤tText,
1822 fx & fxMask, fy & fyMask);
1823 // the assumption is that the metrics haven't changed
1824 SkASSERT(prevAdvX == glyph.fAdvanceX);
1825 SkASSERT(prevAdvY == glyph.fAdvanceY);
1826 SkASSERT(glyph.fWidth);
1828 proc(d1g, fx, fy, glyph);
1830 pos += scalarsPerPosition;
1833 } else { // not subpixel
1834 if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1835 while (text < stop) {
1836 // the last 2 parameters are ignored
1837 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1841 tmsProc(pos, &tmsLoc);
1844 SkScalarTo48Dot16(tmsLoc.fX + SK_ScalarHalf), //d1g.fHalfSampleX,
1845 SkScalarTo48Dot16(tmsLoc.fY + SK_ScalarHalf), //d1g.fHalfSampleY,
1848 pos += scalarsPerPosition;
1851 while (text < stop) {
1852 // the last 2 parameters are ignored
1853 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1857 tmsProc(pos, &tmsLoc);
1860 alignProc(tmsLoc, glyph, &alignLoc);
1863 SkScalarTo48Dot16(alignLoc.fX + SK_ScalarHalf), //d1g.fHalfSampleX,
1864 SkScalarTo48Dot16(alignLoc.fY + SK_ScalarHalf), //d1g.fHalfSampleY,
1867 pos += scalarsPerPosition;
1873 #if defined _WIN32 && _MSC_VER >= 1300
1874 #pragma warning ( pop )
1877 ///////////////////////////////////////////////////////////////////////////////
1879 typedef void (*HairProc)(const SkPoint&, const SkPoint&, const SkRasterClip&,
1882 static HairProc ChooseHairProc(bool doAntiAlias) {
1883 return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
1886 static bool texture_to_matrix(const VertState& state, const SkPoint verts[],
1887 const SkPoint texs[], SkMatrix* matrix) {
1888 SkPoint src[3], dst[3];
1890 src[0] = texs[state.f0];
1891 src[1] = texs[state.f1];
1892 src[2] = texs[state.f2];
1893 dst[0] = verts[state.f0];
1894 dst[1] = verts[state.f1];
1895 dst[2] = verts[state.f2];
1896 return matrix->setPolyToPoly(src, dst, 3);
1899 class SkTriColorShader : public SkShader {
1901 SkTriColorShader() {}
1903 size_t contextSize() const override;
1905 class TriColorShaderContext : public SkShader::Context {
1907 TriColorShaderContext(const SkTriColorShader& shader, const ContextRec&);
1908 virtual ~TriColorShaderContext();
1910 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
1912 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override;
1915 SkMatrix fDstToUnit;
1916 SkPMColor fColors[3];
1918 typedef SkShader::Context INHERITED;
1921 SK_TO_STRING_OVERRIDE()
1923 // For serialization. This will never be called.
1924 Factory getFactory() const override { sk_throw(); return NULL; }
1927 Context* onCreateContext(const ContextRec& rec, void* storage) const override {
1928 return SkNEW_PLACEMENT_ARGS(storage, TriColorShaderContext, (*this, rec));
1932 typedef SkShader INHERITED;
1935 bool SkTriColorShader::TriColorShaderContext::setup(const SkPoint pts[], const SkColor colors[],
1936 int index0, int index1, int index2) {
1938 fColors[0] = SkPreMultiplyColor(colors[index0]);
1939 fColors[1] = SkPreMultiplyColor(colors[index1]);
1940 fColors[2] = SkPreMultiplyColor(colors[index2]);
1944 m.set(0, pts[index1].fX - pts[index0].fX);
1945 m.set(1, pts[index2].fX - pts[index0].fX);
1946 m.set(2, pts[index0].fX);
1947 m.set(3, pts[index1].fY - pts[index0].fY);
1948 m.set(4, pts[index2].fY - pts[index0].fY);
1949 m.set(5, pts[index0].fY);
1950 if (!m.invert(&im)) {
1953 // We can't call getTotalInverse(), because we explicitly don't want to look at the localmatrix
1954 // as our interators are intrinsically tied to the vertices, and nothing else.
1956 if (!this->getCTM().invert(&ctmInv)) {
1959 fDstToUnit.setConcat(im, ctmInv);
1963 #include "SkColorPriv.h"
1964 #include "SkComposeShader.h"
1966 static int ScalarTo256(SkScalar v) {
1967 int scale = SkScalarToFixed(v) >> 8;
1974 return SkAlpha255To256(scale);
1978 SkTriColorShader::TriColorShaderContext::TriColorShaderContext(const SkTriColorShader& shader,
1979 const ContextRec& rec)
1980 : INHERITED(shader, rec) {}
1982 SkTriColorShader::TriColorShaderContext::~TriColorShaderContext() {}
1984 size_t SkTriColorShader::contextSize() const {
1985 return sizeof(TriColorShaderContext);
1987 void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
1988 const int alphaScale = Sk255To256(this->getPaintAlpha());
1992 for (int i = 0; i < count; i++) {
1993 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
1996 int scale1 = ScalarTo256(src.fX);
1997 int scale2 = ScalarTo256(src.fY);
1998 int scale0 = 256 - scale1 - scale2;
2000 if (scale1 > scale2) {
2001 scale2 = 256 - scale1;
2003 scale1 = 256 - scale2;
2008 if (256 != alphaScale) {
2009 scale0 = SkAlphaMul(scale0, alphaScale);
2010 scale1 = SkAlphaMul(scale1, alphaScale);
2011 scale2 = SkAlphaMul(scale2, alphaScale);
2014 dstC[i] = SkAlphaMulQ(fColors[0], scale0) +
2015 SkAlphaMulQ(fColors[1], scale1) +
2016 SkAlphaMulQ(fColors[2], scale2);
2020 #ifndef SK_IGNORE_TO_STRING
2021 void SkTriColorShader::toString(SkString* str) const {
2022 str->append("SkTriColorShader: (");
2024 this->INHERITED::toString(str);
2030 void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
2031 const SkPoint vertices[], const SkPoint textures[],
2032 const SkColor colors[], SkXfermode* xmode,
2033 const uint16_t indices[], int indexCount,
2034 const SkPaint& paint) const {
2035 SkASSERT(0 == count || vertices);
2037 // abort early if there is nothing to draw
2038 if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
2042 // transform out vertices into device coordinates
2043 SkAutoSTMalloc<16, SkPoint> storage(count);
2044 SkPoint* devVerts = storage.get();
2045 fMatrix->mapPoints(devVerts, vertices, count);
2048 We can draw the vertices in 1 of 4 ways:
2050 - solid color (no shader/texture[], no colors[])
2051 - just colors (no shader/texture[], has colors[])
2052 - just texture (has shader/texture[], no colors[])
2053 - colors * texture (has shader/texture[], has colors[])
2055 Thus for texture drawing, we need both texture[] and a shader.
2058 SkTriColorShader triShader; // must be above declaration of p
2061 SkShader* shader = p.getShader();
2062 if (NULL == shader) {
2063 // if we have no shader, we ignore the texture coordinates
2065 } else if (NULL == textures) {
2066 // if we don't have texture coordinates, ignore the shader
2071 // setup the custom shader (if needed)
2072 SkAutoTUnref<SkComposeShader> composeShader;
2074 if (NULL == textures) {
2075 // just colors (no texture)
2076 shader = p.setShader(&triShader);
2080 bool releaseMode = false;
2081 if (NULL == xmode) {
2082 xmode = SkXfermode::Create(SkXfermode::kModulate_Mode);
2085 composeShader.reset(SkNEW_ARGS(SkComposeShader, (&triShader, shader, xmode)));
2086 p.setShader(composeShader);
2093 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
2094 // Abort early if we failed to create a shader context.
2095 if (blitter->isNullBlitter()) {
2099 // setup our state and function pointer for iterating triangles
2100 VertState state(count, indices, indexCount);
2101 VertState::Proc vertProc = state.chooseProc(vmode);
2103 if (textures || colors) {
2104 while (vertProc(&state)) {
2107 if (texture_to_matrix(state, vertices, textures, &tempM)) {
2108 SkShader::ContextRec rec(*fBitmap, p, *fMatrix);
2109 rec.fLocalMatrix = &tempM;
2110 if (!blitter->resetShaderContext(rec)) {
2116 // Find the context for triShader.
2117 SkTriColorShader::TriColorShaderContext* triColorShaderContext;
2119 SkShader::Context* shaderContext = blitter->getShaderContext();
2120 SkASSERT(shaderContext);
2121 if (p.getShader() == &triShader) {
2122 triColorShaderContext =
2123 static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContext);
2125 // The shader is a compose shader and triShader is its first shader.
2126 SkASSERT(p.getShader() == composeShader);
2127 SkASSERT(composeShader->getShaderA() == &triShader);
2128 SkComposeShader::ComposeShaderContext* composeShaderContext =
2129 static_cast<SkComposeShader::ComposeShaderContext*>(shaderContext);
2130 SkShader::Context* shaderContextA = composeShaderContext->getShaderContextA();
2131 triColorShaderContext =
2132 static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContextA);
2135 if (!triColorShaderContext->setup(vertices, colors,
2136 state.f0, state.f1, state.f2)) {
2142 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
2144 SkScan::FillTriangle(tmp, *fRC, blitter.get());
2147 // no colors[] and no texture, stroke hairlines with paint's color.
2148 HairProc hairProc = ChooseHairProc(paint.isAntiAlias());
2149 const SkRasterClip& clip = *fRC;
2150 while (vertProc(&state)) {
2151 hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get());
2152 hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get());
2153 hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get());
2158 ///////////////////////////////////////////////////////////////////////////////
2159 ///////////////////////////////////////////////////////////////////////////////
2163 void SkDraw::validate() const {
2164 SkASSERT(fBitmap != NULL);
2165 SkASSERT(fMatrix != NULL);
2166 SkASSERT(fClip != NULL);
2167 SkASSERT(fRC != NULL);
2169 const SkIRect& cr = fRC->getBounds();
2172 br.set(0, 0, fBitmap->width(), fBitmap->height());
2173 SkASSERT(cr.isEmpty() || br.contains(cr));
2178 ////////////////////////////////////////////////////////////////////////////////////////////////
2182 #include "SkRegion.h"
2183 #include "SkBlitter.h"
2185 static bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds,
2186 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
2188 if (devPath.isEmpty()) {
2192 // init our bounds from the path
2193 *bounds = devPath.getBounds().makeOutset(SK_ScalarHalf, SK_ScalarHalf).roundOut();
2195 SkIPoint margin = SkIPoint::Make(0, 0);
2197 SkASSERT(filterMatrix);
2201 srcM.fBounds = *bounds;
2202 srcM.fFormat = SkMask::kA8_Format;
2204 if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
2209 // (possibly) trim the bounds to reflect the clip
2210 // (plus whatever slop the filter needs)
2212 // Ugh. Guard against gigantic margins from wacky filters. Without this
2213 // check we can request arbitrary amounts of slop beyond our visible
2214 // clip, and bring down the renderer (at least on finite RAM machines
2215 // like handsets, etc.). Need to balance this invented value between
2216 // quality of large filters like blurs, and the corresponding memory
2218 static const int MAX_MARGIN = 128;
2219 if (!bounds->intersect(clipBounds->makeOutset(SkMin32(margin.fX, MAX_MARGIN),
2220 SkMin32(margin.fY, MAX_MARGIN)))) {
2228 static void draw_into_mask(const SkMask& mask, const SkPath& devPath,
2229 SkPaint::Style style) {
2236 bm.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()),
2237 mask.fImage, mask.fRowBytes);
2239 clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height()));
2240 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
2241 -SkIntToScalar(mask.fBounds.fTop));
2245 draw.fClip = &clip.bwRgn();
2246 draw.fMatrix = &matrix;
2247 paint.setAntiAlias(true);
2248 paint.setStyle(style);
2249 draw.drawPath(devPath, paint);
2252 bool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
2253 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
2254 SkMask* mask, SkMask::CreateMode mode,
2255 SkPaint::Style style) {
2256 if (SkMask::kJustRenderImage_CreateMode != mode) {
2257 if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds))
2261 if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
2262 mask->fFormat = SkMask::kA8_Format;
2263 mask->fRowBytes = mask->fBounds.width();
2264 size_t size = mask->computeImageSize();
2266 // we're too big to allocate the mask, abort
2269 mask->fImage = SkMask::AllocImage(size);
2270 memset(mask->fImage, 0, mask->computeImageSize());
2273 if (SkMask::kJustComputeBounds_CreateMode != mode) {
2274 draw_into_mask(*mask, devPath, style);