SkDraw.cpp revision 7f5350346aa69447d59ef976011746fd3fe6904e
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDraw.h"
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBlitter.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBounder.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDevice.h"
152211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com#include "SkFixed.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkMaskFilter.h"
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPaint.h"
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPathEffect.h"
19045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com#include "SkRasterClip.h"
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRasterizer.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkScan.h"
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkShader.h"
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkStroke.h"
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTemplatesPriv.h"
2532e5d97ccf60f859db063ebd6e903c362e625767reed@google.com#include "SkTLazy.h"
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h"
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkAutoKern.h"
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBitmapProcShader.h"
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDrawProcs.h"
31ae57358447bac678e3fc458fa2857a349a6a7081reed@google.com#include "SkMatrixUtils.h"
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//#define TRACE_BITMAP_DRAWS
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define kBlitterStorageLongCount    (sizeof(SkBitmapProcShader) >> 2)
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
37fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com/** Helper for allocating small blitters on the stack.
38fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com */
3940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.comclass SkAutoBlitterChoose : SkNoncopyable {
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
411d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    SkAutoBlitterChoose() {
421d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com        fBlitter = NULL;
431d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose(const SkBitmap& device, const SkMatrix& matrix,
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        const SkPaint& paint) {
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fBlitter = SkBlitter::Choose(device, matrix, paint,
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                     fStorage, sizeof(fStorage));
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
49fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoBlitterChoose();
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*  operator->() { return fBlitter; }
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*  get() const { return fBlitter; }
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
551d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    void choose(const SkBitmap& device, const SkMatrix& matrix,
561d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com                const SkPaint& paint) {
571d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com        SkASSERT(!fBlitter);
581d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com        fBlitter = SkBlitter::Choose(device, matrix, paint,
591d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com                                     fStorage, sizeof(fStorage));
601d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
611d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*  fBlitter;
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t    fStorage[kBlitterStorageLongCount];
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkAutoBlitterChoose::~SkAutoBlitterChoose() {
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if ((void*)fBlitter == (void*)fStorage) {
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fBlitter->~SkBlitter();
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDELETE(fBlitter);
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com/**
7640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com *  Since we are providing the storage for the shader (to avoid the perf cost
7740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com *  of calling new) we insist that in our destructor we can account for all
7840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com *  owners of the shader.
7940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com */
8040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.comclass SkAutoBitmapShaderInstall : SkNoncopyable {
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
8240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkAutoBitmapShaderInstall(const SkBitmap& src, const SkPaint& paint)
8340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            : fPaint(paint) /* makes a copy of the paint */ {
8440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        fPaint.setShader(SkShader::CreateBitmapShader(src,
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           fStorage, sizeof(fStorage)));
8740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        // we deliberately left the shader with an owner-count of 2
8840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkASSERT(2 == fPaint.getShader()->getRefCnt());
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9082065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoBitmapShaderInstall() {
9240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkShader* shader = fPaint.getShader();
9340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        // since we manually destroy shader, we insist that owners == 2
9440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkASSERT(2 == shader->getRefCnt());
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        fPaint.setShader(NULL); // unref the shader by 1
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        // now destroy to take care of the 2nd owner-count
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if ((void*)shader == (void*)fStorage) {
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shader->~SkShader();
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkDELETE(shader);
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10582065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
10640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    // return the new paint that has the shader applied
10740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    const SkPaint& paintWithShader() const { return fPaint; }
10882065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
11040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkPaint     fPaint; // copy of caller's paint (which we then modify)
11140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    uint32_t    fStorage[kBlitterStorageLongCount];
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
116f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comSkDraw::SkDraw() {
117f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    sk_bzero(this, sizeof(*this));
118f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
119f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDraw::SkDraw(const SkDraw& src) {
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memcpy(this, &src, sizeof(*this));
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data);
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) {
1294516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com    sk_bzero(pixels, bytes);
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {}
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    sk_memset32((uint32_t*)pixels, data, bytes >> 2);
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    sk_memset16((uint16_t*)pixels, data, bytes >> 1);
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memset(pixels, data, bytes);
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic BitmapXferProc ChooseBitmapXferProc(const SkBitmap& bitmap,
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                           const SkPaint& paint,
1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                           uint32_t* data) {
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // todo: we can apply colorfilter up front if no shader, so we wouldn't
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need to abort this fastpath
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getShader() || paint.getColorFilter()) {
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return NULL;
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
155845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com    SkXfermode::Mode mode;
156be2aa2aa1f8bf73d974bdd9438fc741bbf0cfbe6mike@reedtribe.org    if (!SkXfermode::AsMode(paint.getXfermode(), &mode)) {
1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return NULL;
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
159a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkColor color = paint.getColor();
161a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // collaps modes based on color...
163845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com    if (SkXfermode::kSrcOver_Mode == mode) {
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        unsigned alpha = SkColorGetA(color);
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (0 == alpha) {
166845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com            mode = SkXfermode::kDst_Mode;
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else if (0xFF == alpha) {
168845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com            mode = SkXfermode::kSrc_Mode;
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
171a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (mode) {
173845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com        case SkXfermode::kClear_Mode:
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//            SkDebugf("--- D_Clear_BitmapXferProc\n");
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return D_Clear_BitmapXferProc;  // ignore data
176845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com        case SkXfermode::kDst_Mode:
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//            SkDebugf("--- D_Dst_BitmapXferProc\n");
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return D_Dst_BitmapXferProc;    // ignore data
179845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com        case SkXfermode::kSrc_Mode: {
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            /*
181a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com                should I worry about dithering for the lower depths?
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            */
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPMColor pmc = SkPreMultiplyColor(color);
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            switch (bitmap.config()) {
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kARGB_8888_Config:
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = pmc;
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- D32_Src_BitmapXferProc\n");
1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return D32_Src_BitmapXferProc;
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kARGB_4444_Config:
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = SkPixel32ToPixel4444(pmc);
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- D16_Src_BitmapXferProc\n");
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return D16_Src_BitmapXferProc;
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kRGB_565_Config:
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = SkPixel32ToPixel16(pmc);
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- D16_Src_BitmapXferProc\n");
2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return D16_Src_BitmapXferProc;
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kA8_Config:
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = SkGetPackedA32(pmc);
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- DA8_Src_BitmapXferProc\n");
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return DA8_Src_BitmapXferProc;
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                default:
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    break;
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return NULL;
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void CallBitmapXferProc(const SkBitmap& bitmap, const SkIRect& rect,
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                               BitmapXferProc proc, uint32_t procData) {
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int shiftPerPixel;
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (bitmap.config()) {
2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kARGB_8888_Config:
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shiftPerPixel = 2;
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kARGB_4444_Config:
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kRGB_565_Config:
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shiftPerPixel = 1;
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kA8_Config:
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shiftPerPixel = 0;
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
2350c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com            SkDEBUGFAIL("Can't use xferproc on this config");
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint8_t* pixels = (uint8_t*)bitmap.getPixels();
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(pixels);
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const size_t rowBytes = bitmap.rowBytes();
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const int widthBytes = rect.width() << shiftPerPixel;
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // skip down to the first scanline and X position
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel);
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int scans = rect.height() - 1; scans >= 0; --scans) {
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        proc(pixels, widthBytes, procData);
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pixels += rowBytes;
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawPaint(const SkPaint& paint) const {
253f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
255045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty()) {
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    devRect;
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    devRect.set(0, 0, fBitmap->width(), fBitmap->height());
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder && !fBounder->doIRect(devRect)) {
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
264a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
265045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isBW()) {
266045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        /*  If we don't have a shader (i.e. we're just a solid color) we may
267045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            be faster to operate directly on the device bitmap, rather than invoking
268045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            a blitter. Esp. true for xfermodes, which require a colorshader to be
269045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            present, which is just redundant work. Since we're drawing everywhere
270045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            in the clip, we don't have to worry about antialiasing.
271045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        */
272045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        uint32_t procData = 0;  // to avoid the warning
273045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        BitmapXferProc proc = ChooseBitmapXferProc(*fBitmap, paint, &procData);
274045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (proc) {
275045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            if (D_Dst_BitmapXferProc == proc) { // nothing to do
276045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                return;
277045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            }
278a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
279045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkRegion::Iterator iter(fRC->bwRgn());
280045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            while (!iter.done()) {
281045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                CallBitmapXferProc(*fBitmap, iter.rect(), proc, procData);
282045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                iter.next();
283045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            }
284045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            return;
2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
287045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
288045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    // normal case: use a blitter
289045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
290045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkScan::FillIRect(devRect, *fRC, blitter.get());
2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct PtProcRec {
2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkCanvas::PointMode fMode;
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPaint*  fPaint;
2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkRegion* fClip;
299045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkRasterClip* fRC;
300a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // computed values
3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed fRadius;
303a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count,
3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         SkBlitter*);
3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix,
308045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com              const SkRasterClip*);
309045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    Proc chooseProc(SkBlitter** blitter);
310045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
311045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comprivate:
312045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAAClipBlitterWrapper fWrapper;
3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                 int count, SkBlitter* blitter) {
3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(rec.fClip->isRect());
3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkIRect& r = rec.fClip->getBounds();
319a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3212d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int x = SkScalarFloorToInt(devPts[i].fX);
3222d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int y = SkScalarFloorToInt(devPts[i].fY);
3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (r.contains(x, y)) {
3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            blitter->blitH(x, y, 1);
3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_pt_rect_16_hair_proc(const PtProcRec& rec,
3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                    const SkPoint devPts[], int count,
3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                    SkBlitter* blitter) {
332045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkASSERT(rec.fRC->isRect());
333045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkIRect& r = rec.fRC->getBounds();
3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t value;
3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(bitmap);
337c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint16_t* addr = bitmap->getAddr16(0, 0);
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int rb = bitmap->rowBytes();
340c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3422d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int x = SkScalarFloorToInt(devPts[i].fX);
3432d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int y = SkScalarFloorToInt(devPts[i].fY);
3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (r.contains(x, y)) {
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value);
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3502d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.comstatic void bw_pt_rect_32_hair_proc(const PtProcRec& rec,
3512d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com                                    const SkPoint devPts[], int count,
3522d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com                                    SkBlitter* blitter) {
3532d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    SkASSERT(rec.fRC->isRect());
3542d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    const SkIRect& r = rec.fRC->getBounds();
3552d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    uint32_t value;
3562d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
3572d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    SkASSERT(bitmap);
358c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
3592d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    SkPMColor* addr = bitmap->getAddr32(0, 0);
3602d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    int rb = bitmap->rowBytes();
361c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
3622d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    for (int i = 0; i < count; i++) {
3632d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int x = SkScalarFloorToInt(devPts[i].fX);
3642d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int y = SkScalarFloorToInt(devPts[i].fY);
3652d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        if (r.contains(x, y)) {
3662d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com            ((SkPMColor*)((char*)addr + y * rb))[x] = value;
3672d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        }
3682d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    }
3692d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com}
3702d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com
3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            int count, SkBlitter* blitter) {
3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int x = SkScalarFloor(devPts[i].fX);
3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int y = SkScalarFloor(devPts[i].fY);
3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (rec.fClip->contains(x, y)) {
3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            blitter->blitH(x, y, 1);
3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i += 2) {
385045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count - 1; i++) {
392045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// aa versions
3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i += 2) {
401045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
4068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count - 1; i++) {
408045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// square procs (strokeWidth > 0 but matrix is square-scale (sx == sy)
4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[],
4158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           int count, SkBlitter* blitter) {
4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkFixed radius = rec.fRadius;
4178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
4188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed x = SkScalarToFixed(devPts[i].fX);
4198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed y = SkScalarToFixed(devPts[i].fY);
420c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkXRect r;
4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fLeft = x - radius;
4238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fTop = y - radius;
4248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fRight = x + radius;
4258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fBottom = y + radius;
426c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
427045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::FillXRect(r, *rec.fRC, blitter);
4288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[],
4328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           int count, SkBlitter* blitter) {
4338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkFixed radius = rec.fRadius;
4348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
4358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed x = SkScalarToFixed(devPts[i].fX);
4368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed y = SkScalarToFixed(devPts[i].fY);
437a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
4388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkXRect r;
4398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fLeft = x - radius;
4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fTop = y - radius;
4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fRight = x + radius;
4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fBottom = y + radius;
443a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
444045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::AntiFillXRect(r, *rec.fRC, blitter);
4458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
448b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com// If this guy returns true, then chooseProc() must return a valid proc
4498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint,
450045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                     const SkMatrix* matrix, const SkRasterClip* rc) {
4518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getPathEffect()) {
4528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
4538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar width = paint.getStrokeWidth();
4558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (0 == width) {
4568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fMode = mode;
4578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPaint = &paint;
458045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = NULL;
459045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fRC = rc;
4602d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        fRadius = SK_FixedHalf;
4618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return true;
4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
463b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com    if (paint.getStrokeCap() != SkPaint::kRound_Cap &&
464b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com            matrix->rectStaysRect() && SkCanvas::kPoints_PointMode == mode) {
4658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sx = matrix->get(SkMatrix::kMScaleX);
4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sy = matrix->get(SkMatrix::kMScaleY);
4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (SkScalarNearlyZero(sx - sy)) {
4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (sx < 0) {
4698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                sx = -sx;
4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
4718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fMode = mode;
4738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fPaint = &paint;
474045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            fClip = NULL;
475045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            fRC = rc;
4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1;
4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return true;
4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return false;
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
483045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comPtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) {
484b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com    Proc proc = NULL;
485a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
486045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter* blitter = *blitterPtr;
487045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isBW()) {
488045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = &fRC->bwRgn();
489045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    } else {
490045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fWrapper.init(*fRC, blitter);
491045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = &fWrapper.getRgn();
492045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = fWrapper.getBlitter();
493045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        *blitterPtr = blitter;
494045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    }
495045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // for our arrays
4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(0 == SkCanvas::kPoints_PointMode);
4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == SkCanvas::kLines_PointMode);
4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(2 == SkCanvas::kPolygon_PointMode);
5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode);
5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5022d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    if (fPaint->isAntiAlias()) {
5032d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        if (0 == fPaint->getStrokeWidth()) {
5048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            static const Proc gAAProcs[] = {
5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc
5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            };
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc = gAAProcs[fMode];
5082d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) {
5092d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com            SkASSERT(SkCanvas::kPoints_PointMode == fMode);
5102d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com            proc = aa_square_proc;
5112d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        }
5122d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    } else {    // BW
5132d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        if (fRadius <= SK_FixedHalf) {    // small radii and hairline
5148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) {
5158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                uint32_t value;
5168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const SkBitmap* bm = blitter->justAnOpaqueColor(&value);
5172d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com                if (bm && SkBitmap::kRGB_565_Config == bm->config()) {
5188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    proc = bw_pt_rect_16_hair_proc;
5192d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com                } else if (bm && SkBitmap::kARGB_8888_Config == bm->config()) {
5202d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com                    proc = bw_pt_rect_32_hair_proc;
5218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                } else {
5228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    proc = bw_pt_rect_hair_proc;
5238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
5248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } else {
5258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                static Proc gBWProcs[] = {
5268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc
5278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                };
5288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                proc = gBWProcs[fMode];
5298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
5308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
5318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc = bw_square_proc;
5328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
5338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return proc;
5358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool bounder_points(SkBounder* bounder, SkCanvas::PointMode mode,
5388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           size_t count, const SkPoint pts[],
5398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           const SkPaint& paint, const SkMatrix& matrix) {
5408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect ibounds;
5418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect bounds;
5428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar inset = paint.getStrokeWidth();
5438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.set(pts, count);
5458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.inset(-inset, -inset);
5468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.mapRect(&bounds);
5478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.roundOut(&ibounds);
5498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return bounder->doIRect(ibounds);
5508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// each of these costs 8-bytes of stack space, so don't make it too large
5538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// must be even for lines/polygon to work
5548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define MAX_DEV_PTS     32
5558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count,
557f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        const SkPoint pts[], const SkPaint& paint,
558f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        bool forceUseDevice) const {
5598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // if we're in lines mode, force count to be even
5608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkCanvas::kLines_PointMode == mode) {
5618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        count &= ~(size_t)1;
5628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if ((long)count <= 0) {
5658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
5668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
567a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
5688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(pts != NULL);
569f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
570a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
5718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     // nothing to draw
572045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty()) {
5738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
5748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
576fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    if (fBounder) {
577fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        if (!bounder_points(fBounder, mode, count, pts, paint, *fMatrix)) {
578fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com            return;
579fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        }
580fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
581fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        // clear the bounder and call this again, so we don't invoke the bounder
582fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        // later if we happen to call ourselves for drawRect, drawPath, etc.
583fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        SkDraw noBounder(*this);
584fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        noBounder.fBounder = NULL;
585fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        noBounder.drawPoints(mode, count, pts, paint, forceUseDevice);
586fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        return;
587fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    }
588fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
5898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    PtProcRec rec;
590045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (!forceUseDevice && rec.init(mode, paint, fMatrix, fRC)) {
5918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
5928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint             devPts[MAX_DEV_PTS];
5948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkMatrix*     matrix = fMatrix;
5958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkBlitter*          bltr = blitter.get();
596045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        PtProcRec::Proc     proc = rec.chooseProc(&bltr);
5978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // we have to back up subsequent passes if we're in polygon mode
5988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const size_t backup = (SkCanvas::kPolygon_PointMode == mode);
599a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
6008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        do {
6018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            size_t n = count;
6028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (n > MAX_DEV_PTS) {
6038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                n = MAX_DEV_PTS;
6048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
6058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix->mapPoints(devPts, pts, n);
6068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc(rec, devPts, n, bltr);
6078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pts += n - backup;
6088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(count >= n);
6098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            count -= n;
6108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (count > 0) {
6118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                count += backup;
6128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
6138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } while (count != 0);
6148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
6158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        switch (mode) {
6168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kPoints_PointMode: {
6178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // temporarily mark the paint as filling.
61840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                SkPaint newPaint(paint);
61940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                newPaint.setStyle(SkPaint::kFill_Style);
6208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
62140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                SkScalar width = newPaint.getStrokeWidth();
6228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkScalar radius = SkScalarHalf(width);
623a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
62440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) {
6258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkPath      path;
6268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkMatrix    preMatrix;
627a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
6288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.addCircle(0, 0, radius);
6298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    for (size_t i = 0; i < count; i++) {
6308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        preMatrix.setTranslate(pts[i].fX, pts[i].fY);
6318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        // pass true for the last point, since we can modify
6328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        // then path then
633f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        if (fDevice) {
63440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            fDevice->drawPath(*this, path, newPaint, &preMatrix,
635f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                              (count-1) == i);
636f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        } else {
63740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            this->drawPath(path, newPaint, &preMatrix,
63840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                                           (count-1) == i);
639f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        }
6408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
6418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                } else {
6428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkRect  r;
643a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
6448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    for (size_t i = 0; i < count; i++) {
6458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fLeft = pts[i].fX - radius;
6468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fTop = pts[i].fY - radius;
6478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fRight = r.fLeft + width;
6488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fBottom = r.fTop + width;
649f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        if (fDevice) {
65040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            fDevice->drawRect(*this, r, newPaint);
651f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        } else {
65240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            this->drawRect(r, newPaint);
653f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        }
6548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
6558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
6568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
6578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
6588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kLines_PointMode:
659629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com#ifndef SK_DISABLE_DASHING_OPTIMIZATION
660629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                if (2 == count && NULL != paint.getPathEffect()) {
661629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    // most likely a dashed line - see if it is one of the ones
662629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    // we can accelerate
663629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    SkStrokeRec rec(paint);
6646d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                    SkPathEffect::PointData pointData;
665629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com
666629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    SkPath path;
667629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    path.moveTo(pts[0]);
668629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    path.lineTo(pts[1]);
669629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com
6706d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                    if (paint.getPathEffect()->asPoints(&pointData, path, rec, *fMatrix)) {
6716d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                        // 'asPoints' managed to find some fast path
6726d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
673629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                        SkPaint newP(paint);
674629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                        newP.setPathEffect(NULL);
675935ad026826fb7d31d562ff7326b84ec3a827456robertphillips@google.com                        newP.setStyle(SkPaint::kFill_Style);
676629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com
6776d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                        if (!pointData.fFirst.isEmpty()) {
6786d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            if (fDevice) {
6796d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                fDevice->drawPath(*this, pointData.fFirst, newP);
6806d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            } else {
6816d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                this->drawPath(pointData.fFirst, newP);
6826d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            }
683629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                        }
6846d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
6856d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                        if (!pointData.fLast.isEmpty()) {
6866d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            if (fDevice) {
6876d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                fDevice->drawPath(*this, pointData.fLast, newP);
6886d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            } else {
6896d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                this->drawPath(pointData.fLast, newP);
6906d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            }
6916d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                        }
6926d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
6936d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                        if (pointData.fSize.fX == pointData.fSize.fY) {
6946d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            // The rest of the dashed line can just be drawn as points
6956d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            SkASSERT(pointData.fSize.fX == SkScalarHalf(newP.getStrokeWidth()));
6966d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
6976d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            if (SkPathEffect::PointData::kCircles_PointFlag & pointData.fFlags) {
6986d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                newP.setStrokeCap(SkPaint::kRound_Cap);
6996d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            } else {
7006d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                newP.setStrokeCap(SkPaint::kButt_Cap);
7016d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            }
7026d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
7036d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            if (fDevice) {
7047a03d86a3d9adcb13432fbd82039725149487c97skia.committer@gmail.com                                fDevice->drawPoints(*this,
7056d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                    SkCanvas::kPoints_PointMode,
7066d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                    pointData.fNumPoints,
7076d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                    pointData.fPoints,
7086d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                    newP);
7096d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            } else {
7106d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                this->drawPoints(SkCanvas::kPoints_PointMode,
7116d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                 pointData.fNumPoints,
7126d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                 pointData.fPoints,
7136d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                 newP,
7146d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                 forceUseDevice);
7156d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            }
7166d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            break;
717935ad026826fb7d31d562ff7326b84ec3a827456robertphillips@google.com                        } else {
7186d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            // The rest of the dashed line must be drawn as rects
7197a03d86a3d9adcb13432fbd82039725149487c97skia.committer@gmail.com                            SkASSERT(!(SkPathEffect::PointData::kCircles_PointFlag &
7206d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                      pointData.fFlags));
7216d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
7226d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            SkRect r;
7236d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
7246d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            for (int i = 0; i < pointData.fNumPoints; ++i) {
7256d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                r.set(pointData.fPoints[i].fX - pointData.fSize.fX,
7266d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                      pointData.fPoints[i].fY - pointData.fSize.fY,
7276d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                      pointData.fPoints[i].fX + pointData.fSize.fX,
7286d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                      pointData.fPoints[i].fY + pointData.fSize.fY);
7296d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                if (fDevice) {
7306d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                    fDevice->drawRect(*this, r, newP);
7316d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                } else {
7326d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                    this->drawRect(r, newP);
7336d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                }
7346d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            }
735935ad026826fb7d31d562ff7326b84ec3a827456robertphillips@google.com                        }
7366d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
737629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                        break;
738629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    }
739629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                }
740629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com#endif // DISABLE_DASHING_OPTIMIZATION
741629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                // couldn't take fast path so fall through!
7428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kPolygon_PointMode: {
7438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                count -= 1;
7448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkPath path;
7458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkPaint p(paint);
7468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                p.setStyle(SkPaint::kStroke_Style);
7478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
7488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                for (size_t i = 0; i < count; i += inc) {
7498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.moveTo(pts[i]);
7508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.lineTo(pts[i+1]);
751f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    if (fDevice) {
752f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        fDevice->drawPath(*this, path, p, NULL, true);
753f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    } else {
754f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        this->drawPath(path, p, NULL, true);
755f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    }
7568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.rewind();
7578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
7588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
7598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
7608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
7618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic inline SkPoint* as_lefttop(SkRect* r) {
7658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (SkPoint*)(void*)r;
7668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic inline SkPoint* as_rightbottom(SkRect* r) {
7698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return ((SkPoint*)(void*)r) + 1;
7708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
772761fb62b0eb174783316d2a8b933fba896ca6355reed@google.comstatic bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix,
773761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                           SkPoint* strokeSize) {
774761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    if (SkPaint::kMiter_Join != paint.getStrokeJoin() ||
775761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com        paint.getStrokeMiter() < SK_ScalarSqrt2) {
776761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com        return false;
777761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    }
778fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
779761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    SkASSERT(matrix.rectStaysRect());
780761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() };
781761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    matrix.mapVectors(strokeSize, &pt, 1);
7826115338c59799b4ef09cda187f23867dea093f6ereed@google.com    strokeSize->fX = SkScalarAbs(strokeSize->fX);
7836115338c59799b4ef09cda187f23867dea093f6ereed@google.com    strokeSize->fY = SkScalarAbs(strokeSize->fY);
784761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    return true;
7857ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org}
7867ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org
78762ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.comSkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint,
78862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com                                         const SkMatrix& matrix,
78962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com                                         SkPoint* strokeSize) {
7907ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    RectType rtype;
7917ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    const SkScalar width = paint.getStrokeWidth();
79262ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    const bool zeroWidth = (0 == width);
7937ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    SkPaint::Style style = paint.getStyle();
794fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
7957ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) {
7967ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        style = SkPaint::kFill_Style;
7977ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    }
798fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
7998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getPathEffect() || paint.getMaskFilter() ||
80062ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        paint.getRasterizer() || !matrix.rectStaysRect() ||
8017ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        SkPaint::kStrokeAndFill_Style == style) {
80262ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        rtype = kPath_RectType;
80362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    } else if (SkPaint::kFill_Style == style) {
8047ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kFill_RectType;
8057ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    } else if (zeroWidth) {
8067ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kHair_RectType;
80762ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    } else if (easy_rect_join(paint, matrix, strokeSize)) {
8087ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kStroke_RectType;
8097ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    } else {
8107ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kPath_RectType;
8117ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    }
81262ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    return rtype;
81362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com}
81462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
8157324415759fe0c5a0902877b664aa942a89bd940reed@google.comstatic const SkPoint* rect_points(const SkRect& r) {
8167324415759fe0c5a0902877b664aa942a89bd940reed@google.com    return (const SkPoint*)(void*)&r;
8177324415759fe0c5a0902877b664aa942a89bd940reed@google.com}
8187324415759fe0c5a0902877b664aa942a89bd940reed@google.com
8197324415759fe0c5a0902877b664aa942a89bd940reed@google.comstatic SkPoint* rect_points(SkRect& r) {
8207324415759fe0c5a0902877b664aa942a89bd940reed@google.com    return (SkPoint*)(void*)&r;
82140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com}
82240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com
82362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.comvoid SkDraw::drawRect(const SkRect& rect, const SkPaint& paint) const {
82462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    SkDEBUGCODE(this->validate();)
8257ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org
82662ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    // nothing to draw
827045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty()) {
82862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        return;
82962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    }
83062ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
83162ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    SkPoint strokeSize;
83262ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize);
83362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
8347ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    if (kPath_RectType == rtype) {
8358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPath  tmp;
8368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tmp.addRect(rect);
8378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tmp.setFillType(SkPath::kWinding_FillType);
838187d5595901d1120d9425851e5afdd773f574502reed@android.com        this->drawPath(tmp, paint, NULL, true);
8398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
8408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix& matrix = *fMatrix;
8438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect          devRect;
8448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform rect into devRect
8467324415759fe0c5a0902877b664aa942a89bd940reed@google.com    matrix.mapPoints(rect_points(devRect), rect_points(rect), 2);
8477324415759fe0c5a0902877b664aa942a89bd940reed@google.com    devRect.sort();
8488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder && !fBounder->doRect(devRect, paint)) {
8508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
8518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // look for the quick exit, before we build a blitter
8547324415759fe0c5a0902877b664aa942a89bd940reed@google.com    if (true) {
8558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkIRect ir;
8568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        devRect.roundOut(&ir);
85755e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        if (paint.getStyle() != SkPaint::kFill_Style) {
85855e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            // extra space for hairlines
85955e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            ir.inset(-1, -1);
86055e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        }
861045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (fRC->quickReject(ir))
8628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
8638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose blitterStorage(*fBitmap, matrix, paint);
866045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkRasterClip& clip = *fRC;
8678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*          blitter = blitterStorage.get();
8688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
869b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
870b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // case we are also hairline (if we've gotten to here), which devolves to
871b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // effectively just kFill
8727ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    switch (rtype) {
8737ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kFill_RectType:
8747ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
8757ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::AntiFillRect(devRect, clip, blitter);
8767ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
8777ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::FillRect(devRect, clip, blitter);
8787ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
8797ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
8807ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kStroke_RectType:
8817ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
882761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                SkScan::AntiFrameRect(devRect, strokeSize, clip, blitter);
8837ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
884761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                SkScan::FrameRect(devRect, strokeSize, clip, blitter);
8857ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
8867ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
8877ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kHair_RectType:
8887ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
8897ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::AntiHairRect(devRect, clip, blitter);
8907ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
8917ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::HairRect(devRect, clip, blitter);
8927ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
8937ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
8947ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        default:
8950c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com            SkDEBUGFAIL("bad rtype");
8968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const {
9008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (srcM.fBounds.isEmpty()) {
9018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
9028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9040a60b3d32eae945688b69599f11679662657f751bungeman@google.com    const SkMask* mask = &srcM;
9058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9060a60b3d32eae945688b69599f11679662657f751bungeman@google.com    SkMask dstM;
9078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getMaskFilter() &&
9088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, NULL)) {
9098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask = &dstM;
910bf2ac7e52f84095368dc50600fd6e78cc96044e3bungeman@google.com    } else {
911bf2ac7e52f84095368dc50600fd6e78cc96044e3bungeman@google.com        dstM.fImage = NULL;
9128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
91302f55841854ae32f21a13417e9ee711463e488cfbungeman@google.com    SkAutoMaskFreeImage ami(dstM.fImage);
9148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder && !fBounder->doIRect(mask->fBounds)) {
9168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
9178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
919045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAutoBlitterChoose blitterChooser(*fBitmap, *fMatrix, paint);
920045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter* blitter = blitterChooser.get();
921045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
922045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAAClipBlitterWrapper wrapper;
923045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkRegion* clipRgn;
9248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
925045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isBW()) {
926045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        clipRgn = &fRC->bwRgn();
927045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    } else {
928045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        wrapper.init(*fRC, blitter);
929045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        clipRgn = &wrapper.getRgn();
930045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = wrapper.getBlitter();
931045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    }
932045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    blitter->blitMaskRegion(*mask, *clipRgn);
9338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
935ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.comstatic SkScalar fast_len(const SkVector& vec) {
936ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar x = SkScalarAbs(vec.fX);
937ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar y = SkScalarAbs(vec.fY);
938ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    if (x < y) {
939ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        SkTSwap(x, y);
940ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
941ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    return x + SkScalarHalf(y);
942ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com}
943ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com
944ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.comstatic bool xfermodeSupportsCoverageAsAlpha(SkXfermode* xfer) {
945ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    SkXfermode::Coeff dc;
946ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (!SkXfermode::AsCoeff(xfer, NULL, &dc)) {
947ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return false;
948ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
949fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
950ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    switch (dc) {
951ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        case SkXfermode::kOne_Coeff:
952ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        case SkXfermode::kISA_Coeff:
953ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        case SkXfermode::kISC_Coeff:
954ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com            return true;
955ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        default:
956ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com            return false;
957ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
958ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com}
959ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
960ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.combool SkDrawTreatAsHairline(const SkPaint& paint, const SkMatrix& matrix,
961dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                           SkScalar* coverage) {
962dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com    SkASSERT(coverage);
963ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (SkPaint::kStroke_Style != paint.getStyle()) {
964ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return false;
965ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
966ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    SkScalar strokeWidth = paint.getStrokeWidth();
967ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (0 == strokeWidth) {
968dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        *coverage = SK_Scalar1;
969ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return true;
970ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
971ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
972ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    // if we get here, we need to try to fake a thick-stroke with a modulated
973ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    // hairline
974ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
975ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (!paint.isAntiAlias()) {
976ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return false;
977ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
9788d430185e08d2067584837a76b7193b803fee7a0tomhudson@google.com    if (matrix.hasPerspective()) {
979ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        return false;
980ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
981ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
982ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkVector src[2], dst[2];
983ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    src[0].set(strokeWidth, 0);
984ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    src[1].set(0, strokeWidth);
985ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    matrix.mapVectors(dst, src, 2);
986ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar len0 = fast_len(dst[0]);
987ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar len1 = fast_len(dst[1]);
988652807bbc8c57e5fa9622126b51fd369f5c67935agl@chromium.org    if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) {
989dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        *coverage = SkScalarAve(len0, len1);
990ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        return true;
991ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
992ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    return false;
993ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com}
994ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com
99532e5d97ccf60f859db063ebd6e903c362e625767reed@google.comvoid SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint,
9968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      const SkMatrix* prePathMatrix, bool pathIsMutable) const {
997f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
9988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1000045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty()) {
10018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
10028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath*         pathPtr = (SkPath*)&origSrcPath;
10058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool            doFill = true;
10068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath          tmpPath;
10078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        tmpMatrix;
10088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix* matrix = fMatrix;
10098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (prePathMatrix) {
101132e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style ||
101232e5d97ccf60f859db063ebd6e903c362e625767reed@google.com                origPaint.getRasterizer()) {
10138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPath* result = pathPtr;
1014a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!pathIsMutable) {
10168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                result = &tmpPath;
10178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pathIsMutable = true;
10188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
10198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathPtr->transform(*prePathMatrix, result);
10208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathPtr = result;
10218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
10228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!tmpMatrix.setConcat(*matrix, *prePathMatrix)) {
10238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // overflow
10248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
10258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
10268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix = &tmpMatrix;
10278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
10288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // at this point we're done with prePathMatrix
10308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
1031a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10325dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1033a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1034ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    {
1035dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        SkScalar coverage;
1036dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        if (SkDrawTreatAsHairline(origPaint, *matrix, &coverage)) {
1037dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com            if (SK_Scalar1 == coverage) {
10385dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                paint.writable()->setStrokeWidth(0);
1039dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com            } else if (xfermodeSupportsCoverageAsAlpha(origPaint.getXfermode())) {
1040dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                U8CPU newAlpha;
1041dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com#if 0
1042dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                newAlpha = SkToU8(SkScalarRoundToInt(coverage *
1043dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                                                     origPaint.getAlpha()));
1044dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com#else
1045dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                // this is the old technique, which we preserve for now so
1046dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                // we don't change previous results (testing)
1047dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                // the new way seems fine, its just (a tiny bit) different
1048dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                int scale = (int)SkScalarMul(coverage, 256);
1049dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                newAlpha = origPaint.getAlpha() * scale >> 8;
1050dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com#endif
10515dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                SkPaint* writablePaint = paint.writable();
10525dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                writablePaint->setStrokeWidth(0);
10535dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                writablePaint->setAlpha(newAlpha);
1054dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com            }
10558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
10568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1057a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
105832e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) {
105932e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        doFill = paint->getFillPath(*pathPtr, &tmpPath);
10608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathPtr = &tmpPath;
10618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1062a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
106332e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (paint->getRasterizer()) {
10648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
106532e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->getRasterizer()->rasterize(*pathPtr, *matrix,
1066045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                            &fRC->getBounds(), paint->getMaskFilter(), &mask,
10678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
106832e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            this->drawDevMask(mask, *paint);
10698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkMask::FreeImage(mask.fImage);
10708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
10718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
10728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // avoid possibly allocating a new path in transform if we can
10758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
10768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform the path into device space
10788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pathPtr->transform(*matrix, devPathPtr);
10798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
108032e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, *paint);
10818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10822ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org    if (paint->getMaskFilter()) {
1083fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        SkPaint::Style style = doFill ? SkPaint::kFill_Style :
10842ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org            SkPaint::kStroke_Style;
10852ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org        if (paint->getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fRC,
10862ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                                               fBounder, blitter.get(),
10872ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                                               style)) {
10882ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org            return; // filterPath() called the blitter, so we're done
10892ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org        }
10908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
109232e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (fBounder && !fBounder->doPath(*devPathPtr, *paint, doFill)) {
10938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
10948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1096045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    void (*proc)(const SkPath&, const SkRasterClip&, SkBlitter*);
10978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (doFill) {
109832e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->isAntiAlias()) {
1099045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::AntiFillPath;
11008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1101045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::FillPath;
11028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
11038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // hairline
110432e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->isAntiAlias()) {
1105045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::AntiHairPath;
11068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1107045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::HairPath;
11088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
11098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1110045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    proc(*devPathPtr, *fRC, blitter.get());
11118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11130baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com/** For the purposes of drawing bitmaps, if a matrix is "almost" translate
11140baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    go ahead and treat it as if it were, so that subsequent code can go fast.
11150baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com */
11160baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.comstatic bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) {
1117070dcd8ad37a4c5b8a9015938409b27bb3ce3ab9reed@google.com#ifdef SK_IGNORE_TRANS_CLAMP_FIX
11180baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    SkMatrix::TypeMask mask = matrix.getType();
11190baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com
11200baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (mask & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
11210baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        return false;
11220baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    }
11230baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (mask & SkMatrix::kScale_Mask) {
11240baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        SkScalar sx = matrix[SkMatrix::kMScaleX];
11250baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        SkScalar sy = matrix[SkMatrix::kMScaleY];
11260baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int w = bitmap.width();
11270baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int h = bitmap.height();
11280baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int sw = SkScalarRound(SkScalarMul(sx, SkIntToScalar(w)));
11290baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int sh = SkScalarRound(SkScalarMul(sy, SkIntToScalar(h)));
11300baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        return sw == w && sh == h;
11310baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    }
11320baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    // if we got here, we're either kTranslate_Mask or identity
11330baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    return true;
1134ae57358447bac678e3fc458fa2857a349a6a7081reed@google.com#else
1135070dcd8ad37a4c5b8a9015938409b27bb3ce3ab9reed@google.com    unsigned bits = 0;  // TODO: find a way to allow the caller to tell us to
1136070dcd8ad37a4c5b8a9015938409b27bb3ce3ab9reed@google.com                        // respect filtering.
1137070dcd8ad37a4c5b8a9015938409b27bb3ce3ab9reed@google.com    return SkTreatAsSprite(matrix, bitmap.width(), bitmap.height(), bits);
1138ae57358447bac678e3fc458fa2857a349a6a7081reed@google.com#endif
11398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawBitmapAsMask(const SkBitmap& bitmap,
11428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint) const {
11438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(bitmap.getConfig() == SkBitmap::kA8_Config);
11448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1145a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    if (just_translate(*fMatrix, bitmap)) {
11468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int ix = SkScalarRound(fMatrix->getTranslateX());
11478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int iy = SkScalarRound(fMatrix->getTranslateY());
11488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1149a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com        SkAutoLockPixels alp(bitmap);
1150a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com        if (!bitmap.readyToDraw()) {
1151a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com            return;
1152a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com        }
1153a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com
11548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
11558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fBounds.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
11568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fFormat = SkMask::kA8_Format;
11578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fRowBytes = bitmap.rowBytes();
11588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fImage = bitmap.getAddr8(0, 0);
1159a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawDevMask(mask, paint);
11618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // need to xform the bitmap first
11628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect  r;
11638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
1164a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.set(0, 0,
11668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com              SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
11678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fMatrix->mapRect(&r);
11688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.round(&mask.fBounds);
1169a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // set the mask's bounds to the transformed bitmap-bounds,
11718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // clipped to the actual device
11728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
11738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkIRect    devBounds;
11748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            devBounds.set(0, 0, fBitmap->width(), fBitmap->height());
11758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // need intersect(l, t, r, b) on irect
11768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!mask.fBounds.intersect(devBounds)) {
11778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
11788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
11798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1180543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com
11818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fFormat = SkMask::kA8_Format;
11828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fRowBytes = SkAlign4(mask.fBounds.width());
1183543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        size_t size = mask.computeImageSize();
1184543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        if (0 == size) {
1185543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            // the mask is too big to allocated, draw nothing
1186543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            return;
1187543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        }
11888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // allocate (and clear) our temp buffer to hold the transformed bitmap
11908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAutoMalloc    storage(size);
11918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fImage = (uint8_t*)storage.get();
11928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        memset(mask.fImage, 0, size);
1193a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // now draw our bitmap(src) into mask(dst), transformed by the matrix
11958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
11968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkBitmap    device;
11978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            device.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(),
11988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             mask.fBounds.height(), mask.fRowBytes);
11998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            device.setPixels(mask.fImage);
1200a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkCanvas c(device);
12028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // need the unclipped top/left for the translate
12038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            c.translate(-SkIntToScalar(mask.fBounds.fLeft),
12048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        -SkIntToScalar(mask.fBounds.fTop));
12058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            c.concat(*fMatrix);
12063469c76c40790b409621fd7eff34f56240718549reed@android.com
12073469c76c40790b409621fd7eff34f56240718549reed@android.com            // We can't call drawBitmap, or we'll infinitely recurse. Instead
1208fb12c3e6ba84f95dc15fbaddc239dede0ba1d60ereed@android.com            // we manually build a shader and draw that into our new mask
12093469c76c40790b409621fd7eff34f56240718549reed@android.com            SkPaint tmpPaint;
12103469c76c40790b409621fd7eff34f56240718549reed@android.com            tmpPaint.setFlags(paint.getFlags());
121140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            SkAutoBitmapShaderInstall install(bitmap, tmpPaint);
12123469c76c40790b409621fd7eff34f56240718549reed@android.com            SkRect rr;
12133469c76c40790b409621fd7eff34f56240718549reed@android.com            rr.set(0, 0, SkIntToScalar(bitmap.width()),
12143469c76c40790b409621fd7eff34f56240718549reed@android.com                   SkIntToScalar(bitmap.height()));
121540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            c.drawRect(rr, install.paintWithShader());
12168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
12178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawDevMask(mask, paint);
12188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1221045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic bool clipped_out(const SkMatrix& m, const SkRasterClip& c,
12228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        const SkRect& srcR) {
12238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect  dstR;
12248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect devIR;
1225a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.mapRect(&dstR, srcR);
1227a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    dstR.roundOut(&devIR);
12288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return c.quickReject(devIR);
12298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1231045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip,
12328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        int width, int height) {
12338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect  r;
12348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
12358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return clipped_out(matrix, clip, r);
12368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1238045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic bool clipHandlesSprite(const SkRasterClip& clip, int x, int y,
1239045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                              const SkBitmap& bitmap) {
1240045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    return clip.isBW() ||
1241045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com           clip.quickContains(x, y, x + bitmap.width(), y + bitmap.height());
1242045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com}
1243045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
12448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
124540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                        const SkPaint& origPaint) const {
1246f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
12478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1249045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty() ||
12508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bitmap.width() == 0 || bitmap.height() == 0 ||
1251dcd0f3a980095b77ebe605b49777a4bd37ca7b0areed@google.com            bitmap.getConfig() == SkBitmap::kNo_Config) {
12528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
12538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1254a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1255a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#ifndef SK_ALLOW_OVER_32K_BITMAPS
12568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // run away on too-big bitmaps for now (exceed 16.16)
12578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (bitmap.width() > 32767 || bitmap.height() > 32767) {
12588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
12598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1260a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#endif
1261a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
126240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkPaint paint(origPaint);
126340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    paint.setStyle(SkPaint::kFill_Style);
1264a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix matrix;
12668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (!matrix.setConcat(*fMatrix, prematrix)) {
12678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
12688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1270045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) {
12718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
12728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1274218521e15706c4377b1be49d931c4d7c8d597445reed@android.com    if (fBounder && just_translate(matrix, bitmap)) {
1275218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        SkIRect ir;
1276218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        int32_t ix = SkScalarRound(matrix.getTranslateX());
1277218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        int32_t iy = SkScalarRound(matrix.getTranslateY());
1278218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1279218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        if (!fBounder->doIRect(ir)) {
1280218521e15706c4377b1be49d931c4d7c8d597445reed@android.com            return;
1281218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        }
1282218521e15706c4377b1be49d931c4d7c8d597445reed@android.com    }
1283218521e15706c4377b1be49d931c4d7c8d597445reed@android.com
12840baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (bitmap.getConfig() != SkBitmap::kA8_Config &&
12850baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com            just_translate(matrix, bitmap)) {
1286f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        //
1287f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        // It is safe to call lock pixels now, since we know the matrix is
1288f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        // (more or less) identity.
1289f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        //
1290f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        SkAutoLockPixels alp(bitmap);
1291f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        if (!bitmap.readyToDraw()) {
1292f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com            return;
1293f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        }
1294045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        int ix = SkScalarRound(matrix.getTranslateX());
1295045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        int iy = SkScalarRound(matrix.getTranslateY());
1296045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (clipHandlesSprite(*fRC, ix, iy, bitmap)) {
1297045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            uint32_t    storage[kBlitterStorageLongCount];
1298045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkBlitter*  blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1299045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                                                ix, iy, storage, sizeof(storage));
1300045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            if (blitter) {
1301045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                SkAutoTPlacementDelete<SkBlitter>   ad(blitter, storage);
1302045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1303045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                SkIRect    ir;
1304045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1305045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1306045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                SkScan::FillIRect(ir, *fRC, blitter);
1307045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                return;
13088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
13098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
13108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1311a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
13128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // now make a temp draw on the stack, and use it
13138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //
13148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDraw draw(*this);
13158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix = &matrix;
1316a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
13178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (bitmap.getConfig() == SkBitmap::kA8_Config) {
13188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        draw.drawBitmapAsMask(bitmap, paint);
13198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
132040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkAutoBitmapShaderInstall install(bitmap, paint);
13218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect  r;
13238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.set(0, 0, SkIntToScalar(bitmap.width()),
13248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com              SkIntToScalar(bitmap.height()));
1325a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com        // is this ok if paint has a rasterizer?
132640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        draw.drawRect(r, install.paintWithShader());
13278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y,
133140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                        const SkPaint& origPaint) const {
1332f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
1333a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
13348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1335045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty() ||
13368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bitmap.width() == 0 || bitmap.height() == 0 ||
1337dcd0f3a980095b77ebe605b49777a4bd37ca7b0areed@google.com            bitmap.getConfig() == SkBitmap::kNo_Config) {
13388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
13398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    bounds;
13428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.set(x, y, x + bitmap.width(), y + bitmap.height());
13438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1344045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->quickReject(bounds)) {
13458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return; // nothing to draw
13468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
134840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkPaint paint(origPaint);
134940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    paint.setStyle(SkPaint::kFill_Style);
13508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1351045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (NULL == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, bitmap)) {
13528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        uint32_t    storage[kBlitterStorageLongCount];
13538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkBlitter*  blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
13548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                                x, y, storage, sizeof(storage));
13558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (blitter) {
13578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkAutoTPlacementDelete<SkBlitter> ad(blitter, storage);
13588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (fBounder && !fBounder->doIRect(bounds)) {
13608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
13618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
13628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1363045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkScan::FillIRect(bounds, *fRC, blitter);
13648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
13658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
13668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
136840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkAutoBitmapShaderInstall install(bitmap, paint);
136940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    const SkPaint& shaderPaint = install.paintWithShader();
13708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        matrix;
13728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect          r;
13738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // get a scalar version of our rect
13758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.set(bounds);
13768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // tell the shader our offset
13788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setTranslate(r.fLeft, r.fTop);
137940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    shaderPaint.getShader()->setLocalMatrix(matrix);
1380a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
13818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDraw draw(*this);
13828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.reset();
13838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix = &matrix;
13848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // call ourself with a rect
1385a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    // is this OK if paint has a rasterizer?
138640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    draw.drawRect(r, shaderPaint);
13878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
13908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkScalerContext.h"
13928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGlyphCache.h"
1393e69137620ab0b5b40d230318c8e11b822f63cb9dreed@google.com#include "SkTextToPathIter.h"
13948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h"
13958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void measure_text(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
13978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const char text[], size_t byteLength, SkVector* stopVector) {
13988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed     x = 0, y = 0;
13998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* stop = text + byteLength;
14008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoKern  autokern;
1402a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
14038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (text < stop) {
14048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // don't need x, y here, since all subpixel variants will have the
14058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // same advance
14068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
14078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x += autokern.adjust(glyph) + glyph.fAdvanceX;
14098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y += glyph.fAdvanceY;
14108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
14128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(text == stop);
14148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawText_asPaths(const char text[], size_t byteLength,
14178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              SkScalar x, SkScalar y,
14188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint) const {
1419f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
14208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1421166e653f67f3fffc3846184a25ce45ab083f07a2djsollen@google.com    SkTextToPathIter iter(text, byteLength, paint, true);
14228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    matrix;
14248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setScale(iter.getPathScale(), iter.getPathScale());
14258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.postTranslate(x, y);
14268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPath* iterPath;
14288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar xpos, prevXPos = 0;
14298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14307b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com    while (iter.next(&iterPath, &xpos)) {
14318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        matrix.postTranslate(xpos - prevXPos, 0);
14327b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com        if (iterPath) {
14337b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            const SkPaint& pnt = iter.getPaint();
14347b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            if (fDevice) {
14357b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                fDevice->drawPath(*this, *iterPath, pnt, &matrix, false);
14367b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            } else {
14377b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                this->drawPath(*iterPath, pnt, &matrix, false);
14387b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            }
1439f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
14408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        prevXPos = xpos;
14418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// disable warning : local variable used without having been initialized
1445a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#if defined _WIN32 && _MSC_VER >= 1300
14468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( push )
14478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( disable : 4701 )
14488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
14498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
14518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_NoBounder_RectClip(const SkDraw1Glyph& state,
1453f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                   SkFixed fx, SkFixed fy,
1454f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                   const SkGlyph& glyph) {
1455f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1456f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
14578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
145883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT(NULL == state.fBounder);
145983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT((NULL == state.fClip && state.fAAClip) ||
146083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com             (state.fClip && NULL == state.fAAClip && state.fClip->isRect()));
14618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
14638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
14648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int right   = left + glyph.fWidth;
14668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int bottom  = top + glyph.fHeight;
14678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1468fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkMask        mask;
1469fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkIRect        storage;
1470fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkIRect*    bounds = &mask.fBounds;
147183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
147283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fBounds.set(left, top, right, bottom);
147383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
147483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    // this extra test is worth it, assuming that most of the time it succeeds
147583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    // since we can avoid writing to storage
147683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) {
147783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds))
147883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            return;
147983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        bounds = &storage;
148083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
148183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
148283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    uint8_t* aa = (uint8_t*)glyph.fImage;
148383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (NULL == aa) {
148483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        aa = (uint8_t*)state.fCache->findImage(glyph);
148583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (NULL == aa) {
148683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            return; // can't rasterize glyph
14878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
148883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
14898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
149083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fRowBytes = glyph.rowBytes();
149183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
149283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fImage = aa;
149383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    state.fBlitter->blitMask(mask, *bounds);
14948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_NoBounder_RgnClip(const SkDraw1Glyph& state,
1497f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                  SkFixed fx, SkFixed fy,
149883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                                  const SkGlyph& glyph) {
1499f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1500f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
15018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
150283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT(!state.fClip->isRect());
150383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT(NULL == state.fBounder);
15048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMask  mask;
15068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
15088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
15098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
151183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
151283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
151383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (!clipper.done()) {
151483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const SkIRect&  cr = clipper.rect();
151583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const uint8_t*  aa = (const uint8_t*)glyph.fImage;
151683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (NULL == aa) {
151783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            aa = (uint8_t*)state.fCache->findImage(glyph);
151883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            if (NULL == aa) {
1519fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com                return;
15208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
152183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        }
152283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
152383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        mask.fRowBytes = glyph.rowBytes();
152483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
152583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        mask.fImage = (uint8_t*)aa;
152683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        do {
152783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            state.fBlitter->blitMask(mask, cr);
152883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            clipper.next();
152983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        } while (!clipper.done());
153083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
15318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_Bounder(const SkDraw1Glyph& state,
1534f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        SkFixed fx, SkFixed fy,
153583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                        const SkGlyph& glyph) {
1536f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1537f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
15388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1539fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
15408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMask  mask;
1541fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
15428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
15438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
1544fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
15458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
15468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
1547fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
154883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (!clipper.done()) {
154983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const SkIRect&  cr = clipper.rect();
155083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const uint8_t*  aa = (const uint8_t*)glyph.fImage;
155183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (NULL == aa) {
155283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            aa = (uint8_t*)state.fCache->findImage(glyph);
155383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            if (NULL == aa) {
155483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                return;
15558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
1556fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        }
1557fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
1558d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        // we need to pass the origin, which we approximate with our
1559d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        // (unadjusted) left,top coordinates (the caller called fixedfloor)
156083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (state.fBounder->doIRectGlyph(cr,
1561d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                                         left - glyph.fLeft,
1562d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                                         top - glyph.fTop, glyph)) {
156383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            mask.fRowBytes = glyph.rowBytes();
156483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
156583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            mask.fImage = (uint8_t*)aa;
156683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            do {
156783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                state.fBlitter->blitMask(mask, cr);
156883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                clipper.next();
156983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            } while (!clipper.done());
157083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        }
157183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
15728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1574045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic void D1G_Bounder_AAClip(const SkDraw1Glyph& state,
1575045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                               SkFixed fx, SkFixed fy,
1576045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                               const SkGlyph& glyph) {
1577045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    int left = SkFixedFloor(fx);
1578045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    int top = SkFixedFloor(fy);
1579045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkIRect bounds;
1580045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    bounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
1581045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1582045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (state.fBounder->doIRectGlyph(bounds, left, top, glyph)) {
1583045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        D1G_NoBounder_RectClip(state, fx, fy, glyph);
1584045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    }
1585045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com}
1586045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1587fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.comstatic bool hasCustomD1GProc(const SkDraw& draw) {
1588fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    return draw.fProcs && draw.fProcs->fD1GProc;
1589fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com}
1590fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com
1591fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.comstatic bool needsRasterTextBlit(const SkDraw& draw) {
1592fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    return !hasCustomD1GProc(draw);
1593fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com}
1594fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com
15958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter,
15968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                      SkGlyphCache* cache) {
15978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDraw = draw;
15982211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    fBounder = draw->fBounder;
15992211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    fBlitter = blitter;
16002211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    fCache = cache;
16018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16021d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (hasCustomD1GProc(*draw)) {
1603045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        // todo: fix this assumption about clips w/ custom
1604045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = draw->fClip;
1605045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClipBounds = fClip->getBounds();
16068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return draw->fProcs->fD1GProc;
16078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1609045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (draw->fRC->isBW()) {
1610045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fAAClip = NULL;
1611045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = &draw->fRC->bwRgn();
1612045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClipBounds = fClip->getBounds();
1613045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (NULL == fBounder) {
1614045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            if (fClip->isRect()) {
1615045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                return D1G_NoBounder_RectClip;
1616045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            } else {
1617045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                return D1G_NoBounder_RgnClip;
1618045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            }
1619045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        } else {
1620045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            return D1G_Bounder;
1621045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        }
1622045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    } else {    // aaclip
1623045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fAAClip = &draw->fRC->aaRgn();
1624045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = NULL;
1625045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClipBounds = fAAClip->getBounds();
1626045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (NULL == fBounder) {
16278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return D1G_NoBounder_RectClip;
16288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1629045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            return D1G_Bounder_AAClip;
16308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
16318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
16338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
16358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawText(const char text[], size_t byteLength,
16378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      SkScalar x, SkScalar y, const SkPaint& paint) const {
16388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
16398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1640f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
16418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1643045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
16448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
16458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
164736d6edac9f3e63d9a5f499d0076550d08b80eacabsalomon@google.com    // SkScalarRec doesn't currently have a way of representing hairline stroke and
164836d6edac9f3e63d9a5f499d0076550d08b80eacabsalomon@google.com    // will fill if its frame-width is 0.
16498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (/*paint.isLinearText() ||*/
1650fc84359aa920567e72742877a1249f52d076ad35skia.committer@gmail.com        (fMatrix->hasPerspective()) ||
165136d6edac9f3e63d9a5f499d0076550d08b80eacabsalomon@google.com        (0 == paint.getStrokeWidth() && SkPaint::kStroke_Style == paint.getStyle())) {
16528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawText_asPaths(text, byteLength, x, y, paint);
16538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
16548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
16578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1658f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkMatrix* matrix = fMatrix;
1659f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1660f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkAutoGlyphCache    autoCache(paint, matrix);
16618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkGlyphCache*       cache = autoCache.getCache();
1662a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform our starting point
16648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
16658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint loc;
1666f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        matrix->mapXY(x, y, &loc);
16678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x = loc.fX;
16688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y = loc.fY;
16698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need to measure first
16728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getTextAlign() != SkPaint::kLeft_Align) {
16738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkVector    stop;
16748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        measure_text(cache, glyphCacheProc, text, byteLength, &stop);
16768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar    stopX = stop.fX;
16788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar    stopY = stop.fY;
16798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.getTextAlign() == SkPaint::kCenter_Align) {
16818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            stopX = SkScalarHalf(stopX);
16828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            stopY = SkScalarHalf(stopY);
16838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
16848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x -= stopX;
16858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y -= stopY;
16868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1687a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed fx = SkScalarToFixed(x);
16898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed fy = SkScalarToFixed(y);
16908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* stop = text + byteLength;
16918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1692f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkFixed fxMask = ~0;
1693f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkFixed fyMask = ~0;
16942211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    if (cache->isSubpixel()) {
1695cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*matrix);
1696cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        if (kX_SkAxisAlignment == baseline) {
1697f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fyMask = 0;
1698cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        } else if (kY_SkAxisAlignment == baseline) {
1699f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fxMask = 0;
17008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1701fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
17022211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    // apply bias here to avoid adding 1/2 the sampling frequency in the loop
17032211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fx += SK_FixedHalf >> SkGlyph::kSubBits;
17042211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fy += SK_FixedHalf >> SkGlyph::kSubBits;
17052211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    } else {
17062211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fx += SK_FixedHalf;
17072211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fy += SK_FixedHalf;
17088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
17098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1710045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAAClipBlitter     aaBlitter;
1711045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAutoBlitterChoose blitterChooser;
1712045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter*          blitter = NULL;
17131d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (needsRasterTextBlit(*this)) {
1714045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitterChooser.choose(*fBitmap, *matrix, paint);
1715045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = blitterChooser.get();
1716045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (fRC->isAA()) {
1717045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            aaBlitter.init(blitter, &fRC->aaRgn());
1718045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            blitter = &aaBlitter;
1719045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        }
17201d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
17211d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com
17228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoKern          autokern;
172352c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com    SkDraw1Glyph        d1g;
1724045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkDraw1Glyph::Proc  proc = d1g.init(this, blitter, cache);
17258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (text < stop) {
17272211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
17288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fx += autokern.adjust(glyph);
17308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (glyph.fWidth) {
173252c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com            proc(d1g, fx, fy, glyph);
17338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
17348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fx += glyph.fAdvanceX;
17358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fy += glyph.fAdvanceY;
17368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
17378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// last parameter is interpreted as SkFixed [x, y]
17408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// return the fixed position, which may be rounded or not by the caller
17418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//   e.g. subpixel doesn't round
17428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef void (*AlignProc)(const SkPoint&, const SkGlyph&, SkIPoint*);
17438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void leftAlignProc(const SkPoint& loc, const SkGlyph& glyph,
17458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          SkIPoint* dst) {
17468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX), SkScalarToFixed(loc.fY));
17478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void centerAlignProc(const SkPoint& loc, const SkGlyph& glyph,
17508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            SkIPoint* dst) {
17518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX) - (glyph.fAdvanceX >> 1),
17528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com             SkScalarToFixed(loc.fY) - (glyph.fAdvanceY >> 1));
17538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void rightAlignProc(const SkPoint& loc, const SkGlyph& glyph,
17568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkIPoint* dst) {
17578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX) - glyph.fAdvanceX,
17588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com             SkScalarToFixed(loc.fY) - glyph.fAdvanceY);
17598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic AlignProc pick_align_proc(SkPaint::Align align) {
17628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static const AlignProc gProcs[] = {
17638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        leftAlignProc, centerAlignProc, rightAlignProc
17648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
1765a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((unsigned)align < SK_ARRAY_COUNT(gProcs));
17678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return gProcs[align];
17698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass TextMapState {
17728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
17738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mutable SkPoint fLoc;
1774a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    TextMapState(const SkMatrix& matrix, SkScalar y)
17768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        : fMatrix(matrix), fProc(matrix.getMapXYProc()), fY(y) {}
17778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef void (*Proc)(const TextMapState&, const SkScalar pos[]);
1779a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Proc pickProc(int scalarsPerPosition);
1781a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
17838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix&     fMatrix;
17848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix::MapXYProc fProc;
17858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            fY; // ignored by MapXYProc
17868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // these are only used by Only... procs
17878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            fScaleX, fTransX, fTransformedY;
17888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapXProc(const TextMapState& state, const SkScalar pos[]) {
17908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fProc(state.fMatrix, *pos, state.fY, &state.fLoc);
17918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1792a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapXYProc(const TextMapState& state, const SkScalar pos[]) {
17948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fProc(state.fMatrix, pos[0], pos[1], &state.fLoc);
17958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1796a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapOnlyScaleXProc(const TextMapState& state,
17988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                  const SkScalar pos[]) {
17998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fLoc.set(SkScalarMul(state.fScaleX, *pos) + state.fTransX,
18008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                       state.fTransformedY);
18018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1802a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapOnlyTransXProc(const TextMapState& state,
18048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                  const SkScalar pos[]) {
18058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fLoc.set(*pos + state.fTransX, state.fTransformedY);
18068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
18088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comTextMapState::Proc TextMapState::pickProc(int scalarsPerPosition) {
18108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1811a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (1 == scalarsPerPosition) {
18138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        unsigned mtype = fMatrix.getType();
18148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (mtype & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
18158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return MapXProc;
18168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
18178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fScaleX = fMatrix.getScaleX();
18188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fTransX = fMatrix.getTranslateX();
18198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fTransformedY = SkScalarMul(fY, fMatrix.getScaleY()) +
18208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            fMatrix.getTranslateY();
18218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return (mtype & SkMatrix::kScale_Mask) ?
18228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        MapOnlyScaleXProc : MapOnlyTransXProc;
18238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
18248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
18258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return MapXYProc;
18268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
18288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
18308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawPosText(const char text[], size_t byteLength,
18328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         const SkScalar pos[], SkScalar constY,
18338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         int scalarsPerPosition, const SkPaint& paint) const {
18348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
18358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
18368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1837f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
18388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1840045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
18418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
18428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (/*paint.isLinearText() ||*/
18458d430185e08d2067584837a76b7193b803fee7a0tomhudson@google.com        (fMatrix->hasPerspective())) {
18468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // TODO !!!!
18478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//      this->drawText_asPaths(text, byteLength, x, y, paint);
18488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
18498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1851f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkMatrix* matrix = fMatrix;
1852a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawCacheProc     glyphCacheProc = paint.getDrawCacheProc();
1854f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkAutoGlyphCache    autoCache(paint, matrix);
18558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkGlyphCache*       cache = autoCache.getCache();
1856a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1857045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAAClipBlitterWrapper wrapper;
1858045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAutoBlitterChoose blitterChooser;
1859045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter* blitter = NULL;
18601d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (needsRasterTextBlit(*this)) {
1861045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitterChooser.choose(*fBitmap, *matrix, paint);
1862045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = blitterChooser.get();
1863045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (fRC->isAA()) {
1864045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            wrapper.init(*fRC, blitter);
1865045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            blitter = wrapper.getBlitter();
1866045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        }
18671d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
1868fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
18698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char*        stop = text + byteLength;
18708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    AlignProc          alignProc = pick_align_proc(paint.getTextAlign());
18712211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    SkDraw1Glyph       d1g;
18722211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache);
1873f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    TextMapState       tms(*matrix, constY);
18748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    TextMapState::Proc tmsProc = tms.pickProc(scalarsPerPosition);
18758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18762211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    if (cache->isSubpixel()) {
18778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // maybe we should skip the rounding if linearText is set
1878cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        SkAxisAlignment roundBaseline = SkComputeAxisAlignmentForHText(*matrix);
18798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (SkPaint::kLeft_Align == paint.getTextAlign()) {
18818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            while (text < stop) {
1882a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                tmsProc(tms, pos);
1884a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18852211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                SkFixed fx = SkScalarToFixed(tms.fLoc.fX) + (SK_FixedHalf >> SkGlyph::kSubBits);
18862211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                SkFixed fy = SkScalarToFixed(tms.fLoc.fY) + (SK_FixedHalf >> SkGlyph::kSubBits);
18877f5350346aa69447d59ef976011746fd3fe6904ebungeman@google.com
1888f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                SkFixed fxMask = ~0;
1889f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                SkFixed fyMask = ~0;
18908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1891cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                if (kX_SkAxisAlignment == roundBaseline) {
1892f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    fyMask = 0;
1893cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                } else if (kY_SkAxisAlignment == roundBaseline) {
1894f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    fxMask = 0;
18958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
1896a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1897f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                const SkGlyph& glyph = glyphCacheProc(cache, &text,
1898f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                                      fx & fxMask, fy & fyMask);
1899a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
19008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (glyph.fWidth) {
1901f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    proc(d1g, fx, fy, glyph);
19028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
19038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pos += scalarsPerPosition;
19048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
19058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
19068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            while (text < stop) {
19079330cfe851b14b8985acc14caa3dc5ad20b0b2a8bungeman@google.com                const char* currentText = text;
19088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const SkGlyph* glyph = &glyphCacheProc(cache, &text, 0, 0);
1909a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
19108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (glyph->fWidth) {
19118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkDEBUGCODE(SkFixed prevAdvX = glyph->fAdvanceX;)
19128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkDEBUGCODE(SkFixed prevAdvY = glyph->fAdvanceY;)
19138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkFixed fx, fy;
1915f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    SkFixed fxMask = ~0;
1916f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    SkFixed fyMask = ~0;
19178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    tmsProc(tms, pos);
1918a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
19198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    {
19208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkIPoint fixedLoc;
19218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        alignProc(tms.fLoc, *glyph, &fixedLoc);
19222211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                        fx = fixedLoc.fX + (SK_FixedHalf >> SkGlyph::kSubBits);
19232211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                        fy = fixedLoc.fY + (SK_FixedHalf >> SkGlyph::kSubBits);
19248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1925cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                        if (kX_SkAxisAlignment == roundBaseline) {
1926f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            fyMask = 0;
1927cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                        } else if (kY_SkAxisAlignment == roundBaseline) {
1928f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            fxMask = 0;
19298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        }
19308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
1931a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
19328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // have to call again, now that we've been "aligned"
19339330cfe851b14b8985acc14caa3dc5ad20b0b2a8bungeman@google.com                    glyph = &glyphCacheProc(cache, &currentText,
19349330cfe851b14b8985acc14caa3dc5ad20b0b2a8bungeman@google.com                                            fx & fxMask, fy & fyMask);
19358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // the assumption is that the advance hasn't changed
19368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkASSERT(prevAdvX == glyph->fAdvanceX);
19378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkASSERT(prevAdvY == glyph->fAdvanceY);
1938a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1939f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    proc(d1g, fx, fy, *glyph);
19408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
19418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pos += scalarsPerPosition;
19428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
19438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
19448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // not subpixel
1945aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com        if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1946aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com            while (text < stop) {
1947aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                // the last 2 parameters are ignored
1948aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1949fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
1950aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                if (glyph.fWidth) {
1951aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    tmsProc(tms, pos);
1952a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1953aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    proc(d1g,
1954aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         SkScalarToFixed(tms.fLoc.fX) + SK_FixedHalf,
1955aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         SkScalarToFixed(tms.fLoc.fY) + SK_FixedHalf,
1956aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         glyph);
1957aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                }
1958aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                pos += scalarsPerPosition;
1959aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com            }
1960aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com        } else {
1961aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com            while (text < stop) {
1962aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                // the last 2 parameters are ignored
1963aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1964a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1965aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                if (glyph.fWidth) {
1966aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    tmsProc(tms, pos);
1967a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1968aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    SkIPoint fixedLoc;
1969aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    alignProc(tms.fLoc, glyph, &fixedLoc);
1970aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com
1971aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    proc(d1g,
1972aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         fixedLoc.fX + SK_FixedHalf,
1973aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         fixedLoc.fY + SK_FixedHalf,
1974aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         glyph);
1975aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                }
1976aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                pos += scalarsPerPosition;
19778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
19788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
19798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
19808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
19818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if defined _WIN32 && _MSC_VER >= 1300
19838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( pop )
19848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
19858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
19878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPathMeasure.h"
19898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void morphpoints(SkPoint dst[], const SkPoint src[], int count,
19918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkPathMeasure& meas, const SkMatrix& matrix) {
19928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix::MapXYProc proc = matrix.getMapXYProc();
19938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
19958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint pos;
19968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkVector tangent;
19978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        proc(matrix, src[i].fX, src[i].fY, &pos);
19998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sx = pos.fX;
20008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sy = pos.fY;
20018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20028f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com        if (!meas.getPosTan(sx, &pos, &tangent)) {
20038f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com            // set to 0 if the measure failed, so that we just set dst == pos
20048f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com            tangent.set(0, 0);
20058f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com        }
20068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        /*  This is the old way (that explains our approach but is way too slow
20088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkMatrix    matrix;
20098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPoint     pt;
20108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pt.set(sx, sy);
20128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.setSinCos(tangent.fY, tangent.fX);
20138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.preTranslate(-sx, 0);
20148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.postTranslate(pos.fX, pos.fY);
20158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.mapPoints(&dst[i], &pt, 1);
20168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        */
20178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy),
20188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                   pos.fY + SkScalarMul(tangent.fX, sy));
20198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  TODO
20238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Need differentially more subdivisions when the follow-path is curvy. Not sure how to
20258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    determine that, but we need it. I guess a cheap answer is let the caller tell us,
20268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
20278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
20288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
20298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      const SkMatrix& matrix) {
20308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath::Iter    iter(src, false);
20318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint         srcP[4], dstP[3];
20328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath::Verb    verb;
20338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
20358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        switch (verb) {
20368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kMove_Verb:
20378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, srcP, 1, meas, matrix);
20388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->moveTo(dstP[0]);
20398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kLine_Verb:
20418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // turn lines into quads to look bendy
20428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX);
20438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY);
20448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, srcP, 2, meas, matrix);
20458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->quadTo(dstP[0], dstP[1]);
20468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kQuad_Verb:
20488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, &srcP[1], 2, meas, matrix);
20498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->quadTo(dstP[0], dstP[1]);
20508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kCubic_Verb:
20528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, &srcP[1], 3, meas, matrix);
20538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->cubicTo(dstP[0], dstP[1], dstP[2]);
20548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kClose_Verb:
20568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->close();
20578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            default:
20590c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com                SkDEBUGFAIL("unknown verb");
20608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
20628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawTextOnPath(const char text[], size_t byteLength,
20668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPath& follow, const SkMatrix* matrix,
20678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPaint& paint) const {
20688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
20698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
2071045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
20728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
20738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2075166e653f67f3fffc3846184a25ce45ab083f07a2djsollen@google.com    SkTextToPathIter    iter(text, byteLength, paint, true);
20768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPathMeasure       meas(follow, false);
20778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            hOffset = 0;
20788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need to measure first
20808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getTextAlign() != SkPaint::kLeft_Align) {
20818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar pathLen = meas.getLength();
20828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.getTextAlign() == SkPaint::kCenter_Align) {
20838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathLen = SkScalarHalf(pathLen);
20848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
20858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hOffset += pathLen;
20868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPath*   iterPath;
20898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar        xpos;
20908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        scaledMatrix;
20918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar        scale = iter.getPathScale();
20928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    scaledMatrix.setScale(scale, scale);
2094a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
20957b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com    while (iter.next(&iterPath, &xpos)) {
20967b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com        if (iterPath) {
20977b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            SkPath      tmp;
20987b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            SkMatrix    m(scaledMatrix);
20998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21007b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            m.postTranslate(xpos + hOffset, 0);
21017b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            if (matrix) {
21027b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                m.postConcat(*matrix);
21037b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            }
21047b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            morphpath(&tmp, *iterPath, meas, m);
21057b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            if (fDevice) {
21067b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                fDevice->drawPath(*this, tmp, iter.getPaint(), NULL, true);
21077b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            } else {
21087b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                this->drawPath(tmp, iter.getPaint(), NULL, true);
21097b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            }
2110f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
21118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
211456c69773aea56c6c6bd47bc7e7970dd081205184djsollen@google.com#ifdef SK_BUILD_FOR_ANDROID
2115cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.comvoid SkDraw::drawPosTextOnPath(const char text[], size_t byteLength,
2116cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                               const SkPoint pos[], const SkPaint& paint,
2117cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                               const SkPath& path, const SkMatrix* matrix) const {
2118cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // nothing to draw
2119045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
2120cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        return;
2121cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
2122cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2123cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkMatrix scaledMatrix;
2124cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkPathMeasure meas(path, false);
2125cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2126cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkMeasureCacheProc glyphCacheProc = paint.getMeasureCacheProc(
2127cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            SkPaint::kForward_TextBufferDirection, true);
2128cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2129cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Copied (modified) from SkTextToPathIter constructor to setup paint
2130cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkPaint tempPaint(paint);
2131cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2132cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    tempPaint.setLinearText(true);
2133cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    tempPaint.setMaskFilter(NULL); // don't want this affecting our path-cache lookup
2134cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2135cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    if (tempPaint.getPathEffect() == NULL && !(tempPaint.getStrokeWidth() > 0
2136cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            && tempPaint.getStyle() != SkPaint::kFill_Style)) {
2137cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        tempPaint.setStyle(SkPaint::kFill_Style);
2138cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        tempPaint.setPathEffect(NULL);
2139cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
2140cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // End copied from SkTextToPathIter constructor
2141cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2142cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // detach cache
2143cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkGlyphCache* cache = tempPaint.detachCache(NULL);
2144cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2145cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Must set scale, even if 1
2146cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkScalar scale = SK_Scalar1;
2147cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    scaledMatrix.setScale(scale, scale);
2148cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2149cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Loop over all glyph ids
2150cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    for (const char* stop = text + byteLength; text < stop; pos++) {
2151cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2152cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        const SkGlyph& glyph = glyphCacheProc(cache, &text);
2153cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        SkPath tmp;
2154cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2155cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        const SkPath* glyphPath = cache->findPath(glyph);
2156cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        if (glyphPath == NULL) {
2157cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            continue;
2158cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        }
2159cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2160cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        SkMatrix m(scaledMatrix);
2161cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        m.postTranslate(pos->fX, 0);
2162cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2163cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        if (matrix) {
2164cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            m.postConcat(*matrix);
2165cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        }
2166cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2167cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        morphpath(&tmp, *glyphPath, meas, m);
2168cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        this->drawPath(tmp, tempPaint);
2169cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2170cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
2171cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2172cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // re-attach cache
2173cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkGlyphCache::AttachCache(cache);
2174cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com}
2175cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com#endif
2176cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
21778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
21788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct VertState {
21808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int f0, f1, f2;
21818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState(int vCount, const uint16_t indices[], int indexCount)
21838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            : fIndices(indices) {
21848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCurrIndex = 0;
21858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (indices) {
21868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fCount = indexCount;
21878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
21888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fCount = vCount;
21898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
21908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2191a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
2192a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    typedef bool (*Proc)(VertState*);
21938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Proc chooseProc(SkCanvas::VertexMode mode);
21948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
21968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int             fCount;
21978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int             fCurrIndex;
21988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* fIndices;
2199a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool Triangles(VertState*);
22018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TrianglesX(VertState*);
22028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleStrip(VertState*);
22038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleStripX(VertState*);
22048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleFan(VertState*);
22058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleFanX(VertState*);
22068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
22078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::Triangles(VertState* state) {
22098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = index + 0;
22148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = index + 1;
22158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
22168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 3;
22178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TrianglesX(VertState* state) {
22218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
22228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = indices[index + 0];
22278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = indices[index + 1];
22288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
22298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 3;
22308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleStrip(VertState* state) {
22348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
22398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index & 1) {
22408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = index + 1;
22418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = index + 0;
22428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
22438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = index + 0;
22448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = index + 1;
22458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
22478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleStripX(VertState* state) {
22518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
22528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
22578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index & 1) {
22588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = indices[index + 1];
22598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = indices[index + 0];
22608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
22618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = indices[index + 0];
22628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = indices[index + 1];
22638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
22658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleFan(VertState* state) {
22698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = 0;
22748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = index + 1;
22758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
22768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
22778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleFanX(VertState* state) {
22818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
22828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = indices[0];
22878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = indices[index + 1];
22888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
22898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
22908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comVertState::Proc VertState::chooseProc(SkCanvas::VertexMode mode) {
22948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (mode) {
22958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangles_VertexMode:
22968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TrianglesX : Triangles;
22978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangleStrip_VertexMode:
22988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TriangleStripX : TriangleStrip;
22998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangleFan_VertexMode:
23008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TriangleFanX : TriangleFan;
23018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
23028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return NULL;
23038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2306045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comtypedef void (*HairProc)(const SkPoint&, const SkPoint&, const SkRasterClip&,
23078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         SkBlitter*);
23088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic HairProc ChooseHairProc(bool doAntiAlias) {
23108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
23118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool texture_to_matrix(const VertState& state, const SkPoint verts[],
23148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPoint texs[], SkMatrix* matrix) {
23158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint src[3], dst[3];
2316a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[0] = texs[state.f0];
23188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[1] = texs[state.f1];
23198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[2] = texs[state.f2];
23208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[0] = verts[state.f0];
23218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[1] = verts[state.f1];
23228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[2] = verts[state.f2];
23238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return matrix->setPolyToPoly(src, dst, 3);
23248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkTriColorShader : public SkShader {
23278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
23288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader() {}
23298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
2331a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
2332a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com    virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
2333a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
2334ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTriColorShader)
2335ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com
23368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
23378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader(SkFlattenableReadBuffer& buffer) : SkShader(buffer) {}
2338a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
23408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    fDstToUnit;
23418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPMColor   fColors[3];
2342a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkShader INHERITED;
23448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
23458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkTriColorShader::setup(const SkPoint pts[], const SkColor colors[],
23478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             int index0, int index1, int index2) {
2348a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[0] = SkPreMultiplyColor(colors[index0]);
23508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[1] = SkPreMultiplyColor(colors[index1]);
23518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[2] = SkPreMultiplyColor(colors[index2]);
2352a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix m, im;
23548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.reset();
23558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(0, pts[index1].fX - pts[index0].fX);
23568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(1, pts[index2].fX - pts[index0].fX);
23578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(2, pts[index0].fX);
23588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(3, pts[index1].fY - pts[index0].fY);
23598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(4, pts[index2].fY - pts[index0].fY);
23608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(5, pts[index0].fY);
23618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (!m.invert(&im)) {
23628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
23638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fDstToUnit.setConcat(im, this->getTotalInverse());
23658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
2368689411a87ca7d19abe04b5c9baff26bd96b2123ereed@android.com#include "SkComposeShader.h"
23698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic int ScalarTo256(SkScalar v) {
23718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int scale = SkScalarToFixed(v) >> 8;
23728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (scale < 0) {
23738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        scale = 0;
23748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (scale > 255) {
23768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        scale = 255;
23778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkAlpha255To256(scale);
23798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkTriColorShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
23828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint src;
2383a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
23858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
23868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x += 1;
2387a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale1 = ScalarTo256(src.fX);
23898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale2 = ScalarTo256(src.fY);
23908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale0 = 256 - scale1 - scale2;
23918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (scale0 < 0) {
23928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (scale1 > scale2) {
23938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                scale2 = 256 - scale1;
23948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } else {
23958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                scale1 = 256 - scale2;
23968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
23978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            scale0 = 0;
23988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2399a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        dstC[i] = SkAlphaMulQ(fColors[0], scale0) +
24018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAlphaMulQ(fColors[1], scale1) +
24028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAlphaMulQ(fColors[2], scale2);
24038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
24058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
24078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPoint vertices[], const SkPoint textures[],
24088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkColor colors[], SkXfermode* xmode,
24098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const uint16_t indices[], int indexCount,
24108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint& paint) const {
24118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(0 == count || NULL != vertices);
2412a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // abort early if there is nothing to draw
2414045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
24158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
24168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2417a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform out vertices into device coordinates
24198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoSTMalloc<16, SkPoint> storage(count);
24208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint* devVerts = storage.get();
24218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMatrix->mapPoints(devVerts, vertices, count);
2422a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder) {
24248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect bounds;
24258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.set(devVerts, count);
24268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!fBounder->doRect(bounds, paint)) {
24278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
24288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
24298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2430a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*
24328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        We can draw the vertices in 1 of 4 ways:
24338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - solid color (no shader/texture[], no colors[])
24358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - just colors (no shader/texture[], has colors[])
24368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - just texture (has shader/texture[], no colors[])
24378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - colors * texture (has shader/texture[], has colors[])
2438a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Thus for texture drawing, we need both texture[] and a shader.
24408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
24418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader triShader; // must be above declaration of p
24438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint p(paint);
24448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkShader* shader = p.getShader();
24468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == shader) {
24478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if we have no shader, we ignore the texture coordinates
24488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        textures = NULL;
24498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else if (NULL == textures) {
24508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if we don't have texture coordinates, ignore the shader
24518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        p.setShader(NULL);
24528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        shader = NULL;
24538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // setup the custom shader (if needed)
24568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL != colors) {
24578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL == textures) {
24588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // just colors (no texture)
2459a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com            shader = p.setShader(&triShader);
24608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
24618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // colors * texture
24628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(shader);
24638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bool releaseMode = false;
24648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL == xmode) {
2465845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com                xmode = SkXfermode::Create(SkXfermode::kMultiply_Mode);
24668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                releaseMode = true;
24678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
24688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkShader* compose = SkNEW_ARGS(SkComposeShader,
24698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                           (&triShader, shader, xmode));
24708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            p.setShader(compose)->unref();
24718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (releaseMode) {
24728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                xmode->unref();
24738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
2474a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com            shader = compose;
24758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
24768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
2479ea033606a06d05d2d42aa7118409fee798e53167reed@google.com    // important that we abort early, as below we may manipulate the shader
2480ea033606a06d05d2d42aa7118409fee798e53167reed@google.com    // and that is only valid if the shader returned true from setContext.
2481ea033606a06d05d2d42aa7118409fee798e53167reed@google.com    // If it returned false, then our blitter will be the NullBlitter.
2482ea033606a06d05d2d42aa7118409fee798e53167reed@google.com    if (blitter->isNullBlitter()) {
2483ea033606a06d05d2d42aa7118409fee798e53167reed@google.com        return;
2484ea033606a06d05d2d42aa7118409fee798e53167reed@google.com    }
2485ea033606a06d05d2d42aa7118409fee798e53167reed@google.com
24868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // setup our state and function pointer for iterating triangles
24878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState       state(count, indices, indexCount);
24888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState::Proc vertProc = state.chooseProc(vmode);
2489a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL != textures || NULL != colors) {
2491f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        SkMatrix  tempM;
2492f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        SkMatrix  savedLocalM;
2493f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        if (shader) {
2494f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com            savedLocalM = shader->getLocalMatrix();
2495f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        }
2496a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while (vertProc(&state)) {
24988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL != textures) {
24998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (texture_to_matrix(state, vertices, textures, &tempM)) {
2500f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com                    tempM.postConcat(savedLocalM);
25018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    shader->setLocalMatrix(tempM);
25028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // need to recal setContext since we changed the local matrix
2503a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com                    shader->endContext();
25048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (!shader->setContext(*fBitmap, p, *fMatrix)) {
25058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        continue;
25068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
25078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
25088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
25098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL != colors) {
25108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (!triShader.setup(vertices, colors,
25118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                     state.f0, state.f1, state.f2)) {
25128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    continue;
25138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
25148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
2515045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
2516045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkPoint tmp[] = {
2517045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
2518045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            };
2519045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkScan::FillTriangle(tmp, *fRC, blitter.get());
25208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
25218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // now restore the shader's original local matrix
25228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != shader) {
2523f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com            shader->setLocalMatrix(savedLocalM);
25248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
25258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
25268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // no colors[] and no texture
25278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        HairProc hairProc = ChooseHairProc(paint.isAntiAlias());
2528045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        const SkRasterClip& clip = *fRC;
25298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while (vertProc(&state)) {
2530045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get());
2531045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get());
2532045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get());
25338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
25348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
25358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25370a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
25380a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
25398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
25418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2542f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comvoid SkDraw::validate() const {
25438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fBitmap != NULL);
25448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fMatrix != NULL);
25458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fClip != NULL);
2546045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkASSERT(fRC != NULL);
25478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2548045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkIRect&  cr = fRC->getBounds();
25498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect         br;
25508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2551f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    br.set(0, 0, fBitmap->width(), fBitmap->height());
25528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(cr.isEmpty() || br.contains(cr));
25538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
25568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25570a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
25580a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com
25590a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.comSkBounder::SkBounder() {
25600a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com    // initialize up front. This gets reset by SkCanvas before each draw call.
25610a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com    fClip = &SkRegion::GetEmptyRegion();
25620a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com}
25638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doIRect(const SkIRect& r) {
25658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    rr;
25668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return rr.intersect(fClip->getBounds(), r) && this->onIRect(rr);
25678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2569d055c1fde2128514167b315f4d104b177e04a3dereed@android.com// TODO: change the prototype to take fixed, and update the callers
2570d055c1fde2128514167b315f4d104b177e04a3dereed@android.combool SkBounder::doIRectGlyph(const SkIRect& r, int x, int y,
2571d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                             const SkGlyph& glyph) {
2572474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com    SkIRect    rr;
2573d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    if (!rr.intersect(fClip->getBounds(), r)) {
2574d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        return false;
2575d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    }
2576d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    GlyphRec rec;
2577d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fLSB.set(SkIntToFixed(x), SkIntToFixed(y));
2578d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fRSB.set(rec.fLSB.fX + glyph.fAdvanceX,
2579d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                 rec.fLSB.fY + glyph.fAdvanceY);
2580d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fGlyphID = glyph.getGlyphID();
2581d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fFlags = 0;
2582d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    return this->onIRectGlyph(rr, rec);
2583474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com}
2584474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com
25858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doHairline(const SkPoint& pt0, const SkPoint& pt1,
25868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           const SkPaint& paint) {
25878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect     r;
25888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    v0, v1;
25898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v0 = pt0.fX;
25918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v1 = pt1.fX;
25928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (v0 > v1) {
25938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkTSwap<SkScalar>(v0, v1);
25948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
25958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fLeft     = SkScalarFloor(v0);
25968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fRight    = SkScalarCeil(v1);
25978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v0 = pt0.fY;
25998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v1 = pt1.fY;
26008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (v0 > v1) {
26018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkTSwap<SkScalar>(v0, v1);
26028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fTop      = SkScalarFloor(v0);
26048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fBottom   = SkScalarCeil(v1);
26058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.isAntiAlias()) {
26078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(-1, -1);
26088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
26108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
26118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doRect(const SkRect& rect, const SkPaint& paint) {
26138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    r;
26148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getStyle() == SkPaint::kFill_Style) {
26168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rect.round(&r);
26178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
26188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int rad = -1;
26198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rect.roundOut(&r);
26208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.isAntiAlias()) {
26218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            rad = -2;
26228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
26238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(rad, rad);
26248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
26268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
26278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doPath(const SkPath& path, const SkPaint& paint, bool doFill) {
2629d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    SkIRect       r;
2630d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    const SkRect& bounds = path.getBounds();
26318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (doFill) {
26338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.round(&r);
26348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // hairline
26358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.roundOut(&r);
26368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.isAntiAlias()) {
26398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(-1, -1);
26408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
26428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
26438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkBounder::commit() {
26458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // override in subclass
26468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
26478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////////////////////////
26498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPath.h"
26518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDraw.h"
26528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRegion.h"
26538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBlitter.h"
26548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds,
265630711b764be6bbb58caa30a0ac5d1474c894efe7reed@google.com                       const SkMaskFilter* filter, const SkMatrix* filterMatrix,
26578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkIRect* bounds) {
26588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (devPath.isEmpty()) {
26598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
26608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //  init our bounds from the path
26638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
2664d252db03d9650013b545ef9781fe993c07f8f314reed@android.com        SkRect pathBounds = devPath.getBounds();
26658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathBounds.inset(-SK_ScalarHalf, -SK_ScalarHalf);
26668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathBounds.roundOut(bounds);
26678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2668a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
26696db75fc2c393ba86a3f533597a8bcd348477e79ctomhudson@google.com    SkIPoint margin = SkIPoint::Make(0, 0);
26708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (filter) {
26718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(filterMatrix);
2672a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
26735af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        SkMask srcM, dstM;
2674a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
26758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fBounds = *bounds;
26768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fFormat = SkMask::kA8_Format;
26778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fImage = NULL;
26788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
26798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return false;
26808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
26818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2682a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
26835af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com    // (possibly) trim the bounds to reflect the clip
26848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // (plus whatever slop the filter needs)
26855af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com    if (clipBounds) {
26865af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        SkIRect tmp = *clipBounds;
26873555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // Ugh. Guard against gigantic margins from wacky filters. Without this
26883555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // check we can request arbitrary amounts of slop beyond our visible
26893555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // clip, and bring down the renderer (at least on finite RAM machines
26903555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // like handsets, etc.). Need to balance this invented value between
26913555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // quality of large filters like blurs, and the corresponding memory
26923555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // requests.
26933555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        static const int MAX_MARGIN = 128;
26943555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        tmp.inset(-SkMin32(margin.fX, MAX_MARGIN),
26953555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com                  -SkMin32(margin.fY, MAX_MARGIN));
26965af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        if (!bounds->intersect(tmp)) {
26975af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com            return false;
26985af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        }
26998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
27008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
27028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
27038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27042ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.orgstatic void draw_into_mask(const SkMask& mask, const SkPath& devPath,
27052ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                           SkPaint::Style style) {
2706045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBitmap        bm;
2707045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkDraw          draw;
2708045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkRasterClip    clip;
2709045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkMatrix        matrix;
2710045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkPaint         paint;
27118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), mask.fBounds.height(), mask.fRowBytes);
27138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bm.setPixels(mask.fImage);
27148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2715045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height()));
27168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
27178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        -SkIntToScalar(mask.fBounds.fTop));
27188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fBitmap    = &bm;
2720045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    draw.fRC        = &clip;
2721045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    draw.fClip      = &clip.bwRgn();
27228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix    = &matrix;
27238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fBounder   = NULL;
27248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    paint.setAntiAlias(true);
27252ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org    paint.setStyle(style);
27268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.drawPath(devPath, paint);
27278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
27288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
273030711b764be6bbb58caa30a0ac5d1474c894efe7reed@google.com                        const SkMaskFilter* filter, const SkMatrix* filterMatrix,
27312ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                        SkMask* mask, SkMask::CreateMode mode,
27322ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                        SkPaint::Style style) {
27338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kJustRenderImage_CreateMode != mode) {
27348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds))
27358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return false;
27368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2737a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
27388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
27398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask->fFormat = SkMask::kA8_Format;
27408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask->fRowBytes = mask->fBounds.width();
2741543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        size_t size = mask->computeImageSize();
2742543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        if (0 == size) {
2743543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            // we're too big to allocate the mask, abort
2744543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            return false;
2745543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        }
2746543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        mask->fImage = SkMask::AllocImage(size);
27478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        memset(mask->fImage, 0, mask->computeImageSize());
27488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
27498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kJustComputeBounds_CreateMode != mode) {
27512ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org        draw_into_mask(*mask, devPath, style);
27528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2753a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
27548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
27558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2756