michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkDraw.h" michael@0: #include "SkBlitter.h" michael@0: #include "SkBounder.h" michael@0: #include "SkCanvas.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkDevice.h" michael@0: #include "SkDeviceLooper.h" michael@0: #include "SkFixed.h" michael@0: #include "SkMaskFilter.h" michael@0: #include "SkPaint.h" michael@0: #include "SkPathEffect.h" michael@0: #include "SkRasterClip.h" michael@0: #include "SkRasterizer.h" michael@0: #include "SkRRect.h" michael@0: #include "SkScan.h" michael@0: #include "SkShader.h" michael@0: #include "SkSmallAllocator.h" michael@0: #include "SkString.h" michael@0: #include "SkStroke.h" michael@0: #include "SkTLazy.h" michael@0: #include "SkUtils.h" michael@0: michael@0: #include "SkAutoKern.h" michael@0: #include "SkBitmapProcShader.h" michael@0: #include "SkDrawProcs.h" michael@0: #include "SkMatrixUtils.h" michael@0: michael@0: michael@0: //#define TRACE_BITMAP_DRAWS michael@0: michael@0: michael@0: /** Helper for allocating small blitters on the stack. michael@0: */ michael@0: class SkAutoBlitterChoose : SkNoncopyable { michael@0: public: michael@0: SkAutoBlitterChoose() { michael@0: fBlitter = NULL; michael@0: } michael@0: SkAutoBlitterChoose(const SkBitmap& device, const SkMatrix& matrix, michael@0: const SkPaint& paint, bool drawCoverage = false) { michael@0: fBlitter = SkBlitter::Choose(device, matrix, paint, &fAllocator, michael@0: drawCoverage); michael@0: } michael@0: michael@0: SkBlitter* operator->() { return fBlitter; } michael@0: SkBlitter* get() const { return fBlitter; } michael@0: michael@0: void choose(const SkBitmap& device, const SkMatrix& matrix, michael@0: const SkPaint& paint) { michael@0: SkASSERT(!fBlitter); michael@0: fBlitter = SkBlitter::Choose(device, matrix, paint, &fAllocator); michael@0: } michael@0: michael@0: private: michael@0: // Owned by fAllocator, which will handle the delete. michael@0: SkBlitter* fBlitter; michael@0: SkTBlitterAllocator fAllocator; michael@0: }; michael@0: #define SkAutoBlitterChoose(...) SK_REQUIRE_LOCAL_VAR(SkAutoBlitterChoose) michael@0: michael@0: /** michael@0: * Since we are providing the storage for the shader (to avoid the perf cost michael@0: * of calling new) we insist that in our destructor we can account for all michael@0: * owners of the shader. michael@0: */ michael@0: class SkAutoBitmapShaderInstall : SkNoncopyable { michael@0: public: michael@0: SkAutoBitmapShaderInstall(const SkBitmap& src, const SkPaint& paint) michael@0: : fPaint(paint) /* makes a copy of the paint */ { michael@0: fPaint.setShader(CreateBitmapShader(src, SkShader::kClamp_TileMode, michael@0: SkShader::kClamp_TileMode, michael@0: &fAllocator)); michael@0: // we deliberately left the shader with an owner-count of 2 michael@0: SkASSERT(2 == fPaint.getShader()->getRefCnt()); michael@0: } michael@0: michael@0: ~SkAutoBitmapShaderInstall() { michael@0: // since fAllocator will destroy shader, we insist that owners == 2 michael@0: SkASSERT(2 == fPaint.getShader()->getRefCnt()); michael@0: michael@0: fPaint.setShader(NULL); // unref the shader by 1 michael@0: michael@0: } michael@0: michael@0: // return the new paint that has the shader applied michael@0: const SkPaint& paintWithShader() const { return fPaint; } michael@0: michael@0: private: michael@0: // copy of caller's paint (which we then modify) michael@0: SkPaint fPaint; michael@0: // Stores the shader. michael@0: SkTBlitterAllocator fAllocator; michael@0: }; michael@0: #define SkAutoBitmapShaderInstall(...) SK_REQUIRE_LOCAL_VAR(SkAutoBitmapShaderInstall) michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkDraw::SkDraw() { michael@0: sk_bzero(this, sizeof(*this)); michael@0: } michael@0: michael@0: SkDraw::SkDraw(const SkDraw& src) { michael@0: memcpy(this, &src, sizeof(*this)); michael@0: } michael@0: michael@0: bool SkDraw::computeConservativeLocalClipBounds(SkRect* localBounds) const { michael@0: if (fRC->isEmpty()) { michael@0: return false; michael@0: } michael@0: michael@0: SkMatrix inverse; michael@0: if (!fMatrix->invert(&inverse)) { michael@0: return false; michael@0: } michael@0: michael@0: SkIRect devBounds = fRC->getBounds(); michael@0: // outset to have slop for antialasing and hairlines michael@0: devBounds.outset(1, 1); michael@0: inverse.mapRect(localBounds, SkRect::Make(devBounds)); michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: typedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data); michael@0: michael@0: static void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) { michael@0: sk_bzero(pixels, bytes); michael@0: } michael@0: michael@0: static void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {} michael@0: michael@0: static void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) { michael@0: sk_memset32((uint32_t*)pixels, data, SkToInt(bytes >> 2)); michael@0: } michael@0: michael@0: static void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) { michael@0: sk_memset16((uint16_t*)pixels, data, SkToInt(bytes >> 1)); michael@0: } michael@0: michael@0: static void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) { michael@0: memset(pixels, data, bytes); michael@0: } michael@0: michael@0: static BitmapXferProc ChooseBitmapXferProc(const SkBitmap& bitmap, michael@0: const SkPaint& paint, michael@0: uint32_t* data) { michael@0: // todo: we can apply colorfilter up front if no shader, so we wouldn't michael@0: // need to abort this fastpath michael@0: if (paint.getShader() || paint.getColorFilter()) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkXfermode::Mode mode; michael@0: if (!SkXfermode::AsMode(paint.getXfermode(), &mode)) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkColor color = paint.getColor(); michael@0: michael@0: // collaps modes based on color... michael@0: if (SkXfermode::kSrcOver_Mode == mode) { michael@0: unsigned alpha = SkColorGetA(color); michael@0: if (0 == alpha) { michael@0: mode = SkXfermode::kDst_Mode; michael@0: } else if (0xFF == alpha) { michael@0: mode = SkXfermode::kSrc_Mode; michael@0: } michael@0: } michael@0: michael@0: switch (mode) { michael@0: case SkXfermode::kClear_Mode: michael@0: // SkDebugf("--- D_Clear_BitmapXferProc\n"); michael@0: return D_Clear_BitmapXferProc; // ignore data michael@0: case SkXfermode::kDst_Mode: michael@0: // SkDebugf("--- D_Dst_BitmapXferProc\n"); michael@0: return D_Dst_BitmapXferProc; // ignore data michael@0: case SkXfermode::kSrc_Mode: { michael@0: /* michael@0: should I worry about dithering for the lower depths? michael@0: */ michael@0: SkPMColor pmc = SkPreMultiplyColor(color); michael@0: switch (bitmap.colorType()) { michael@0: case kPMColor_SkColorType: michael@0: if (data) { michael@0: *data = pmc; michael@0: } michael@0: // SkDebugf("--- D32_Src_BitmapXferProc\n"); michael@0: return D32_Src_BitmapXferProc; michael@0: case kRGB_565_SkColorType: michael@0: if (data) { michael@0: *data = SkPixel32ToPixel16(pmc); michael@0: } michael@0: // SkDebugf("--- D16_Src_BitmapXferProc\n"); michael@0: return D16_Src_BitmapXferProc; michael@0: case kAlpha_8_SkColorType: michael@0: if (data) { michael@0: *data = SkGetPackedA32(pmc); michael@0: } michael@0: // SkDebugf("--- DA8_Src_BitmapXferProc\n"); michael@0: return DA8_Src_BitmapXferProc; michael@0: default: michael@0: break; michael@0: } michael@0: break; michael@0: } michael@0: default: michael@0: break; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: static void CallBitmapXferProc(const SkBitmap& bitmap, const SkIRect& rect, michael@0: BitmapXferProc proc, uint32_t procData) { michael@0: int shiftPerPixel; michael@0: switch (bitmap.colorType()) { michael@0: case kPMColor_SkColorType: michael@0: shiftPerPixel = 2; michael@0: break; michael@0: case kRGB_565_SkColorType: michael@0: shiftPerPixel = 1; michael@0: break; michael@0: case kAlpha_8_SkColorType: michael@0: shiftPerPixel = 0; michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("Can't use xferproc on this config"); michael@0: return; michael@0: } michael@0: michael@0: uint8_t* pixels = (uint8_t*)bitmap.getPixels(); michael@0: SkASSERT(pixels); michael@0: const size_t rowBytes = bitmap.rowBytes(); michael@0: const int widthBytes = rect.width() << shiftPerPixel; michael@0: michael@0: // skip down to the first scanline and X position michael@0: pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel); michael@0: for (int scans = rect.height() - 1; scans >= 0; --scans) { michael@0: proc(pixels, widthBytes, procData); michael@0: pixels += rowBytes; michael@0: } michael@0: } michael@0: michael@0: void SkDraw::drawPaint(const SkPaint& paint) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: if (fRC->isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: SkIRect devRect; michael@0: devRect.set(0, 0, fBitmap->width(), fBitmap->height()); michael@0: if (fBounder && !fBounder->doIRect(devRect)) { michael@0: return; michael@0: } michael@0: michael@0: if (fRC->isBW()) { michael@0: /* If we don't have a shader (i.e. we're just a solid color) we may michael@0: be faster to operate directly on the device bitmap, rather than invoking michael@0: a blitter. Esp. true for xfermodes, which require a colorshader to be michael@0: present, which is just redundant work. Since we're drawing everywhere michael@0: in the clip, we don't have to worry about antialiasing. michael@0: */ michael@0: uint32_t procData = 0; // to avoid the warning michael@0: BitmapXferProc proc = ChooseBitmapXferProc(*fBitmap, paint, &procData); michael@0: if (proc) { michael@0: if (D_Dst_BitmapXferProc == proc) { // nothing to do michael@0: return; michael@0: } michael@0: michael@0: SkRegion::Iterator iter(fRC->bwRgn()); michael@0: while (!iter.done()) { michael@0: CallBitmapXferProc(*fBitmap, iter.rect(), proc, procData); michael@0: iter.next(); michael@0: } michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // normal case: use a blitter michael@0: SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint); michael@0: SkScan::FillIRect(devRect, *fRC, blitter.get()); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: struct PtProcRec { michael@0: SkCanvas::PointMode fMode; michael@0: const SkPaint* fPaint; michael@0: const SkRegion* fClip; michael@0: const SkRasterClip* fRC; michael@0: michael@0: // computed values michael@0: SkFixed fRadius; michael@0: michael@0: typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count, michael@0: SkBlitter*); michael@0: michael@0: bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix, michael@0: const SkRasterClip*); michael@0: Proc chooseProc(SkBlitter** blitter); michael@0: michael@0: private: michael@0: SkAAClipBlitterWrapper fWrapper; michael@0: }; michael@0: michael@0: static void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[], michael@0: int count, SkBlitter* blitter) { michael@0: SkASSERT(rec.fClip->isRect()); michael@0: const SkIRect& r = rec.fClip->getBounds(); michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: int x = SkScalarFloorToInt(devPts[i].fX); michael@0: int y = SkScalarFloorToInt(devPts[i].fY); michael@0: if (r.contains(x, y)) { michael@0: blitter->blitH(x, y, 1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void bw_pt_rect_16_hair_proc(const PtProcRec& rec, michael@0: const SkPoint devPts[], int count, michael@0: SkBlitter* blitter) { michael@0: SkASSERT(rec.fRC->isRect()); michael@0: const SkIRect& r = rec.fRC->getBounds(); michael@0: uint32_t value; michael@0: const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value); michael@0: SkASSERT(bitmap); michael@0: michael@0: uint16_t* addr = bitmap->getAddr16(0, 0); michael@0: size_t rb = bitmap->rowBytes(); michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: int x = SkScalarFloorToInt(devPts[i].fX); michael@0: int y = SkScalarFloorToInt(devPts[i].fY); michael@0: if (r.contains(x, y)) { michael@0: ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void bw_pt_rect_32_hair_proc(const PtProcRec& rec, michael@0: const SkPoint devPts[], int count, michael@0: SkBlitter* blitter) { michael@0: SkASSERT(rec.fRC->isRect()); michael@0: const SkIRect& r = rec.fRC->getBounds(); michael@0: uint32_t value; michael@0: const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value); michael@0: SkASSERT(bitmap); michael@0: michael@0: SkPMColor* addr = bitmap->getAddr32(0, 0); michael@0: size_t rb = bitmap->rowBytes(); michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: int x = SkScalarFloorToInt(devPts[i].fX); michael@0: int y = SkScalarFloorToInt(devPts[i].fY); michael@0: if (r.contains(x, y)) { michael@0: ((SkPMColor*)((char*)addr + y * rb))[x] = value; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[], michael@0: int count, SkBlitter* blitter) { michael@0: for (int i = 0; i < count; i++) { michael@0: int x = SkScalarFloorToInt(devPts[i].fX); michael@0: int y = SkScalarFloorToInt(devPts[i].fY); michael@0: if (rec.fClip->contains(x, y)) { michael@0: blitter->blitH(x, y, 1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[], michael@0: int count, SkBlitter* blitter) { michael@0: for (int i = 0; i < count; i += 2) { michael@0: SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter); michael@0: } michael@0: } michael@0: michael@0: static void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[], michael@0: int count, SkBlitter* blitter) { michael@0: for (int i = 0; i < count - 1; i++) { michael@0: SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter); michael@0: } michael@0: } michael@0: michael@0: // aa versions michael@0: michael@0: static void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[], michael@0: int count, SkBlitter* blitter) { michael@0: for (int i = 0; i < count; i += 2) { michael@0: SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter); michael@0: } michael@0: } michael@0: michael@0: static void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[], michael@0: int count, SkBlitter* blitter) { michael@0: for (int i = 0; i < count - 1; i++) { michael@0: SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter); michael@0: } michael@0: } michael@0: michael@0: // square procs (strokeWidth > 0 but matrix is square-scale (sx == sy) michael@0: michael@0: static void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[], michael@0: int count, SkBlitter* blitter) { michael@0: const SkFixed radius = rec.fRadius; michael@0: for (int i = 0; i < count; i++) { michael@0: SkFixed x = SkScalarToFixed(devPts[i].fX); michael@0: SkFixed y = SkScalarToFixed(devPts[i].fY); michael@0: michael@0: SkXRect r; michael@0: r.fLeft = x - radius; michael@0: r.fTop = y - radius; michael@0: r.fRight = x + radius; michael@0: r.fBottom = y + radius; michael@0: michael@0: SkScan::FillXRect(r, *rec.fRC, blitter); michael@0: } michael@0: } michael@0: michael@0: static void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[], michael@0: int count, SkBlitter* blitter) { michael@0: const SkFixed radius = rec.fRadius; michael@0: for (int i = 0; i < count; i++) { michael@0: SkFixed x = SkScalarToFixed(devPts[i].fX); michael@0: SkFixed y = SkScalarToFixed(devPts[i].fY); michael@0: michael@0: SkXRect r; michael@0: r.fLeft = x - radius; michael@0: r.fTop = y - radius; michael@0: r.fRight = x + radius; michael@0: r.fBottom = y + radius; michael@0: michael@0: SkScan::AntiFillXRect(r, *rec.fRC, blitter); michael@0: } michael@0: } michael@0: michael@0: // If this guy returns true, then chooseProc() must return a valid proc michael@0: bool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint, michael@0: const SkMatrix* matrix, const SkRasterClip* rc) { michael@0: if (paint.getPathEffect()) { michael@0: return false; michael@0: } michael@0: SkScalar width = paint.getStrokeWidth(); michael@0: if (0 == width) { michael@0: fMode = mode; michael@0: fPaint = &paint; michael@0: fClip = NULL; michael@0: fRC = rc; michael@0: fRadius = SK_FixedHalf; michael@0: return true; michael@0: } michael@0: if (paint.getStrokeCap() != SkPaint::kRound_Cap && michael@0: matrix->rectStaysRect() && SkCanvas::kPoints_PointMode == mode) { michael@0: SkScalar sx = matrix->get(SkMatrix::kMScaleX); michael@0: SkScalar sy = matrix->get(SkMatrix::kMScaleY); michael@0: if (SkScalarNearlyZero(sx - sy)) { michael@0: if (sx < 0) { michael@0: sx = -sx; michael@0: } michael@0: michael@0: fMode = mode; michael@0: fPaint = &paint; michael@0: fClip = NULL; michael@0: fRC = rc; michael@0: fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1; michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: PtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) { michael@0: Proc proc = NULL; michael@0: michael@0: SkBlitter* blitter = *blitterPtr; michael@0: if (fRC->isBW()) { michael@0: fClip = &fRC->bwRgn(); michael@0: } else { michael@0: fWrapper.init(*fRC, blitter); michael@0: fClip = &fWrapper.getRgn(); michael@0: blitter = fWrapper.getBlitter(); michael@0: *blitterPtr = blitter; michael@0: } michael@0: michael@0: // for our arrays michael@0: SkASSERT(0 == SkCanvas::kPoints_PointMode); michael@0: SkASSERT(1 == SkCanvas::kLines_PointMode); michael@0: SkASSERT(2 == SkCanvas::kPolygon_PointMode); michael@0: SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode); michael@0: michael@0: if (fPaint->isAntiAlias()) { michael@0: if (0 == fPaint->getStrokeWidth()) { michael@0: static const Proc gAAProcs[] = { michael@0: aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc michael@0: }; michael@0: proc = gAAProcs[fMode]; michael@0: } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) { michael@0: SkASSERT(SkCanvas::kPoints_PointMode == fMode); michael@0: proc = aa_square_proc; michael@0: } michael@0: } else { // BW michael@0: if (fRadius <= SK_FixedHalf) { // small radii and hairline michael@0: if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) { michael@0: uint32_t value; michael@0: const SkBitmap* bm = blitter->justAnOpaqueColor(&value); michael@0: if (bm && kRGB_565_SkColorType == bm->colorType()) { michael@0: proc = bw_pt_rect_16_hair_proc; michael@0: } else if (bm && kPMColor_SkColorType == bm->colorType()) { michael@0: proc = bw_pt_rect_32_hair_proc; michael@0: } else { michael@0: proc = bw_pt_rect_hair_proc; michael@0: } michael@0: } else { michael@0: static Proc gBWProcs[] = { michael@0: bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc michael@0: }; michael@0: proc = gBWProcs[fMode]; michael@0: } michael@0: } else { michael@0: proc = bw_square_proc; michael@0: } michael@0: } michael@0: return proc; michael@0: } michael@0: michael@0: static bool bounder_points(SkBounder* bounder, SkCanvas::PointMode mode, michael@0: size_t count, const SkPoint pts[], michael@0: const SkPaint& paint, const SkMatrix& matrix) { michael@0: SkIRect ibounds; michael@0: SkRect bounds; michael@0: SkScalar inset = paint.getStrokeWidth(); michael@0: michael@0: bounds.set(pts, SkToInt(count)); michael@0: bounds.inset(-inset, -inset); michael@0: matrix.mapRect(&bounds); michael@0: michael@0: bounds.roundOut(&ibounds); michael@0: return bounder->doIRect(ibounds); michael@0: } michael@0: michael@0: // each of these costs 8-bytes of stack space, so don't make it too large michael@0: // must be even for lines/polygon to work michael@0: #define MAX_DEV_PTS 32 michael@0: michael@0: void SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count, michael@0: const SkPoint pts[], const SkPaint& paint, michael@0: bool forceUseDevice) const { michael@0: // if we're in lines mode, force count to be even michael@0: if (SkCanvas::kLines_PointMode == mode) { michael@0: count &= ~(size_t)1; michael@0: } michael@0: michael@0: if ((long)count <= 0) { michael@0: return; michael@0: } michael@0: michael@0: SkASSERT(pts != NULL); michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: // nothing to draw michael@0: if (fRC->isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: if (fBounder) { michael@0: if (!bounder_points(fBounder, mode, count, pts, paint, *fMatrix)) { michael@0: return; michael@0: } michael@0: michael@0: // clear the bounder and call this again, so we don't invoke the bounder michael@0: // later if we happen to call ourselves for drawRect, drawPath, etc. michael@0: SkDraw noBounder(*this); michael@0: noBounder.fBounder = NULL; michael@0: noBounder.drawPoints(mode, count, pts, paint, forceUseDevice); michael@0: return; michael@0: } michael@0: michael@0: PtProcRec rec; michael@0: if (!forceUseDevice && rec.init(mode, paint, fMatrix, fRC)) { michael@0: SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint); michael@0: michael@0: SkPoint devPts[MAX_DEV_PTS]; michael@0: const SkMatrix* matrix = fMatrix; michael@0: SkBlitter* bltr = blitter.get(); michael@0: PtProcRec::Proc proc = rec.chooseProc(&bltr); michael@0: // we have to back up subsequent passes if we're in polygon mode michael@0: const size_t backup = (SkCanvas::kPolygon_PointMode == mode); michael@0: michael@0: do { michael@0: int n = SkToInt(count); michael@0: if (n > MAX_DEV_PTS) { michael@0: n = MAX_DEV_PTS; michael@0: } michael@0: matrix->mapPoints(devPts, pts, n); michael@0: proc(rec, devPts, n, bltr); michael@0: pts += n - backup; michael@0: SkASSERT(SkToInt(count) >= n); michael@0: count -= n; michael@0: if (count > 0) { michael@0: count += backup; michael@0: } michael@0: } while (count != 0); michael@0: } else { michael@0: switch (mode) { michael@0: case SkCanvas::kPoints_PointMode: { michael@0: // temporarily mark the paint as filling. michael@0: SkPaint newPaint(paint); michael@0: newPaint.setStyle(SkPaint::kFill_Style); michael@0: michael@0: SkScalar width = newPaint.getStrokeWidth(); michael@0: SkScalar radius = SkScalarHalf(width); michael@0: michael@0: if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) { michael@0: SkPath path; michael@0: SkMatrix preMatrix; michael@0: michael@0: path.addCircle(0, 0, radius); michael@0: for (size_t i = 0; i < count; i++) { michael@0: preMatrix.setTranslate(pts[i].fX, pts[i].fY); michael@0: // pass true for the last point, since we can modify michael@0: // then path then michael@0: if (fDevice) { michael@0: fDevice->drawPath(*this, path, newPaint, &preMatrix, michael@0: (count-1) == i); michael@0: } else { michael@0: this->drawPath(path, newPaint, &preMatrix, michael@0: (count-1) == i); michael@0: } michael@0: } michael@0: } else { michael@0: SkRect r; michael@0: michael@0: for (size_t i = 0; i < count; i++) { michael@0: r.fLeft = pts[i].fX - radius; michael@0: r.fTop = pts[i].fY - radius; michael@0: r.fRight = r.fLeft + width; michael@0: r.fBottom = r.fTop + width; michael@0: if (fDevice) { michael@0: fDevice->drawRect(*this, r, newPaint); michael@0: } else { michael@0: this->drawRect(r, newPaint); michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: case SkCanvas::kLines_PointMode: michael@0: #ifndef SK_DISABLE_DASHING_OPTIMIZATION michael@0: if (2 == count && NULL != paint.getPathEffect()) { michael@0: // most likely a dashed line - see if it is one of the ones michael@0: // we can accelerate michael@0: SkStrokeRec rec(paint); michael@0: SkPathEffect::PointData pointData; michael@0: michael@0: SkPath path; michael@0: path.moveTo(pts[0]); michael@0: path.lineTo(pts[1]); michael@0: michael@0: SkRect cullRect = SkRect::Make(fRC->getBounds()); michael@0: michael@0: if (paint.getPathEffect()->asPoints(&pointData, path, rec, michael@0: *fMatrix, &cullRect)) { michael@0: // 'asPoints' managed to find some fast path michael@0: michael@0: SkPaint newP(paint); michael@0: newP.setPathEffect(NULL); michael@0: newP.setStyle(SkPaint::kFill_Style); michael@0: michael@0: if (!pointData.fFirst.isEmpty()) { michael@0: if (fDevice) { michael@0: fDevice->drawPath(*this, pointData.fFirst, newP); michael@0: } else { michael@0: this->drawPath(pointData.fFirst, newP); michael@0: } michael@0: } michael@0: michael@0: if (!pointData.fLast.isEmpty()) { michael@0: if (fDevice) { michael@0: fDevice->drawPath(*this, pointData.fLast, newP); michael@0: } else { michael@0: this->drawPath(pointData.fLast, newP); michael@0: } michael@0: } michael@0: michael@0: if (pointData.fSize.fX == pointData.fSize.fY) { michael@0: // The rest of the dashed line can just be drawn as points michael@0: SkASSERT(pointData.fSize.fX == SkScalarHalf(newP.getStrokeWidth())); michael@0: michael@0: if (SkPathEffect::PointData::kCircles_PointFlag & pointData.fFlags) { michael@0: newP.setStrokeCap(SkPaint::kRound_Cap); michael@0: } else { michael@0: newP.setStrokeCap(SkPaint::kButt_Cap); michael@0: } michael@0: michael@0: if (fDevice) { michael@0: fDevice->drawPoints(*this, michael@0: SkCanvas::kPoints_PointMode, michael@0: pointData.fNumPoints, michael@0: pointData.fPoints, michael@0: newP); michael@0: } else { michael@0: this->drawPoints(SkCanvas::kPoints_PointMode, michael@0: pointData.fNumPoints, michael@0: pointData.fPoints, michael@0: newP, michael@0: forceUseDevice); michael@0: } michael@0: break; michael@0: } else { michael@0: // The rest of the dashed line must be drawn as rects michael@0: SkASSERT(!(SkPathEffect::PointData::kCircles_PointFlag & michael@0: pointData.fFlags)); michael@0: michael@0: SkRect r; michael@0: michael@0: for (int i = 0; i < pointData.fNumPoints; ++i) { michael@0: r.set(pointData.fPoints[i].fX - pointData.fSize.fX, michael@0: pointData.fPoints[i].fY - pointData.fSize.fY, michael@0: pointData.fPoints[i].fX + pointData.fSize.fX, michael@0: pointData.fPoints[i].fY + pointData.fSize.fY); michael@0: if (fDevice) { michael@0: fDevice->drawRect(*this, r, newP); michael@0: } else { michael@0: this->drawRect(r, newP); michael@0: } michael@0: } michael@0: } michael@0: michael@0: break; michael@0: } michael@0: } michael@0: #endif // DISABLE_DASHING_OPTIMIZATION michael@0: // couldn't take fast path so fall through! michael@0: case SkCanvas::kPolygon_PointMode: { michael@0: count -= 1; michael@0: SkPath path; michael@0: SkPaint p(paint); michael@0: p.setStyle(SkPaint::kStroke_Style); michael@0: size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1; michael@0: for (size_t i = 0; i < count; i += inc) { michael@0: path.moveTo(pts[i]); michael@0: path.lineTo(pts[i+1]); michael@0: if (fDevice) { michael@0: fDevice->drawPath(*this, path, p, NULL, true); michael@0: } else { michael@0: this->drawPath(path, p, NULL, true); michael@0: } michael@0: path.rewind(); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: static bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix, michael@0: SkPoint* strokeSize) { michael@0: if (SkPaint::kMiter_Join != paint.getStrokeJoin() || michael@0: paint.getStrokeMiter() < SK_ScalarSqrt2) { michael@0: return false; michael@0: } michael@0: michael@0: SkASSERT(matrix.rectStaysRect()); michael@0: SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() }; michael@0: matrix.mapVectors(strokeSize, &pt, 1); michael@0: strokeSize->fX = SkScalarAbs(strokeSize->fX); michael@0: strokeSize->fY = SkScalarAbs(strokeSize->fY); michael@0: return true; michael@0: } michael@0: michael@0: SkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint, michael@0: const SkMatrix& matrix, michael@0: SkPoint* strokeSize) { michael@0: RectType rtype; michael@0: const SkScalar width = paint.getStrokeWidth(); michael@0: const bool zeroWidth = (0 == width); michael@0: SkPaint::Style style = paint.getStyle(); michael@0: michael@0: if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) { michael@0: style = SkPaint::kFill_Style; michael@0: } michael@0: michael@0: if (paint.getPathEffect() || paint.getMaskFilter() || michael@0: paint.getRasterizer() || !matrix.rectStaysRect() || michael@0: SkPaint::kStrokeAndFill_Style == style) { michael@0: rtype = kPath_RectType; michael@0: } else if (SkPaint::kFill_Style == style) { michael@0: rtype = kFill_RectType; michael@0: } else if (zeroWidth) { michael@0: rtype = kHair_RectType; michael@0: } else if (easy_rect_join(paint, matrix, strokeSize)) { michael@0: rtype = kStroke_RectType; michael@0: } else { michael@0: rtype = kPath_RectType; michael@0: } michael@0: return rtype; michael@0: } michael@0: michael@0: static const SkPoint* rect_points(const SkRect& r) { michael@0: return SkTCast(&r); michael@0: } michael@0: michael@0: static SkPoint* rect_points(SkRect& r) { michael@0: return SkTCast(&r); michael@0: } michael@0: michael@0: void SkDraw::drawRect(const SkRect& rect, const SkPaint& paint) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: // nothing to draw michael@0: if (fRC->isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: SkPoint strokeSize; michael@0: RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize); michael@0: michael@0: if (kPath_RectType == rtype) { michael@0: SkPath tmp; michael@0: tmp.addRect(rect); michael@0: tmp.setFillType(SkPath::kWinding_FillType); michael@0: this->drawPath(tmp, paint, NULL, true); michael@0: return; michael@0: } michael@0: michael@0: const SkMatrix& matrix = *fMatrix; michael@0: SkRect devRect; michael@0: michael@0: // transform rect into devRect michael@0: matrix.mapPoints(rect_points(devRect), rect_points(rect), 2); michael@0: devRect.sort(); michael@0: michael@0: if (fBounder && !fBounder->doRect(devRect, paint)) { michael@0: return; michael@0: } michael@0: michael@0: // look for the quick exit, before we build a blitter michael@0: SkIRect ir; michael@0: devRect.roundOut(&ir); michael@0: if (paint.getStyle() != SkPaint::kFill_Style) { michael@0: // extra space for hairlines michael@0: ir.inset(-1, -1); michael@0: } michael@0: if (fRC->quickReject(ir)) { michael@0: return; michael@0: } michael@0: michael@0: SkDeviceLooper looper(*fBitmap, *fRC, ir, paint.isAntiAlias()); michael@0: while (looper.next()) { michael@0: SkRect localDevRect; michael@0: looper.mapRect(&localDevRect, devRect); michael@0: SkMatrix localMatrix; michael@0: looper.mapMatrix(&localMatrix, matrix); michael@0: michael@0: SkAutoBlitterChoose blitterStorage(looper.getBitmap(), localMatrix, michael@0: paint); michael@0: const SkRasterClip& clip = looper.getRC(); michael@0: SkBlitter* blitter = blitterStorage.get(); michael@0: michael@0: // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter michael@0: // case we are also hairline (if we've gotten to here), which devolves to michael@0: // effectively just kFill michael@0: switch (rtype) { michael@0: case kFill_RectType: michael@0: if (paint.isAntiAlias()) { michael@0: SkScan::AntiFillRect(localDevRect, clip, blitter); michael@0: } else { michael@0: SkScan::FillRect(localDevRect, clip, blitter); michael@0: } michael@0: break; michael@0: case kStroke_RectType: michael@0: if (paint.isAntiAlias()) { michael@0: SkScan::AntiFrameRect(localDevRect, strokeSize, clip, blitter); michael@0: } else { michael@0: SkScan::FrameRect(localDevRect, strokeSize, clip, blitter); michael@0: } michael@0: break; michael@0: case kHair_RectType: michael@0: if (paint.isAntiAlias()) { michael@0: SkScan::AntiHairRect(localDevRect, clip, blitter); michael@0: } else { michael@0: SkScan::HairRect(localDevRect, clip, blitter); michael@0: } michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("bad rtype"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const { michael@0: if (srcM.fBounds.isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: const SkMask* mask = &srcM; michael@0: michael@0: SkMask dstM; michael@0: if (paint.getMaskFilter() && michael@0: paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, NULL)) { michael@0: mask = &dstM; michael@0: } else { michael@0: dstM.fImage = NULL; michael@0: } michael@0: SkAutoMaskFreeImage ami(dstM.fImage); michael@0: michael@0: if (fBounder && !fBounder->doIRect(mask->fBounds)) { michael@0: return; michael@0: } michael@0: michael@0: SkAutoBlitterChoose blitterChooser(*fBitmap, *fMatrix, paint); michael@0: SkBlitter* blitter = blitterChooser.get(); michael@0: michael@0: SkAAClipBlitterWrapper wrapper; michael@0: const SkRegion* clipRgn; michael@0: michael@0: if (fRC->isBW()) { michael@0: clipRgn = &fRC->bwRgn(); michael@0: } else { michael@0: wrapper.init(*fRC, blitter); michael@0: clipRgn = &wrapper.getRgn(); michael@0: blitter = wrapper.getBlitter(); michael@0: } michael@0: blitter->blitMaskRegion(*mask, *clipRgn); michael@0: } michael@0: michael@0: static SkScalar fast_len(const SkVector& vec) { michael@0: SkScalar x = SkScalarAbs(vec.fX); michael@0: SkScalar y = SkScalarAbs(vec.fY); michael@0: if (x < y) { michael@0: SkTSwap(x, y); michael@0: } michael@0: return x + SkScalarHalf(y); michael@0: } michael@0: michael@0: static bool xfermodeSupportsCoverageAsAlpha(SkXfermode* xfer) { michael@0: SkXfermode::Coeff dc; michael@0: if (!SkXfermode::AsCoeff(xfer, NULL, &dc)) { michael@0: return false; michael@0: } michael@0: michael@0: switch (dc) { michael@0: case SkXfermode::kOne_Coeff: michael@0: case SkXfermode::kISA_Coeff: michael@0: case SkXfermode::kISC_Coeff: michael@0: return true; michael@0: default: michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: bool SkDrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix& matrix, michael@0: SkScalar* coverage) { michael@0: SkASSERT(strokeWidth > 0); michael@0: // We need to try to fake a thick-stroke with a modulated hairline. michael@0: michael@0: if (matrix.hasPerspective()) { michael@0: return false; michael@0: } michael@0: michael@0: SkVector src[2], dst[2]; michael@0: src[0].set(strokeWidth, 0); michael@0: src[1].set(0, strokeWidth); michael@0: matrix.mapVectors(dst, src, 2); michael@0: SkScalar len0 = fast_len(dst[0]); michael@0: SkScalar len1 = fast_len(dst[1]); michael@0: if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) { michael@0: if (NULL != coverage) { michael@0: *coverage = SkScalarAve(len0, len1); michael@0: } michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void SkDraw::drawRRect(const SkRRect& rrect, const SkPaint& paint) const { michael@0: SkDEBUGCODE(this->validate()); michael@0: michael@0: if (fRC->isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: { michael@0: // TODO: Investigate optimizing these options. They are in the same michael@0: // order as SkDraw::drawPath, which handles each case. It may be michael@0: // that there is no way to optimize for these using the SkRRect path. michael@0: SkScalar coverage; michael@0: if (SkDrawTreatAsHairline(paint, *fMatrix, &coverage)) { michael@0: goto DRAW_PATH; michael@0: } michael@0: michael@0: if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { michael@0: goto DRAW_PATH; michael@0: } michael@0: michael@0: if (paint.getRasterizer()) { michael@0: goto DRAW_PATH; michael@0: } michael@0: } michael@0: michael@0: if (paint.getMaskFilter()) { michael@0: // Transform the rrect into device space. michael@0: SkRRect devRRect; michael@0: if (rrect.transform(*fMatrix, &devRRect)) { michael@0: SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint); michael@0: if (paint.getMaskFilter()->filterRRect(devRRect, *fMatrix, *fRC, michael@0: fBounder, blitter.get(), michael@0: SkPaint::kFill_Style)) { michael@0: return; // filterRRect() called the blitter, so we're done michael@0: } michael@0: } michael@0: } michael@0: michael@0: DRAW_PATH: michael@0: // Now fall back to the default case of using a path. michael@0: SkPath path; michael@0: path.addRRect(rrect); michael@0: this->drawPath(path, paint, NULL, true); michael@0: } michael@0: michael@0: void SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint, michael@0: const SkMatrix* prePathMatrix, bool pathIsMutable, michael@0: bool drawCoverage) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: // nothing to draw michael@0: if (fRC->isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: SkPath* pathPtr = (SkPath*)&origSrcPath; michael@0: bool doFill = true; michael@0: SkPath tmpPath; michael@0: SkMatrix tmpMatrix; michael@0: const SkMatrix* matrix = fMatrix; michael@0: michael@0: if (prePathMatrix) { michael@0: if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style || michael@0: origPaint.getRasterizer()) { michael@0: SkPath* result = pathPtr; michael@0: michael@0: if (!pathIsMutable) { michael@0: result = &tmpPath; michael@0: pathIsMutable = true; michael@0: } michael@0: pathPtr->transform(*prePathMatrix, result); michael@0: pathPtr = result; michael@0: } else { michael@0: if (!tmpMatrix.setConcat(*matrix, *prePathMatrix)) { michael@0: // overflow michael@0: return; michael@0: } michael@0: matrix = &tmpMatrix; michael@0: } michael@0: } michael@0: // at this point we're done with prePathMatrix michael@0: SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) michael@0: michael@0: SkTCopyOnFirstWrite paint(origPaint); michael@0: michael@0: { michael@0: SkScalar coverage; michael@0: if (SkDrawTreatAsHairline(origPaint, *matrix, &coverage)) { michael@0: if (SK_Scalar1 == coverage) { michael@0: paint.writable()->setStrokeWidth(0); michael@0: } else if (xfermodeSupportsCoverageAsAlpha(origPaint.getXfermode())) { michael@0: U8CPU newAlpha; michael@0: #if 0 michael@0: newAlpha = SkToU8(SkScalarRoundToInt(coverage * michael@0: origPaint.getAlpha())); michael@0: #else michael@0: // this is the old technique, which we preserve for now so michael@0: // we don't change previous results (testing) michael@0: // the new way seems fine, its just (a tiny bit) different michael@0: int scale = (int)SkScalarMul(coverage, 256); michael@0: newAlpha = origPaint.getAlpha() * scale >> 8; michael@0: #endif michael@0: SkPaint* writablePaint = paint.writable(); michael@0: writablePaint->setStrokeWidth(0); michael@0: writablePaint->setAlpha(newAlpha); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) { michael@0: SkRect cullRect; michael@0: const SkRect* cullRectPtr = NULL; michael@0: if (this->computeConservativeLocalClipBounds(&cullRect)) { michael@0: cullRectPtr = &cullRect; michael@0: } michael@0: doFill = paint->getFillPath(*pathPtr, &tmpPath, cullRectPtr); michael@0: pathPtr = &tmpPath; michael@0: } michael@0: michael@0: if (paint->getRasterizer()) { michael@0: SkMask mask; michael@0: if (paint->getRasterizer()->rasterize(*pathPtr, *matrix, michael@0: &fRC->getBounds(), paint->getMaskFilter(), &mask, michael@0: SkMask::kComputeBoundsAndRenderImage_CreateMode)) { michael@0: this->drawDevMask(mask, *paint); michael@0: SkMask::FreeImage(mask.fImage); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: // avoid possibly allocating a new path in transform if we can michael@0: SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath; michael@0: michael@0: // transform the path into device space michael@0: pathPtr->transform(*matrix, devPathPtr); michael@0: michael@0: SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, *paint, drawCoverage); michael@0: michael@0: if (paint->getMaskFilter()) { michael@0: SkPaint::Style style = doFill ? SkPaint::kFill_Style : michael@0: SkPaint::kStroke_Style; michael@0: if (paint->getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fRC, michael@0: fBounder, blitter.get(), michael@0: style)) { michael@0: return; // filterPath() called the blitter, so we're done michael@0: } michael@0: } michael@0: michael@0: if (fBounder && !fBounder->doPath(*devPathPtr, *paint, doFill)) { michael@0: return; michael@0: } michael@0: michael@0: void (*proc)(const SkPath&, const SkRasterClip&, SkBlitter*); michael@0: if (doFill) { michael@0: if (paint->isAntiAlias()) { michael@0: proc = SkScan::AntiFillPath; michael@0: } else { michael@0: proc = SkScan::FillPath; michael@0: } michael@0: } else { // hairline michael@0: if (paint->isAntiAlias()) { michael@0: proc = SkScan::AntiHairPath; michael@0: } else { michael@0: proc = SkScan::HairPath; michael@0: } michael@0: } michael@0: proc(*devPathPtr, *fRC, blitter.get()); michael@0: } michael@0: michael@0: /** For the purposes of drawing bitmaps, if a matrix is "almost" translate michael@0: go ahead and treat it as if it were, so that subsequent code can go fast. michael@0: */ michael@0: static bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) { michael@0: unsigned bits = 0; // TODO: find a way to allow the caller to tell us to michael@0: // respect filtering. michael@0: return SkTreatAsSprite(matrix, bitmap.width(), bitmap.height(), bits); michael@0: } michael@0: michael@0: void SkDraw::drawBitmapAsMask(const SkBitmap& bitmap, michael@0: const SkPaint& paint) const { michael@0: SkASSERT(bitmap.colorType() == kAlpha_8_SkColorType); michael@0: michael@0: if (just_translate(*fMatrix, bitmap)) { michael@0: int ix = SkScalarRoundToInt(fMatrix->getTranslateX()); michael@0: int iy = SkScalarRoundToInt(fMatrix->getTranslateY()); michael@0: michael@0: SkAutoLockPixels alp(bitmap); michael@0: if (!bitmap.readyToDraw()) { michael@0: return; michael@0: } michael@0: michael@0: SkMask mask; michael@0: mask.fBounds.set(ix, iy, ix + bitmap.width(), iy + bitmap.height()); michael@0: mask.fFormat = SkMask::kA8_Format; michael@0: mask.fRowBytes = SkToU32(bitmap.rowBytes()); michael@0: mask.fImage = bitmap.getAddr8(0, 0); michael@0: michael@0: this->drawDevMask(mask, paint); michael@0: } else { // need to xform the bitmap first michael@0: SkRect r; michael@0: SkMask mask; michael@0: michael@0: r.set(0, 0, michael@0: SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())); michael@0: fMatrix->mapRect(&r); michael@0: r.round(&mask.fBounds); michael@0: michael@0: // set the mask's bounds to the transformed bitmap-bounds, michael@0: // clipped to the actual device michael@0: { michael@0: SkIRect devBounds; michael@0: devBounds.set(0, 0, fBitmap->width(), fBitmap->height()); michael@0: // need intersect(l, t, r, b) on irect michael@0: if (!mask.fBounds.intersect(devBounds)) { michael@0: return; michael@0: } michael@0: } michael@0: michael@0: mask.fFormat = SkMask::kA8_Format; michael@0: mask.fRowBytes = SkAlign4(mask.fBounds.width()); michael@0: size_t size = mask.computeImageSize(); michael@0: if (0 == size) { michael@0: // the mask is too big to allocated, draw nothing michael@0: return; michael@0: } michael@0: michael@0: // allocate (and clear) our temp buffer to hold the transformed bitmap michael@0: SkAutoMalloc storage(size); michael@0: mask.fImage = (uint8_t*)storage.get(); michael@0: memset(mask.fImage, 0, size); michael@0: michael@0: // now draw our bitmap(src) into mask(dst), transformed by the matrix michael@0: { michael@0: SkBitmap device; michael@0: device.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), michael@0: mask.fBounds.height(), mask.fRowBytes); michael@0: device.setPixels(mask.fImage); michael@0: michael@0: SkCanvas c(device); michael@0: // need the unclipped top/left for the translate michael@0: c.translate(-SkIntToScalar(mask.fBounds.fLeft), michael@0: -SkIntToScalar(mask.fBounds.fTop)); michael@0: c.concat(*fMatrix); michael@0: michael@0: // We can't call drawBitmap, or we'll infinitely recurse. Instead michael@0: // we manually build a shader and draw that into our new mask michael@0: SkPaint tmpPaint; michael@0: tmpPaint.setFlags(paint.getFlags()); michael@0: SkAutoBitmapShaderInstall install(bitmap, tmpPaint); michael@0: SkRect rr; michael@0: rr.set(0, 0, SkIntToScalar(bitmap.width()), michael@0: SkIntToScalar(bitmap.height())); michael@0: c.drawRect(rr, install.paintWithShader()); michael@0: } michael@0: this->drawDevMask(mask, paint); michael@0: } michael@0: } michael@0: michael@0: static bool clipped_out(const SkMatrix& m, const SkRasterClip& c, michael@0: const SkRect& srcR) { michael@0: SkRect dstR; michael@0: SkIRect devIR; michael@0: michael@0: m.mapRect(&dstR, srcR); michael@0: dstR.roundOut(&devIR); michael@0: return c.quickReject(devIR); michael@0: } michael@0: michael@0: static bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip, michael@0: int width, int height) { michael@0: SkRect r; michael@0: r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height)); michael@0: return clipped_out(matrix, clip, r); michael@0: } michael@0: michael@0: static bool clipHandlesSprite(const SkRasterClip& clip, int x, int y, michael@0: const SkBitmap& bitmap) { michael@0: return clip.isBW() || michael@0: clip.quickContains(x, y, x + bitmap.width(), y + bitmap.height()); michael@0: } michael@0: michael@0: void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix, michael@0: const SkPaint& origPaint) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: // nothing to draw michael@0: if (fRC->isEmpty() || michael@0: bitmap.width() == 0 || bitmap.height() == 0 || michael@0: bitmap.colorType() == kUnknown_SkColorType) { michael@0: return; michael@0: } michael@0: michael@0: SkPaint paint(origPaint); michael@0: paint.setStyle(SkPaint::kFill_Style); michael@0: michael@0: SkMatrix matrix; michael@0: if (!matrix.setConcat(*fMatrix, prematrix)) { michael@0: return; michael@0: } michael@0: michael@0: if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) { michael@0: return; michael@0: } michael@0: michael@0: if (fBounder && just_translate(matrix, bitmap)) { michael@0: SkIRect ir; michael@0: int32_t ix = SkScalarRoundToInt(matrix.getTranslateX()); michael@0: int32_t iy = SkScalarRoundToInt(matrix.getTranslateY()); michael@0: ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height()); michael@0: if (!fBounder->doIRect(ir)) { michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if (bitmap.colorType() != kAlpha_8_SkColorType && michael@0: just_translate(matrix, bitmap)) { michael@0: // michael@0: // It is safe to call lock pixels now, since we know the matrix is michael@0: // (more or less) identity. michael@0: // michael@0: SkAutoLockPixels alp(bitmap); michael@0: if (!bitmap.readyToDraw()) { michael@0: return; michael@0: } michael@0: int ix = SkScalarRoundToInt(matrix.getTranslateX()); michael@0: int iy = SkScalarRoundToInt(matrix.getTranslateY()); michael@0: if (clipHandlesSprite(*fRC, ix, iy, bitmap)) { michael@0: SkTBlitterAllocator allocator; michael@0: // blitter will be owned by the allocator. michael@0: SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap, michael@0: ix, iy, &allocator); michael@0: if (blitter) { michael@0: SkIRect ir; michael@0: ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height()); michael@0: michael@0: SkScan::FillIRect(ir, *fRC, blitter); michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // now make a temp draw on the stack, and use it michael@0: // michael@0: SkDraw draw(*this); michael@0: draw.fMatrix = &matrix; michael@0: michael@0: if (bitmap.colorType() == kAlpha_8_SkColorType) { michael@0: draw.drawBitmapAsMask(bitmap, paint); michael@0: } else { michael@0: SkAutoBitmapShaderInstall install(bitmap, paint); michael@0: michael@0: SkRect r; michael@0: r.set(0, 0, SkIntToScalar(bitmap.width()), michael@0: SkIntToScalar(bitmap.height())); michael@0: // is this ok if paint has a rasterizer? michael@0: draw.drawRect(r, install.paintWithShader()); michael@0: } michael@0: } michael@0: michael@0: void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y, michael@0: const SkPaint& origPaint) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: // nothing to draw michael@0: if (fRC->isEmpty() || michael@0: bitmap.width() == 0 || bitmap.height() == 0 || michael@0: bitmap.colorType() == kUnknown_SkColorType) { michael@0: return; michael@0: } michael@0: michael@0: SkIRect bounds; michael@0: bounds.set(x, y, x + bitmap.width(), y + bitmap.height()); michael@0: michael@0: if (fRC->quickReject(bounds)) { michael@0: return; // nothing to draw michael@0: } michael@0: michael@0: SkPaint paint(origPaint); michael@0: paint.setStyle(SkPaint::kFill_Style); michael@0: michael@0: if (NULL == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, bitmap)) { michael@0: SkTBlitterAllocator allocator; michael@0: // blitter will be owned by the allocator. michael@0: SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap, michael@0: x, y, &allocator); michael@0: michael@0: if (blitter) { michael@0: if (fBounder && !fBounder->doIRect(bounds)) { michael@0: return; michael@0: } michael@0: michael@0: SkScan::FillIRect(bounds, *fRC, blitter); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: SkAutoBitmapShaderInstall install(bitmap, paint); michael@0: const SkPaint& shaderPaint = install.paintWithShader(); michael@0: michael@0: SkMatrix matrix; michael@0: SkRect r; michael@0: michael@0: // get a scalar version of our rect michael@0: r.set(bounds); michael@0: michael@0: // tell the shader our offset michael@0: matrix.setTranslate(r.fLeft, r.fTop); michael@0: shaderPaint.getShader()->setLocalMatrix(matrix); michael@0: michael@0: SkDraw draw(*this); michael@0: matrix.reset(); michael@0: draw.fMatrix = &matrix; michael@0: // call ourself with a rect michael@0: // is this OK if paint has a rasterizer? michael@0: draw.drawRect(r, shaderPaint); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkScalerContext.h" michael@0: #include "SkGlyphCache.h" michael@0: #include "SkTextToPathIter.h" michael@0: #include "SkUtils.h" michael@0: michael@0: static void measure_text(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc, michael@0: const char text[], size_t byteLength, SkVector* stopVector) { michael@0: SkFixed x = 0, y = 0; michael@0: const char* stop = text + byteLength; michael@0: michael@0: SkAutoKern autokern; michael@0: michael@0: while (text < stop) { michael@0: // don't need x, y here, since all subpixel variants will have the michael@0: // same advance michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); michael@0: michael@0: x += autokern.adjust(glyph) + glyph.fAdvanceX; michael@0: y += glyph.fAdvanceY; michael@0: } michael@0: stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y)); michael@0: michael@0: SkASSERT(text == stop); michael@0: } michael@0: michael@0: bool SkDraw::ShouldDrawTextAsPaths(const SkPaint& paint, const SkMatrix& ctm) { michael@0: // hairline glyphs are fast enough so we don't need to cache them michael@0: if (SkPaint::kStroke_Style == paint.getStyle() && 0 == paint.getStrokeWidth()) { michael@0: return true; michael@0: } michael@0: michael@0: // we don't cache perspective michael@0: if (ctm.hasPerspective()) { michael@0: return true; michael@0: } michael@0: michael@0: SkMatrix textM; michael@0: return SkPaint::TooBigToUseCache(ctm, *paint.setTextMatrix(&textM)); michael@0: } michael@0: michael@0: void SkDraw::drawText_asPaths(const char text[], size_t byteLength, michael@0: SkScalar x, SkScalar y, michael@0: const SkPaint& paint) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: SkTextToPathIter iter(text, byteLength, paint, true); michael@0: michael@0: SkMatrix matrix; michael@0: matrix.setScale(iter.getPathScale(), iter.getPathScale()); michael@0: matrix.postTranslate(x, y); michael@0: michael@0: const SkPath* iterPath; michael@0: SkScalar xpos, prevXPos = 0; michael@0: michael@0: while (iter.next(&iterPath, &xpos)) { michael@0: matrix.postTranslate(xpos - prevXPos, 0); michael@0: if (iterPath) { michael@0: const SkPaint& pnt = iter.getPaint(); michael@0: if (fDevice) { michael@0: fDevice->drawPath(*this, *iterPath, pnt, &matrix, false); michael@0: } else { michael@0: this->drawPath(*iterPath, pnt, &matrix, false); michael@0: } michael@0: } michael@0: prevXPos = xpos; michael@0: } michael@0: } michael@0: michael@0: // disable warning : local variable used without having been initialized michael@0: #if defined _WIN32 && _MSC_VER >= 1300 michael@0: #pragma warning ( push ) michael@0: #pragma warning ( disable : 4701 ) michael@0: #endif michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static void D1G_NoBounder_RectClip(const SkDraw1Glyph& state, michael@0: SkFixed fx, SkFixed fy, michael@0: const SkGlyph& glyph) { michael@0: int left = SkFixedFloorToInt(fx); michael@0: int top = SkFixedFloorToInt(fy); michael@0: SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); michael@0: SkASSERT(NULL == state.fBounder); michael@0: SkASSERT((NULL == state.fClip && state.fAAClip) || michael@0: (state.fClip && NULL == state.fAAClip && state.fClip->isRect())); michael@0: michael@0: left += glyph.fLeft; michael@0: top += glyph.fTop; michael@0: michael@0: int right = left + glyph.fWidth; michael@0: int bottom = top + glyph.fHeight; michael@0: michael@0: SkMask mask; michael@0: SkIRect storage; michael@0: SkIRect* bounds = &mask.fBounds; michael@0: michael@0: mask.fBounds.set(left, top, right, bottom); michael@0: michael@0: // this extra test is worth it, assuming that most of the time it succeeds michael@0: // since we can avoid writing to storage michael@0: if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) { michael@0: if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds)) michael@0: return; michael@0: bounds = &storage; michael@0: } michael@0: michael@0: uint8_t* aa = (uint8_t*)glyph.fImage; michael@0: if (NULL == aa) { michael@0: aa = (uint8_t*)state.fCache->findImage(glyph); michael@0: if (NULL == aa) { michael@0: return; // can't rasterize glyph michael@0: } michael@0: } michael@0: michael@0: mask.fRowBytes = glyph.rowBytes(); michael@0: mask.fFormat = static_cast(glyph.fMaskFormat); michael@0: mask.fImage = aa; michael@0: state.blitMask(mask, *bounds); michael@0: } michael@0: michael@0: static void D1G_NoBounder_RgnClip(const SkDraw1Glyph& state, michael@0: SkFixed fx, SkFixed fy, michael@0: const SkGlyph& glyph) { michael@0: int left = SkFixedFloorToInt(fx); michael@0: int top = SkFixedFloorToInt(fy); michael@0: SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); michael@0: SkASSERT(!state.fClip->isRect()); michael@0: SkASSERT(NULL == state.fBounder); michael@0: michael@0: SkMask mask; michael@0: michael@0: left += glyph.fLeft; michael@0: top += glyph.fTop; michael@0: michael@0: mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight); michael@0: SkRegion::Cliperator clipper(*state.fClip, mask.fBounds); michael@0: michael@0: if (!clipper.done()) { michael@0: const SkIRect& cr = clipper.rect(); michael@0: const uint8_t* aa = (const uint8_t*)glyph.fImage; michael@0: if (NULL == aa) { michael@0: aa = (uint8_t*)state.fCache->findImage(glyph); michael@0: if (NULL == aa) { michael@0: return; michael@0: } michael@0: } michael@0: michael@0: mask.fRowBytes = glyph.rowBytes(); michael@0: mask.fFormat = static_cast(glyph.fMaskFormat); michael@0: mask.fImage = (uint8_t*)aa; michael@0: do { michael@0: state.blitMask(mask, cr); michael@0: clipper.next(); michael@0: } while (!clipper.done()); michael@0: } michael@0: } michael@0: michael@0: static void D1G_Bounder(const SkDraw1Glyph& state, michael@0: SkFixed fx, SkFixed fy, michael@0: const SkGlyph& glyph) { michael@0: int left = SkFixedFloorToInt(fx); michael@0: int top = SkFixedFloorToInt(fy); michael@0: SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); michael@0: michael@0: SkMask mask; michael@0: michael@0: left += glyph.fLeft; michael@0: top += glyph.fTop; michael@0: michael@0: mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight); michael@0: SkRegion::Cliperator clipper(*state.fClip, mask.fBounds); michael@0: michael@0: if (!clipper.done()) { michael@0: const SkIRect& cr = clipper.rect(); michael@0: const uint8_t* aa = (const uint8_t*)glyph.fImage; michael@0: if (NULL == aa) { michael@0: aa = (uint8_t*)state.fCache->findImage(glyph); michael@0: if (NULL == aa) { michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // we need to pass the origin, which we approximate with our michael@0: // (unadjusted) left,top coordinates (the caller called fixedfloor) michael@0: if (state.fBounder->doIRectGlyph(cr, michael@0: left - glyph.fLeft, michael@0: top - glyph.fTop, glyph)) { michael@0: mask.fRowBytes = glyph.rowBytes(); michael@0: mask.fFormat = static_cast(glyph.fMaskFormat); michael@0: mask.fImage = (uint8_t*)aa; michael@0: do { michael@0: state.blitMask(mask, cr); michael@0: clipper.next(); michael@0: } while (!clipper.done()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void D1G_Bounder_AAClip(const SkDraw1Glyph& state, michael@0: SkFixed fx, SkFixed fy, michael@0: const SkGlyph& glyph) { michael@0: int left = SkFixedFloorToInt(fx); michael@0: int top = SkFixedFloorToInt(fy); michael@0: SkIRect bounds; michael@0: bounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight); michael@0: michael@0: if (state.fBounder->doIRectGlyph(bounds, left, top, glyph)) { michael@0: D1G_NoBounder_RectClip(state, fx, fy, glyph); michael@0: } michael@0: } michael@0: michael@0: static bool hasCustomD1GProc(const SkDraw& draw) { michael@0: return draw.fProcs && draw.fProcs->fD1GProc; michael@0: } michael@0: michael@0: static bool needsRasterTextBlit(const SkDraw& draw) { michael@0: return !hasCustomD1GProc(draw); michael@0: } michael@0: michael@0: SkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter, michael@0: SkGlyphCache* cache, const SkPaint& pnt) { michael@0: fDraw = draw; michael@0: fBounder = draw->fBounder; michael@0: fBlitter = blitter; michael@0: fCache = cache; michael@0: fPaint = &pnt; michael@0: michael@0: if (cache->isSubpixel()) { michael@0: fHalfSampleX = fHalfSampleY = (SK_FixedHalf >> SkGlyph::kSubBits); michael@0: } else { michael@0: fHalfSampleX = fHalfSampleY = SK_FixedHalf; michael@0: } michael@0: michael@0: if (hasCustomD1GProc(*draw)) { michael@0: // todo: fix this assumption about clips w/ custom michael@0: fClip = draw->fClip; michael@0: fClipBounds = fClip->getBounds(); michael@0: return draw->fProcs->fD1GProc; michael@0: } michael@0: michael@0: if (draw->fRC->isBW()) { michael@0: fAAClip = NULL; michael@0: fClip = &draw->fRC->bwRgn(); michael@0: fClipBounds = fClip->getBounds(); michael@0: if (NULL == fBounder) { michael@0: if (fClip->isRect()) { michael@0: return D1G_NoBounder_RectClip; michael@0: } else { michael@0: return D1G_NoBounder_RgnClip; michael@0: } michael@0: } else { michael@0: return D1G_Bounder; michael@0: } michael@0: } else { // aaclip michael@0: fAAClip = &draw->fRC->aaRgn(); michael@0: fClip = NULL; michael@0: fClipBounds = fAAClip->getBounds(); michael@0: if (NULL == fBounder) { michael@0: return D1G_NoBounder_RectClip; michael@0: } else { michael@0: return D1G_Bounder_AAClip; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkDraw1Glyph::blitMaskAsSprite(const SkMask& mask) const { michael@0: SkASSERT(SkMask::kARGB32_Format == mask.fFormat); michael@0: michael@0: SkBitmap bm; michael@0: bm.setConfig(SkBitmap::kARGB_8888_Config, michael@0: mask.fBounds.width(), mask.fBounds.height(), mask.fRowBytes); michael@0: bm.setPixels((SkPMColor*)mask.fImage); michael@0: michael@0: fDraw->drawSprite(bm, mask.fBounds.x(), mask.fBounds.y(), *fPaint); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkDraw::drawText(const char text[], size_t byteLength, michael@0: SkScalar x, SkScalar y, const SkPaint& paint) const { michael@0: SkASSERT(byteLength == 0 || text != NULL); michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: // nothing to draw michael@0: if (text == NULL || byteLength == 0 || fRC->isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: // SkScalarRec doesn't currently have a way of representing hairline stroke and michael@0: // will fill if its frame-width is 0. michael@0: if (ShouldDrawTextAsPaths(paint, *fMatrix)) { michael@0: this->drawText_asPaths(text, byteLength, x, y, paint); michael@0: return; michael@0: } michael@0: michael@0: SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc(); michael@0: michael@0: SkAutoGlyphCache autoCache(paint, &fDevice->fLeakyProperties, fMatrix); michael@0: SkGlyphCache* cache = autoCache.getCache(); michael@0: michael@0: // transform our starting point michael@0: { michael@0: SkPoint loc; michael@0: fMatrix->mapXY(x, y, &loc); michael@0: x = loc.fX; michael@0: y = loc.fY; michael@0: } michael@0: michael@0: // need to measure first michael@0: if (paint.getTextAlign() != SkPaint::kLeft_Align) { michael@0: SkVector stop; michael@0: michael@0: measure_text(cache, glyphCacheProc, text, byteLength, &stop); michael@0: michael@0: SkScalar stopX = stop.fX; michael@0: SkScalar stopY = stop.fY; michael@0: michael@0: if (paint.getTextAlign() == SkPaint::kCenter_Align) { michael@0: stopX = SkScalarHalf(stopX); michael@0: stopY = SkScalarHalf(stopY); michael@0: } michael@0: x -= stopX; michael@0: y -= stopY; michael@0: } michael@0: michael@0: const char* stop = text + byteLength; michael@0: michael@0: SkAAClipBlitter aaBlitter; michael@0: SkAutoBlitterChoose blitterChooser; michael@0: SkBlitter* blitter = NULL; michael@0: if (needsRasterTextBlit(*this)) { michael@0: blitterChooser.choose(*fBitmap, *fMatrix, paint); michael@0: blitter = blitterChooser.get(); michael@0: if (fRC->isAA()) { michael@0: aaBlitter.init(blitter, &fRC->aaRgn()); michael@0: blitter = &aaBlitter; michael@0: } michael@0: } michael@0: michael@0: SkAutoKern autokern; michael@0: SkDraw1Glyph d1g; michael@0: SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint); michael@0: michael@0: SkFixed fxMask = ~0; michael@0: SkFixed fyMask = ~0; michael@0: if (cache->isSubpixel()) { michael@0: SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*fMatrix); michael@0: if (kX_SkAxisAlignment == baseline) { michael@0: fyMask = 0; michael@0: d1g.fHalfSampleY = SK_FixedHalf; michael@0: } else if (kY_SkAxisAlignment == baseline) { michael@0: fxMask = 0; michael@0: d1g.fHalfSampleX = SK_FixedHalf; michael@0: } michael@0: } michael@0: michael@0: SkFixed fx = SkScalarToFixed(x) + d1g.fHalfSampleX; michael@0: SkFixed fy = SkScalarToFixed(y) + d1g.fHalfSampleY; michael@0: michael@0: while (text < stop) { michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask); michael@0: michael@0: fx += autokern.adjust(glyph); michael@0: michael@0: if (glyph.fWidth) { michael@0: proc(d1g, fx, fy, glyph); michael@0: } michael@0: michael@0: fx += glyph.fAdvanceX; michael@0: fy += glyph.fAdvanceY; michael@0: } michael@0: } michael@0: michael@0: // last parameter is interpreted as SkFixed [x, y] michael@0: // return the fixed position, which may be rounded or not by the caller michael@0: // e.g. subpixel doesn't round michael@0: typedef void (*AlignProc)(const SkPoint&, const SkGlyph&, SkIPoint*); michael@0: michael@0: static void leftAlignProc(const SkPoint& loc, const SkGlyph& glyph, SkIPoint* dst) { michael@0: dst->set(SkScalarToFixed(loc.fX), SkScalarToFixed(loc.fY)); michael@0: } michael@0: michael@0: static void centerAlignProc(const SkPoint& loc, const SkGlyph& glyph, SkIPoint* dst) { michael@0: dst->set(SkScalarToFixed(loc.fX) - (glyph.fAdvanceX >> 1), michael@0: SkScalarToFixed(loc.fY) - (glyph.fAdvanceY >> 1)); michael@0: } michael@0: michael@0: static void rightAlignProc(const SkPoint& loc, const SkGlyph& glyph, SkIPoint* dst) { michael@0: dst->set(SkScalarToFixed(loc.fX) - glyph.fAdvanceX, michael@0: SkScalarToFixed(loc.fY) - glyph.fAdvanceY); michael@0: } michael@0: michael@0: static AlignProc pick_align_proc(SkPaint::Align align) { michael@0: static const AlignProc gProcs[] = { michael@0: leftAlignProc, centerAlignProc, rightAlignProc michael@0: }; michael@0: michael@0: SkASSERT((unsigned)align < SK_ARRAY_COUNT(gProcs)); michael@0: michael@0: return gProcs[align]; michael@0: } michael@0: michael@0: typedef void (*AlignProc_scalar)(const SkPoint&, const SkGlyph&, SkPoint*); michael@0: michael@0: static void leftAlignProc_scalar(const SkPoint& loc, const SkGlyph& glyph, SkPoint* dst) { michael@0: dst->set(loc.fX, loc.fY); michael@0: } michael@0: michael@0: static void centerAlignProc_scalar(const SkPoint& loc, const SkGlyph& glyph, SkPoint* dst) { michael@0: dst->set(loc.fX - SkFixedToScalar(glyph.fAdvanceX >> 1), michael@0: loc.fY - SkFixedToScalar(glyph.fAdvanceY >> 1)); michael@0: } michael@0: michael@0: static void rightAlignProc_scalar(const SkPoint& loc, const SkGlyph& glyph, SkPoint* dst) { michael@0: dst->set(loc.fX - SkFixedToScalar(glyph.fAdvanceX), michael@0: loc.fY - SkFixedToScalar(glyph.fAdvanceY)); michael@0: } michael@0: michael@0: static AlignProc_scalar pick_align_proc_scalar(SkPaint::Align align) { michael@0: static const AlignProc_scalar gProcs[] = { michael@0: leftAlignProc_scalar, centerAlignProc_scalar, rightAlignProc_scalar michael@0: }; michael@0: michael@0: SkASSERT((unsigned)align < SK_ARRAY_COUNT(gProcs)); michael@0: michael@0: return gProcs[align]; michael@0: } michael@0: michael@0: class TextMapState { michael@0: public: michael@0: mutable SkPoint fLoc; michael@0: michael@0: TextMapState(const SkMatrix& matrix, SkScalar y) michael@0: : fMatrix(matrix), fProc(matrix.getMapXYProc()), fY(y) {} michael@0: michael@0: typedef void (*Proc)(const TextMapState&, const SkScalar pos[]); michael@0: michael@0: Proc pickProc(int scalarsPerPosition); michael@0: michael@0: private: michael@0: const SkMatrix& fMatrix; michael@0: SkMatrix::MapXYProc fProc; michael@0: SkScalar fY; // ignored by MapXYProc michael@0: // these are only used by Only... procs michael@0: SkScalar fScaleX, fTransX, fTransformedY; michael@0: michael@0: static void MapXProc(const TextMapState& state, const SkScalar pos[]) { michael@0: state.fProc(state.fMatrix, *pos, state.fY, &state.fLoc); michael@0: } michael@0: michael@0: static void MapXYProc(const TextMapState& state, const SkScalar pos[]) { michael@0: state.fProc(state.fMatrix, pos[0], pos[1], &state.fLoc); michael@0: } michael@0: michael@0: static void MapOnlyScaleXProc(const TextMapState& state, michael@0: const SkScalar pos[]) { michael@0: state.fLoc.set(SkScalarMul(state.fScaleX, *pos) + state.fTransX, michael@0: state.fTransformedY); michael@0: } michael@0: michael@0: static void MapOnlyTransXProc(const TextMapState& state, michael@0: const SkScalar pos[]) { michael@0: state.fLoc.set(*pos + state.fTransX, state.fTransformedY); michael@0: } michael@0: }; michael@0: michael@0: TextMapState::Proc TextMapState::pickProc(int scalarsPerPosition) { michael@0: SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); michael@0: michael@0: if (1 == scalarsPerPosition) { michael@0: unsigned mtype = fMatrix.getType(); michael@0: if (mtype & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) { michael@0: return MapXProc; michael@0: } else { michael@0: fScaleX = fMatrix.getScaleX(); michael@0: fTransX = fMatrix.getTranslateX(); michael@0: fTransformedY = SkScalarMul(fY, fMatrix.getScaleY()) + michael@0: fMatrix.getTranslateY(); michael@0: return (mtype & SkMatrix::kScale_Mask) ? michael@0: MapOnlyScaleXProc : MapOnlyTransXProc; michael@0: } michael@0: } else { michael@0: return MapXYProc; michael@0: } michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkDraw::drawPosText_asPaths(const char text[], size_t byteLength, michael@0: const SkScalar pos[], SkScalar constY, michael@0: int scalarsPerPosition, michael@0: const SkPaint& origPaint) const { michael@0: // setup our std paint, in hopes of getting hits in the cache michael@0: SkPaint paint(origPaint); michael@0: SkScalar matrixScale = paint.setupForAsPaths(); michael@0: michael@0: SkMatrix matrix; michael@0: matrix.setScale(matrixScale, matrixScale); michael@0: michael@0: SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc(); michael@0: SkAutoGlyphCache autoCache(paint, NULL, NULL); michael@0: SkGlyphCache* cache = autoCache.getCache(); michael@0: michael@0: const char* stop = text + byteLength; michael@0: AlignProc_scalar alignProc = pick_align_proc_scalar(paint.getTextAlign()); michael@0: TextMapState tms(SkMatrix::I(), constY); michael@0: TextMapState::Proc tmsProc = tms.pickProc(scalarsPerPosition); michael@0: michael@0: while (text < stop) { michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); michael@0: if (glyph.fWidth) { michael@0: const SkPath* path = cache->findPath(glyph); michael@0: if (path) { michael@0: tmsProc(tms, pos); michael@0: SkPoint loc; michael@0: alignProc(tms.fLoc, glyph, &loc); michael@0: michael@0: matrix[SkMatrix::kMTransX] = loc.fX; michael@0: matrix[SkMatrix::kMTransY] = loc.fY; michael@0: if (fDevice) { michael@0: fDevice->drawPath(*this, *path, paint, &matrix, false); michael@0: } else { michael@0: this->drawPath(*path, paint, &matrix, false); michael@0: } michael@0: } michael@0: } michael@0: pos += scalarsPerPosition; michael@0: } michael@0: } michael@0: michael@0: void SkDraw::drawPosText(const char text[], size_t byteLength, michael@0: const SkScalar pos[], SkScalar constY, michael@0: int scalarsPerPosition, const SkPaint& paint) const { michael@0: SkASSERT(byteLength == 0 || text != NULL); michael@0: SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: // nothing to draw michael@0: if (text == NULL || byteLength == 0 || fRC->isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: if (ShouldDrawTextAsPaths(paint, *fMatrix)) { michael@0: this->drawPosText_asPaths(text, byteLength, pos, constY, michael@0: scalarsPerPosition, paint); michael@0: return; michael@0: } michael@0: michael@0: SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc(); michael@0: SkAutoGlyphCache autoCache(paint, &fDevice->fLeakyProperties, fMatrix); michael@0: SkGlyphCache* cache = autoCache.getCache(); michael@0: michael@0: SkAAClipBlitterWrapper wrapper; michael@0: SkAutoBlitterChoose blitterChooser; michael@0: SkBlitter* blitter = NULL; michael@0: if (needsRasterTextBlit(*this)) { michael@0: blitterChooser.choose(*fBitmap, *fMatrix, paint); michael@0: blitter = blitterChooser.get(); michael@0: if (fRC->isAA()) { michael@0: wrapper.init(*fRC, blitter); michael@0: blitter = wrapper.getBlitter(); michael@0: } michael@0: } michael@0: michael@0: const char* stop = text + byteLength; michael@0: AlignProc alignProc = pick_align_proc(paint.getTextAlign()); michael@0: SkDraw1Glyph d1g; michael@0: SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint); michael@0: TextMapState tms(*fMatrix, constY); michael@0: TextMapState::Proc tmsProc = tms.pickProc(scalarsPerPosition); michael@0: michael@0: if (cache->isSubpixel()) { michael@0: // maybe we should skip the rounding if linearText is set michael@0: SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*fMatrix); michael@0: michael@0: SkFixed fxMask = ~0; michael@0: SkFixed fyMask = ~0; michael@0: if (kX_SkAxisAlignment == baseline) { michael@0: fyMask = 0; michael@0: #ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX michael@0: d1g.fHalfSampleY = SK_FixedHalf; michael@0: #endif michael@0: } else if (kY_SkAxisAlignment == baseline) { michael@0: fxMask = 0; michael@0: #ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX michael@0: d1g.fHalfSampleX = SK_FixedHalf; michael@0: #endif michael@0: } michael@0: michael@0: if (SkPaint::kLeft_Align == paint.getTextAlign()) { michael@0: while (text < stop) { michael@0: tmsProc(tms, pos); michael@0: SkFixed fx = SkScalarToFixed(tms.fLoc.fX) + d1g.fHalfSampleX; michael@0: SkFixed fy = SkScalarToFixed(tms.fLoc.fY) + d1g.fHalfSampleY; michael@0: michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, michael@0: fx & fxMask, fy & fyMask); michael@0: michael@0: if (glyph.fWidth) { michael@0: proc(d1g, fx, fy, glyph); michael@0: } michael@0: pos += scalarsPerPosition; michael@0: } michael@0: } else { michael@0: while (text < stop) { michael@0: const char* currentText = text; michael@0: const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0); michael@0: michael@0: if (metricGlyph.fWidth) { michael@0: SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;) michael@0: SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;) michael@0: michael@0: tmsProc(tms, pos); michael@0: SkIPoint fixedLoc; michael@0: alignProc(tms.fLoc, metricGlyph, &fixedLoc); michael@0: michael@0: SkFixed fx = fixedLoc.fX + d1g.fHalfSampleX; michael@0: SkFixed fy = fixedLoc.fY + d1g.fHalfSampleY; michael@0: michael@0: // have to call again, now that we've been "aligned" michael@0: const SkGlyph& glyph = glyphCacheProc(cache, ¤tText, michael@0: fx & fxMask, fy & fyMask); michael@0: // the assumption is that the metrics haven't changed michael@0: SkASSERT(prevAdvX == glyph.fAdvanceX); michael@0: SkASSERT(prevAdvY == glyph.fAdvanceY); michael@0: SkASSERT(glyph.fWidth); michael@0: michael@0: proc(d1g, fx, fy, glyph); michael@0: } michael@0: pos += scalarsPerPosition; michael@0: } michael@0: } michael@0: } else { // not subpixel michael@0: if (SkPaint::kLeft_Align == paint.getTextAlign()) { michael@0: while (text < stop) { michael@0: // the last 2 parameters are ignored michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); michael@0: michael@0: if (glyph.fWidth) { michael@0: tmsProc(tms, pos); michael@0: michael@0: proc(d1g, michael@0: SkScalarToFixed(tms.fLoc.fX) + SK_FixedHalf, //d1g.fHalfSampleX, michael@0: SkScalarToFixed(tms.fLoc.fY) + SK_FixedHalf, //d1g.fHalfSampleY, michael@0: glyph); michael@0: } michael@0: pos += scalarsPerPosition; michael@0: } michael@0: } else { michael@0: while (text < stop) { michael@0: // the last 2 parameters are ignored michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); michael@0: michael@0: if (glyph.fWidth) { michael@0: tmsProc(tms, pos); michael@0: michael@0: SkIPoint fixedLoc; michael@0: alignProc(tms.fLoc, glyph, &fixedLoc); michael@0: michael@0: proc(d1g, michael@0: fixedLoc.fX + SK_FixedHalf, //d1g.fHalfSampleX, michael@0: fixedLoc.fY + SK_FixedHalf, //d1g.fHalfSampleY, michael@0: glyph); michael@0: } michael@0: pos += scalarsPerPosition; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: #if defined _WIN32 && _MSC_VER >= 1300 michael@0: #pragma warning ( pop ) michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkPathMeasure.h" michael@0: michael@0: static void morphpoints(SkPoint dst[], const SkPoint src[], int count, michael@0: SkPathMeasure& meas, const SkMatrix& matrix) { michael@0: SkMatrix::MapXYProc proc = matrix.getMapXYProc(); michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: SkPoint pos; michael@0: SkVector tangent; michael@0: michael@0: proc(matrix, src[i].fX, src[i].fY, &pos); michael@0: SkScalar sx = pos.fX; michael@0: SkScalar sy = pos.fY; michael@0: michael@0: if (!meas.getPosTan(sx, &pos, &tangent)) { michael@0: // set to 0 if the measure failed, so that we just set dst == pos michael@0: tangent.set(0, 0); michael@0: } michael@0: michael@0: /* This is the old way (that explains our approach but is way too slow michael@0: SkMatrix matrix; michael@0: SkPoint pt; michael@0: michael@0: pt.set(sx, sy); michael@0: matrix.setSinCos(tangent.fY, tangent.fX); michael@0: matrix.preTranslate(-sx, 0); michael@0: matrix.postTranslate(pos.fX, pos.fY); michael@0: matrix.mapPoints(&dst[i], &pt, 1); michael@0: */ michael@0: dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy), michael@0: pos.fY + SkScalarMul(tangent.fX, sy)); michael@0: } michael@0: } michael@0: michael@0: /* TODO michael@0: michael@0: Need differentially more subdivisions when the follow-path is curvy. Not sure how to michael@0: determine that, but we need it. I guess a cheap answer is let the caller tell us, michael@0: but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out. michael@0: */ michael@0: static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas, michael@0: const SkMatrix& matrix) { michael@0: SkPath::Iter iter(src, false); michael@0: SkPoint srcP[4], dstP[3]; michael@0: SkPath::Verb verb; michael@0: michael@0: while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) { michael@0: switch (verb) { michael@0: case SkPath::kMove_Verb: michael@0: morphpoints(dstP, srcP, 1, meas, matrix); michael@0: dst->moveTo(dstP[0]); michael@0: break; michael@0: case SkPath::kLine_Verb: michael@0: // turn lines into quads to look bendy michael@0: srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX); michael@0: srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY); michael@0: morphpoints(dstP, srcP, 2, meas, matrix); michael@0: dst->quadTo(dstP[0], dstP[1]); michael@0: break; michael@0: case SkPath::kQuad_Verb: michael@0: morphpoints(dstP, &srcP[1], 2, meas, matrix); michael@0: dst->quadTo(dstP[0], dstP[1]); michael@0: break; michael@0: case SkPath::kCubic_Verb: michael@0: morphpoints(dstP, &srcP[1], 3, meas, matrix); michael@0: dst->cubicTo(dstP[0], dstP[1], dstP[2]); michael@0: break; michael@0: case SkPath::kClose_Verb: michael@0: dst->close(); michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("unknown verb"); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkDraw::drawTextOnPath(const char text[], size_t byteLength, michael@0: const SkPath& follow, const SkMatrix* matrix, michael@0: const SkPaint& paint) const { michael@0: SkASSERT(byteLength == 0 || text != NULL); michael@0: michael@0: // nothing to draw michael@0: if (text == NULL || byteLength == 0 || fRC->isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: SkTextToPathIter iter(text, byteLength, paint, true); michael@0: SkPathMeasure meas(follow, false); michael@0: SkScalar hOffset = 0; michael@0: michael@0: // need to measure first michael@0: if (paint.getTextAlign() != SkPaint::kLeft_Align) { michael@0: SkScalar pathLen = meas.getLength(); michael@0: if (paint.getTextAlign() == SkPaint::kCenter_Align) { michael@0: pathLen = SkScalarHalf(pathLen); michael@0: } michael@0: hOffset += pathLen; michael@0: } michael@0: michael@0: const SkPath* iterPath; michael@0: SkScalar xpos; michael@0: SkMatrix scaledMatrix; michael@0: SkScalar scale = iter.getPathScale(); michael@0: michael@0: scaledMatrix.setScale(scale, scale); michael@0: michael@0: while (iter.next(&iterPath, &xpos)) { michael@0: if (iterPath) { michael@0: SkPath tmp; michael@0: SkMatrix m(scaledMatrix); michael@0: michael@0: m.postTranslate(xpos + hOffset, 0); michael@0: if (matrix) { michael@0: m.postConcat(*matrix); michael@0: } michael@0: morphpath(&tmp, *iterPath, meas, m); michael@0: if (fDevice) { michael@0: fDevice->drawPath(*this, tmp, iter.getPaint(), NULL, true); michael@0: } else { michael@0: this->drawPath(tmp, iter.getPaint(), NULL, true); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: struct VertState { michael@0: int f0, f1, f2; michael@0: michael@0: VertState(int vCount, const uint16_t indices[], int indexCount) michael@0: : fIndices(indices) { michael@0: fCurrIndex = 0; michael@0: if (indices) { michael@0: fCount = indexCount; michael@0: } else { michael@0: fCount = vCount; michael@0: } michael@0: } michael@0: michael@0: typedef bool (*Proc)(VertState*); michael@0: Proc chooseProc(SkCanvas::VertexMode mode); michael@0: michael@0: private: michael@0: int fCount; michael@0: int fCurrIndex; michael@0: const uint16_t* fIndices; michael@0: michael@0: static bool Triangles(VertState*); michael@0: static bool TrianglesX(VertState*); michael@0: static bool TriangleStrip(VertState*); michael@0: static bool TriangleStripX(VertState*); michael@0: static bool TriangleFan(VertState*); michael@0: static bool TriangleFanX(VertState*); michael@0: }; michael@0: michael@0: bool VertState::Triangles(VertState* state) { michael@0: int index = state->fCurrIndex; michael@0: if (index + 3 > state->fCount) { michael@0: return false; michael@0: } michael@0: state->f0 = index + 0; michael@0: state->f1 = index + 1; michael@0: state->f2 = index + 2; michael@0: state->fCurrIndex = index + 3; michael@0: return true; michael@0: } michael@0: michael@0: bool VertState::TrianglesX(VertState* state) { michael@0: const uint16_t* indices = state->fIndices; michael@0: int index = state->fCurrIndex; michael@0: if (index + 3 > state->fCount) { michael@0: return false; michael@0: } michael@0: state->f0 = indices[index + 0]; michael@0: state->f1 = indices[index + 1]; michael@0: state->f2 = indices[index + 2]; michael@0: state->fCurrIndex = index + 3; michael@0: return true; michael@0: } michael@0: michael@0: bool VertState::TriangleStrip(VertState* state) { michael@0: int index = state->fCurrIndex; michael@0: if (index + 3 > state->fCount) { michael@0: return false; michael@0: } michael@0: state->f2 = index + 2; michael@0: if (index & 1) { michael@0: state->f0 = index + 1; michael@0: state->f1 = index + 0; michael@0: } else { michael@0: state->f0 = index + 0; michael@0: state->f1 = index + 1; michael@0: } michael@0: state->fCurrIndex = index + 1; michael@0: return true; michael@0: } michael@0: michael@0: bool VertState::TriangleStripX(VertState* state) { michael@0: const uint16_t* indices = state->fIndices; michael@0: int index = state->fCurrIndex; michael@0: if (index + 3 > state->fCount) { michael@0: return false; michael@0: } michael@0: state->f2 = indices[index + 2]; michael@0: if (index & 1) { michael@0: state->f0 = indices[index + 1]; michael@0: state->f1 = indices[index + 0]; michael@0: } else { michael@0: state->f0 = indices[index + 0]; michael@0: state->f1 = indices[index + 1]; michael@0: } michael@0: state->fCurrIndex = index + 1; michael@0: return true; michael@0: } michael@0: michael@0: bool VertState::TriangleFan(VertState* state) { michael@0: int index = state->fCurrIndex; michael@0: if (index + 3 > state->fCount) { michael@0: return false; michael@0: } michael@0: state->f0 = 0; michael@0: state->f1 = index + 1; michael@0: state->f2 = index + 2; michael@0: state->fCurrIndex = index + 1; michael@0: return true; michael@0: } michael@0: michael@0: bool VertState::TriangleFanX(VertState* state) { michael@0: const uint16_t* indices = state->fIndices; michael@0: int index = state->fCurrIndex; michael@0: if (index + 3 > state->fCount) { michael@0: return false; michael@0: } michael@0: state->f0 = indices[0]; michael@0: state->f1 = indices[index + 1]; michael@0: state->f2 = indices[index + 2]; michael@0: state->fCurrIndex = index + 1; michael@0: return true; michael@0: } michael@0: michael@0: VertState::Proc VertState::chooseProc(SkCanvas::VertexMode mode) { michael@0: switch (mode) { michael@0: case SkCanvas::kTriangles_VertexMode: michael@0: return fIndices ? TrianglesX : Triangles; michael@0: case SkCanvas::kTriangleStrip_VertexMode: michael@0: return fIndices ? TriangleStripX : TriangleStrip; michael@0: case SkCanvas::kTriangleFan_VertexMode: michael@0: return fIndices ? TriangleFanX : TriangleFan; michael@0: default: michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: typedef void (*HairProc)(const SkPoint&, const SkPoint&, const SkRasterClip&, michael@0: SkBlitter*); michael@0: michael@0: static HairProc ChooseHairProc(bool doAntiAlias) { michael@0: return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine; michael@0: } michael@0: michael@0: static bool texture_to_matrix(const VertState& state, const SkPoint verts[], michael@0: const SkPoint texs[], SkMatrix* matrix) { michael@0: SkPoint src[3], dst[3]; michael@0: michael@0: src[0] = texs[state.f0]; michael@0: src[1] = texs[state.f1]; michael@0: src[2] = texs[state.f2]; michael@0: dst[0] = verts[state.f0]; michael@0: dst[1] = verts[state.f1]; michael@0: dst[2] = verts[state.f2]; michael@0: return matrix->setPolyToPoly(src, dst, 3); michael@0: } michael@0: michael@0: class SkTriColorShader : public SkShader { michael@0: public: michael@0: SkTriColorShader() {} michael@0: michael@0: bool setup(const SkPoint pts[], const SkColor colors[], int, int, int); michael@0: michael@0: virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE; michael@0: michael@0: SK_TO_STRING_OVERRIDE() michael@0: SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTriColorShader) michael@0: michael@0: protected: michael@0: SkTriColorShader(SkReadBuffer& buffer) : SkShader(buffer) {} michael@0: michael@0: private: michael@0: SkMatrix fDstToUnit; michael@0: SkPMColor fColors[3]; michael@0: michael@0: typedef SkShader INHERITED; michael@0: }; michael@0: michael@0: bool SkTriColorShader::setup(const SkPoint pts[], const SkColor colors[], michael@0: int index0, int index1, int index2) { michael@0: michael@0: fColors[0] = SkPreMultiplyColor(colors[index0]); michael@0: fColors[1] = SkPreMultiplyColor(colors[index1]); michael@0: fColors[2] = SkPreMultiplyColor(colors[index2]); michael@0: michael@0: SkMatrix m, im; michael@0: m.reset(); michael@0: m.set(0, pts[index1].fX - pts[index0].fX); michael@0: m.set(1, pts[index2].fX - pts[index0].fX); michael@0: m.set(2, pts[index0].fX); michael@0: m.set(3, pts[index1].fY - pts[index0].fY); michael@0: m.set(4, pts[index2].fY - pts[index0].fY); michael@0: m.set(5, pts[index0].fY); michael@0: if (!m.invert(&im)) { michael@0: return false; michael@0: } michael@0: return fDstToUnit.setConcat(im, this->getTotalInverse()); michael@0: } michael@0: michael@0: #include "SkColorPriv.h" michael@0: #include "SkComposeShader.h" michael@0: michael@0: static int ScalarTo256(SkScalar v) { michael@0: int scale = SkScalarToFixed(v) >> 8; michael@0: if (scale < 0) { michael@0: scale = 0; michael@0: } michael@0: if (scale > 255) { michael@0: scale = 255; michael@0: } michael@0: return SkAlpha255To256(scale); michael@0: } michael@0: michael@0: void SkTriColorShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) { michael@0: SkPoint src; michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src); michael@0: x += 1; michael@0: michael@0: int scale1 = ScalarTo256(src.fX); michael@0: int scale2 = ScalarTo256(src.fY); michael@0: int scale0 = 256 - scale1 - scale2; michael@0: if (scale0 < 0) { michael@0: if (scale1 > scale2) { michael@0: scale2 = 256 - scale1; michael@0: } else { michael@0: scale1 = 256 - scale2; michael@0: } michael@0: scale0 = 0; michael@0: } michael@0: michael@0: dstC[i] = SkAlphaMulQ(fColors[0], scale0) + michael@0: SkAlphaMulQ(fColors[1], scale1) + michael@0: SkAlphaMulQ(fColors[2], scale2); michael@0: } michael@0: } michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkTriColorShader::toString(SkString* str) const { michael@0: str->append("SkTriColorShader: ("); michael@0: michael@0: this->INHERITED::toString(str); michael@0: michael@0: str->append(")"); michael@0: } michael@0: #endif michael@0: michael@0: void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count, michael@0: const SkPoint vertices[], const SkPoint textures[], michael@0: const SkColor colors[], SkXfermode* xmode, michael@0: const uint16_t indices[], int indexCount, michael@0: const SkPaint& paint) const { michael@0: SkASSERT(0 == count || NULL != vertices); michael@0: michael@0: // abort early if there is nothing to draw michael@0: if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: // transform out vertices into device coordinates michael@0: SkAutoSTMalloc<16, SkPoint> storage(count); michael@0: SkPoint* devVerts = storage.get(); michael@0: fMatrix->mapPoints(devVerts, vertices, count); michael@0: michael@0: if (fBounder) { michael@0: SkRect bounds; michael@0: bounds.set(devVerts, count); michael@0: if (!fBounder->doRect(bounds, paint)) { michael@0: return; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: We can draw the vertices in 1 of 4 ways: michael@0: michael@0: - solid color (no shader/texture[], no colors[]) michael@0: - just colors (no shader/texture[], has colors[]) michael@0: - just texture (has shader/texture[], no colors[]) michael@0: - colors * texture (has shader/texture[], has colors[]) michael@0: michael@0: Thus for texture drawing, we need both texture[] and a shader. michael@0: */ michael@0: michael@0: SkTriColorShader triShader; // must be above declaration of p michael@0: SkPaint p(paint); michael@0: michael@0: SkShader* shader = p.getShader(); michael@0: if (NULL == shader) { michael@0: // if we have no shader, we ignore the texture coordinates michael@0: textures = NULL; michael@0: } else if (NULL == textures) { michael@0: // if we don't have texture coordinates, ignore the shader michael@0: p.setShader(NULL); michael@0: shader = NULL; michael@0: } michael@0: michael@0: // setup the custom shader (if needed) michael@0: if (NULL != colors) { michael@0: if (NULL == textures) { michael@0: // just colors (no texture) michael@0: shader = p.setShader(&triShader); michael@0: } else { michael@0: // colors * texture michael@0: SkASSERT(shader); michael@0: bool releaseMode = false; michael@0: if (NULL == xmode) { michael@0: xmode = SkXfermode::Create(SkXfermode::kModulate_Mode); michael@0: releaseMode = true; michael@0: } michael@0: SkShader* compose = SkNEW_ARGS(SkComposeShader, michael@0: (&triShader, shader, xmode)); michael@0: p.setShader(compose)->unref(); michael@0: if (releaseMode) { michael@0: xmode->unref(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p); michael@0: // important that we abort early, as below we may manipulate the shader michael@0: // and that is only valid if the shader returned true from setContext. michael@0: // If it returned false, then our blitter will be the NullBlitter. michael@0: if (blitter->isNullBlitter()) { michael@0: return; michael@0: } michael@0: michael@0: // setup our state and function pointer for iterating triangles michael@0: VertState state(count, indices, indexCount); michael@0: VertState::Proc vertProc = state.chooseProc(vmode); michael@0: michael@0: if (NULL != textures || NULL != colors) { michael@0: SkMatrix tempM; michael@0: SkMatrix savedLocalM; michael@0: if (shader) { michael@0: savedLocalM = shader->getLocalMatrix(); michael@0: } michael@0: michael@0: // setContext has already been called and verified to return true michael@0: // by the constructor of SkAutoBlitterChoose michael@0: bool prevContextSuccess = true; michael@0: while (vertProc(&state)) { michael@0: if (NULL != textures) { michael@0: if (texture_to_matrix(state, vertices, textures, &tempM)) { michael@0: tempM.postConcat(savedLocalM); michael@0: shader->setLocalMatrix(tempM); michael@0: // Need to recall setContext since we changed the local matrix. michael@0: // However, we also need to balance the calls this with a michael@0: // call to endContext which requires tracking the result of michael@0: // the previous call to setContext. michael@0: if (prevContextSuccess) { michael@0: shader->endContext(); michael@0: } michael@0: prevContextSuccess = shader->setContext(*fBitmap, p, *fMatrix); michael@0: if (!prevContextSuccess) { michael@0: continue; michael@0: } michael@0: } michael@0: } michael@0: if (NULL != colors) { michael@0: if (!triShader.setup(vertices, colors, michael@0: state.f0, state.f1, state.f2)) { michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: SkPoint tmp[] = { michael@0: devVerts[state.f0], devVerts[state.f1], devVerts[state.f2] michael@0: }; michael@0: SkScan::FillTriangle(tmp, *fRC, blitter.get()); michael@0: } michael@0: michael@0: // now restore the shader's original local matrix michael@0: if (NULL != shader) { michael@0: shader->setLocalMatrix(savedLocalM); michael@0: } michael@0: michael@0: // If the final call to setContext fails we must make it suceed so that the michael@0: // call to endContext in the destructor for SkAutoBlitterChoose is balanced. michael@0: if (!prevContextSuccess) { michael@0: prevContextSuccess = shader->setContext(*fBitmap, paint, SkMatrix::I()); michael@0: SkASSERT(prevContextSuccess); michael@0: } michael@0: } else { michael@0: // no colors[] and no texture michael@0: HairProc hairProc = ChooseHairProc(paint.isAntiAlias()); michael@0: const SkRasterClip& clip = *fRC; michael@0: while (vertProc(&state)) { michael@0: hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get()); michael@0: hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get()); michael@0: hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SK_DEBUG michael@0: michael@0: void SkDraw::validate() const { michael@0: SkASSERT(fBitmap != NULL); michael@0: SkASSERT(fMatrix != NULL); michael@0: SkASSERT(fClip != NULL); michael@0: SkASSERT(fRC != NULL); michael@0: michael@0: const SkIRect& cr = fRC->getBounds(); michael@0: SkIRect br; michael@0: michael@0: br.set(0, 0, fBitmap->width(), fBitmap->height()); michael@0: SkASSERT(cr.isEmpty() || br.contains(cr)); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkBounder::SkBounder() { michael@0: // initialize up front. This gets reset by SkCanvas before each draw call. michael@0: fClip = &SkRegion::GetEmptyRegion(); michael@0: } michael@0: michael@0: bool SkBounder::doIRect(const SkIRect& r) { michael@0: SkIRect rr; michael@0: return rr.intersect(fClip->getBounds(), r) && this->onIRect(rr); michael@0: } michael@0: michael@0: // TODO: change the prototype to take fixed, and update the callers michael@0: bool SkBounder::doIRectGlyph(const SkIRect& r, int x, int y, michael@0: const SkGlyph& glyph) { michael@0: SkIRect rr; michael@0: if (!rr.intersect(fClip->getBounds(), r)) { michael@0: return false; michael@0: } michael@0: GlyphRec rec; michael@0: rec.fLSB.set(SkIntToFixed(x), SkIntToFixed(y)); michael@0: rec.fRSB.set(rec.fLSB.fX + glyph.fAdvanceX, michael@0: rec.fLSB.fY + glyph.fAdvanceY); michael@0: rec.fGlyphID = glyph.getGlyphID(); michael@0: rec.fFlags = 0; michael@0: return this->onIRectGlyph(rr, rec); michael@0: } michael@0: michael@0: bool SkBounder::doHairline(const SkPoint& pt0, const SkPoint& pt1, michael@0: const SkPaint& paint) { michael@0: SkIRect r; michael@0: SkScalar v0, v1; michael@0: michael@0: v0 = pt0.fX; michael@0: v1 = pt1.fX; michael@0: if (v0 > v1) { michael@0: SkTSwap(v0, v1); michael@0: } michael@0: r.fLeft = SkScalarFloorToInt(v0); michael@0: r.fRight = SkScalarCeilToInt(v1); michael@0: michael@0: v0 = pt0.fY; michael@0: v1 = pt1.fY; michael@0: if (v0 > v1) { michael@0: SkTSwap(v0, v1); michael@0: } michael@0: r.fTop = SkScalarFloorToInt(v0); michael@0: r.fBottom = SkScalarCeilToInt(v1); michael@0: michael@0: if (paint.isAntiAlias()) { michael@0: r.inset(-1, -1); michael@0: } michael@0: return this->doIRect(r); michael@0: } michael@0: michael@0: bool SkBounder::doRect(const SkRect& rect, const SkPaint& paint) { michael@0: SkIRect r; michael@0: michael@0: if (paint.getStyle() == SkPaint::kFill_Style) { michael@0: rect.round(&r); michael@0: } else { michael@0: int rad = -1; michael@0: rect.roundOut(&r); michael@0: if (paint.isAntiAlias()) { michael@0: rad = -2; michael@0: } michael@0: r.inset(rad, rad); michael@0: } michael@0: return this->doIRect(r); michael@0: } michael@0: michael@0: bool SkBounder::doPath(const SkPath& path, const SkPaint& paint, bool doFill) { michael@0: SkIRect r; michael@0: const SkRect& bounds = path.getBounds(); michael@0: michael@0: if (doFill) { michael@0: bounds.round(&r); michael@0: } else { // hairline michael@0: bounds.roundOut(&r); michael@0: } michael@0: michael@0: if (paint.isAntiAlias()) { michael@0: r.inset(-1, -1); michael@0: } michael@0: return this->doIRect(r); michael@0: } michael@0: michael@0: void SkBounder::commit() { michael@0: // override in subclass michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkPath.h" michael@0: #include "SkDraw.h" michael@0: #include "SkRegion.h" michael@0: #include "SkBlitter.h" michael@0: michael@0: static bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds, michael@0: const SkMaskFilter* filter, const SkMatrix* filterMatrix, michael@0: SkIRect* bounds) { michael@0: if (devPath.isEmpty()) { michael@0: return false; michael@0: } michael@0: michael@0: // init our bounds from the path michael@0: { michael@0: SkRect pathBounds = devPath.getBounds(); michael@0: pathBounds.inset(-SK_ScalarHalf, -SK_ScalarHalf); michael@0: pathBounds.roundOut(bounds); michael@0: } michael@0: michael@0: SkIPoint margin = SkIPoint::Make(0, 0); michael@0: if (filter) { michael@0: SkASSERT(filterMatrix); michael@0: michael@0: SkMask srcM, dstM; michael@0: michael@0: srcM.fBounds = *bounds; michael@0: srcM.fFormat = SkMask::kA8_Format; michael@0: srcM.fImage = NULL; michael@0: if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // (possibly) trim the bounds to reflect the clip michael@0: // (plus whatever slop the filter needs) michael@0: if (clipBounds) { michael@0: SkIRect tmp = *clipBounds; michael@0: // Ugh. Guard against gigantic margins from wacky filters. Without this michael@0: // check we can request arbitrary amounts of slop beyond our visible michael@0: // clip, and bring down the renderer (at least on finite RAM machines michael@0: // like handsets, etc.). Need to balance this invented value between michael@0: // quality of large filters like blurs, and the corresponding memory michael@0: // requests. michael@0: static const int MAX_MARGIN = 128; michael@0: tmp.inset(-SkMin32(margin.fX, MAX_MARGIN), michael@0: -SkMin32(margin.fY, MAX_MARGIN)); michael@0: if (!bounds->intersect(tmp)) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static void draw_into_mask(const SkMask& mask, const SkPath& devPath, michael@0: SkPaint::Style style) { michael@0: SkBitmap bm; michael@0: SkDraw draw; michael@0: SkRasterClip clip; michael@0: SkMatrix matrix; michael@0: SkPaint paint; michael@0: michael@0: bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), mask.fBounds.height(), mask.fRowBytes); michael@0: bm.setPixels(mask.fImage); michael@0: michael@0: clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height())); michael@0: matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft), michael@0: -SkIntToScalar(mask.fBounds.fTop)); michael@0: michael@0: draw.fBitmap = &bm; michael@0: draw.fRC = &clip; michael@0: draw.fClip = &clip.bwRgn(); michael@0: draw.fMatrix = &matrix; michael@0: draw.fBounder = NULL; michael@0: paint.setAntiAlias(true); michael@0: paint.setStyle(style); michael@0: draw.drawPath(devPath, paint); michael@0: } michael@0: michael@0: bool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds, michael@0: const SkMaskFilter* filter, const SkMatrix* filterMatrix, michael@0: SkMask* mask, SkMask::CreateMode mode, michael@0: SkPaint::Style style) { michael@0: if (SkMask::kJustRenderImage_CreateMode != mode) { michael@0: if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds)) michael@0: return false; michael@0: } michael@0: michael@0: if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) { michael@0: mask->fFormat = SkMask::kA8_Format; michael@0: mask->fRowBytes = mask->fBounds.width(); michael@0: size_t size = mask->computeImageSize(); michael@0: if (0 == size) { michael@0: // we're too big to allocate the mask, abort michael@0: return false; michael@0: } michael@0: mask->fImage = SkMask::AllocImage(size); michael@0: memset(mask->fImage, 0, mask->computeImageSize()); michael@0: } michael@0: michael@0: if (SkMask::kJustComputeBounds_CreateMode != mode) { michael@0: draw_into_mask(*mask, devPath, style); michael@0: } michael@0: michael@0: return true; michael@0: }