SkDraw.cpp revision f38f93c118b71e414c33fe2619ed1d7b1267a7a1
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDraw.h"
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBlitter.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBounder.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDevice.h"
152211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com#include "SkFixed.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkMaskFilter.h"
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPaint.h"
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPathEffect.h"
19045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com#include "SkRasterClip.h"
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRasterizer.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkScan.h"
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkShader.h"
2376f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#include "SkString.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"
32ae57358447bac678e3fc458fa2857a349a6a7081reed@google.com#include "SkMatrixUtils.h"
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//#define TRACE_BITMAP_DRAWS
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define kBlitterStorageLongCount    (sizeof(SkBitmapProcShader) >> 2)
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
38fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com/** Helper for allocating small blitters on the stack.
39fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com */
4040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.comclass SkAutoBlitterChoose : SkNoncopyable {
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
421d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    SkAutoBlitterChoose() {
431d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com        fBlitter = NULL;
441d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose(const SkBitmap& device, const SkMatrix& matrix,
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        const SkPaint& paint) {
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fBlitter = SkBlitter::Choose(device, matrix, paint,
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                     fStorage, sizeof(fStorage));
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
50fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoBlitterChoose();
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*  operator->() { return fBlitter; }
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*  get() const { return fBlitter; }
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
561d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    void choose(const SkBitmap& device, const SkMatrix& matrix,
571d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com                const SkPaint& paint) {
581d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com        SkASSERT(!fBlitter);
591d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com        fBlitter = SkBlitter::Choose(device, matrix, paint,
601d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com                                     fStorage, sizeof(fStorage));
611d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
621d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*  fBlitter;
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t    fStorage[kBlitterStorageLongCount];
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkAutoBlitterChoose::~SkAutoBlitterChoose() {
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if ((void*)fBlitter == (void*)fStorage) {
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fBlitter->~SkBlitter();
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDELETE(fBlitter);
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com/**
7740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com *  Since we are providing the storage for the shader (to avoid the perf cost
7840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com *  of calling new) we insist that in our destructor we can account for all
7940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com *  owners of the shader.
8040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com */
8140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.comclass SkAutoBitmapShaderInstall : SkNoncopyable {
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
8340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkAutoBitmapShaderInstall(const SkBitmap& src, const SkPaint& paint)
8440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            : fPaint(paint) /* makes a copy of the paint */ {
8540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        fPaint.setShader(SkShader::CreateBitmapShader(src,
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           fStorage, sizeof(fStorage)));
8840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        // we deliberately left the shader with an owner-count of 2
8940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkASSERT(2 == fPaint.getShader()->getRefCnt());
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9182065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoBitmapShaderInstall() {
9340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkShader* shader = fPaint.getShader();
9440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        // since we manually destroy shader, we insist that owners == 2
9540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkASSERT(2 == shader->getRefCnt());
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        fPaint.setShader(NULL); // unref the shader by 1
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        // now destroy to take care of the 2nd owner-count
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if ((void*)shader == (void*)fStorage) {
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shader->~SkShader();
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkDELETE(shader);
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10682065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
10740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    // return the new paint that has the shader applied
10840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    const SkPaint& paintWithShader() const { return fPaint; }
10982065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
11140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkPaint     fPaint; // copy of caller's paint (which we then modify)
11240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    uint32_t    fStorage[kBlitterStorageLongCount];
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
117f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comSkDraw::SkDraw() {
118f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    sk_bzero(this, sizeof(*this));
119f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
120f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDraw::SkDraw(const SkDraw& src) {
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memcpy(this, &src, sizeof(*this));
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data);
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) {
1304516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com    sk_bzero(pixels, bytes);
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {}
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    sk_memset32((uint32_t*)pixels, data, bytes >> 2);
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    sk_memset16((uint16_t*)pixels, data, bytes >> 1);
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memset(pixels, data, bytes);
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic BitmapXferProc ChooseBitmapXferProc(const SkBitmap& bitmap,
1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                           const SkPaint& paint,
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                           uint32_t* data) {
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // todo: we can apply colorfilter up front if no shader, so we wouldn't
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need to abort this fastpath
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getShader() || paint.getColorFilter()) {
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return NULL;
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
156845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com    SkXfermode::Mode mode;
157be2aa2aa1f8bf73d974bdd9438fc741bbf0cfbe6mike@reedtribe.org    if (!SkXfermode::AsMode(paint.getXfermode(), &mode)) {
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return NULL;
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
160a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkColor color = paint.getColor();
162a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // collaps modes based on color...
164845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com    if (SkXfermode::kSrcOver_Mode == mode) {
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        unsigned alpha = SkColorGetA(color);
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (0 == alpha) {
167845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com            mode = SkXfermode::kDst_Mode;
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else if (0xFF == alpha) {
169845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com            mode = SkXfermode::kSrc_Mode;
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
172a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (mode) {
174845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com        case SkXfermode::kClear_Mode:
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//            SkDebugf("--- D_Clear_BitmapXferProc\n");
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return D_Clear_BitmapXferProc;  // ignore data
177845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com        case SkXfermode::kDst_Mode:
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//            SkDebugf("--- D_Dst_BitmapXferProc\n");
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return D_Dst_BitmapXferProc;    // ignore data
180845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com        case SkXfermode::kSrc_Mode: {
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            /*
182a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com                should I worry about dithering for the lower depths?
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            */
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPMColor pmc = SkPreMultiplyColor(color);
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            switch (bitmap.config()) {
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kARGB_8888_Config:
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = pmc;
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- D32_Src_BitmapXferProc\n");
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return D32_Src_BitmapXferProc;
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kARGB_4444_Config:
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = SkPixel32ToPixel4444(pmc);
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- D16_Src_BitmapXferProc\n");
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return D16_Src_BitmapXferProc;
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kRGB_565_Config:
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = SkPixel32ToPixel16(pmc);
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- D16_Src_BitmapXferProc\n");
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return D16_Src_BitmapXferProc;
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kA8_Config:
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = SkGetPackedA32(pmc);
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- DA8_Src_BitmapXferProc\n");
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return DA8_Src_BitmapXferProc;
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                default:
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    break;
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return NULL;
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void CallBitmapXferProc(const SkBitmap& bitmap, const SkIRect& rect,
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                               BitmapXferProc proc, uint32_t procData) {
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int shiftPerPixel;
2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (bitmap.config()) {
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kARGB_8888_Config:
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shiftPerPixel = 2;
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kARGB_4444_Config:
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kRGB_565_Config:
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shiftPerPixel = 1;
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kA8_Config:
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shiftPerPixel = 0;
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
2360c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com            SkDEBUGFAIL("Can't use xferproc on this config");
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint8_t* pixels = (uint8_t*)bitmap.getPixels();
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(pixels);
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const size_t rowBytes = bitmap.rowBytes();
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const int widthBytes = rect.width() << shiftPerPixel;
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // skip down to the first scanline and X position
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel);
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int scans = rect.height() - 1; scans >= 0; --scans) {
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        proc(pixels, widthBytes, procData);
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pixels += rowBytes;
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawPaint(const SkPaint& paint) const {
254f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
256045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty()) {
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    devRect;
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    devRect.set(0, 0, fBitmap->width(), fBitmap->height());
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder && !fBounder->doIRect(devRect)) {
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
265a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
266045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isBW()) {
267045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        /*  If we don't have a shader (i.e. we're just a solid color) we may
268045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            be faster to operate directly on the device bitmap, rather than invoking
269045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            a blitter. Esp. true for xfermodes, which require a colorshader to be
270045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            present, which is just redundant work. Since we're drawing everywhere
271045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            in the clip, we don't have to worry about antialiasing.
272045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        */
273045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        uint32_t procData = 0;  // to avoid the warning
274045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        BitmapXferProc proc = ChooseBitmapXferProc(*fBitmap, paint, &procData);
275045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (proc) {
276045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            if (D_Dst_BitmapXferProc == proc) { // nothing to do
277045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                return;
278045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            }
279a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
280045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkRegion::Iterator iter(fRC->bwRgn());
281045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            while (!iter.done()) {
282045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                CallBitmapXferProc(*fBitmap, iter.rect(), proc, procData);
283045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                iter.next();
284045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            }
285045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            return;
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
288045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
289045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    // normal case: use a blitter
290045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
291045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkScan::FillIRect(devRect, *fRC, blitter.get());
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct PtProcRec {
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkCanvas::PointMode fMode;
2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPaint*  fPaint;
2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkRegion* fClip;
300045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkRasterClip* fRC;
301a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // computed values
3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed fRadius;
304a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count,
3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         SkBlitter*);
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix,
309045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com              const SkRasterClip*);
310045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    Proc chooseProc(SkBlitter** blitter);
311045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
312045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comprivate:
313045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAAClipBlitterWrapper fWrapper;
3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                 int count, SkBlitter* blitter) {
3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(rec.fClip->isRect());
3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkIRect& r = rec.fClip->getBounds();
320a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3222d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int x = SkScalarFloorToInt(devPts[i].fX);
3232d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int y = SkScalarFloorToInt(devPts[i].fY);
3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (r.contains(x, y)) {
3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            blitter->blitH(x, y, 1);
3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_pt_rect_16_hair_proc(const PtProcRec& rec,
3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                    const SkPoint devPts[], int count,
3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                    SkBlitter* blitter) {
333045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkASSERT(rec.fRC->isRect());
334045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkIRect& r = rec.fRC->getBounds();
3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t value;
3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(bitmap);
338c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint16_t* addr = bitmap->getAddr16(0, 0);
3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int rb = bitmap->rowBytes();
341c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3432d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int x = SkScalarFloorToInt(devPts[i].fX);
3442d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int y = SkScalarFloorToInt(devPts[i].fY);
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (r.contains(x, y)) {
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value);
3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3512d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.comstatic void bw_pt_rect_32_hair_proc(const PtProcRec& rec,
3522d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com                                    const SkPoint devPts[], int count,
3532d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com                                    SkBlitter* blitter) {
3542d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    SkASSERT(rec.fRC->isRect());
3552d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    const SkIRect& r = rec.fRC->getBounds();
3562d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    uint32_t value;
3572d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
3582d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    SkASSERT(bitmap);
359c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
3602d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    SkPMColor* addr = bitmap->getAddr32(0, 0);
3612d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    int rb = bitmap->rowBytes();
362c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
3632d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    for (int i = 0; i < count; i++) {
3642d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int x = SkScalarFloorToInt(devPts[i].fX);
3652d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        int y = SkScalarFloorToInt(devPts[i].fY);
3662d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        if (r.contains(x, y)) {
3672d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com            ((SkPMColor*)((char*)addr + y * rb))[x] = value;
3682d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        }
3692d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    }
3702d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com}
3712d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com
3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            int count, SkBlitter* blitter) {
3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int x = SkScalarFloor(devPts[i].fX);
3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int y = SkScalarFloor(devPts[i].fY);
3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (rec.fClip->contains(x, y)) {
3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            blitter->blitH(x, y, 1);
3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i += 2) {
386045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count - 1; i++) {
393045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// aa versions
3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i += 2) {
402045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count - 1; i++) {
409045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// square procs (strokeWidth > 0 but matrix is square-scale (sx == sy)
4148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[],
4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           int count, SkBlitter* blitter) {
4178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkFixed radius = rec.fRadius;
4188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
4198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed x = SkScalarToFixed(devPts[i].fX);
4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed y = SkScalarToFixed(devPts[i].fY);
421c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkXRect r;
4238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fLeft = x - radius;
4248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fTop = y - radius;
4258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fRight = x + radius;
4268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fBottom = y + radius;
427c3d7d90973528527131c72549b10c2a21300e0acskia.committer@gmail.com
428045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::FillXRect(r, *rec.fRC, blitter);
4298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[],
4338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           int count, SkBlitter* blitter) {
4348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkFixed radius = rec.fRadius;
4358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
4368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed x = SkScalarToFixed(devPts[i].fX);
4378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed y = SkScalarToFixed(devPts[i].fY);
438a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
4398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkXRect r;
4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fLeft = x - radius;
4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fTop = y - radius;
4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fRight = x + radius;
4438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fBottom = y + radius;
444a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
445045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        SkScan::AntiFillXRect(r, *rec.fRC, blitter);
4468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
449b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com// If this guy returns true, then chooseProc() must return a valid proc
4508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint,
451045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                     const SkMatrix* matrix, const SkRasterClip* rc) {
4528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getPathEffect()) {
4538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
4548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar width = paint.getStrokeWidth();
4568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (0 == width) {
4578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fMode = mode;
4588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPaint = &paint;
459045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = NULL;
460045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fRC = rc;
4612d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        fRadius = SK_FixedHalf;
4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return true;
4638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
464b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com    if (paint.getStrokeCap() != SkPaint::kRound_Cap &&
465b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com            matrix->rectStaysRect() && SkCanvas::kPoints_PointMode == mode) {
4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sx = matrix->get(SkMatrix::kMScaleX);
4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sy = matrix->get(SkMatrix::kMScaleY);
4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (SkScalarNearlyZero(sx - sy)) {
4698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (sx < 0) {
4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                sx = -sx;
4718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
4728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fMode = mode;
4748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fPaint = &paint;
475045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            fClip = NULL;
476045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            fRC = rc;
4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1;
4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return true;
4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return false;
4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
484045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comPtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) {
485b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com    Proc proc = NULL;
486a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
487045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter* blitter = *blitterPtr;
488045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isBW()) {
489045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = &fRC->bwRgn();
490045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    } else {
491045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fWrapper.init(*fRC, blitter);
492045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = &fWrapper.getRgn();
493045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = fWrapper.getBlitter();
494045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        *blitterPtr = blitter;
495045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    }
496045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // for our arrays
4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(0 == SkCanvas::kPoints_PointMode);
4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == SkCanvas::kLines_PointMode);
5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(2 == SkCanvas::kPolygon_PointMode);
5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode);
5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5032d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    if (fPaint->isAntiAlias()) {
5042d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        if (0 == fPaint->getStrokeWidth()) {
5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            static const Proc gAAProcs[] = {
5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            };
5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc = gAAProcs[fMode];
5092d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) {
5102d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com            SkASSERT(SkCanvas::kPoints_PointMode == fMode);
5112d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com            proc = aa_square_proc;
5122d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        }
5132d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com    } else {    // BW
5142d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com        if (fRadius <= SK_FixedHalf) {    // small radii and hairline
5158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) {
5168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                uint32_t value;
5178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const SkBitmap* bm = blitter->justAnOpaqueColor(&value);
5182d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com                if (bm && SkBitmap::kRGB_565_Config == bm->config()) {
5198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    proc = bw_pt_rect_16_hair_proc;
5202d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com                } else if (bm && SkBitmap::kARGB_8888_Config == bm->config()) {
5212d47a21b27035798d53de4d5cb8ac7c03534ab27reed@google.com                    proc = bw_pt_rect_32_hair_proc;
5228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                } else {
5238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    proc = bw_pt_rect_hair_proc;
5248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
5258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } else {
5268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                static Proc gBWProcs[] = {
5278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc
5288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                };
5298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                proc = gBWProcs[fMode];
5308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
5318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
5328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc = bw_square_proc;
5338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
5348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return proc;
5368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool bounder_points(SkBounder* bounder, SkCanvas::PointMode mode,
5398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           size_t count, const SkPoint pts[],
5408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           const SkPaint& paint, const SkMatrix& matrix) {
5418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect ibounds;
5428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect bounds;
5438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar inset = paint.getStrokeWidth();
5448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.set(pts, count);
5468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.inset(-inset, -inset);
5478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.mapRect(&bounds);
5488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.roundOut(&ibounds);
5508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return bounder->doIRect(ibounds);
5518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// each of these costs 8-bytes of stack space, so don't make it too large
5548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// must be even for lines/polygon to work
5558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define MAX_DEV_PTS     32
5568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count,
558f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        const SkPoint pts[], const SkPaint& paint,
559f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        bool forceUseDevice) const {
5608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // if we're in lines mode, force count to be even
5618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkCanvas::kLines_PointMode == mode) {
5628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        count &= ~(size_t)1;
5638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if ((long)count <= 0) {
5668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
5678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
568a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
5698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(pts != NULL);
570f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
571a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
5728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     // nothing to draw
573045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty()) {
5748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
5758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
577fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    if (fBounder) {
578fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        if (!bounder_points(fBounder, mode, count, pts, paint, *fMatrix)) {
579fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com            return;
580fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        }
581fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
582fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        // clear the bounder and call this again, so we don't invoke the bounder
583fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        // later if we happen to call ourselves for drawRect, drawPath, etc.
584fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        SkDraw noBounder(*this);
585fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        noBounder.fBounder = NULL;
586fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        noBounder.drawPoints(mode, count, pts, paint, forceUseDevice);
587fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        return;
588fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    }
589fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
5908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    PtProcRec rec;
591045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (!forceUseDevice && rec.init(mode, paint, fMatrix, fRC)) {
5928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
5938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint             devPts[MAX_DEV_PTS];
5958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkMatrix*     matrix = fMatrix;
5968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkBlitter*          bltr = blitter.get();
597045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        PtProcRec::Proc     proc = rec.chooseProc(&bltr);
5988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // we have to back up subsequent passes if we're in polygon mode
5998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const size_t backup = (SkCanvas::kPolygon_PointMode == mode);
600a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
6018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        do {
6028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            size_t n = count;
6038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (n > MAX_DEV_PTS) {
6048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                n = MAX_DEV_PTS;
6058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
6068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix->mapPoints(devPts, pts, n);
6078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc(rec, devPts, n, bltr);
6088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pts += n - backup;
6098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(count >= n);
6108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            count -= n;
6118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (count > 0) {
6128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                count += backup;
6138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
6148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } while (count != 0);
6158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
6168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        switch (mode) {
6178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kPoints_PointMode: {
6188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // temporarily mark the paint as filling.
61940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                SkPaint newPaint(paint);
62040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                newPaint.setStyle(SkPaint::kFill_Style);
6218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
62240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                SkScalar width = newPaint.getStrokeWidth();
6238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkScalar radius = SkScalarHalf(width);
624a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
62540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) {
6268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkPath      path;
6278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkMatrix    preMatrix;
628a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
6298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.addCircle(0, 0, radius);
6308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    for (size_t i = 0; i < count; i++) {
6318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        preMatrix.setTranslate(pts[i].fX, pts[i].fY);
6328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        // pass true for the last point, since we can modify
6338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        // then path then
634f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        if (fDevice) {
63540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            fDevice->drawPath(*this, path, newPaint, &preMatrix,
636f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                              (count-1) == i);
637f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        } else {
63840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            this->drawPath(path, newPaint, &preMatrix,
63940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                                           (count-1) == i);
640f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        }
6418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
6428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                } else {
6438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkRect  r;
644a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
6458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    for (size_t i = 0; i < count; i++) {
6468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fLeft = pts[i].fX - radius;
6478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fTop = pts[i].fY - radius;
6488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fRight = r.fLeft + width;
6498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fBottom = r.fTop + width;
650f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        if (fDevice) {
65140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            fDevice->drawRect(*this, r, newPaint);
652f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        } else {
65340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            this->drawRect(r, newPaint);
654f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        }
6558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
6568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
6578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
6588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
6598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kLines_PointMode:
660629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com#ifndef SK_DISABLE_DASHING_OPTIMIZATION
661629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                if (2 == count && NULL != paint.getPathEffect()) {
662629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    // most likely a dashed line - see if it is one of the ones
663629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    // we can accelerate
664629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    SkStrokeRec rec(paint);
6656d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                    SkPathEffect::PointData pointData;
666629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com
667629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    SkPath path;
668629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    path.moveTo(pts[0]);
669629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    path.lineTo(pts[1]);
670629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com
6716d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                    if (paint.getPathEffect()->asPoints(&pointData, path, rec, *fMatrix)) {
6726d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                        // 'asPoints' managed to find some fast path
6736d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
674629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                        SkPaint newP(paint);
675629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                        newP.setPathEffect(NULL);
676935ad026826fb7d31d562ff7326b84ec3a827456robertphillips@google.com                        newP.setStyle(SkPaint::kFill_Style);
677629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com
6786d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                        if (!pointData.fFirst.isEmpty()) {
6796d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            if (fDevice) {
6806d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                fDevice->drawPath(*this, pointData.fFirst, newP);
6816d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            } else {
6826d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                this->drawPath(pointData.fFirst, newP);
6836d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            }
684629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                        }
6856d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
6866d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                        if (!pointData.fLast.isEmpty()) {
6876d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            if (fDevice) {
6886d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                fDevice->drawPath(*this, pointData.fLast, newP);
6896d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            } else {
6906d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                this->drawPath(pointData.fLast, newP);
6916d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            }
6926d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                        }
6936d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
6946d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                        if (pointData.fSize.fX == pointData.fSize.fY) {
6956d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            // The rest of the dashed line can just be drawn as points
6966d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            SkASSERT(pointData.fSize.fX == SkScalarHalf(newP.getStrokeWidth()));
6976d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
6986d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            if (SkPathEffect::PointData::kCircles_PointFlag & pointData.fFlags) {
6996d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                newP.setStrokeCap(SkPaint::kRound_Cap);
7006d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            } else {
7016d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                newP.setStrokeCap(SkPaint::kButt_Cap);
7026d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            }
7036d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
7046d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            if (fDevice) {
7057a03d86a3d9adcb13432fbd82039725149487c97skia.committer@gmail.com                                fDevice->drawPoints(*this,
7066d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                    SkCanvas::kPoints_PointMode,
7076d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                    pointData.fNumPoints,
7086d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                    pointData.fPoints,
7096d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                    newP);
7106d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            } else {
7116d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                this->drawPoints(SkCanvas::kPoints_PointMode,
7126d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                 pointData.fNumPoints,
7136d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                 pointData.fPoints,
7146d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                 newP,
7156d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                                 forceUseDevice);
7166d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            }
7176d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            break;
718935ad026826fb7d31d562ff7326b84ec3a827456robertphillips@google.com                        } else {
7196d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            // The rest of the dashed line must be drawn as rects
7207a03d86a3d9adcb13432fbd82039725149487c97skia.committer@gmail.com                            SkASSERT(!(SkPathEffect::PointData::kCircles_PointFlag &
7216d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                      pointData.fFlags));
7226d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
7236d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            SkRect r;
7246d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
7256d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            for (int i = 0; i < pointData.fNumPoints; ++i) {
7266d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                r.set(pointData.fPoints[i].fX - pointData.fSize.fX,
7276d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                      pointData.fPoints[i].fY - pointData.fSize.fY,
7286d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                      pointData.fPoints[i].fX + pointData.fSize.fX,
7296d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                      pointData.fPoints[i].fY + pointData.fSize.fY);
7306d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                if (fDevice) {
7316d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                    fDevice->drawRect(*this, r, newP);
7326d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                } else {
7336d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                    this->drawRect(r, newP);
7346d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                                }
7356d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com                            }
736935ad026826fb7d31d562ff7326b84ec3a827456robertphillips@google.com                        }
7376d87557278052c131957e5d6e093d3a675162d22robertphillips@google.com
738629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                        break;
739629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                    }
740629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                }
741629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com#endif // DISABLE_DASHING_OPTIMIZATION
742629ab540667422d3edcb97c51e9628b7051e1ba4robertphillips@google.com                // couldn't take fast path so fall through!
7438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kPolygon_PointMode: {
7448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                count -= 1;
7458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkPath path;
7468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkPaint p(paint);
7478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                p.setStyle(SkPaint::kStroke_Style);
7488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
7498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                for (size_t i = 0; i < count; i += inc) {
7508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.moveTo(pts[i]);
7518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.lineTo(pts[i+1]);
752f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    if (fDevice) {
753f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        fDevice->drawPath(*this, path, p, NULL, true);
754f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    } else {
755f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        this->drawPath(path, p, NULL, true);
756f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    }
7578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.rewind();
7588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
7598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
7608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
7618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
7628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic inline SkPoint* as_lefttop(SkRect* r) {
7668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (SkPoint*)(void*)r;
7678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic inline SkPoint* as_rightbottom(SkRect* r) {
7708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return ((SkPoint*)(void*)r) + 1;
7718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
773761fb62b0eb174783316d2a8b933fba896ca6355reed@google.comstatic bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix,
774761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                           SkPoint* strokeSize) {
775761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    if (SkPaint::kMiter_Join != paint.getStrokeJoin() ||
776761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com        paint.getStrokeMiter() < SK_ScalarSqrt2) {
777761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com        return false;
778761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    }
779fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
780761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    SkASSERT(matrix.rectStaysRect());
781761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() };
782761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    matrix.mapVectors(strokeSize, &pt, 1);
7836115338c59799b4ef09cda187f23867dea093f6ereed@google.com    strokeSize->fX = SkScalarAbs(strokeSize->fX);
7846115338c59799b4ef09cda187f23867dea093f6ereed@google.com    strokeSize->fY = SkScalarAbs(strokeSize->fY);
785761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    return true;
7867ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org}
7877ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org
78862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.comSkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint,
78962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com                                         const SkMatrix& matrix,
79062ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com                                         SkPoint* strokeSize) {
7917ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    RectType rtype;
7927ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    const SkScalar width = paint.getStrokeWidth();
79362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    const bool zeroWidth = (0 == width);
7947ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    SkPaint::Style style = paint.getStyle();
795fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
7967ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) {
7977ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        style = SkPaint::kFill_Style;
7987ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    }
799fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
8008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getPathEffect() || paint.getMaskFilter() ||
80162ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        paint.getRasterizer() || !matrix.rectStaysRect() ||
8027ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        SkPaint::kStrokeAndFill_Style == style) {
80362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        rtype = kPath_RectType;
80462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    } else if (SkPaint::kFill_Style == style) {
8057ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kFill_RectType;
8067ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    } else if (zeroWidth) {
8077ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kHair_RectType;
80862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    } else if (easy_rect_join(paint, matrix, strokeSize)) {
8097ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kStroke_RectType;
8107ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    } else {
8117ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kPath_RectType;
8127ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    }
81362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    return rtype;
81462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com}
81562ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
8167324415759fe0c5a0902877b664aa942a89bd940reed@google.comstatic const SkPoint* rect_points(const SkRect& r) {
8177324415759fe0c5a0902877b664aa942a89bd940reed@google.com    return (const SkPoint*)(void*)&r;
8187324415759fe0c5a0902877b664aa942a89bd940reed@google.com}
8197324415759fe0c5a0902877b664aa942a89bd940reed@google.com
8207324415759fe0c5a0902877b664aa942a89bd940reed@google.comstatic SkPoint* rect_points(SkRect& r) {
8217324415759fe0c5a0902877b664aa942a89bd940reed@google.com    return (SkPoint*)(void*)&r;
82240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com}
82340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com
82462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.comvoid SkDraw::drawRect(const SkRect& rect, const SkPaint& paint) const {
82562ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    SkDEBUGCODE(this->validate();)
8267ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org
82762ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    // nothing to draw
828045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty()) {
82962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        return;
83062ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    }
83162ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
83262ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    SkPoint strokeSize;
83362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize);
83462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
8357ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    if (kPath_RectType == rtype) {
8368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPath  tmp;
8378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tmp.addRect(rect);
8388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tmp.setFillType(SkPath::kWinding_FillType);
839187d5595901d1120d9425851e5afdd773f574502reed@android.com        this->drawPath(tmp, paint, NULL, true);
8408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
8418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix& matrix = *fMatrix;
8448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect          devRect;
8458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform rect into devRect
8477324415759fe0c5a0902877b664aa942a89bd940reed@google.com    matrix.mapPoints(rect_points(devRect), rect_points(rect), 2);
8487324415759fe0c5a0902877b664aa942a89bd940reed@google.com    devRect.sort();
8498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder && !fBounder->doRect(devRect, paint)) {
8518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
8528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // look for the quick exit, before we build a blitter
8557324415759fe0c5a0902877b664aa942a89bd940reed@google.com    if (true) {
8568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkIRect ir;
8578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        devRect.roundOut(&ir);
85855e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        if (paint.getStyle() != SkPaint::kFill_Style) {
85955e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            // extra space for hairlines
86055e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            ir.inset(-1, -1);
86155e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        }
862045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (fRC->quickReject(ir))
8638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
8648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose blitterStorage(*fBitmap, matrix, paint);
867045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkRasterClip& clip = *fRC;
8688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*          blitter = blitterStorage.get();
8698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
870b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
871b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // case we are also hairline (if we've gotten to here), which devolves to
872b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // effectively just kFill
8737ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    switch (rtype) {
8747ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kFill_RectType:
8757ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
8767ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::AntiFillRect(devRect, clip, blitter);
8777ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
8787ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::FillRect(devRect, clip, blitter);
8797ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
8807ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
8817ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kStroke_RectType:
8827ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
883761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                SkScan::AntiFrameRect(devRect, strokeSize, clip, blitter);
8847ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
885761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                SkScan::FrameRect(devRect, strokeSize, clip, blitter);
8867ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
8877ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
8887ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kHair_RectType:
8897ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
8907ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::AntiHairRect(devRect, clip, blitter);
8917ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
8927ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::HairRect(devRect, clip, blitter);
8937ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
8947ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
8957ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        default:
8960c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com            SkDEBUGFAIL("bad rtype");
8978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const {
9018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (srcM.fBounds.isEmpty()) {
9028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
9038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9050a60b3d32eae945688b69599f11679662657f751bungeman@google.com    const SkMask* mask = &srcM;
9068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9070a60b3d32eae945688b69599f11679662657f751bungeman@google.com    SkMask dstM;
9088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getMaskFilter() &&
9098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, NULL)) {
9108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask = &dstM;
911bf2ac7e52f84095368dc50600fd6e78cc96044e3bungeman@google.com    } else {
912bf2ac7e52f84095368dc50600fd6e78cc96044e3bungeman@google.com        dstM.fImage = NULL;
9138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
91402f55841854ae32f21a13417e9ee711463e488cfbungeman@google.com    SkAutoMaskFreeImage ami(dstM.fImage);
9158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder && !fBounder->doIRect(mask->fBounds)) {
9178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
9188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
920045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAutoBlitterChoose blitterChooser(*fBitmap, *fMatrix, paint);
921045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter* blitter = blitterChooser.get();
922045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
923045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAAClipBlitterWrapper wrapper;
924045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkRegion* clipRgn;
9258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
926045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isBW()) {
927045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        clipRgn = &fRC->bwRgn();
928045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    } else {
929045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        wrapper.init(*fRC, blitter);
930045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        clipRgn = &wrapper.getRgn();
931045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = wrapper.getBlitter();
932045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    }
933045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    blitter->blitMaskRegion(*mask, *clipRgn);
9348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
936ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.comstatic SkScalar fast_len(const SkVector& vec) {
937ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar x = SkScalarAbs(vec.fX);
938ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar y = SkScalarAbs(vec.fY);
939ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    if (x < y) {
940ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        SkTSwap(x, y);
941ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
942ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    return x + SkScalarHalf(y);
943ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com}
944ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com
945ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.comstatic bool xfermodeSupportsCoverageAsAlpha(SkXfermode* xfer) {
946ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    SkXfermode::Coeff dc;
947ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (!SkXfermode::AsCoeff(xfer, NULL, &dc)) {
948ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return false;
949ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
950fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
951ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    switch (dc) {
952ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        case SkXfermode::kOne_Coeff:
953ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        case SkXfermode::kISA_Coeff:
954ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        case SkXfermode::kISC_Coeff:
955ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com            return true;
956ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        default:
957ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com            return false;
958ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
959ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com}
960ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
961ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.combool SkDrawTreatAsHairline(const SkPaint& paint, const SkMatrix& matrix,
962dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                           SkScalar* coverage) {
963dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com    SkASSERT(coverage);
964ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (SkPaint::kStroke_Style != paint.getStyle()) {
965ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return false;
966ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
967ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    SkScalar strokeWidth = paint.getStrokeWidth();
968ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (0 == strokeWidth) {
969dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        *coverage = SK_Scalar1;
970ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return true;
971ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
972ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
973ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    // if we get here, we need to try to fake a thick-stroke with a modulated
974ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    // hairline
975ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
976ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    if (!paint.isAntiAlias()) {
977ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com        return false;
978ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    }
9798d430185e08d2067584837a76b7193b803fee7a0tomhudson@google.com    if (matrix.hasPerspective()) {
980ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        return false;
981ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
982ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com
983ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkVector src[2], dst[2];
984ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    src[0].set(strokeWidth, 0);
985ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    src[1].set(0, strokeWidth);
986ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    matrix.mapVectors(dst, src, 2);
987ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar len0 = fast_len(dst[0]);
988ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar len1 = fast_len(dst[1]);
989652807bbc8c57e5fa9622126b51fd369f5c67935agl@chromium.org    if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) {
990dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        *coverage = SkScalarAve(len0, len1);
991ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        return true;
992ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
993ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    return false;
994ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com}
995ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com
99632e5d97ccf60f859db063ebd6e903c362e625767reed@google.comvoid SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint,
9978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      const SkMatrix* prePathMatrix, bool pathIsMutable) const {
998f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
9998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1001045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty()) {
10028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
10038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath*         pathPtr = (SkPath*)&origSrcPath;
10068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool            doFill = true;
10078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath          tmpPath;
10088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        tmpMatrix;
10098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix* matrix = fMatrix;
10108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (prePathMatrix) {
101232e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style ||
101332e5d97ccf60f859db063ebd6e903c362e625767reed@google.com                origPaint.getRasterizer()) {
10148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPath* result = pathPtr;
1015a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!pathIsMutable) {
10178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                result = &tmpPath;
10188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pathIsMutable = true;
10198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
10208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathPtr->transform(*prePathMatrix, result);
10218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathPtr = result;
10228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
10238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!tmpMatrix.setConcat(*matrix, *prePathMatrix)) {
10248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // overflow
10258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
10268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
10278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix = &tmpMatrix;
10288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
10298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // at this point we're done with prePathMatrix
10318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
1032a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10335dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1034a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1035ecadf99c8450646dfd9c2754f3e845245beab8b6reed@google.com    {
1036dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        SkScalar coverage;
1037dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com        if (SkDrawTreatAsHairline(origPaint, *matrix, &coverage)) {
1038dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com            if (SK_Scalar1 == coverage) {
10395dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                paint.writable()->setStrokeWidth(0);
1040dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com            } else if (xfermodeSupportsCoverageAsAlpha(origPaint.getXfermode())) {
1041dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                U8CPU newAlpha;
1042dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com#if 0
1043dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                newAlpha = SkToU8(SkScalarRoundToInt(coverage *
1044dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                                                     origPaint.getAlpha()));
1045dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com#else
1046dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                // this is the old technique, which we preserve for now so
1047dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                // we don't change previous results (testing)
1048dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                // the new way seems fine, its just (a tiny bit) different
1049dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                int scale = (int)SkScalarMul(coverage, 256);
1050dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com                newAlpha = origPaint.getAlpha() * scale >> 8;
1051dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com#endif
10525dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                SkPaint* writablePaint = paint.writable();
10535dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                writablePaint->setStrokeWidth(0);
10545dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                writablePaint->setAlpha(newAlpha);
1055dd1be60702b3622f49d97651e31d13eaf2175cf8bsalomon@google.com            }
10568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
10578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1058a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
105932e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) {
106032e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        doFill = paint->getFillPath(*pathPtr, &tmpPath);
10618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathPtr = &tmpPath;
10628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1063a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
106432e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (paint->getRasterizer()) {
10658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
106632e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->getRasterizer()->rasterize(*pathPtr, *matrix,
1067045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                            &fRC->getBounds(), paint->getMaskFilter(), &mask,
10688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
106932e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            this->drawDevMask(mask, *paint);
10708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkMask::FreeImage(mask.fImage);
10718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
10728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
10738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // avoid possibly allocating a new path in transform if we can
10768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
10778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform the path into device space
10798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pathPtr->transform(*matrix, devPathPtr);
10808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
108132e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, *paint);
10828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10832ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org    if (paint->getMaskFilter()) {
1084fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        SkPaint::Style style = doFill ? SkPaint::kFill_Style :
10852ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org            SkPaint::kStroke_Style;
10862ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org        if (paint->getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fRC,
10872ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                                               fBounder, blitter.get(),
10882ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                                               style)) {
10892ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org            return; // filterPath() called the blitter, so we're done
10902ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org        }
10918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
109332e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (fBounder && !fBounder->doPath(*devPathPtr, *paint, doFill)) {
10948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
10958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1097045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    void (*proc)(const SkPath&, const SkRasterClip&, SkBlitter*);
10988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (doFill) {
109932e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->isAntiAlias()) {
1100045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::AntiFillPath;
11018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1102045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::FillPath;
11038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
11048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // hairline
110532e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->isAntiAlias()) {
1106045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::AntiHairPath;
11078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1108045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            proc = SkScan::HairPath;
11098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
11108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1111045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    proc(*devPathPtr, *fRC, blitter.get());
11128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11140baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com/** For the purposes of drawing bitmaps, if a matrix is "almost" translate
11150baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    go ahead and treat it as if it were, so that subsequent code can go fast.
11160baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com */
11170baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.comstatic bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) {
1118070dcd8ad37a4c5b8a9015938409b27bb3ce3ab9reed@google.com#ifdef SK_IGNORE_TRANS_CLAMP_FIX
11190baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    SkMatrix::TypeMask mask = matrix.getType();
11200baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com
11210baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (mask & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
11220baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        return false;
11230baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    }
11240baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (mask & SkMatrix::kScale_Mask) {
11250baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        SkScalar sx = matrix[SkMatrix::kMScaleX];
11260baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        SkScalar sy = matrix[SkMatrix::kMScaleY];
11270baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int w = bitmap.width();
11280baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int h = bitmap.height();
11290baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int sw = SkScalarRound(SkScalarMul(sx, SkIntToScalar(w)));
11300baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int sh = SkScalarRound(SkScalarMul(sy, SkIntToScalar(h)));
11310baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        return sw == w && sh == h;
11320baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    }
11330baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    // if we got here, we're either kTranslate_Mask or identity
11340baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    return true;
1135ae57358447bac678e3fc458fa2857a349a6a7081reed@google.com#else
1136070dcd8ad37a4c5b8a9015938409b27bb3ce3ab9reed@google.com    unsigned bits = 0;  // TODO: find a way to allow the caller to tell us to
1137070dcd8ad37a4c5b8a9015938409b27bb3ce3ab9reed@google.com                        // respect filtering.
1138070dcd8ad37a4c5b8a9015938409b27bb3ce3ab9reed@google.com    return SkTreatAsSprite(matrix, bitmap.width(), bitmap.height(), bits);
1139ae57358447bac678e3fc458fa2857a349a6a7081reed@google.com#endif
11408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawBitmapAsMask(const SkBitmap& bitmap,
11438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint) const {
11448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(bitmap.getConfig() == SkBitmap::kA8_Config);
11458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1146a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    if (just_translate(*fMatrix, bitmap)) {
11478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int ix = SkScalarRound(fMatrix->getTranslateX());
11488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int iy = SkScalarRound(fMatrix->getTranslateY());
11498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1150a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com        SkAutoLockPixels alp(bitmap);
1151a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com        if (!bitmap.readyToDraw()) {
1152a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com            return;
1153a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com        }
1154a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com
11558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
11568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fBounds.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
11578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fFormat = SkMask::kA8_Format;
11588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fRowBytes = bitmap.rowBytes();
11598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fImage = bitmap.getAddr8(0, 0);
1160a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawDevMask(mask, paint);
11628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // need to xform the bitmap first
11638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect  r;
11648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
1165a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.set(0, 0,
11678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com              SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
11688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fMatrix->mapRect(&r);
11698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.round(&mask.fBounds);
1170a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // set the mask's bounds to the transformed bitmap-bounds,
11728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // clipped to the actual device
11738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
11748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkIRect    devBounds;
11758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            devBounds.set(0, 0, fBitmap->width(), fBitmap->height());
11768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // need intersect(l, t, r, b) on irect
11778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!mask.fBounds.intersect(devBounds)) {
11788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
11798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
11808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1181543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com
11828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fFormat = SkMask::kA8_Format;
11838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fRowBytes = SkAlign4(mask.fBounds.width());
1184543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        size_t size = mask.computeImageSize();
1185543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        if (0 == size) {
1186543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            // the mask is too big to allocated, draw nothing
1187543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            return;
1188543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        }
11898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // allocate (and clear) our temp buffer to hold the transformed bitmap
11918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAutoMalloc    storage(size);
11928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fImage = (uint8_t*)storage.get();
11938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        memset(mask.fImage, 0, size);
1194a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // now draw our bitmap(src) into mask(dst), transformed by the matrix
11968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
11978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkBitmap    device;
11988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            device.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(),
11998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             mask.fBounds.height(), mask.fRowBytes);
12008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            device.setPixels(mask.fImage);
1201a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkCanvas c(device);
12038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // need the unclipped top/left for the translate
12048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            c.translate(-SkIntToScalar(mask.fBounds.fLeft),
12058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        -SkIntToScalar(mask.fBounds.fTop));
12068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            c.concat(*fMatrix);
12073469c76c40790b409621fd7eff34f56240718549reed@android.com
12083469c76c40790b409621fd7eff34f56240718549reed@android.com            // We can't call drawBitmap, or we'll infinitely recurse. Instead
1209fb12c3e6ba84f95dc15fbaddc239dede0ba1d60ereed@android.com            // we manually build a shader and draw that into our new mask
12103469c76c40790b409621fd7eff34f56240718549reed@android.com            SkPaint tmpPaint;
12113469c76c40790b409621fd7eff34f56240718549reed@android.com            tmpPaint.setFlags(paint.getFlags());
121240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            SkAutoBitmapShaderInstall install(bitmap, tmpPaint);
12133469c76c40790b409621fd7eff34f56240718549reed@android.com            SkRect rr;
12143469c76c40790b409621fd7eff34f56240718549reed@android.com            rr.set(0, 0, SkIntToScalar(bitmap.width()),
12153469c76c40790b409621fd7eff34f56240718549reed@android.com                   SkIntToScalar(bitmap.height()));
121640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            c.drawRect(rr, install.paintWithShader());
12178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
12188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawDevMask(mask, paint);
12198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1222045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic bool clipped_out(const SkMatrix& m, const SkRasterClip& c,
12238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        const SkRect& srcR) {
12248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect  dstR;
12258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect devIR;
1226a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.mapRect(&dstR, srcR);
1228a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    dstR.roundOut(&devIR);
12298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return c.quickReject(devIR);
12308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1232045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip,
12338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        int width, int height) {
12348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect  r;
12358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
12368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return clipped_out(matrix, clip, r);
12378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1239045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic bool clipHandlesSprite(const SkRasterClip& clip, int x, int y,
1240045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                              const SkBitmap& bitmap) {
1241045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    return clip.isBW() ||
1242045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com           clip.quickContains(x, y, x + bitmap.width(), y + bitmap.height());
1243045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com}
1244045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
12458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
124640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                        const SkPaint& origPaint) const {
1247f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
12488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1250045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty() ||
12518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bitmap.width() == 0 || bitmap.height() == 0 ||
1252dcd0f3a980095b77ebe605b49777a4bd37ca7b0areed@google.com            bitmap.getConfig() == SkBitmap::kNo_Config) {
12538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
12548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1255a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1256a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#ifndef SK_ALLOW_OVER_32K_BITMAPS
12578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // run away on too-big bitmaps for now (exceed 16.16)
12588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (bitmap.width() > 32767 || bitmap.height() > 32767) {
12598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
12608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1261a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#endif
1262a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
126340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkPaint paint(origPaint);
126440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    paint.setStyle(SkPaint::kFill_Style);
1265a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix matrix;
12678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (!matrix.setConcat(*fMatrix, prematrix)) {
12688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
12698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1271045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) {
12728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
12738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1275218521e15706c4377b1be49d931c4d7c8d597445reed@android.com    if (fBounder && just_translate(matrix, bitmap)) {
1276218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        SkIRect ir;
1277218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        int32_t ix = SkScalarRound(matrix.getTranslateX());
1278218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        int32_t iy = SkScalarRound(matrix.getTranslateY());
1279218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1280218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        if (!fBounder->doIRect(ir)) {
1281218521e15706c4377b1be49d931c4d7c8d597445reed@android.com            return;
1282218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        }
1283218521e15706c4377b1be49d931c4d7c8d597445reed@android.com    }
1284218521e15706c4377b1be49d931c4d7c8d597445reed@android.com
12850baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (bitmap.getConfig() != SkBitmap::kA8_Config &&
12860baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com            just_translate(matrix, bitmap)) {
1287f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        //
1288f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        // It is safe to call lock pixels now, since we know the matrix is
1289f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        // (more or less) identity.
1290f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        //
1291f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        SkAutoLockPixels alp(bitmap);
1292f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        if (!bitmap.readyToDraw()) {
1293f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com            return;
1294f7ef56d9104fc651769760df9d5f861ef36de658reed@google.com        }
1295045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        int ix = SkScalarRound(matrix.getTranslateX());
1296045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        int iy = SkScalarRound(matrix.getTranslateY());
1297045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (clipHandlesSprite(*fRC, ix, iy, bitmap)) {
1298045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            uint32_t    storage[kBlitterStorageLongCount];
1299045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkBlitter*  blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1300045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                                                ix, iy, storage, sizeof(storage));
1301045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            if (blitter) {
1302045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                SkAutoTPlacementDelete<SkBlitter>   ad(blitter, storage);
1303045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1304045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                SkIRect    ir;
1305045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1306045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1307045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                SkScan::FillIRect(ir, *fRC, blitter);
1308045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                return;
13098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
13108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
13118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1312a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
13138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // now make a temp draw on the stack, and use it
13148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //
13158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDraw draw(*this);
13168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix = &matrix;
1317a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
13188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (bitmap.getConfig() == SkBitmap::kA8_Config) {
13198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        draw.drawBitmapAsMask(bitmap, paint);
13208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
132140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkAutoBitmapShaderInstall install(bitmap, paint);
13228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect  r;
13248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.set(0, 0, SkIntToScalar(bitmap.width()),
13258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com              SkIntToScalar(bitmap.height()));
1326a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com        // is this ok if paint has a rasterizer?
132740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        draw.drawRect(r, install.paintWithShader());
13288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y,
133240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                        const SkPaint& origPaint) const {
1333f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
1334a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
13358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1336045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->isEmpty() ||
13378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bitmap.width() == 0 || bitmap.height() == 0 ||
1338dcd0f3a980095b77ebe605b49777a4bd37ca7b0areed@google.com            bitmap.getConfig() == SkBitmap::kNo_Config) {
13398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
13408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    bounds;
13438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.set(x, y, x + bitmap.width(), y + bitmap.height());
13448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1345045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (fRC->quickReject(bounds)) {
13468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return; // nothing to draw
13478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
134940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkPaint paint(origPaint);
135040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    paint.setStyle(SkPaint::kFill_Style);
13518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1352045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (NULL == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, bitmap)) {
13538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        uint32_t    storage[kBlitterStorageLongCount];
13548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkBlitter*  blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
13558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                                x, y, storage, sizeof(storage));
13568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (blitter) {
13588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkAutoTPlacementDelete<SkBlitter> ad(blitter, storage);
13598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (fBounder && !fBounder->doIRect(bounds)) {
13618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
13628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
13638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1364045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkScan::FillIRect(bounds, *fRC, blitter);
13658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
13668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
13678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
136940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkAutoBitmapShaderInstall install(bitmap, paint);
137040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    const SkPaint& shaderPaint = install.paintWithShader();
13718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        matrix;
13738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect          r;
13748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // get a scalar version of our rect
13768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.set(bounds);
13778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // tell the shader our offset
13798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setTranslate(r.fLeft, r.fTop);
138040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    shaderPaint.getShader()->setLocalMatrix(matrix);
1381a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
13828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDraw draw(*this);
13838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.reset();
13848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix = &matrix;
13858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // call ourself with a rect
1386a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    // is this OK if paint has a rasterizer?
138740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    draw.drawRect(r, shaderPaint);
13888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
13918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkScalerContext.h"
13938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGlyphCache.h"
1394e69137620ab0b5b40d230318c8e11b822f63cb9dreed@google.com#include "SkTextToPathIter.h"
13958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h"
13968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void measure_text(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
13988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const char text[], size_t byteLength, SkVector* stopVector) {
13998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed     x = 0, y = 0;
14008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* stop = text + byteLength;
14018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoKern  autokern;
1403a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
14048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (text < stop) {
14058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // don't need x, y here, since all subpixel variants will have the
14068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // same advance
14078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
14088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x += autokern.adjust(glyph) + glyph.fAdvanceX;
14108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y += glyph.fAdvanceY;
14118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
14138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(text == stop);
14158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawText_asPaths(const char text[], size_t byteLength,
14188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              SkScalar x, SkScalar y,
14198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint) const {
1420f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
14218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1422166e653f67f3fffc3846184a25ce45ab083f07a2djsollen@google.com    SkTextToPathIter iter(text, byteLength, paint, true);
14238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    matrix;
14258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setScale(iter.getPathScale(), iter.getPathScale());
14268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.postTranslate(x, y);
14278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPath* iterPath;
14298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar xpos, prevXPos = 0;
14308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14317b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com    while (iter.next(&iterPath, &xpos)) {
14328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        matrix.postTranslate(xpos - prevXPos, 0);
14337b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com        if (iterPath) {
14347b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            const SkPaint& pnt = iter.getPaint();
14357b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            if (fDevice) {
14367b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                fDevice->drawPath(*this, *iterPath, pnt, &matrix, false);
14377b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            } else {
14387b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                this->drawPath(*iterPath, pnt, &matrix, false);
14397b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            }
1440f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
14418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        prevXPos = xpos;
14428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// disable warning : local variable used without having been initialized
1446a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#if defined _WIN32 && _MSC_VER >= 1300
14478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( push )
14488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( disable : 4701 )
14498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
14508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
14528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_NoBounder_RectClip(const SkDraw1Glyph& state,
1454f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                   SkFixed fx, SkFixed fy,
1455f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                   const SkGlyph& glyph) {
1456f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1457f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
14588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
145983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT(NULL == state.fBounder);
146083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT((NULL == state.fClip && state.fAAClip) ||
146183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com             (state.fClip && NULL == state.fAAClip && state.fClip->isRect()));
14628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
14648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
14658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int right   = left + glyph.fWidth;
14678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int bottom  = top + glyph.fHeight;
14688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1469fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkMask        mask;
1470fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkIRect        storage;
1471fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkIRect*    bounds = &mask.fBounds;
147283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
147383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fBounds.set(left, top, right, bottom);
147483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
147583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    // this extra test is worth it, assuming that most of the time it succeeds
147683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    // since we can avoid writing to storage
147783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) {
147883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds))
147983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            return;
148083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        bounds = &storage;
148183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
148283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
148383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    uint8_t* aa = (uint8_t*)glyph.fImage;
148483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (NULL == aa) {
148583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        aa = (uint8_t*)state.fCache->findImage(glyph);
148683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (NULL == aa) {
148783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            return; // can't rasterize glyph
14888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
148983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
14908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
149183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fRowBytes = glyph.rowBytes();
149283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
149383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    mask.fImage = aa;
149483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    state.fBlitter->blitMask(mask, *bounds);
14958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_NoBounder_RgnClip(const SkDraw1Glyph& state,
1498f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                  SkFixed fx, SkFixed fy,
149983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                                  const SkGlyph& glyph) {
1500f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1501f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
15028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
150383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT(!state.fClip->isRect());
150483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkASSERT(NULL == state.fBounder);
15058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMask  mask;
15078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
15098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
15108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
151283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
151383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
151483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (!clipper.done()) {
151583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const SkIRect&  cr = clipper.rect();
151683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const uint8_t*  aa = (const uint8_t*)glyph.fImage;
151783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (NULL == aa) {
151883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            aa = (uint8_t*)state.fCache->findImage(glyph);
151983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            if (NULL == aa) {
1520fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com                return;
15218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
152283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        }
152383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com
152483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        mask.fRowBytes = glyph.rowBytes();
152583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
152683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        mask.fImage = (uint8_t*)aa;
152783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        do {
152883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            state.fBlitter->blitMask(mask, cr);
152983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            clipper.next();
153083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        } while (!clipper.done());
153183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
15328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_Bounder(const SkDraw1Glyph& state,
1535f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        SkFixed fx, SkFixed fy,
153683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                        const SkGlyph& glyph) {
1537f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1538f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
15398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1540fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
15418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMask  mask;
1542fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
15438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
15448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
1545fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
15468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
15478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
1548fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
154983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    if (!clipper.done()) {
155083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const SkIRect&  cr = clipper.rect();
155183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        const uint8_t*  aa = (const uint8_t*)glyph.fImage;
155283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (NULL == aa) {
155383a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            aa = (uint8_t*)state.fCache->findImage(glyph);
155483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            if (NULL == aa) {
155583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                return;
15568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
1557fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        }
1558fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
1559d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        // we need to pass the origin, which we approximate with our
1560d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        // (unadjusted) left,top coordinates (the caller called fixedfloor)
156183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        if (state.fBounder->doIRectGlyph(cr,
1562d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                                         left - glyph.fLeft,
1563d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                                         top - glyph.fTop, glyph)) {
156483a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            mask.fRowBytes = glyph.rowBytes();
156583a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
156683a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            mask.fImage = (uint8_t*)aa;
156783a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            do {
156883a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                state.fBlitter->blitMask(mask, cr);
156983a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com                clipper.next();
157083a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com            } while (!clipper.done());
157183a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com        }
157283a444602ec580a0040713eed588c245b4ae0ee9tomhudson@google.com    }
15738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1575045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comstatic void D1G_Bounder_AAClip(const SkDraw1Glyph& state,
1576045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                               SkFixed fx, SkFixed fy,
1577045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                               const SkGlyph& glyph) {
1578045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    int left = SkFixedFloor(fx);
1579045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    int top = SkFixedFloor(fy);
1580045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkIRect bounds;
1581045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    bounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
1582045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1583045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (state.fBounder->doIRectGlyph(bounds, left, top, glyph)) {
1584045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        D1G_NoBounder_RectClip(state, fx, fy, glyph);
1585045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    }
1586045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com}
1587045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
1588fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.comstatic bool hasCustomD1GProc(const SkDraw& draw) {
1589fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    return draw.fProcs && draw.fProcs->fD1GProc;
1590fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com}
1591fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com
1592fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.comstatic bool needsRasterTextBlit(const SkDraw& draw) {
1593fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    return !hasCustomD1GProc(draw);
1594fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com}
1595fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com
15968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter,
15978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                      SkGlyphCache* cache) {
15988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDraw = draw;
15992211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    fBounder = draw->fBounder;
16002211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    fBlitter = blitter;
16012211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    fCache = cache;
16028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16031d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (hasCustomD1GProc(*draw)) {
1604045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        // todo: fix this assumption about clips w/ custom
1605045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = draw->fClip;
1606045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClipBounds = fClip->getBounds();
16078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return draw->fProcs->fD1GProc;
16088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1610045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (draw->fRC->isBW()) {
1611045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fAAClip = NULL;
1612045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = &draw->fRC->bwRgn();
1613045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClipBounds = fClip->getBounds();
1614045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (NULL == fBounder) {
1615045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            if (fClip->isRect()) {
1616045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                return D1G_NoBounder_RectClip;
1617045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            } else {
1618045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                return D1G_NoBounder_RgnClip;
1619045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            }
1620045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        } else {
1621045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            return D1G_Bounder;
1622045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        }
1623045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    } else {    // aaclip
1624045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fAAClip = &draw->fRC->aaRgn();
1625045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClip = NULL;
1626045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        fClipBounds = fAAClip->getBounds();
1627045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (NULL == fBounder) {
16288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return D1G_NoBounder_RectClip;
16298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1630045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            return D1G_Bounder_AAClip;
16318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
16328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
16348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
16368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawText(const char text[], size_t byteLength,
16388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      SkScalar x, SkScalar y, const SkPaint& paint) const {
16398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
16408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1641f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
16428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1644045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
16458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
16468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
164836d6edac9f3e63d9a5f499d0076550d08b80eacabsalomon@google.com    // SkScalarRec doesn't currently have a way of representing hairline stroke and
164936d6edac9f3e63d9a5f499d0076550d08b80eacabsalomon@google.com    // will fill if its frame-width is 0.
16508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (/*paint.isLinearText() ||*/
1651fc84359aa920567e72742877a1249f52d076ad35skia.committer@gmail.com        (fMatrix->hasPerspective()) ||
165236d6edac9f3e63d9a5f499d0076550d08b80eacabsalomon@google.com        (0 == paint.getStrokeWidth() && SkPaint::kStroke_Style == paint.getStyle())) {
16538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawText_asPaths(text, byteLength, x, y, paint);
16548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
16558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
16588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1659f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkMatrix* matrix = fMatrix;
1660f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1661532470f34dbe9fc0b8b71e3917eca8894feaf336bungeman@google.com    SkAutoGlyphCache    autoCache(paint, &fDevice->fLeakyProperties, matrix);
16628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkGlyphCache*       cache = autoCache.getCache();
1663a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform our starting point
16658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
16668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint loc;
1667f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        matrix->mapXY(x, y, &loc);
16688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x = loc.fX;
16698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y = loc.fY;
16708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need to measure first
16738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getTextAlign() != SkPaint::kLeft_Align) {
16748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkVector    stop;
16758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        measure_text(cache, glyphCacheProc, text, byteLength, &stop);
16778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar    stopX = stop.fX;
16798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar    stopY = stop.fY;
16808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.getTextAlign() == SkPaint::kCenter_Align) {
16828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            stopX = SkScalarHalf(stopX);
16838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            stopY = SkScalarHalf(stopY);
16848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
16858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x -= stopX;
16868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y -= stopY;
16878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1688a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed fx = SkScalarToFixed(x);
16908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed fy = SkScalarToFixed(y);
16918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* stop = text + byteLength;
16928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1693f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkFixed fxMask = ~0;
1694f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkFixed fyMask = ~0;
16952211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    if (cache->isSubpixel()) {
1696cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*matrix);
1697cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        if (kX_SkAxisAlignment == baseline) {
1698f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fyMask = 0;
1699cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        } else if (kY_SkAxisAlignment == baseline) {
1700f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fxMask = 0;
17018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1702fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
17032211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    // apply bias here to avoid adding 1/2 the sampling frequency in the loop
17042211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fx += SK_FixedHalf >> SkGlyph::kSubBits;
17052211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fy += SK_FixedHalf >> SkGlyph::kSubBits;
17062211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    } else {
17072211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fx += SK_FixedHalf;
17082211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        fy += SK_FixedHalf;
17098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
17108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1711045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAAClipBlitter     aaBlitter;
1712045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAutoBlitterChoose blitterChooser;
1713045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter*          blitter = NULL;
17141d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (needsRasterTextBlit(*this)) {
1715045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitterChooser.choose(*fBitmap, *matrix, paint);
1716045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = blitterChooser.get();
1717045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (fRC->isAA()) {
1718045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            aaBlitter.init(blitter, &fRC->aaRgn());
1719045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            blitter = &aaBlitter;
1720045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        }
17211d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
17221d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com
17238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoKern          autokern;
172452c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com    SkDraw1Glyph        d1g;
1725045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkDraw1Glyph::Proc  proc = d1g.init(this, blitter, cache);
17268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (text < stop) {
17282211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com        const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
17298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fx += autokern.adjust(glyph);
17318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (glyph.fWidth) {
173352c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com            proc(d1g, fx, fy, glyph);
17348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
17358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fx += glyph.fAdvanceX;
17368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fy += glyph.fAdvanceY;
17378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
17388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// last parameter is interpreted as SkFixed [x, y]
17418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// return the fixed position, which may be rounded or not by the caller
17428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//   e.g. subpixel doesn't round
17438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef void (*AlignProc)(const SkPoint&, const SkGlyph&, SkIPoint*);
17448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void leftAlignProc(const SkPoint& loc, const SkGlyph& glyph,
17468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          SkIPoint* dst) {
17478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX), SkScalarToFixed(loc.fY));
17488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void centerAlignProc(const SkPoint& loc, const SkGlyph& glyph,
17518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            SkIPoint* dst) {
17528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX) - (glyph.fAdvanceX >> 1),
17538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com             SkScalarToFixed(loc.fY) - (glyph.fAdvanceY >> 1));
17548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void rightAlignProc(const SkPoint& loc, const SkGlyph& glyph,
17578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkIPoint* dst) {
17588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX) - glyph.fAdvanceX,
17598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com             SkScalarToFixed(loc.fY) - glyph.fAdvanceY);
17608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic AlignProc pick_align_proc(SkPaint::Align align) {
17638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static const AlignProc gProcs[] = {
17648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        leftAlignProc, centerAlignProc, rightAlignProc
17658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
1766a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((unsigned)align < SK_ARRAY_COUNT(gProcs));
17688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return gProcs[align];
17708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass TextMapState {
17738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
17748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mutable SkPoint fLoc;
1775a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    TextMapState(const SkMatrix& matrix, SkScalar y)
17778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        : fMatrix(matrix), fProc(matrix.getMapXYProc()), fY(y) {}
17788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef void (*Proc)(const TextMapState&, const SkScalar pos[]);
1780a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Proc pickProc(int scalarsPerPosition);
1782a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
17848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix&     fMatrix;
17858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix::MapXYProc fProc;
17868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            fY; // ignored by MapXYProc
17878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // these are only used by Only... procs
17888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            fScaleX, fTransX, fTransformedY;
17898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapXProc(const TextMapState& state, const SkScalar pos[]) {
17918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fProc(state.fMatrix, *pos, state.fY, &state.fLoc);
17928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1793a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapXYProc(const TextMapState& state, const SkScalar pos[]) {
17958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fProc(state.fMatrix, pos[0], pos[1], &state.fLoc);
17968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1797a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapOnlyScaleXProc(const TextMapState& state,
17998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                  const SkScalar pos[]) {
18008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fLoc.set(SkScalarMul(state.fScaleX, *pos) + state.fTransX,
18018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                       state.fTransformedY);
18028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1803a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapOnlyTransXProc(const TextMapState& state,
18058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                  const SkScalar pos[]) {
18068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fLoc.set(*pos + state.fTransX, state.fTransformedY);
18078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
18098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comTextMapState::Proc TextMapState::pickProc(int scalarsPerPosition) {
18118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1812a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (1 == scalarsPerPosition) {
18148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        unsigned mtype = fMatrix.getType();
18158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (mtype & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
18168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return MapXProc;
18178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
18188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fScaleX = fMatrix.getScaleX();
18198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fTransX = fMatrix.getTranslateX();
18208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fTransformedY = SkScalarMul(fY, fMatrix.getScaleY()) +
18218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            fMatrix.getTranslateY();
18228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return (mtype & SkMatrix::kScale_Mask) ?
18238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        MapOnlyScaleXProc : MapOnlyTransXProc;
18248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
18258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
18268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return MapXYProc;
18278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
18298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
18318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawPosText(const char text[], size_t byteLength,
18338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         const SkScalar pos[], SkScalar constY,
18348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         int scalarsPerPosition, const SkPaint& paint) const {
18358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
18368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
18378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1838f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
18398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
1841045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
18428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
18438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (/*paint.isLinearText() ||*/
18468d430185e08d2067584837a76b7193b803fee7a0tomhudson@google.com        (fMatrix->hasPerspective())) {
18478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // TODO !!!!
18488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//      this->drawText_asPaths(text, byteLength, x, y, paint);
18498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
18508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1852f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkMatrix* matrix = fMatrix;
1853a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawCacheProc     glyphCacheProc = paint.getDrawCacheProc();
1855532470f34dbe9fc0b8b71e3917eca8894feaf336bungeman@google.com    SkAutoGlyphCache    autoCache(paint, &fDevice->fLeakyProperties, matrix);
18568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkGlyphCache*       cache = autoCache.getCache();
1857a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1858045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAAClipBlitterWrapper wrapper;
1859045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkAutoBlitterChoose blitterChooser;
1860045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBlitter* blitter = NULL;
18611d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (needsRasterTextBlit(*this)) {
1862045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitterChooser.choose(*fBitmap, *matrix, paint);
1863045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        blitter = blitterChooser.get();
1864045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        if (fRC->isAA()) {
1865045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            wrapper.init(*fRC, blitter);
1866045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            blitter = wrapper.getBlitter();
1867045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        }
18681d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
1869fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
18708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char*        stop = text + byteLength;
18718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    AlignProc          alignProc = pick_align_proc(paint.getTextAlign());
18722211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    SkDraw1Glyph       d1g;
18732211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache);
1874f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    TextMapState       tms(*matrix, constY);
18758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    TextMapState::Proc tmsProc = tms.pickProc(scalarsPerPosition);
18768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18772211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    if (cache->isSubpixel()) {
18788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // maybe we should skip the rounding if linearText is set
1879cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        SkAxisAlignment roundBaseline = SkComputeAxisAlignmentForHText(*matrix);
18808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (SkPaint::kLeft_Align == paint.getTextAlign()) {
18828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            while (text < stop) {
1883a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                tmsProc(tms, pos);
1885a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18862211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                SkFixed fx = SkScalarToFixed(tms.fLoc.fX) + (SK_FixedHalf >> SkGlyph::kSubBits);
18872211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                SkFixed fy = SkScalarToFixed(tms.fLoc.fY) + (SK_FixedHalf >> SkGlyph::kSubBits);
18887f5350346aa69447d59ef976011746fd3fe6904ebungeman@google.com
1889f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                SkFixed fxMask = ~0;
1890f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                SkFixed fyMask = ~0;
18918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1892cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                if (kX_SkAxisAlignment == roundBaseline) {
1893f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    fyMask = 0;
1894cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                } else if (kY_SkAxisAlignment == roundBaseline) {
1895f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    fxMask = 0;
18968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
1897a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1898f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                const SkGlyph& glyph = glyphCacheProc(cache, &text,
1899f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                                      fx & fxMask, fy & fyMask);
1900a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
19018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (glyph.fWidth) {
1902f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    proc(d1g, fx, fy, glyph);
19038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
19048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pos += scalarsPerPosition;
19058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
19068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
19078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            while (text < stop) {
19089330cfe851b14b8985acc14caa3dc5ad20b0b2a8bungeman@google.com                const char* currentText = text;
19098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const SkGlyph* glyph = &glyphCacheProc(cache, &text, 0, 0);
1910a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
19118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (glyph->fWidth) {
19128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkDEBUGCODE(SkFixed prevAdvX = glyph->fAdvanceX;)
19138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkDEBUGCODE(SkFixed prevAdvY = glyph->fAdvanceY;)
19148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkFixed fx, fy;
1916f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    SkFixed fxMask = ~0;
1917f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    SkFixed fyMask = ~0;
19188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    tmsProc(tms, pos);
1919a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
19208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    {
19218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkIPoint fixedLoc;
19228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        alignProc(tms.fLoc, *glyph, &fixedLoc);
19232211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                        fx = fixedLoc.fX + (SK_FixedHalf >> SkGlyph::kSubBits);
19242211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com                        fy = fixedLoc.fY + (SK_FixedHalf >> SkGlyph::kSubBits);
19258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1926cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                        if (kX_SkAxisAlignment == roundBaseline) {
1927f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            fyMask = 0;
1928cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                        } else if (kY_SkAxisAlignment == roundBaseline) {
1929f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            fxMask = 0;
19308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        }
19318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
1932a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
19338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // have to call again, now that we've been "aligned"
19349330cfe851b14b8985acc14caa3dc5ad20b0b2a8bungeman@google.com                    glyph = &glyphCacheProc(cache, &currentText,
19359330cfe851b14b8985acc14caa3dc5ad20b0b2a8bungeman@google.com                                            fx & fxMask, fy & fyMask);
19368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // the assumption is that the advance hasn't changed
19378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkASSERT(prevAdvX == glyph->fAdvanceX);
19388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkASSERT(prevAdvY == glyph->fAdvanceY);
1939a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1940f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    proc(d1g, fx, fy, *glyph);
19418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
19428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pos += scalarsPerPosition;
19438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
19448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
19458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // not subpixel
1946aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com        if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1947aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com            while (text < stop) {
1948aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                // the last 2 parameters are ignored
1949aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1950fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
1951aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                if (glyph.fWidth) {
1952aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    tmsProc(tms, pos);
1953a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1954aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    proc(d1g,
1955aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         SkScalarToFixed(tms.fLoc.fX) + SK_FixedHalf,
1956aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         SkScalarToFixed(tms.fLoc.fY) + SK_FixedHalf,
1957aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         glyph);
1958aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                }
1959aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                pos += scalarsPerPosition;
1960aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com            }
1961aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com        } else {
1962aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com            while (text < stop) {
1963aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                // the last 2 parameters are ignored
1964aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1965a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1966aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                if (glyph.fWidth) {
1967aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    tmsProc(tms, pos);
1968a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1969aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    SkIPoint fixedLoc;
1970aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    alignProc(tms.fLoc, glyph, &fixedLoc);
1971aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com
1972aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                    proc(d1g,
1973aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         fixedLoc.fX + SK_FixedHalf,
1974aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         fixedLoc.fY + SK_FixedHalf,
1975aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                         glyph);
1976aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                }
1977aeb07864052fde0acba0ae1a42a01db3407f1f6ereed@google.com                pos += scalarsPerPosition;
19788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
19798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
19808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
19818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
19828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if defined _WIN32 && _MSC_VER >= 1300
19848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( pop )
19858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
19868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
19888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPathMeasure.h"
19908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void morphpoints(SkPoint dst[], const SkPoint src[], int count,
19928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkPathMeasure& meas, const SkMatrix& matrix) {
19938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix::MapXYProc proc = matrix.getMapXYProc();
19948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
19968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint pos;
19978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkVector tangent;
19988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        proc(matrix, src[i].fX, src[i].fY, &pos);
20008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sx = pos.fX;
20018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sy = pos.fY;
20028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20038f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com        if (!meas.getPosTan(sx, &pos, &tangent)) {
20048f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com            // set to 0 if the measure failed, so that we just set dst == pos
20058f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com            tangent.set(0, 0);
20068f17b0d5e14fa336f65e33ecf30f15929aa75790reed@google.com        }
20078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        /*  This is the old way (that explains our approach but is way too slow
20098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkMatrix    matrix;
20108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPoint     pt;
20118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pt.set(sx, sy);
20138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.setSinCos(tangent.fY, tangent.fX);
20148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.preTranslate(-sx, 0);
20158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.postTranslate(pos.fX, pos.fY);
20168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.mapPoints(&dst[i], &pt, 1);
20178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        */
20188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy),
20198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                   pos.fY + SkScalarMul(tangent.fX, sy));
20208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  TODO
20248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Need differentially more subdivisions when the follow-path is curvy. Not sure how to
20268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    determine that, but we need it. I guess a cheap answer is let the caller tell us,
20278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
20288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
20298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
20308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      const SkMatrix& matrix) {
20318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath::Iter    iter(src, false);
20328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint         srcP[4], dstP[3];
20338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath::Verb    verb;
20348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
20368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        switch (verb) {
20378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kMove_Verb:
20388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, srcP, 1, meas, matrix);
20398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->moveTo(dstP[0]);
20408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kLine_Verb:
20428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // turn lines into quads to look bendy
20438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX);
20448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY);
20458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, srcP, 2, meas, matrix);
20468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->quadTo(dstP[0], dstP[1]);
20478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kQuad_Verb:
20498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, &srcP[1], 2, meas, matrix);
20508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->quadTo(dstP[0], dstP[1]);
20518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kCubic_Verb:
20538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, &srcP[1], 3, meas, matrix);
20548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->cubicTo(dstP[0], dstP[1], dstP[2]);
20558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kClose_Verb:
20578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->close();
20588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            default:
20600c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com                SkDEBUGFAIL("unknown verb");
20618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
20628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
20638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawTextOnPath(const char text[], size_t byteLength,
20678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPath& follow, const SkMatrix* matrix,
20688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPaint& paint) const {
20698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
20708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
2072045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
20738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
20748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2076166e653f67f3fffc3846184a25ce45ab083f07a2djsollen@google.com    SkTextToPathIter    iter(text, byteLength, paint, true);
20778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPathMeasure       meas(follow, false);
20788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            hOffset = 0;
20798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need to measure first
20818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getTextAlign() != SkPaint::kLeft_Align) {
20828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar pathLen = meas.getLength();
20838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.getTextAlign() == SkPaint::kCenter_Align) {
20848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathLen = SkScalarHalf(pathLen);
20858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
20868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hOffset += pathLen;
20878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPath*   iterPath;
20908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar        xpos;
20918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        scaledMatrix;
20928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar        scale = iter.getPathScale();
20938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    scaledMatrix.setScale(scale, scale);
2095a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
20967b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com    while (iter.next(&iterPath, &xpos)) {
20977b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com        if (iterPath) {
20987b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            SkPath      tmp;
20997b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            SkMatrix    m(scaledMatrix);
21008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21017b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            m.postTranslate(xpos + hOffset, 0);
21027b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            if (matrix) {
21037b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                m.postConcat(*matrix);
21047b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            }
21057b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            morphpath(&tmp, *iterPath, meas, m);
21067b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            if (fDevice) {
21077b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                fDevice->drawPath(*this, tmp, iter.getPaint(), NULL, true);
21087b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            } else {
21097b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com                this->drawPath(tmp, iter.getPaint(), NULL, true);
21107b4531f64cbd85d32a77ceab1bdec8335c5a7864reed@google.com            }
2111f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
21128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
211556c69773aea56c6c6bd47bc7e7970dd081205184djsollen@google.com#ifdef SK_BUILD_FOR_ANDROID
2116cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.comvoid SkDraw::drawPosTextOnPath(const char text[], size_t byteLength,
2117cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                               const SkPoint pos[], const SkPaint& paint,
2118cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                               const SkPath& path, const SkMatrix* matrix) const {
2119cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // nothing to draw
2120045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
2121cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        return;
2122cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
2123cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2124cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkMatrix scaledMatrix;
2125cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkPathMeasure meas(path, false);
2126cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2127cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkMeasureCacheProc glyphCacheProc = paint.getMeasureCacheProc(
2128cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            SkPaint::kForward_TextBufferDirection, true);
2129cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2130cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Copied (modified) from SkTextToPathIter constructor to setup paint
2131cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkPaint tempPaint(paint);
2132cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2133cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    tempPaint.setLinearText(true);
2134cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    tempPaint.setMaskFilter(NULL); // don't want this affecting our path-cache lookup
2135cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2136cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    if (tempPaint.getPathEffect() == NULL && !(tempPaint.getStrokeWidth() > 0
2137cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            && tempPaint.getStyle() != SkPaint::kFill_Style)) {
2138cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        tempPaint.setStyle(SkPaint::kFill_Style);
2139cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        tempPaint.setPathEffect(NULL);
2140cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
2141cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // End copied from SkTextToPathIter constructor
2142cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2143cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // detach cache
2144f38f93c118b71e414c33fe2619ed1d7b1267a7a1bungeman@google.com    SkGlyphCache* cache = tempPaint.detachCache(NULL, NULL);
2145cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2146cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Must set scale, even if 1
2147cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkScalar scale = SK_Scalar1;
2148cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    scaledMatrix.setScale(scale, scale);
2149cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2150cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Loop over all glyph ids
2151cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    for (const char* stop = text + byteLength; text < stop; pos++) {
2152cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2153cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        const SkGlyph& glyph = glyphCacheProc(cache, &text);
2154cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        SkPath tmp;
2155cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2156cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        const SkPath* glyphPath = cache->findPath(glyph);
2157cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        if (glyphPath == NULL) {
2158cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            continue;
2159cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        }
2160cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2161cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        SkMatrix m(scaledMatrix);
2162cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        m.postTranslate(pos->fX, 0);
2163cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2164cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        if (matrix) {
2165cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            m.postConcat(*matrix);
2166cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        }
2167cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2168cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        morphpath(&tmp, *glyphPath, meas, m);
2169cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        this->drawPath(tmp, tempPaint);
2170cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2171cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
2172cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
2173cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // re-attach cache
2174cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkGlyphCache::AttachCache(cache);
2175cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com}
2176cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com#endif
2177cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
21788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
21798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct VertState {
21818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int f0, f1, f2;
21828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState(int vCount, const uint16_t indices[], int indexCount)
21848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            : fIndices(indices) {
21858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCurrIndex = 0;
21868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (indices) {
21878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fCount = indexCount;
21888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
21898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fCount = vCount;
21908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
21918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2192a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
2193a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    typedef bool (*Proc)(VertState*);
21948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Proc chooseProc(SkCanvas::VertexMode mode);
21958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
21978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int             fCount;
21988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int             fCurrIndex;
21998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* fIndices;
2200a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool Triangles(VertState*);
22028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TrianglesX(VertState*);
22038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleStrip(VertState*);
22048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleStripX(VertState*);
22058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleFan(VertState*);
22068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleFanX(VertState*);
22078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
22088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::Triangles(VertState* state) {
22108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = index + 0;
22158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = index + 1;
22168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
22178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 3;
22188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TrianglesX(VertState* state) {
22228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
22238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = indices[index + 0];
22288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = indices[index + 1];
22298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
22308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 3;
22318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleStrip(VertState* state) {
22358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
22408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index & 1) {
22418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = index + 1;
22428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = index + 0;
22438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
22448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = index + 0;
22458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = index + 1;
22468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
22488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleStripX(VertState* state) {
22528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
22538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
22588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index & 1) {
22598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = indices[index + 1];
22608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = indices[index + 0];
22618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
22628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = indices[index + 0];
22638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = indices[index + 1];
22648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
22668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleFan(VertState* state) {
22708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = 0;
22758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = index + 1;
22768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
22778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
22788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleFanX(VertState* state) {
22828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
22838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
22848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
22858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
22868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = indices[0];
22888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = indices[index + 1];
22898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
22908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
22918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
22928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comVertState::Proc VertState::chooseProc(SkCanvas::VertexMode mode) {
22958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (mode) {
22968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangles_VertexMode:
22978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TrianglesX : Triangles;
22988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangleStrip_VertexMode:
22998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TriangleStripX : TriangleStrip;
23008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangleFan_VertexMode:
23018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TriangleFanX : TriangleFan;
23028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
23038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return NULL;
23048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2307045e62d715f5ee9b03deb5af3c750f8318096179reed@google.comtypedef void (*HairProc)(const SkPoint&, const SkPoint&, const SkRasterClip&,
23088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         SkBlitter*);
23098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic HairProc ChooseHairProc(bool doAntiAlias) {
23118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
23128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool texture_to_matrix(const VertState& state, const SkPoint verts[],
23158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPoint texs[], SkMatrix* matrix) {
23168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint src[3], dst[3];
2317a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[0] = texs[state.f0];
23198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[1] = texs[state.f1];
23208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[2] = texs[state.f2];
23218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[0] = verts[state.f0];
23228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[1] = verts[state.f1];
23238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[2] = verts[state.f2];
23248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return matrix->setPolyToPoly(src, dst, 3);
23258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkTriColorShader : public SkShader {
23288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
23298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader() {}
23308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
2332a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
2333a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com    virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
2334a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
233576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    SK_DEVELOPER_TO_STRING()
2336ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTriColorShader)
2337ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com
23388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
23398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader(SkFlattenableReadBuffer& buffer) : SkShader(buffer) {}
2340a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
23428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    fDstToUnit;
23438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPMColor   fColors[3];
2344a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkShader INHERITED;
23468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
23478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkTriColorShader::setup(const SkPoint pts[], const SkColor colors[],
23498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             int index0, int index1, int index2) {
2350a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[0] = SkPreMultiplyColor(colors[index0]);
23528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[1] = SkPreMultiplyColor(colors[index1]);
23538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[2] = SkPreMultiplyColor(colors[index2]);
2354a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix m, im;
23568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.reset();
23578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(0, pts[index1].fX - pts[index0].fX);
23588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(1, pts[index2].fX - pts[index0].fX);
23598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(2, pts[index0].fX);
23608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(3, pts[index1].fY - pts[index0].fY);
23618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(4, pts[index2].fY - pts[index0].fY);
23628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(5, pts[index0].fY);
23638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (!m.invert(&im)) {
23648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
23658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fDstToUnit.setConcat(im, this->getTotalInverse());
23678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
2370689411a87ca7d19abe04b5c9baff26bd96b2123ereed@android.com#include "SkComposeShader.h"
23718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic int ScalarTo256(SkScalar v) {
23738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int scale = SkScalarToFixed(v) >> 8;
23748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (scale < 0) {
23758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        scale = 0;
23768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (scale > 255) {
23788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        scale = 255;
23798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkAlpha255To256(scale);
23818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkTriColorShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
23848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint src;
2385a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
23878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
23888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x += 1;
2389a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
23908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale1 = ScalarTo256(src.fX);
23918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale2 = ScalarTo256(src.fY);
23928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale0 = 256 - scale1 - scale2;
23938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (scale0 < 0) {
23948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (scale1 > scale2) {
23958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                scale2 = 256 - scale1;
23968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } else {
23978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                scale1 = 256 - scale2;
23988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
23998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            scale0 = 0;
24008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2401a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        dstC[i] = SkAlphaMulQ(fColors[0], scale0) +
24038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAlphaMulQ(fColors[1], scale1) +
24048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAlphaMulQ(fColors[2], scale2);
24058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
24078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
240876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#ifdef SK_DEVELOPER
240976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.comvoid SkTriColorShader::toString(SkString* str) const {
241076f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    str->append("SkTriColorShader: (");
241176f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
241276f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    this->INHERITED::toString(str);
241376f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
241476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    str->append(")");
241576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com}
241676f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif
241776f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
24188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
24198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPoint vertices[], const SkPoint textures[],
24208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkColor colors[], SkXfermode* xmode,
24218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const uint16_t indices[], int indexCount,
24228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint& paint) const {
24238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(0 == count || NULL != vertices);
2424a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // abort early if there is nothing to draw
2426045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
24278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
24288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2429a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform out vertices into device coordinates
24318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoSTMalloc<16, SkPoint> storage(count);
24328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint* devVerts = storage.get();
24338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMatrix->mapPoints(devVerts, vertices, count);
2434a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder) {
24368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect bounds;
24378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.set(devVerts, count);
24388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!fBounder->doRect(bounds, paint)) {
24398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
24408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
24418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2442a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*
24448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        We can draw the vertices in 1 of 4 ways:
24458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - solid color (no shader/texture[], no colors[])
24478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - just colors (no shader/texture[], has colors[])
24488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - just texture (has shader/texture[], no colors[])
24498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - colors * texture (has shader/texture[], has colors[])
2450a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Thus for texture drawing, we need both texture[] and a shader.
24528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
24538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader triShader; // must be above declaration of p
24558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint p(paint);
24568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkShader* shader = p.getShader();
24588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == shader) {
24598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if we have no shader, we ignore the texture coordinates
24608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        textures = NULL;
24618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else if (NULL == textures) {
24628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if we don't have texture coordinates, ignore the shader
24638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        p.setShader(NULL);
24648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        shader = NULL;
24658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // setup the custom shader (if needed)
24688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL != colors) {
24698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL == textures) {
24708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // just colors (no texture)
2471a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com            shader = p.setShader(&triShader);
24728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
24738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // colors * texture
24748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(shader);
24758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bool releaseMode = false;
24768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL == xmode) {
2477845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com                xmode = SkXfermode::Create(SkXfermode::kMultiply_Mode);
24788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                releaseMode = true;
24798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
24808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkShader* compose = SkNEW_ARGS(SkComposeShader,
24818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                           (&triShader, shader, xmode));
24828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            p.setShader(compose)->unref();
24838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (releaseMode) {
24848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                xmode->unref();
24858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
2486a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com            shader = compose;
24878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
24888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
2491ea033606a06d05d2d42aa7118409fee798e53167reed@google.com    // important that we abort early, as below we may manipulate the shader
2492ea033606a06d05d2d42aa7118409fee798e53167reed@google.com    // and that is only valid if the shader returned true from setContext.
2493ea033606a06d05d2d42aa7118409fee798e53167reed@google.com    // If it returned false, then our blitter will be the NullBlitter.
2494ea033606a06d05d2d42aa7118409fee798e53167reed@google.com    if (blitter->isNullBlitter()) {
2495ea033606a06d05d2d42aa7118409fee798e53167reed@google.com        return;
2496ea033606a06d05d2d42aa7118409fee798e53167reed@google.com    }
2497ea033606a06d05d2d42aa7118409fee798e53167reed@google.com
24988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // setup our state and function pointer for iterating triangles
24998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState       state(count, indices, indexCount);
25008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState::Proc vertProc = state.chooseProc(vmode);
2501a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
25028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL != textures || NULL != colors) {
2503f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        SkMatrix  tempM;
2504f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        SkMatrix  savedLocalM;
2505f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        if (shader) {
2506f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com            savedLocalM = shader->getLocalMatrix();
2507f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        }
2508a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
25098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while (vertProc(&state)) {
25108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL != textures) {
25118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (texture_to_matrix(state, vertices, textures, &tempM)) {
2512f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com                    tempM.postConcat(savedLocalM);
25138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    shader->setLocalMatrix(tempM);
25148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // need to recal setContext since we changed the local matrix
2515a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com                    shader->endContext();
25168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (!shader->setContext(*fBitmap, p, *fMatrix)) {
25178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        continue;
25188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
25198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
25208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
25218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL != colors) {
25228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (!triShader.setup(vertices, colors,
25238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                     state.f0, state.f1, state.f2)) {
25248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    continue;
25258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
25268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
2527045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com
2528045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkPoint tmp[] = {
2529045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com                devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
2530045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            };
2531045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            SkScan::FillTriangle(tmp, *fRC, blitter.get());
25328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
25338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // now restore the shader's original local matrix
25348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != shader) {
2535f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com            shader->setLocalMatrix(savedLocalM);
25368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
25378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
25388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // no colors[] and no texture
25398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        HairProc hairProc = ChooseHairProc(paint.isAntiAlias());
2540045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        const SkRasterClip& clip = *fRC;
25418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while (vertProc(&state)) {
2542045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get());
2543045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get());
2544045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com            hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get());
25458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
25468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
25478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25490a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
25500a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
25518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
25538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2554f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comvoid SkDraw::validate() const {
25558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fBitmap != NULL);
25568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fMatrix != NULL);
25578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fClip != NULL);
2558045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkASSERT(fRC != NULL);
25598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2560045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    const SkIRect&  cr = fRC->getBounds();
25618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect         br;
25628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2563f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    br.set(0, 0, fBitmap->width(), fBitmap->height());
25648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(cr.isEmpty() || br.contains(cr));
25658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
25688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25690a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
25700a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com
25710a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.comSkBounder::SkBounder() {
25720a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com    // initialize up front. This gets reset by SkCanvas before each draw call.
25730a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com    fClip = &SkRegion::GetEmptyRegion();
25740a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com}
25758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doIRect(const SkIRect& r) {
25778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    rr;
25788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return rr.intersect(fClip->getBounds(), r) && this->onIRect(rr);
25798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2581d055c1fde2128514167b315f4d104b177e04a3dereed@android.com// TODO: change the prototype to take fixed, and update the callers
2582d055c1fde2128514167b315f4d104b177e04a3dereed@android.combool SkBounder::doIRectGlyph(const SkIRect& r, int x, int y,
2583d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                             const SkGlyph& glyph) {
2584474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com    SkIRect    rr;
2585d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    if (!rr.intersect(fClip->getBounds(), r)) {
2586d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        return false;
2587d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    }
2588d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    GlyphRec rec;
2589d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fLSB.set(SkIntToFixed(x), SkIntToFixed(y));
2590d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fRSB.set(rec.fLSB.fX + glyph.fAdvanceX,
2591d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                 rec.fLSB.fY + glyph.fAdvanceY);
2592d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fGlyphID = glyph.getGlyphID();
2593d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fFlags = 0;
2594d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    return this->onIRectGlyph(rr, rec);
2595474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com}
2596474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com
25978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doHairline(const SkPoint& pt0, const SkPoint& pt1,
25988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           const SkPaint& paint) {
25998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect     r;
26008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    v0, v1;
26018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v0 = pt0.fX;
26038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v1 = pt1.fX;
26048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (v0 > v1) {
26058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkTSwap<SkScalar>(v0, v1);
26068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fLeft     = SkScalarFloor(v0);
26088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fRight    = SkScalarCeil(v1);
26098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v0 = pt0.fY;
26118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v1 = pt1.fY;
26128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (v0 > v1) {
26138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkTSwap<SkScalar>(v0, v1);
26148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fTop      = SkScalarFloor(v0);
26168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fBottom   = SkScalarCeil(v1);
26178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.isAntiAlias()) {
26198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(-1, -1);
26208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
26228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
26238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doRect(const SkRect& rect, const SkPaint& paint) {
26258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    r;
26268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getStyle() == SkPaint::kFill_Style) {
26288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rect.round(&r);
26298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
26308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int rad = -1;
26318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rect.roundOut(&r);
26328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.isAntiAlias()) {
26338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            rad = -2;
26348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
26358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(rad, rad);
26368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
26388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
26398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doPath(const SkPath& path, const SkPaint& paint, bool doFill) {
2641d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    SkIRect       r;
2642d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    const SkRect& bounds = path.getBounds();
26438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (doFill) {
26458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.round(&r);
26468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // hairline
26478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.roundOut(&r);
26488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.isAntiAlias()) {
26518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(-1, -1);
26528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
26548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
26558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkBounder::commit() {
26578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // override in subclass
26588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
26598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////////////////////////
26618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPath.h"
26638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDraw.h"
26648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRegion.h"
26658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBlitter.h"
26668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds,
266830711b764be6bbb58caa30a0ac5d1474c894efe7reed@google.com                       const SkMaskFilter* filter, const SkMatrix* filterMatrix,
26698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkIRect* bounds) {
26708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (devPath.isEmpty()) {
26718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
26728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //  init our bounds from the path
26758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
2676d252db03d9650013b545ef9781fe993c07f8f314reed@android.com        SkRect pathBounds = devPath.getBounds();
26778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathBounds.inset(-SK_ScalarHalf, -SK_ScalarHalf);
26788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathBounds.roundOut(bounds);
26798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2680a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
26816db75fc2c393ba86a3f533597a8bcd348477e79ctomhudson@google.com    SkIPoint margin = SkIPoint::Make(0, 0);
26828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (filter) {
26838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(filterMatrix);
2684a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
26855af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        SkMask srcM, dstM;
2686a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
26878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fBounds = *bounds;
26888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fFormat = SkMask::kA8_Format;
26898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fImage = NULL;
26908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
26918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return false;
26928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
26938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2694a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
26955af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com    // (possibly) trim the bounds to reflect the clip
26968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // (plus whatever slop the filter needs)
26975af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com    if (clipBounds) {
26985af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        SkIRect tmp = *clipBounds;
26993555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // Ugh. Guard against gigantic margins from wacky filters. Without this
27003555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // check we can request arbitrary amounts of slop beyond our visible
27013555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // clip, and bring down the renderer (at least on finite RAM machines
27023555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // like handsets, etc.). Need to balance this invented value between
27033555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // quality of large filters like blurs, and the corresponding memory
27043555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // requests.
27053555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        static const int MAX_MARGIN = 128;
27063555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        tmp.inset(-SkMin32(margin.fX, MAX_MARGIN),
27073555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com                  -SkMin32(margin.fY, MAX_MARGIN));
27085af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        if (!bounds->intersect(tmp)) {
27095af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com            return false;
27105af16f8d670b3ce1c7644a4737e02e2e2257614ebungeman@google.com        }
27118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
27128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
27148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
27158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27162ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.orgstatic void draw_into_mask(const SkMask& mask, const SkPath& devPath,
27172ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                           SkPaint::Style style) {
2718045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkBitmap        bm;
2719045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkDraw          draw;
2720045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkRasterClip    clip;
2721045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkMatrix        matrix;
2722045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    SkPaint         paint;
27238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), mask.fBounds.height(), mask.fRowBytes);
27258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bm.setPixels(mask.fImage);
27268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2727045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height()));
27288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
27298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        -SkIntToScalar(mask.fBounds.fTop));
27308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fBitmap    = &bm;
2732045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    draw.fRC        = &clip;
2733045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com    draw.fClip      = &clip.bwRgn();
27348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix    = &matrix;
27358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fBounder   = NULL;
27368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    paint.setAntiAlias(true);
27372ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org    paint.setStyle(style);
27388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.drawPath(devPath, paint);
27398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
27408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
274230711b764be6bbb58caa30a0ac5d1474c894efe7reed@google.com                        const SkMaskFilter* filter, const SkMatrix* filterMatrix,
27432ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                        SkMask* mask, SkMask::CreateMode mode,
27442ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                        SkPaint::Style style) {
27458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kJustRenderImage_CreateMode != mode) {
27468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds))
27478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return false;
27488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2749a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
27508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
27518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask->fFormat = SkMask::kA8_Format;
27528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask->fRowBytes = mask->fBounds.width();
2753543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        size_t size = mask->computeImageSize();
2754543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        if (0 == size) {
2755543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            // we're too big to allocate the mask, abort
2756543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            return false;
2757543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        }
2758543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        mask->fImage = SkMask::AllocImage(size);
27598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        memset(mask->fImage, 0, mask->computeImageSize());
27608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
27618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kJustComputeBounds_CreateMode != mode) {
27632ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org        draw_into_mask(*mask, devPath, style);
27648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2765a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
27668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
27678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2768