SkDraw.cpp revision f94b3a4cebd4adab09c40ebe23c02a615e10c394
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
8ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDraw.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBlitter.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBounder.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDevice.h"
162211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com#include "SkFixed.h"
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkMaskFilter.h"
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPaint.h"
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPathEffect.h"
20045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com#include "SkRasterClip.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRasterizer.h"
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkScan.h"
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkShader.h"
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkStroke.h"
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTemplatesPriv.h"
2632e5d97ccf60f859db063ebd6e903c362e625767reed@google.com#include "SkTLazy.h"
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h"
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkAutoKern.h"
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBitmapProcShader.h"
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDrawProcs.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++) {
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int x = SkScalarFloor(devPts[i].fX);
3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int y = SkScalarFloor(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);
337a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint16_t* addr = bitmap->getAddr16(0, 0);
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int rb = bitmap->rowBytes();
3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int x = SkScalarFloor(devPts[i].fX);
3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int y = SkScalarFloor(devPts[i].fY);
3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (r.contains(x, y)) {
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//            *bitmap->getAddr16(x, y) = SkToU16(value);
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value);
3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            int count, SkBlitter* blitter) {
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int x = SkScalarFloor(devPts[i].fX);
3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int y = SkScalarFloor(devPts[i].fY);
3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (rec.fClip->contains(x, y)) {
3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            blitter->blitH(x, y, 1);
3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i += 2) {
365045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count - 1; i++) {
372045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// aa versions
3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i += 2) {
381045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count - 1; i++) {
388045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// square procs (strokeWidth > 0 but matrix is square-scale (sx == sy)
3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[],
3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           int count, SkBlitter* blitter) {
3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkFixed radius = rec.fRadius;
3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed x = SkScalarToFixed(devPts[i].fX);
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed y = SkScalarToFixed(devPts[i].fY);
400a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkXRect r;
4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fLeft = x - radius;
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fTop = y - radius;
4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fRight = x + radius;
4058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fBottom = y + radius;
406a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
407045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::FillXRect(r, *rec.fRC, blitter);
4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[],
4128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           int count, SkBlitter* blitter) {
4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkFixed radius = rec.fRadius;
4148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
4158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed x = SkScalarToFixed(devPts[i].fX);
4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed y = SkScalarToFixed(devPts[i].fY);
417a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
4188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkXRect r;
4198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fLeft = x - radius;
4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fTop = y - radius;
4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fRight = x + radius;
4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fBottom = y + radius;
423a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
424045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::AntiFillXRect(r, *rec.fRC, blitter);
4258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
428b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com// If this guy returns true, then chooseProc() must return a valid proc
4298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint,
430045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                     const SkMatrix* matrix, const SkRasterClip* rc) {
4318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getPathEffect()) {
4328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
4338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar width = paint.getStrokeWidth();
4358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (0 == width) {
4368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fMode = mode;
4378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPaint = &paint;
438045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = NULL;
439045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fRC = rc;
4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fRadius = SK_Fixed1 >> 1;
4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return true;
4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
443b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com    if (paint.getStrokeCap() != SkPaint::kRound_Cap &&
444b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com            matrix->rectStaysRect() && SkCanvas::kPoints_PointMode == mode) {
4458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sx = matrix->get(SkMatrix::kMScaleX);
4468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sy = matrix->get(SkMatrix::kMScaleY);
4478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (SkScalarNearlyZero(sx - sy)) {
4488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (sx < 0) {
4498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                sx = -sx;
4508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
4518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fMode = mode;
4538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fPaint = &paint;
454045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            fClip = NULL;
455045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            fRC = rc;
4568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1;
4578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return true;
4588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
4598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return false;
4618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
463045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comPtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) {
464b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com    Proc proc = NULL;
465a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
466045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter* blitter = *blitterPtr;
467045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isBW()) {
468045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = &fRC->bwRgn();
469045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    } else {
470045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fWrapper.init(*fRC, blitter);
471045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = &fWrapper.getRgn();
472045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = fWrapper.getBlitter();
473045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        *blitterPtr = blitter;
474045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    }
475045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // for our arrays
4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(0 == SkCanvas::kPoints_PointMode);
4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == SkCanvas::kLines_PointMode);
4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(2 == SkCanvas::kPolygon_PointMode);
4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode);
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // first check for hairlines
4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (0 == fPaint->getStrokeWidth()) {
4848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fPaint->isAntiAlias()) {
4858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            static const Proc gAAProcs[] = {
4868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc
4878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            };
4888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc = gAAProcs[fMode];
4898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
4908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) {
4918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                uint32_t value;
4928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const SkBitmap* bm = blitter->justAnOpaqueColor(&value);
4938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (bm && bm->config() == SkBitmap::kRGB_565_Config) {
4948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    proc = bw_pt_rect_16_hair_proc;
4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                } else {
4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    proc = bw_pt_rect_hair_proc;
4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } else {
4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                static Proc gBWProcs[] = {
5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc
5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                };
5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                proc = gBWProcs[fMode];
5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
5048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
505b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com    } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) {
5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(SkCanvas::kPoints_PointMode == fMode);
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fPaint->isAntiAlias()) {
5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc = aa_square_proc;
5098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
5108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc = bw_square_proc;
5118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
5128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return proc;
5148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool bounder_points(SkBounder* bounder, SkCanvas::PointMode mode,
5178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           size_t count, const SkPoint pts[],
5188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           const SkPaint& paint, const SkMatrix& matrix) {
5198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect ibounds;
5208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect bounds;
5218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar inset = paint.getStrokeWidth();
5228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.set(pts, count);
5248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.inset(-inset, -inset);
5258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.mapRect(&bounds);
5268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.roundOut(&ibounds);
5288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return bounder->doIRect(ibounds);
5298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// each of these costs 8-bytes of stack space, so don't make it too large
5328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// must be even for lines/polygon to work
5338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define MAX_DEV_PTS     32
5348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count,
536f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        const SkPoint pts[], const SkPaint& paint,
537f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        bool forceUseDevice) const {
5388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // if we're in lines mode, force count to be even
5398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkCanvas::kLines_PointMode == mode) {
5408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        count &= ~(size_t)1;
5418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if ((long)count <= 0) {
5448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
5458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
546a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
5478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(pts != NULL);
548f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
549a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
5508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     // nothing to draw
551045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty()) {
5528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
5538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
555fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    if (fBounder) {
556fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        if (!bounder_points(fBounder, mode, count, pts, paint, *fMatrix)) {
557fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com            return;
558fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        }
559fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
560fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        // clear the bounder and call this again, so we don't invoke the bounder
561fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        // later if we happen to call ourselves for drawRect, drawPath, etc.
562fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        SkDraw noBounder(*this);
563fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        noBounder.fBounder = NULL;
564fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        noBounder.drawPoints(mode, count, pts, paint, forceUseDevice);
565fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        return;
566fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    }
567fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
5688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    PtProcRec rec;
569045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (!forceUseDevice && rec.init(mode, paint, fMatrix, fRC)) {
5708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
5718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint             devPts[MAX_DEV_PTS];
5738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkMatrix*     matrix = fMatrix;
5748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkBlitter*          bltr = blitter.get();
575045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        PtProcRec::Proc     proc = rec.chooseProc(&bltr);
5768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // we have to back up subsequent passes if we're in polygon mode
5778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const size_t backup = (SkCanvas::kPolygon_PointMode == mode);
578a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
5798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        do {
5808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            size_t n = count;
5818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (n > MAX_DEV_PTS) {
5828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                n = MAX_DEV_PTS;
5838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
5848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix->mapPoints(devPts, pts, n);
5858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc(rec, devPts, n, bltr);
5868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pts += n - backup;
5878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(count >= n);
5888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            count -= n;
5898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (count > 0) {
5908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                count += backup;
5918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
5928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } while (count != 0);
5938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
5948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        switch (mode) {
5958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kPoints_PointMode: {
5968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // temporarily mark the paint as filling.
59740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                SkPaint newPaint(paint);
59840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                newPaint.setStyle(SkPaint::kFill_Style);
5998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
60040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                SkScalar width = newPaint.getStrokeWidth();
6018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkScalar radius = SkScalarHalf(width);
602a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
60340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) {
6048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkPath      path;
6058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkMatrix    preMatrix;
606a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
6078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.addCircle(0, 0, radius);
6088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    for (size_t i = 0; i < count; i++) {
6098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        preMatrix.setTranslate(pts[i].fX, pts[i].fY);
6108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        // pass true for the last point, since we can modify
6118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        // then path then
612f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        if (fDevice) {
61340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            fDevice->drawPath(*this, path, newPaint, &preMatrix,
614f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                              (count-1) == i);
615f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        } else {
61640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            this->drawPath(path, newPaint, &preMatrix,
61740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                                           (count-1) == i);
618f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        }
6198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
6208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                } else {
6218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkRect  r;
622a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
6238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    for (size_t i = 0; i < count; i++) {
6248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fLeft = pts[i].fX - radius;
6258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fTop = pts[i].fY - radius;
6268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fRight = r.fLeft + width;
6278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fBottom = r.fTop + width;
628f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        if (fDevice) {
62940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            fDevice->drawRect(*this, r, newPaint);
630f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        } else {
63140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            this->drawRect(r, newPaint);
632f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        }
6338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
6348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
6358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
6368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
6378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kLines_PointMode:
6388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kPolygon_PointMode: {
6398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                count -= 1;
6408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkPath path;
6418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkPaint p(paint);
6428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                p.setStyle(SkPaint::kStroke_Style);
6438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
6448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                for (size_t i = 0; i < count; i += inc) {
6458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.moveTo(pts[i]);
6468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.lineTo(pts[i+1]);
647f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    if (fDevice) {
648f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        fDevice->drawPath(*this, path, p, NULL, true);
649f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    } else {
650f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        this->drawPath(path, p, NULL, true);
651f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    }
6528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.rewind();
6538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
6548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
6558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
6568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
6578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
6588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic inline SkPoint* as_lefttop(SkRect* r) {
6618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (SkPoint*)(void*)r;
6628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic inline SkPoint* as_rightbottom(SkRect* r) {
6658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return ((SkPoint*)(void*)r) + 1;
6668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
668761fb62b0eb174783316d2a8b933fba896ca6355reed@google.comstatic bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix,
669761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                           SkPoint* strokeSize) {
670761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    if (SkPaint::kMiter_Join != paint.getStrokeJoin() ||
671761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com        paint.getStrokeMiter() < SK_ScalarSqrt2) {
672761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com        return false;
673761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    }
674fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
675761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    SkASSERT(matrix.rectStaysRect());
676761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() };
677761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    matrix.mapVectors(strokeSize, &pt, 1);
6786115338c59799b4ef09cda187f23867dea093f6ereed@google.com    strokeSize->fX = SkScalarAbs(strokeSize->fX);
6796115338c59799b4ef09cda187f23867dea093f6ereed@google.com    strokeSize->fY = SkScalarAbs(strokeSize->fY);
680761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    return true;
6817ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org}
6827ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org
68362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.comSkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint,
68462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com                                         const SkMatrix& matrix,
68562ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com                                         SkPoint* strokeSize) {
6867ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    RectType rtype;
6877ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    const SkScalar width = paint.getStrokeWidth();
68862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    const bool zeroWidth = (0 == width);
6897ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    SkPaint::Style style = paint.getStyle();
690fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
6917ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) {
6927ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        style = SkPaint::kFill_Style;
6937ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    }
694fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
6958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getPathEffect() || paint.getMaskFilter() ||
69662ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        paint.getRasterizer() || !matrix.rectStaysRect() ||
6977ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        SkPaint::kStrokeAndFill_Style == style) {
69862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        rtype = kPath_RectType;
69962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    } else if (SkPaint::kFill_Style == style) {
7007ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kFill_RectType;
7017ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    } else if (zeroWidth) {
7027ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kHair_RectType;
70362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    } else if (easy_rect_join(paint, matrix, strokeSize)) {
7047ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kStroke_RectType;
7057ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    } else {
7067ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kPath_RectType;
7077ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    }
70862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    return rtype;
70962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com}
71062ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
7117324415759fe0c5a0902877b664aa942a89bd940reed@google.comstatic const SkPoint* rect_points(const SkRect& r) {
7127324415759fe0c5a0902877b664aa942a89bd940reed@google.com    return (const SkPoint*)(void*)&r;
7137324415759fe0c5a0902877b664aa942a89bd940reed@google.com}
7147324415759fe0c5a0902877b664aa942a89bd940reed@google.com
7157324415759fe0c5a0902877b664aa942a89bd940reed@google.comstatic SkPoint* rect_points(SkRect& r) {
7167324415759fe0c5a0902877b664aa942a89bd940reed@google.com    return (SkPoint*)(void*)&r;
71740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com}
71840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com
71962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.comvoid SkDraw::drawRect(const SkRect& rect, const SkPaint& paint) const {
72062ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    SkDEBUGCODE(this->validate();)
7217ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org
72262ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    // nothing to draw
723045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty()) {
72462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        return;
72562ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    }
72662ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
72762ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    SkPoint strokeSize;
72862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize);
72962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
73062ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com#ifdef SK_DISABLE_FAST_AA_STROKE_RECT
73162ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    if (kStroke_RectType == rtype && paint.isAntiAlias()) {
73262ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        rtype = kPath_RectType;
73362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    }
73462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com#endif
735045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
7367ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    if (kPath_RectType == rtype) {
7378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPath  tmp;
7388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tmp.addRect(rect);
7398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tmp.setFillType(SkPath::kWinding_FillType);
740187d5595901d1120d9425851e5afdd773f574502reed@android.com        this->drawPath(tmp, paint, NULL, true);
7418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
7428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix& matrix = *fMatrix;
7458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect          devRect;
7468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform rect into devRect
7487324415759fe0c5a0902877b664aa942a89bd940reed@google.com    matrix.mapPoints(rect_points(devRect), rect_points(rect), 2);
7497324415759fe0c5a0902877b664aa942a89bd940reed@google.com    devRect.sort();
7508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder && !fBounder->doRect(devRect, paint)) {
7528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
7538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // look for the quick exit, before we build a blitter
7567324415759fe0c5a0902877b664aa942a89bd940reed@google.com    if (true) {
7578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkIRect ir;
7588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        devRect.roundOut(&ir);
75955e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        if (paint.getStyle() != SkPaint::kFill_Style) {
76055e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            // extra space for hairlines
76155e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            ir.inset(-1, -1);
76255e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        }
763045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (fRC->quickReject(ir))
7648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
7658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose blitterStorage(*fBitmap, matrix, paint);
768045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkRasterClip& clip = *fRC;
7698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*          blitter = blitterStorage.get();
7708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
771b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
772b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // case we are also hairline (if we've gotten to here), which devolves to
773b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // effectively just kFill
7747ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    switch (rtype) {
7757ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kFill_RectType:
7767ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
7777ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::AntiFillRect(devRect, clip, blitter);
7787ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
7797ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::FillRect(devRect, clip, blitter);
7807ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
7817ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
7827ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kStroke_RectType:
7837ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
784761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                SkScan::AntiFrameRect(devRect, strokeSize, clip, blitter);
7857ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
786761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                SkScan::FrameRect(devRect, strokeSize, clip, blitter);
7877ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
7887ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
7897ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kHair_RectType:
7907ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
7917ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::AntiHairRect(devRect, clip, blitter);
7927ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
7937ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::HairRect(devRect, clip, blitter);
7947ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
7957ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
7967ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        default:
7970c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com            SkDEBUGFAIL("bad rtype");
7988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const {
8028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (srcM.fBounds.isEmpty()) {
8038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
8048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8060a60b3d32eae945688b69599f11679662657f751bungeman@google.com    const SkMask* mask = &srcM;
8078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8080a60b3d32eae945688b69599f11679662657f751bungeman@google.com    SkMask dstM;
8098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getMaskFilter() &&
8108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, NULL)) {
8118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask = &dstM;
812bf2ac7e52f84095368dc50600fd6e78cc96044e3bungeman@google.com    } else {
813bf2ac7e52f84095368dc50600fd6e78cc96044e3bungeman@google.com        dstM.fImage = NULL;
8148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
81502f55841854ae32f21a13417e9ee711463e488cfbungeman@google.com    SkAutoMaskFreeImage ami(dstM.fImage);
8168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder && !fBounder->doIRect(mask->fBounds)) {
8188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
8198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
821045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAutoBlitterChoose blitterChooser(*fBitmap, *fMatrix, paint);
822045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter* blitter = blitterChooser.get();
823045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
824045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAAClipBlitterWrapper wrapper;
825045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkRegion* clipRgn;
8268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
827045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isBW()) {
828045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        clipRgn = &fRC->bwRgn();
829045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    } else {
830045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        wrapper.init(*fRC, blitter);
831045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        clipRgn = &wrapper.getRgn();
832045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = wrapper.getBlitter();
833045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    }
834045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    blitter->blitMaskRegion(*mask, *clipRgn);
8358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
837ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.comstatic SkScalar fast_len(const SkVector& vec) {
838ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar x = SkScalarAbs(vec.fX);
839ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar y = SkScalarAbs(vec.fY);
840ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    if (x < y) {
841ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        SkTSwap(x, y);
842ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
843ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    return x + SkScalarHalf(y);
844ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com}
845ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com
846ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.comstatic bool xfermodeSupportsCoverageAsAlpha(SkXfermode* xfer) {
847ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    SkXfermode::Coeff dc;
848ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (!SkXfermode::AsCoeff(xfer, NULL, &dc)) {
849ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return false;
850ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
851fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
852ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    switch (dc) {
853ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        case SkXfermode::kOne_Coeff:
854ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        case SkXfermode::kISA_Coeff:
855ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        case SkXfermode::kISC_Coeff:
856ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com            return true;
857ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        default:
858ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com            return false;
859ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
860ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com}
861ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
862ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.combool SkDrawTreatAsHairline(const SkPaint& paint, const SkMatrix& matrix,
863dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                           SkScalar* coverage) {
864dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com    SkASSERT(coverage);
865ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (SkPaint::kStroke_Style != paint.getStyle()) {
866ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return false;
867ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
868ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    SkScalar strokeWidth = paint.getStrokeWidth();
869ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (0 == strokeWidth) {
870dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        *coverage = SK_Scalar1;
871ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return true;
872ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
873ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
874ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    // if we get here, we need to try to fake a thick-stroke with a modulated
875ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    // hairline
876ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
877ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (!paint.isAntiAlias()) {
878ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return false;
879ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
8808d430185e08d2067584837a76b7193b803fee7a0tomhudson@google.com    if (matrix.hasPerspective()) {
881ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        return false;
882ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
883ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
884ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkVector src[2], dst[2];
885ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    src[0].set(strokeWidth, 0);
886ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    src[1].set(0, strokeWidth);
887ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    matrix.mapVectors(dst, src, 2);
888ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar len0 = fast_len(dst[0]);
889ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar len1 = fast_len(dst[1]);
890652807bbc8c57e5fa9622126b51fd369f5c67935agl@chromium.org    if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) {
891dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        *coverage = SkScalarAve(len0, len1);
892ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        return true;
893ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
894ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    return false;
895ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com}
896ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com
89732e5d97ccf60f859db063ebd6e903c362e625767reed@google.comvoid SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint,
8988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      const SkMatrix* prePathMatrix, bool pathIsMutable) const {
899f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
9008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
902045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty()) {
9038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
9048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath*         pathPtr = (SkPath*)&origSrcPath;
9078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool            doFill = true;
9088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath          tmpPath;
9098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        tmpMatrix;
9108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix* matrix = fMatrix;
9118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (prePathMatrix) {
91332e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style ||
91432e5d97ccf60f859db063ebd6e903c362e625767reed@google.com                origPaint.getRasterizer()) {
9158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPath* result = pathPtr;
916a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
9178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!pathIsMutable) {
9188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                result = &tmpPath;
9198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pathIsMutable = true;
9208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
9218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathPtr->transform(*prePathMatrix, result);
9228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathPtr = result;
9238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
9248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!tmpMatrix.setConcat(*matrix, *prePathMatrix)) {
9258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // overflow
9268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
9278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
9288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix = &tmpMatrix;
9298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
9308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // at this point we're done with prePathMatrix
9328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
933a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
9345dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
935a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
936ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    {
937dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        SkScalar coverage;
938dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        if (SkDrawTreatAsHairline(origPaint, *matrix, &coverage)) {
939dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com            if (SK_Scalar1 == coverage) {
9405dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                paint.writable()->setStrokeWidth(0);
941dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com            } else if (xfermodeSupportsCoverageAsAlpha(origPaint.getXfermode())) {
942dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                U8CPU newAlpha;
943dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com#if 0
944dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                newAlpha = SkToU8(SkScalarRoundToInt(coverage *
945dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                                                     origPaint.getAlpha()));
946dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com#else
947dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                // this is the old technique, which we preserve for now so
948dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                // we don't change previous results (testing)
949dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                // the new way seems fine, its just (a tiny bit) different
950dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                int scale = (int)SkScalarMul(coverage, 256);
951dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                newAlpha = origPaint.getAlpha() * scale >> 8;
952dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com#endif
9535dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                SkPaint* writablePaint = paint.writable();
9545dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                writablePaint->setStrokeWidth(0);
9555dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                writablePaint->setAlpha(newAlpha);
956dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com            }
9578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
9588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
959a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
96032e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) {
96132e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        doFill = paint->getFillPath(*pathPtr, &tmpPath);
9628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathPtr = &tmpPath;
9638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
964a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
96532e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (paint->getRasterizer()) {
9668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
96732e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->getRasterizer()->rasterize(*pathPtr, *matrix,
968045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                            &fRC->getBounds(), paint->getMaskFilter(), &mask,
9698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
97032e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            this->drawDevMask(mask, *paint);
9718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkMask::FreeImage(mask.fImage);
9728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
9738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
9748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // avoid possibly allocating a new path in transform if we can
9778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
9788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform the path into device space
9808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pathPtr->transform(*matrix, devPathPtr);
9818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
98232e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, *paint);
9838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9842ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org    if (paint->getMaskFilter()) {
985fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        SkPaint::Style style = doFill ? SkPaint::kFill_Style :
9862ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org            SkPaint::kStroke_Style;
9872ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org        if (paint->getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fRC,
9882ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                                               fBounder, blitter.get(),
9892ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                                               style)) {
9902ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org            return; // filterPath() called the blitter, so we're done
9912ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org        }
9928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
99432e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (fBounder && !fBounder->doPath(*devPathPtr, *paint, doFill)) {
9958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
9968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
998045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    void (*proc)(const SkPath&, const SkRasterClip&, SkBlitter*);
9998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (doFill) {
100032e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->isAntiAlias()) {
1001045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::AntiFillPath;
10028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1003045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::FillPath;
10048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
10058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // hairline
100632e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->isAntiAlias()) {
1007045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::AntiHairPath;
10088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1009045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::HairPath;
10108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
10118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1012045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    proc(*devPathPtr, *fRC, blitter.get());
10138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10150baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com/** For the purposes of drawing bitmaps, if a matrix is "almost" translate
10160baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    go ahead and treat it as if it were, so that subsequent code can go fast.
10170baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com */
10180baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.comstatic bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) {
10190baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    SkMatrix::TypeMask mask = matrix.getType();
10200baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com
10210baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (mask & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
10220baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        return false;
10230baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    }
10240baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (mask & SkMatrix::kScale_Mask) {
10250baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        SkScalar sx = matrix[SkMatrix::kMScaleX];
10260baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        SkScalar sy = matrix[SkMatrix::kMScaleY];
10270baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int w = bitmap.width();
10280baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int h = bitmap.height();
10290baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int sw = SkScalarRound(SkScalarMul(sx, SkIntToScalar(w)));
10300baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int sh = SkScalarRound(SkScalarMul(sy, SkIntToScalar(h)));
10310baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        return sw == w && sh == h;
10320baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    }
10330baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    // if we got here, we're either kTranslate_Mask or identity
10340baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    return true;
10358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawBitmapAsMask(const SkBitmap& bitmap,
10388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint) const {
10398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(bitmap.getConfig() == SkBitmap::kA8_Config);
10408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1041a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    if (just_translate(*fMatrix, bitmap)) {
10428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int ix = SkScalarRound(fMatrix->getTranslateX());
10438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int iy = SkScalarRound(fMatrix->getTranslateY());
10448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
10468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fBounds.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
10478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fFormat = SkMask::kA8_Format;
10488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fRowBytes = bitmap.rowBytes();
10498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fImage = bitmap.getAddr8(0, 0);
1050a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawDevMask(mask, paint);
10528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // need to xform the bitmap first
10538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect  r;
10548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
1055a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.set(0, 0,
10578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com              SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
10588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fMatrix->mapRect(&r);
10598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.round(&mask.fBounds);
1060a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // set the mask's bounds to the transformed bitmap-bounds,
10628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // clipped to the actual device
10638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
10648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkIRect    devBounds;
10658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            devBounds.set(0, 0, fBitmap->width(), fBitmap->height());
10668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // need intersect(l, t, r, b) on irect
10678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!mask.fBounds.intersect(devBounds)) {
10688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
10698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
10708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1071543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com
10728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fFormat = SkMask::kA8_Format;
10738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fRowBytes = SkAlign4(mask.fBounds.width());
1074543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        size_t size = mask.computeImageSize();
1075543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        if (0 == size) {
1076543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            // the mask is too big to allocated, draw nothing
1077543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            return;
1078543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        }
10798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // allocate (and clear) our temp buffer to hold the transformed bitmap
10818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAutoMalloc    storage(size);
10828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fImage = (uint8_t*)storage.get();
10838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        memset(mask.fImage, 0, size);
1084a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // now draw our bitmap(src) into mask(dst), transformed by the matrix
10868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
10878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkBitmap    device;
10888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            device.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(),
10898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             mask.fBounds.height(), mask.fRowBytes);
10908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            device.setPixels(mask.fImage);
1091a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkCanvas c(device);
10938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // need the unclipped top/left for the translate
10948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            c.translate(-SkIntToScalar(mask.fBounds.fLeft),
10958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        -SkIntToScalar(mask.fBounds.fTop));
10968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            c.concat(*fMatrix);
10973469c76c40790b409621fd7eff34f56240718549reed@android.com
10983469c76c40790b409621fd7eff34f56240718549reed@android.com            // We can't call drawBitmap, or we'll infinitely recurse. Instead
1099fb12c3e6ba84f95dc15fbaddc239dede0ba1d60ereed@android.com            // we manually build a shader and draw that into our new mask
11003469c76c40790b409621fd7eff34f56240718549reed@android.com            SkPaint tmpPaint;
11013469c76c40790b409621fd7eff34f56240718549reed@android.com            tmpPaint.setFlags(paint.getFlags());
110240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            SkAutoBitmapShaderInstall install(bitmap, tmpPaint);
11033469c76c40790b409621fd7eff34f56240718549reed@android.com            SkRect rr;
11043469c76c40790b409621fd7eff34f56240718549reed@android.com            rr.set(0, 0, SkIntToScalar(bitmap.width()),
11053469c76c40790b409621fd7eff34f56240718549reed@android.com                   SkIntToScalar(bitmap.height()));
110640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            c.drawRect(rr, install.paintWithShader());
11078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
11088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawDevMask(mask, paint);
11098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1112045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic bool clipped_out(const SkMatrix& m, const SkRasterClip& c,
11138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        const SkRect& srcR) {
11148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect  dstR;
11158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect devIR;
1116a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.mapRect(&dstR, srcR);
1118a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    dstR.roundOut(&devIR);
11198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return c.quickReject(devIR);
11208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1122045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip,
11238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        int width, int height) {
11248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect  r;
11258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
11268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return clipped_out(matrix, clip, r);
11278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1129045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic bool clipHandlesSprite(const SkRasterClip& clip, int x, int y,
1130045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                              const SkBitmap& bitmap) {
1131045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    return clip.isBW() ||
1132045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com           clip.quickContains(x, y, x + bitmap.width(), y + bitmap.height());
1133045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com}
1134045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
11358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
113640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                        const SkPaint& origPaint) const {
1137f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
11388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1140045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty() ||
11418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bitmap.width() == 0 || bitmap.height() == 0 ||
1142dcd0f3a980095b77ebe605b49777a4bd37ca7b0areed@google.com            bitmap.getConfig() == SkBitmap::kNo_Config) {
11438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
11448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1145a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1146a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#ifndef SK_ALLOW_OVER_32K_BITMAPS
11478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // run away on too-big bitmaps for now (exceed 16.16)
11488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (bitmap.width() > 32767 || bitmap.height() > 32767) {
11498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
11508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1151a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#endif
1152a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
115340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkPaint paint(origPaint);
115440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    paint.setStyle(SkPaint::kFill_Style);
1155a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix matrix;
11578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (!matrix.setConcat(*fMatrix, prematrix)) {
11588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
11598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1161045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) {
11628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
11638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1165218521e15706c4377b1be49d931c4d7c8d597445reed@android.com    if (fBounder && just_translate(matrix, bitmap)) {
1166218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        SkIRect ir;
1167218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        int32_t ix = SkScalarRound(matrix.getTranslateX());
1168218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        int32_t iy = SkScalarRound(matrix.getTranslateY());
1169218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1170218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        if (!fBounder->doIRect(ir)) {
1171218521e15706c4377b1be49d931c4d7c8d597445reed@android.com            return;
1172218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        }
1173218521e15706c4377b1be49d931c4d7c8d597445reed@android.com    }
1174218521e15706c4377b1be49d931c4d7c8d597445reed@android.com
1175218521e15706c4377b1be49d931c4d7c8d597445reed@android.com    // only lock the pixels if we passed the clip and bounder tests
11768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoLockPixels alp(bitmap);
11778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // after the lock, check if we are valid
11788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (!bitmap.readyToDraw()) {
11798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
11808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11820baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (bitmap.getConfig() != SkBitmap::kA8_Config &&
11830baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com            just_translate(matrix, bitmap)) {
1184045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        int ix = SkScalarRound(matrix.getTranslateX());
1185045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        int iy = SkScalarRound(matrix.getTranslateY());
1186045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (clipHandlesSprite(*fRC, ix, iy, bitmap)) {
1187045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            uint32_t    storage[kBlitterStorageLongCount];
1188045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkBlitter*  blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1189045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                                                ix, iy, storage, sizeof(storage));
1190045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            if (blitter) {
1191045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                SkAutoTPlacementDelete<SkBlitter>   ad(blitter, storage);
1192045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1193045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                SkIRect    ir;
1194045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1195045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1196045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                SkScan::FillIRect(ir, *fRC, blitter);
1197045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                return;
11988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
11998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
12008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1201a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // now make a temp draw on the stack, and use it
12038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //
12048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDraw draw(*this);
12058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix = &matrix;
1206a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (bitmap.getConfig() == SkBitmap::kA8_Config) {
12088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        draw.drawBitmapAsMask(bitmap, paint);
12098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
121040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkAutoBitmapShaderInstall install(bitmap, paint);
12118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect  r;
12138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.set(0, 0, SkIntToScalar(bitmap.width()),
12148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com              SkIntToScalar(bitmap.height()));
1215a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com        // is this ok if paint has a rasterizer?
121640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        draw.drawRect(r, install.paintWithShader());
12178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y,
122140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                        const SkPaint& origPaint) const {
1222f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
1223a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1225045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty() ||
12268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bitmap.width() == 0 || bitmap.height() == 0 ||
1227dcd0f3a980095b77ebe605b49777a4bd37ca7b0areed@google.com            bitmap.getConfig() == SkBitmap::kNo_Config) {
12288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
12298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    bounds;
12328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.set(x, y, x + bitmap.width(), y + bitmap.height());
12338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1234045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->quickReject(bounds)) {
12358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return; // nothing to draw
12368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
123840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkPaint paint(origPaint);
123940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    paint.setStyle(SkPaint::kFill_Style);
12408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1241045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (NULL == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, bitmap)) {
12428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        uint32_t    storage[kBlitterStorageLongCount];
12438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkBlitter*  blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
12448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                                x, y, storage, sizeof(storage));
12458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (blitter) {
12478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkAutoTPlacementDelete<SkBlitter> ad(blitter, storage);
12488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (fBounder && !fBounder->doIRect(bounds)) {
12508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
12518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
12528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1253045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkScan::FillIRect(bounds, *fRC, blitter);
12548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
12558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
12568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
125840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkAutoBitmapShaderInstall install(bitmap, paint);
125940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    const SkPaint& shaderPaint = install.paintWithShader();
12608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        matrix;
12628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect          r;
12638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // get a scalar version of our rect
12658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.set(bounds);
12668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // tell the shader our offset
12688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setTranslate(r.fLeft, r.fTop);
126940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    shaderPaint.getShader()->setLocalMatrix(matrix);
1270a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDraw draw(*this);
12728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.reset();
12738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix = &matrix;
12748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // call ourself with a rect
1275a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    // is this OK if paint has a rasterizer?
127640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    draw.drawRect(r, shaderPaint);
12778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
12808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkScalerContext.h"
12828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGlyphCache.h"
1283e69137620ab0b5b40d230318c8e11b822f63cb9dreed@google.com#include "SkTextToPathIter.h"
12848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h"
12858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void measure_text(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
12878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const char text[], size_t byteLength, SkVector* stopVector) {
12888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed     x = 0, y = 0;
12898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* stop = text + byteLength;
12908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoKern  autokern;
1292a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (text < stop) {
12948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // don't need x, y here, since all subpixel variants will have the
12958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // same advance
12968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
12978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x += autokern.adjust(glyph) + glyph.fAdvanceX;
12998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y += glyph.fAdvanceY;
13008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
13028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(text == stop);
13048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawText_asPaths(const char text[], size_t byteLength,
13078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              SkScalar x, SkScalar y,
13088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint) const {
1309f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
13108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1311166e653f67f3fffc3846184a25ce45ab083f07a2djsollen@google.com    SkTextToPathIter iter(text, byteLength, paint, true);
13128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    matrix;
13148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setScale(iter.getPathScale(), iter.getPathScale());
13158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.postTranslate(x, y);
13168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPath* iterPath;
13188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar xpos, prevXPos = 0;
13198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13207b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com    while (iter.next(&iterPath, &xpos)) {
13218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        matrix.postTranslate(xpos - prevXPos, 0);
13227b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com        if (iterPath) {
13237b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            const SkPaint& pnt = iter.getPaint();
13247b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            if (fDevice) {
13257b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                fDevice->drawPath(*this, *iterPath, pnt, &matrix, false);
13267b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            } else {
13277b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                this->drawPath(*iterPath, pnt, &matrix, false);
13287b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            }
1329f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
13308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        prevXPos = xpos;
13318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// disable warning : local variable used without having been initialized
1335a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#if defined _WIN32 && _MSC_VER >= 1300
13368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( push )
13378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( disable : 4701 )
13388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
13398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
13418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_NoBounder_RectClip(const SkDraw1Glyph& state,
1343f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                   SkFixed fx, SkFixed fy,
1344f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                   const SkGlyph& glyph) {
1345f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1346f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
13478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
134883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT(NULL == state.fBounder);
134983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT((NULL == state.fClip && state.fAAClip) ||
135083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com             (state.fClip && NULL == state.fAAClip && state.fClip->isRect()));
13518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
13538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
13548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int right   = left + glyph.fWidth;
13568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int bottom  = top + glyph.fHeight;
13578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1358fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkMask        mask;
1359fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkIRect        storage;
1360fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkIRect*    bounds = &mask.fBounds;
136183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
136283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fBounds.set(left, top, right, bottom);
136383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
136483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    // this extra test is worth it, assuming that most of the time it succeeds
136583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    // since we can avoid writing to storage
136683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) {
136783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds))
136883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            return;
136983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        bounds = &storage;
137083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
137183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
137283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    uint8_t* aa = (uint8_t*)glyph.fImage;
137383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (NULL == aa) {
137483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        aa = (uint8_t*)state.fCache->findImage(glyph);
137583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (NULL == aa) {
137683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            return; // can't rasterize glyph
13778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
137883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
13798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
138083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fRowBytes = glyph.rowBytes();
138183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
138283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fImage = aa;
138383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    state.fBlitter->blitMask(mask, *bounds);
13848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_NoBounder_RgnClip(const SkDraw1Glyph& state,
1387f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                  SkFixed fx, SkFixed fy,
138883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                                  const SkGlyph& glyph) {
1389f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1390f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
13918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
139283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT(!state.fClip->isRect());
139383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT(NULL == state.fBounder);
13948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMask  mask;
13968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
13988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
13998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
140183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
140283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
140383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (!clipper.done()) {
140483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const SkIRect&  cr = clipper.rect();
140583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const uint8_t*  aa = (const uint8_t*)glyph.fImage;
140683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (NULL == aa) {
140783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            aa = (uint8_t*)state.fCache->findImage(glyph);
140883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            if (NULL == aa) {
1409fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com                return;
14108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
141183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        }
141283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
141383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        mask.fRowBytes = glyph.rowBytes();
141483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
141583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        mask.fImage = (uint8_t*)aa;
141683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        do {
141783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            state.fBlitter->blitMask(mask, cr);
141883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            clipper.next();
141983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        } while (!clipper.done());
142083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
14218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_Bounder(const SkDraw1Glyph& state,
1424f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        SkFixed fx, SkFixed fy,
142583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                        const SkGlyph& glyph) {
1426f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1427f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
14288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1429fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
14308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMask  mask;
1431fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
14328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
14338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
1434fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
14358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
14368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
1437fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
143883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (!clipper.done()) {
143983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const SkIRect&  cr = clipper.rect();
144083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const uint8_t*  aa = (const uint8_t*)glyph.fImage;
144183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (NULL == aa) {
144283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            aa = (uint8_t*)state.fCache->findImage(glyph);
144383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            if (NULL == aa) {
144483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                return;
14458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
1446fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        }
1447fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
1448d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        // we need to pass the origin, which we approximate with our
1449d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        // (unadjusted) left,top coordinates (the caller called fixedfloor)
145083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (state.fBounder->doIRectGlyph(cr,
1451d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                                         left - glyph.fLeft,
1452d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                                         top - glyph.fTop, glyph)) {
145383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            mask.fRowBytes = glyph.rowBytes();
145483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
145583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            mask.fImage = (uint8_t*)aa;
145683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            do {
145783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                state.fBlitter->blitMask(mask, cr);
145883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                clipper.next();
145983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            } while (!clipper.done());
146083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        }
146183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
14628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1464045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic void D1G_Bounder_AAClip(const SkDraw1Glyph& state,
1465045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                               SkFixed fx, SkFixed fy,
1466045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                               const SkGlyph& glyph) {
1467045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    int left = SkFixedFloor(fx);
1468045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    int top = SkFixedFloor(fy);
1469045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkIRect bounds;
1470045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    bounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
1471045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1472045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (state.fBounder->doIRectGlyph(bounds, left, top, glyph)) {
1473045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        D1G_NoBounder_RectClip(state, fx, fy, glyph);
1474045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    }
1475045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com}
1476045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1477fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.comstatic bool hasCustomD1GProc(const SkDraw& draw) {
1478fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    return draw.fProcs && draw.fProcs->fD1GProc;
1479fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com}
1480fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com
1481fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.comstatic bool needsRasterTextBlit(const SkDraw& draw) {
1482fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    return !hasCustomD1GProc(draw);
1483fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com}
1484fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com
14858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter,
14868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                      SkGlyphCache* cache) {
14878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDraw = draw;
14882211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    fBounder = draw->fBounder;
14892211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    fBlitter = blitter;
14902211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    fCache = cache;
14918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14921d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (hasCustomD1GProc(*draw)) {
1493045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        // todo: fix this assumption about clips w/ custom
1494045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = draw->fClip;
1495045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClipBounds = fClip->getBounds();
14968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return draw->fProcs->fD1GProc;
14978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1499045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (draw->fRC->isBW()) {
1500045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fAAClip = NULL;
1501045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = &draw->fRC->bwRgn();
1502045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClipBounds = fClip->getBounds();
1503045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (NULL == fBounder) {
1504045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            if (fClip->isRect()) {
1505045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                return D1G_NoBounder_RectClip;
1506045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            } else {
1507045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                return D1G_NoBounder_RgnClip;
1508045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            }
1509045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        } else {
1510045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            return D1G_Bounder;
1511045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        }
1512045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    } else {    // aaclip
1513045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fAAClip = &draw->fRC->aaRgn();
1514045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = NULL;
1515045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClipBounds = fAAClip->getBounds();
1516045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (NULL == fBounder) {
15178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return D1G_NoBounder_RectClip;
15188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1519045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            return D1G_Bounder_AAClip;
15208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
15218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
15228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
15258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawText(const char text[], size_t byteLength,
15278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      SkScalar x, SkScalar y, const SkPaint& paint) const {
15288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
15298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1530f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
15318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1533045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
15348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
15358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
15368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
153736d6edac9f3e63d9a5f499d0076550d08b80eacabsalomon@google.com    // SkScalarRec doesn't currently have a way of representing hairline stroke and
153836d6edac9f3e63d9a5f499d0076550d08b80eacabsalomon@google.com    // will fill if its frame-width is 0.
15398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (/*paint.isLinearText() ||*/
1540fc84359aa920567e72742877a1249f52d076ad35skia.committer@gmail.com        (fMatrix->hasPerspective()) ||
154136d6edac9f3e63d9a5f499d0076550d08b80eacabsalomon@google.com        (0 == paint.getStrokeWidth() && SkPaint::kStroke_Style == paint.getStyle())) {
15428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawText_asPaths(text, byteLength, x, y, paint);
15438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
15448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
15458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
15478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1548f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkMatrix* matrix = fMatrix;
1549f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1550f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkAutoGlyphCache    autoCache(paint, matrix);
15518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkGlyphCache*       cache = autoCache.getCache();
1552a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
15538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform our starting point
15548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
15558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint loc;
1556f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        matrix->mapXY(x, y, &loc);
15578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x = loc.fX;
15588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y = loc.fY;
15598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
15608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need to measure first
15628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getTextAlign() != SkPaint::kLeft_Align) {
15638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkVector    stop;
15648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        measure_text(cache, glyphCacheProc, text, byteLength, &stop);
15668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar    stopX = stop.fX;
15688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar    stopY = stop.fY;
15698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.getTextAlign() == SkPaint::kCenter_Align) {
15718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            stopX = SkScalarHalf(stopX);
15728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            stopY = SkScalarHalf(stopY);
15738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
15748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x -= stopX;
15758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y -= stopY;
15768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1577a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
15788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed fx = SkScalarToFixed(x);
15798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed fy = SkScalarToFixed(y);
15808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* stop = text + byteLength;
15818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1582f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkFixed fxMask = ~0;
1583f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkFixed fyMask = ~0;
15842211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    if (cache->isSubpixel()) {
1585cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*matrix);
1586cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        if (kX_SkAxisAlignment == baseline) {
1587f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fyMask = 0;
1588cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        } else if (kY_SkAxisAlignment == baseline) {
1589f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fxMask = 0;
15908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1591fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
15922211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    // apply bias here to avoid adding 1/2 the sampling frequency in the loop
15932211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fx += SK_FixedHalf >> SkGlyph::kSubBits;
15942211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fy += SK_FixedHalf >> SkGlyph::kSubBits;
15952211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    } else {
15962211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fx += SK_FixedHalf;
15972211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fy += SK_FixedHalf;
15988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
15998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1600045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAAClipBlitter     aaBlitter;
1601045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAutoBlitterChoose blitterChooser;
1602045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter*          blitter = NULL;
16031d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (needsRasterTextBlit(*this)) {
1604045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitterChooser.choose(*fBitmap, *matrix, paint);
1605045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = blitterChooser.get();
1606045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (fRC->isAA()) {
1607045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            aaBlitter.init(blitter, &fRC->aaRgn());
1608045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            blitter = &aaBlitter;
1609045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        }
16101d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
16111d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com
16128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoKern          autokern;
161352c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com    SkDraw1Glyph        d1g;
1614045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkDraw1Glyph::Proc  proc = d1g.init(this, blitter, cache);
16158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (text < stop) {
16172211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
16188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fx += autokern.adjust(glyph);
16208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (glyph.fWidth) {
162252c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com            proc(d1g, fx, fy, glyph);
16238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
16248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fx += glyph.fAdvanceX;
16258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fy += glyph.fAdvanceY;
16268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
16288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// last parameter is interpreted as SkFixed [x, y]
16308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// return the fixed position, which may be rounded or not by the caller
16318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//   e.g. subpixel doesn't round
16328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef void (*AlignProc)(const SkPoint&, const SkGlyph&, SkIPoint*);
16338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void leftAlignProc(const SkPoint& loc, const SkGlyph& glyph,
16358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          SkIPoint* dst) {
16368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX), SkScalarToFixed(loc.fY));
16378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
16388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void centerAlignProc(const SkPoint& loc, const SkGlyph& glyph,
16408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            SkIPoint* dst) {
16418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX) - (glyph.fAdvanceX >> 1),
16428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com             SkScalarToFixed(loc.fY) - (glyph.fAdvanceY >> 1));
16438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
16448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void rightAlignProc(const SkPoint& loc, const SkGlyph& glyph,
16468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkIPoint* dst) {
16478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX) - glyph.fAdvanceX,
16488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com             SkScalarToFixed(loc.fY) - glyph.fAdvanceY);
16498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
16508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic AlignProc pick_align_proc(SkPaint::Align align) {
16528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static const AlignProc gProcs[] = {
16538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        leftAlignProc, centerAlignProc, rightAlignProc
16548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
1655a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((unsigned)align < SK_ARRAY_COUNT(gProcs));
16578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return gProcs[align];
16598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
16608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass TextMapState {
16628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
16638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mutable SkPoint fLoc;
1664a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    TextMapState(const SkMatrix& matrix, SkScalar y)
16668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        : fMatrix(matrix), fProc(matrix.getMapXYProc()), fY(y) {}
16678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef void (*Proc)(const TextMapState&, const SkScalar pos[]);
1669a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Proc pickProc(int scalarsPerPosition);
1671a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
16738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix&     fMatrix;
16748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix::MapXYProc fProc;
16758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            fY; // ignored by MapXYProc
16768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // these are only used by Only... procs
16778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            fScaleX, fTransX, fTransformedY;
16788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapXProc(const TextMapState& state, const SkScalar pos[]) {
16808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fProc(state.fMatrix, *pos, state.fY, &state.fLoc);
16818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1682a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapXYProc(const TextMapState& state, const SkScalar pos[]) {
16848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fProc(state.fMatrix, pos[0], pos[1], &state.fLoc);
16858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1686a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapOnlyScaleXProc(const TextMapState& state,
16888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                  const SkScalar pos[]) {
16898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fLoc.set(SkScalarMul(state.fScaleX, *pos) + state.fTransX,
16908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                       state.fTransformedY);
16918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1692a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapOnlyTransXProc(const TextMapState& state,
16948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                  const SkScalar pos[]) {
16958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fLoc.set(*pos + state.fTransX, state.fTransformedY);
16968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
16988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comTextMapState::Proc TextMapState::pickProc(int scalarsPerPosition) {
17008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1701a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (1 == scalarsPerPosition) {
17038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        unsigned mtype = fMatrix.getType();
17048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (mtype & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
17058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return MapXProc;
17068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
17078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fScaleX = fMatrix.getScaleX();
17088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fTransX = fMatrix.getTranslateX();
17098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fTransformedY = SkScalarMul(fY, fMatrix.getScaleY()) +
17108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            fMatrix.getTranslateY();
17118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return (mtype & SkMatrix::kScale_Mask) ?
17128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        MapOnlyScaleXProc : MapOnlyTransXProc;
17138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
17148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
17158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return MapXYProc;
17168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
17178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
17208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawPosText(const char text[], size_t byteLength,
17228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         const SkScalar pos[], SkScalar constY,
17238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         int scalarsPerPosition, const SkPaint& paint) const {
17248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
17258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
17268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1727f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
17288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1730045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
17318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
17328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
17338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (/*paint.isLinearText() ||*/
17358d430185e08d2067584837a76b7193b803fee7a0tomhudson@google.com        (fMatrix->hasPerspective())) {
17368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // TODO !!!!
17378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//      this->drawText_asPaths(text, byteLength, x, y, paint);
17388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
17398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
17408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1741f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkMatrix* matrix = fMatrix;
1742a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawCacheProc     glyphCacheProc = paint.getDrawCacheProc();
1744f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkAutoGlyphCache    autoCache(paint, matrix);
17458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkGlyphCache*       cache = autoCache.getCache();
1746a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1747045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAAClipBlitterWrapper wrapper;
1748045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAutoBlitterChoose blitterChooser;
1749045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter* blitter = NULL;
17501d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (needsRasterTextBlit(*this)) {
1751045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitterChooser.choose(*fBitmap, *matrix, paint);
1752045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = blitterChooser.get();
1753045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (fRC->isAA()) {
1754045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            wrapper.init(*fRC, blitter);
1755045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            blitter = wrapper.getBlitter();
1756045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        }
17571d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
1758fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
17598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char*        stop = text + byteLength;
17608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    AlignProc          alignProc = pick_align_proc(paint.getTextAlign());
17612211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    SkDraw1Glyph       d1g;
17622211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache);
1763f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    TextMapState       tms(*matrix, constY);
17648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    TextMapState::Proc tmsProc = tms.pickProc(scalarsPerPosition);
17658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17662211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    if (cache->isSubpixel()) {
17678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // maybe we should skip the rounding if linearText is set
1768cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        SkAxisAlignment roundBaseline = SkComputeAxisAlignmentForHText(*matrix);
17698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (SkPaint::kLeft_Align == paint.getTextAlign()) {
17718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            while (text < stop) {
1772a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                tmsProc(tms, pos);
1774a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17752211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com#ifdef SK_DRAW_POS_TEXT_IGNORE_SUBPIXEL_LEFT_ALIGN_FIX
17768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkFixed fx = SkScalarToFixed(tms.fLoc.fX);
17778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkFixed fy = SkScalarToFixed(tms.fLoc.fY);
17782211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com#else
17792211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                SkFixed fx = SkScalarToFixed(tms.fLoc.fX) + (SK_FixedHalf >> SkGlyph::kSubBits);
17802211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                SkFixed fy = SkScalarToFixed(tms.fLoc.fY) + (SK_FixedHalf >> SkGlyph::kSubBits);
17812211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com#endif
1782f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                SkFixed fxMask = ~0;
1783f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                SkFixed fyMask = ~0;
17848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1785cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                if (kX_SkAxisAlignment == roundBaseline) {
1786f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    fyMask = 0;
1787cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                } else if (kY_SkAxisAlignment == roundBaseline) {
1788f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    fxMask = 0;
17898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
1790a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1791f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                const SkGlyph& glyph = glyphCacheProc(cache, &text,
1792f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                                      fx & fxMask, fy & fyMask);
1793a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (glyph.fWidth) {
1795f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    proc(d1g, fx, fy, glyph);
17968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
17978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pos += scalarsPerPosition;
17988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
17998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
18008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            while (text < stop) {
18019330cfe851b14b8985acc14caa3dc5ad20b0b2a8bungeman@google.com                const char* currentText = text;
18028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const SkGlyph* glyph = &glyphCacheProc(cache, &text, 0, 0);
1803a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (glyph->fWidth) {
18058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkDEBUGCODE(SkFixed prevAdvX = glyph->fAdvanceX;)
18068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkDEBUGCODE(SkFixed prevAdvY = glyph->fAdvanceY;)
18078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkFixed fx, fy;
1809f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    SkFixed fxMask = ~0;
1810f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    SkFixed fyMask = ~0;
18118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    tmsProc(tms, pos);
1812a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    {
18148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkIPoint fixedLoc;
18158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        alignProc(tms.fLoc, *glyph, &fixedLoc);
18162211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                        fx = fixedLoc.fX + (SK_FixedHalf >> SkGlyph::kSubBits);
18172211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                        fy = fixedLoc.fY + (SK_FixedHalf >> SkGlyph::kSubBits);
18188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1819cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                        if (kX_SkAxisAlignment == roundBaseline) {
1820f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            fyMask = 0;
1821cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                        } else if (kY_SkAxisAlignment == roundBaseline) {
1822f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            fxMask = 0;
18238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        }
18248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
1825a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // have to call again, now that we've been "aligned"
18279330cfe851b14b8985acc14caa3dc5ad20b0b2a8bungeman@google.com                    glyph = &glyphCacheProc(cache, &currentText,
18289330cfe851b14b8985acc14caa3dc5ad20b0b2a8bungeman@google.com                                            fx & fxMask, fy & fyMask);
18298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // the assumption is that the advance hasn't changed
18308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkASSERT(prevAdvX == glyph->fAdvanceX);
18318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkASSERT(prevAdvY == glyph->fAdvanceY);
1832a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1833f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    proc(d1g, fx, fy, *glyph);
18348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
18358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pos += scalarsPerPosition;
18368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
18378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
18388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // not subpixel
1839aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com        if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1840aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com            while (text < stop) {
1841aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                // the last 2 parameters are ignored
1842aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1843fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
1844aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                if (glyph.fWidth) {
1845aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    tmsProc(tms, pos);
1846a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1847aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    proc(d1g,
1848aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         SkScalarToFixed(tms.fLoc.fX) + SK_FixedHalf,
1849aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         SkScalarToFixed(tms.fLoc.fY) + SK_FixedHalf,
1850aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         glyph);
1851aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                }
1852aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                pos += scalarsPerPosition;
1853aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com            }
1854aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com        } else {
1855aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com            while (text < stop) {
1856aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                // the last 2 parameters are ignored
1857aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1858a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1859aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                if (glyph.fWidth) {
1860aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    tmsProc(tms, pos);
1861a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1862aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    SkIPoint fixedLoc;
1863aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    alignProc(tms.fLoc, glyph, &fixedLoc);
1864aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com
1865aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    proc(d1g,
1866aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         fixedLoc.fX + SK_FixedHalf,
1867aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         fixedLoc.fY + SK_FixedHalf,
1868aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         glyph);
1869aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                }
1870aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                pos += scalarsPerPosition;
18718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
18728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
18738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
18758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if defined _WIN32 && _MSC_VER >= 1300
18778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( pop )
18788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
18798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
18818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPathMeasure.h"
18838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void morphpoints(SkPoint dst[], const SkPoint src[], int count,
18858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkPathMeasure& meas, const SkMatrix& matrix) {
18868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix::MapXYProc proc = matrix.getMapXYProc();
18878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
18898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint pos;
18908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkVector tangent;
18918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        proc(matrix, src[i].fX, src[i].fY, &pos);
18938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sx = pos.fX;
18948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sy = pos.fY;
18958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18968f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com        if (!meas.getPosTan(sx, &pos, &tangent)) {
18978f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com            // set to 0 if the measure failed, so that we just set dst == pos
18988f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com            tangent.set(0, 0);
18998f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com        }
19008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        /*  This is the old way (that explains our approach but is way too slow
19028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkMatrix    matrix;
19038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPoint     pt;
19048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pt.set(sx, sy);
19068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.setSinCos(tangent.fY, tangent.fX);
19078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.preTranslate(-sx, 0);
19088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.postTranslate(pos.fX, pos.fY);
19098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.mapPoints(&dst[i], &pt, 1);
19108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        */
19118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy),
19128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                   pos.fY + SkScalarMul(tangent.fX, sy));
19138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
19148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
19158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  TODO
19178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Need differentially more subdivisions when the follow-path is curvy. Not sure how to
19198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    determine that, but we need it. I guess a cheap answer is let the caller tell us,
19208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
19218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
19228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
19238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      const SkMatrix& matrix) {
19248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath::Iter    iter(src, false);
19258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint         srcP[4], dstP[3];
19268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath::Verb    verb;
19278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
19298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        switch (verb) {
19308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kMove_Verb:
19318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, srcP, 1, meas, matrix);
19328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->moveTo(dstP[0]);
19338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
19348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kLine_Verb:
19358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // turn lines into quads to look bendy
19368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX);
19378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY);
19388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, srcP, 2, meas, matrix);
19398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->quadTo(dstP[0], dstP[1]);
19408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
19418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kQuad_Verb:
19428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, &srcP[1], 2, meas, matrix);
19438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->quadTo(dstP[0], dstP[1]);
19448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
19458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kCubic_Verb:
19468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, &srcP[1], 3, meas, matrix);
19478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->cubicTo(dstP[0], dstP[1], dstP[2]);
19488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
19498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kClose_Verb:
19508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->close();
19518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
19528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            default:
19530c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com                SkDEBUGFAIL("unknown verb");
19548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
19558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
19568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
19578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
19588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawTextOnPath(const char text[], size_t byteLength,
19608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPath& follow, const SkMatrix* matrix,
19618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPaint& paint) const {
19628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
19638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1965045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
19668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
19678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
19688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1969166e653f67f3fffc3846184a25ce45ab083f07a2djsollen@google.com    SkTextToPathIter    iter(text, byteLength, paint, true);
19708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPathMeasure       meas(follow, false);
19718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            hOffset = 0;
19728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need to measure first
19748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getTextAlign() != SkPaint::kLeft_Align) {
19758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar pathLen = meas.getLength();
19768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.getTextAlign() == SkPaint::kCenter_Align) {
19778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathLen = SkScalarHalf(pathLen);
19788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
19798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hOffset += pathLen;
19808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
19818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPath*   iterPath;
19838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar        xpos;
19848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        scaledMatrix;
19858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar        scale = iter.getPathScale();
19868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    scaledMatrix.setScale(scale, scale);
1988a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
19897b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com    while (iter.next(&iterPath, &xpos)) {
19907b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com        if (iterPath) {
19917b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            SkPath      tmp;
19927b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            SkMatrix    m(scaledMatrix);
19938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19947b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            m.postTranslate(xpos + hOffset, 0);
19957b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            if (matrix) {
19967b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                m.postConcat(*matrix);
19977b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            }
19987b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            morphpath(&tmp, *iterPath, meas, m);
19997b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            if (fDevice) {
20007b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                fDevice->drawPath(*this, tmp, iter.getPaint(), NULL, true);
20017b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            } else {
20027b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                this->drawPath(tmp, iter.getPaint(), NULL, true);
20037b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            }
2004f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
20058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
200856c69773aea56c6c6bd47bc7e7970dd081205184djsollen@google.com#ifdef SK_BUILD_FOR_ANDROID
2009cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.comvoid SkDraw::drawPosTextOnPath(const char text[], size_t byteLength,
2010cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                               const SkPoint pos[], const SkPaint& paint,
2011cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                               const SkPath& path, const SkMatrix* matrix) const {
2012cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // nothing to draw
2013045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
2014cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        return;
2015cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
2016cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2017cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkMatrix scaledMatrix;
2018cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkPathMeasure meas(path, false);
2019cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2020cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkMeasureCacheProc glyphCacheProc = paint.getMeasureCacheProc(
2021cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            SkPaint::kForward_TextBufferDirection, true);
2022cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2023cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Copied (modified) from SkTextToPathIter constructor to setup paint
2024cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkPaint tempPaint(paint);
2025cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2026cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    tempPaint.setLinearText(true);
2027cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    tempPaint.setMaskFilter(NULL); // don't want this affecting our path-cache lookup
2028cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2029cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    if (tempPaint.getPathEffect() == NULL && !(tempPaint.getStrokeWidth() > 0
2030cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            && tempPaint.getStyle() != SkPaint::kFill_Style)) {
2031cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        tempPaint.setStyle(SkPaint::kFill_Style);
2032cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        tempPaint.setPathEffect(NULL);
2033cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
2034cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // End copied from SkTextToPathIter constructor
2035cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2036cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // detach cache
2037cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkGlyphCache* cache = tempPaint.detachCache(NULL);
2038cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2039cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Must set scale, even if 1
2040cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkScalar scale = SK_Scalar1;
2041cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    scaledMatrix.setScale(scale, scale);
2042cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2043cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Loop over all glyph ids
2044cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    for (const char* stop = text + byteLength; text < stop; pos++) {
2045cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2046cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        const SkGlyph& glyph = glyphCacheProc(cache, &text);
2047cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        SkPath tmp;
2048cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2049cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        const SkPath* glyphPath = cache->findPath(glyph);
2050cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        if (glyphPath == NULL) {
2051cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            continue;
2052cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        }
2053cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2054cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        SkMatrix m(scaledMatrix);
2055cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        m.postTranslate(pos->fX, 0);
2056cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2057cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        if (matrix) {
2058cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            m.postConcat(*matrix);
2059cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        }
2060cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2061cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        morphpath(&tmp, *glyphPath, meas, m);
2062cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        this->drawPath(tmp, tempPaint);
2063cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2064cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
2065cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2066cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // re-attach cache
2067cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkGlyphCache::AttachCache(cache);
2068cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com}
2069cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com#endif
2070cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
20718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
20728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct VertState {
20748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int f0, f1, f2;
20758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState(int vCount, const uint16_t indices[], int indexCount)
20778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            : fIndices(indices) {
20788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCurrIndex = 0;
20798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (indices) {
20808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fCount = indexCount;
20818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
20828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fCount = vCount;
20838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
20848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2085a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
2086a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    typedef bool (*Proc)(VertState*);
20878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Proc chooseProc(SkCanvas::VertexMode mode);
20888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
20908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int             fCount;
20918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int             fCurrIndex;
20928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* fIndices;
2093a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
20948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool Triangles(VertState*);
20958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TrianglesX(VertState*);
20968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleStrip(VertState*);
20978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleStripX(VertState*);
20988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleFan(VertState*);
20998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleFanX(VertState*);
21008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
21018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::Triangles(VertState* state) {
21038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
21048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
21058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
21068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = index + 0;
21088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = index + 1;
21098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
21108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 3;
21118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
21128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TrianglesX(VertState* state) {
21158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
21168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
21178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
21188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
21198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = indices[index + 0];
21218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = indices[index + 1];
21228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
21238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 3;
21248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
21258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleStrip(VertState* state) {
21288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
21298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
21308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
21318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
21338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index & 1) {
21348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = index + 1;
21358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = index + 0;
21368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
21378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = index + 0;
21388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = index + 1;
21398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
21418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
21428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleStripX(VertState* state) {
21458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
21468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
21478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
21488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
21498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
21518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index & 1) {
21528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = indices[index + 1];
21538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = indices[index + 0];
21548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
21558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = indices[index + 0];
21568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = indices[index + 1];
21578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
21598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
21608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleFan(VertState* state) {
21638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
21648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
21658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
21668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = 0;
21688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = index + 1;
21698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
21708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
21718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
21728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleFanX(VertState* state) {
21758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
21768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
21778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
21788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
21798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = indices[0];
21818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = indices[index + 1];
21828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
21838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
21848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
21858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comVertState::Proc VertState::chooseProc(SkCanvas::VertexMode mode) {
21888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (mode) {
21898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangles_VertexMode:
21908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TrianglesX : Triangles;
21918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangleStrip_VertexMode:
21928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TriangleStripX : TriangleStrip;
21938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangleFan_VertexMode:
21948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TriangleFanX : TriangleFan;
21958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
21968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return NULL;
21978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2200045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comtypedef void (*HairProc)(const SkPoint&, const SkPoint&, const SkRasterClip&,
22018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         SkBlitter*);
22028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic HairProc ChooseHairProc(bool doAntiAlias) {
22048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
22058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool texture_to_matrix(const VertState& state, const SkPoint verts[],
22088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPoint texs[], SkMatrix* matrix) {
22098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint src[3], dst[3];
2210a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[0] = texs[state.f0];
22128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[1] = texs[state.f1];
22138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[2] = texs[state.f2];
22148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[0] = verts[state.f0];
22158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[1] = verts[state.f1];
22168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[2] = verts[state.f2];
22178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return matrix->setPolyToPoly(src, dst, 3);
22188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkTriColorShader : public SkShader {
22218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
22228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader() {}
22238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
2225a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count);
2227a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
2228ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTriColorShader)
2229ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com
22308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
22318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader(SkFlattenableReadBuffer& buffer) : SkShader(buffer) {}
2232a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
22348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    fDstToUnit;
22358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPMColor   fColors[3];
2236a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkShader INHERITED;
22388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
22398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkTriColorShader::setup(const SkPoint pts[], const SkColor colors[],
22418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             int index0, int index1, int index2) {
2242a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[0] = SkPreMultiplyColor(colors[index0]);
22448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[1] = SkPreMultiplyColor(colors[index1]);
22458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[2] = SkPreMultiplyColor(colors[index2]);
2246a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix m, im;
22488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.reset();
22498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(0, pts[index1].fX - pts[index0].fX);
22508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(1, pts[index2].fX - pts[index0].fX);
22518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(2, pts[index0].fX);
22528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(3, pts[index1].fY - pts[index0].fY);
22538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(4, pts[index2].fY - pts[index0].fY);
22548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(5, pts[index0].fY);
22558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (!m.invert(&im)) {
22568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fDstToUnit.setConcat(im, this->getTotalInverse());
22598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
2262689411a87ca7d19abe04b5c9baff26bd96b2123ereed@android.com#include "SkComposeShader.h"
22638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic int ScalarTo256(SkScalar v) {
22658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int scale = SkScalarToFixed(v) >> 8;
22668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (scale < 0) {
22678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        scale = 0;
22688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (scale > 255) {
22708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        scale = 255;
22718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkAlpha255To256(scale);
22738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkTriColorShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
22768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint src;
2277a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
22798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
22808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x += 1;
2281a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale1 = ScalarTo256(src.fX);
22838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale2 = ScalarTo256(src.fY);
22848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale0 = 256 - scale1 - scale2;
22858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (scale0 < 0) {
22868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (scale1 > scale2) {
22878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                scale2 = 256 - scale1;
22888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } else {
22898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                scale1 = 256 - scale2;
22908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
22918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            scale0 = 0;
22928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2293a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        dstC[i] = SkAlphaMulQ(fColors[0], scale0) +
22958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAlphaMulQ(fColors[1], scale1) +
22968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAlphaMulQ(fColors[2], scale2);
22978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
23018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPoint vertices[], const SkPoint textures[],
23028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkColor colors[], SkXfermode* xmode,
23038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const uint16_t indices[], int indexCount,
23048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint& paint) const {
23058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(0 == count || NULL != vertices);
2306a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // abort early if there is nothing to draw
2308045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
23098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
23108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2311a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform out vertices into device coordinates
23138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoSTMalloc<16, SkPoint> storage(count);
23148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint* devVerts = storage.get();
23158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMatrix->mapPoints(devVerts, vertices, count);
2316a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder) {
23188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect bounds;
23198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.set(devVerts, count);
23208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!fBounder->doRect(bounds, paint)) {
23218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
23228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
23238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2324a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*
23268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        We can draw the vertices in 1 of 4 ways:
23278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - solid color (no shader/texture[], no colors[])
23298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - just colors (no shader/texture[], has colors[])
23308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - just texture (has shader/texture[], no colors[])
23318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - colors * texture (has shader/texture[], has colors[])
2332a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Thus for texture drawing, we need both texture[] and a shader.
23348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
23358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader triShader; // must be above declaration of p
23378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint p(paint);
23388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkShader* shader = p.getShader();
23408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == shader) {
23418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if we have no shader, we ignore the texture coordinates
23428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        textures = NULL;
23438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else if (NULL == textures) {
23448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if we don't have texture coordinates, ignore the shader
23458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        p.setShader(NULL);
23468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        shader = NULL;
23478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // setup the custom shader (if needed)
23508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL != colors) {
23518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL == textures) {
23528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // just colors (no texture)
23538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            p.setShader(&triShader);
23548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
23558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // colors * texture
23568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(shader);
23578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bool releaseMode = false;
23588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL == xmode) {
2359845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com                xmode = SkXfermode::Create(SkXfermode::kMultiply_Mode);
23608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                releaseMode = true;
23618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
23628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkShader* compose = SkNEW_ARGS(SkComposeShader,
23638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                           (&triShader, shader, xmode));
23648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            p.setShader(compose)->unref();
23658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (releaseMode) {
23668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                xmode->unref();
23678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
23688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
23698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
23728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // setup our state and function pointer for iterating triangles
23738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState       state(count, indices, indexCount);
23748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState::Proc vertProc = state.chooseProc(vmode);
2375a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL != textures || NULL != colors) {
2377f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        SkMatrix  tempM;
2378f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        SkMatrix  savedLocalM;
2379f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        if (shader) {
2380f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com            savedLocalM = shader->getLocalMatrix();
2381f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        }
2382a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != colors) {
23848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!triShader.setContext(*fBitmap, p, *fMatrix)) {
23858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                colors = NULL;
23868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
23878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2388a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while (vertProc(&state)) {
23908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL != textures) {
23918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (texture_to_matrix(state, vertices, textures, &tempM)) {
2392f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com                    tempM.postConcat(savedLocalM);
23938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    shader->setLocalMatrix(tempM);
23948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // need to recal setContext since we changed the local matrix
23958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (!shader->setContext(*fBitmap, p, *fMatrix)) {
23968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        continue;
23978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
23988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
23998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
24008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL != colors) {
24018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (!triShader.setup(vertices, colors,
24028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                     state.f0, state.f1, state.f2)) {
24038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    continue;
24048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
24058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
2406045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
2407045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkPoint tmp[] = {
2408045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
2409045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            };
2410045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkScan::FillTriangle(tmp, *fRC, blitter.get());
24118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
24128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // now restore the shader's original local matrix
24138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != shader) {
2414f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com            shader->setLocalMatrix(savedLocalM);
24158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
24168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
24178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // no colors[] and no texture
24188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        HairProc hairProc = ChooseHairProc(paint.isAntiAlias());
2419045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        const SkRasterClip& clip = *fRC;
24208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while (vertProc(&state)) {
2421045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get());
2422045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get());
2423045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get());
24248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
24258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
24278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24280a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
24290a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
24308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
24328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2433f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comvoid SkDraw::validate() const {
24348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fBitmap != NULL);
24358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fMatrix != NULL);
24368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fClip != NULL);
2437045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkASSERT(fRC != NULL);
24388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2439045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkIRect&  cr = fRC->getBounds();
24408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect         br;
24418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2442f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    br.set(0, 0, fBitmap->width(), fBitmap->height());
24438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(cr.isEmpty() || br.contains(cr));
24448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
24458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
24478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24480a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
24490a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com
24500a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.comSkBounder::SkBounder() {
24510a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com    // initialize up front. This gets reset by SkCanvas before each draw call.
24520a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com    fClip = &SkRegion::GetEmptyRegion();
24530a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com}
24548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doIRect(const SkIRect& r) {
24568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    rr;
24578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return rr.intersect(fClip->getBounds(), r) && this->onIRect(rr);
24588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
24598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2460d055c1fde2128514167b315f4d104b177e04a3dereed@android.com// TODO: change the prototype to take fixed, and update the callers
2461d055c1fde2128514167b315f4d104b177e04a3dereed@android.combool SkBounder::doIRectGlyph(const SkIRect& r, int x, int y,
2462d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                             const SkGlyph& glyph) {
2463474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com    SkIRect    rr;
2464d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    if (!rr.intersect(fClip->getBounds(), r)) {
2465d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        return false;
2466d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    }
2467d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    GlyphRec rec;
2468d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fLSB.set(SkIntToFixed(x), SkIntToFixed(y));
2469d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fRSB.set(rec.fLSB.fX + glyph.fAdvanceX,
2470d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                 rec.fLSB.fY + glyph.fAdvanceY);
2471d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fGlyphID = glyph.getGlyphID();
2472d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fFlags = 0;
2473d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    return this->onIRectGlyph(rr, rec);
2474474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com}
2475474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com
24768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doHairline(const SkPoint& pt0, const SkPoint& pt1,
24778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           const SkPaint& paint) {
24788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect     r;
24798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    v0, v1;
24808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v0 = pt0.fX;
24828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v1 = pt1.fX;
24838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (v0 > v1) {
24848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkTSwap<SkScalar>(v0, v1);
24858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fLeft     = SkScalarFloor(v0);
24878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fRight    = SkScalarCeil(v1);
24888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v0 = pt0.fY;
24908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v1 = pt1.fY;
24918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (v0 > v1) {
24928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkTSwap<SkScalar>(v0, v1);
24938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fTop      = SkScalarFloor(v0);
24958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fBottom   = SkScalarCeil(v1);
24968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.isAntiAlias()) {
24988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(-1, -1);
24998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
25008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
25018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doRect(const SkRect& rect, const SkPaint& paint) {
25048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    r;
25058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getStyle() == SkPaint::kFill_Style) {
25078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rect.round(&r);
25088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
25098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int rad = -1;
25108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rect.roundOut(&r);
25118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.isAntiAlias()) {
25128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            rad = -2;
25138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
25148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(rad, rad);
25158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
25168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
25178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doPath(const SkPath& path, const SkPaint& paint, bool doFill) {
2520d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    SkIRect       r;
2521d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    const SkRect& bounds = path.getBounds();
25228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (doFill) {
25248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.round(&r);
25258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // hairline
25268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.roundOut(&r);
25278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
25288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.isAntiAlias()) {
25308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(-1, -1);
25318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
25328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
25338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkBounder::commit() {
25368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // override in subclass
25378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////////////////////////
25408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPath.h"
25428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDraw.h"
25438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRegion.h"
25448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBlitter.h"
25458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds,
25478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkMaskFilter* filter, const SkMatrix* filterMatrix,
25488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkIRect* bounds) {
25498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (devPath.isEmpty()) {
25508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
25518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
25528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //  init our bounds from the path
25548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
2555d252db03d9650013b545ef9781fe993c07f8f314reed@android.com        SkRect pathBounds = devPath.getBounds();
25568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathBounds.inset(-SK_ScalarHalf, -SK_ScalarHalf);
25578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathBounds.roundOut(bounds);
25588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2559a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
25606db75fc2c393ba86a3f533597a8bcd348477e79ctomhudson@google.com    SkIPoint margin = SkIPoint::Make(0, 0);
25618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (filter) {
25628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(filterMatrix);
2563a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
25645af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        SkMask srcM, dstM;
2565a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
25668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fBounds = *bounds;
25678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fFormat = SkMask::kA8_Format;
25688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fImage = NULL;
25698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
25708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return false;
25718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
25728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2573a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
25745af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com    // (possibly) trim the bounds to reflect the clip
25758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // (plus whatever slop the filter needs)
25765af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com    if (clipBounds) {
25775af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        SkIRect tmp = *clipBounds;
25783555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // Ugh. Guard against gigantic margins from wacky filters. Without this
25793555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // check we can request arbitrary amounts of slop beyond our visible
25803555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // clip, and bring down the renderer (at least on finite RAM machines
25813555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // like handsets, etc.). Need to balance this invented value between
25823555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // quality of large filters like blurs, and the corresponding memory
25833555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // requests.
25843555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        static const int MAX_MARGIN = 128;
25853555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        tmp.inset(-SkMin32(margin.fX, MAX_MARGIN),
25863555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com                  -SkMin32(margin.fY, MAX_MARGIN));
25875af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        if (!bounds->intersect(tmp)) {
25885af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com            return false;
25895af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        }
25908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
25918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
25938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25952ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.orgstatic void draw_into_mask(const SkMask& mask, const SkPath& devPath,
25962ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                           SkPaint::Style style) {
2597045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBitmap        bm;
2598045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkDraw          draw;
2599045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkRasterClip    clip;
2600045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkMatrix        matrix;
2601045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkPaint         paint;
26028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), mask.fBounds.height(), mask.fRowBytes);
26048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bm.setPixels(mask.fImage);
26058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2606045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height()));
26078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
26088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        -SkIntToScalar(mask.fBounds.fTop));
26098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fBitmap    = &bm;
2611045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    draw.fRC        = &clip;
2612045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    draw.fClip      = &clip.bwRgn();
26138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix    = &matrix;
26148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fBounder   = NULL;
26158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    paint.setAntiAlias(true);
26162ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org    paint.setStyle(style);
26178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.drawPath(devPath, paint);
26188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
26198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
26218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkMaskFilter* filter, const SkMatrix* filterMatrix,
26222ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                        SkMask* mask, SkMask::CreateMode mode,
26232ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                        SkPaint::Style style) {
26248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kJustRenderImage_CreateMode != mode) {
26258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds))
26268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return false;
26278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2628a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
26298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
26308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask->fFormat = SkMask::kA8_Format;
26318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask->fRowBytes = mask->fBounds.width();
2632543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        size_t size = mask->computeImageSize();
2633543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        if (0 == size) {
2634543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            // we're too big to allocate the mask, abort
2635543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            return false;
2636543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        }
2637543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        mask->fImage = SkMask::AllocImage(size);
26388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        memset(mask->fImage, 0, mask->computeImageSize());
26398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kJustComputeBounds_CreateMode != mode) {
26422ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org        draw_into_mask(*mask, devPath, style);
26438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2644a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
26458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
26468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2647