SkDraw.cpp revision cb6ccdde5120ec45df208c0b958b263d8252a505
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
8ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDraw.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBlitter.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBounder.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDevice.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkMaskFilter.h"
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPaint.h"
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPathEffect.h"
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRasterizer.h"
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkScan.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkShader.h"
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkStroke.h"
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTemplatesPriv.h"
2432e5d97ccf60f859db063ebd6e903c362e625767reed@google.com#include "SkTLazy.h"
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h"
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkAutoKern.h"
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBitmapProcShader.h"
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDrawProcs.h"
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//#define TRACE_BITMAP_DRAWS
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define kBlitterStorageLongCount    (sizeof(SkBitmapProcShader) >> 2)
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
35fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com/** Helper for allocating small blitters on the stack.
36fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com */
3740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.comclass SkAutoBlitterChoose : SkNoncopyable {
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
391d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    SkAutoBlitterChoose() {
401d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com        fBlitter = NULL;
411d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose(const SkBitmap& device, const SkMatrix& matrix,
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        const SkPaint& paint) {
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fBlitter = SkBlitter::Choose(device, matrix, paint,
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                     fStorage, sizeof(fStorage));
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
471d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoBlitterChoose();
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*  operator->() { return fBlitter; }
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*  get() const { return fBlitter; }
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
531d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    void choose(const SkBitmap& device, const SkMatrix& matrix,
541d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com                const SkPaint& paint) {
551d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com        SkASSERT(!fBlitter);
561d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com        fBlitter = SkBlitter::Choose(device, matrix, paint,
571d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com                                     fStorage, sizeof(fStorage));
581d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
591d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*  fBlitter;
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t    fStorage[kBlitterStorageLongCount];
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkAutoBlitterChoose::~SkAutoBlitterChoose() {
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if ((void*)fBlitter == (void*)fStorage) {
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fBlitter->~SkBlitter();
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDELETE(fBlitter);
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com/**
7440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com *  Since we are providing the storage for the shader (to avoid the perf cost
7540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com *  of calling new) we insist that in our destructor we can account for all
7640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com *  owners of the shader.
7740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com */
7840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.comclass SkAutoBitmapShaderInstall : SkNoncopyable {
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
8040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkAutoBitmapShaderInstall(const SkBitmap& src, const SkPaint& paint)
8140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            : fPaint(paint) /* makes a copy of the paint */ {
8240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        fPaint.setShader(SkShader::CreateBitmapShader(src,
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           fStorage, sizeof(fStorage)));
8540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        // we deliberately left the shader with an owner-count of 2
8640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkASSERT(2 == fPaint.getShader()->getRefCnt());
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8882065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoBitmapShaderInstall() {
9040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkShader* shader = fPaint.getShader();
9140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        // since we manually destroy shader, we insist that owners == 2
9240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkASSERT(2 == shader->getRefCnt());
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        fPaint.setShader(NULL); // unref the shader by 1
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        // now destroy to take care of the 2nd owner-count
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if ((void*)shader == (void*)fStorage) {
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shader->~SkShader();
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkDELETE(shader);
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10382065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
10440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    // return the new paint that has the shader applied
10540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    const SkPaint& paintWithShader() const { return fPaint; }
10682065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
10840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkPaint     fPaint; // copy of caller's paint (which we then modify)
10940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    uint32_t    fStorage[kBlitterStorageLongCount];
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
114f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comSkDraw::SkDraw() {
115f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    sk_bzero(this, sizeof(*this));
116f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
117f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDraw::SkDraw(const SkDraw& src) {
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memcpy(this, &src, sizeof(*this));
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data);
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) {
1274516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com    sk_bzero(pixels, bytes);
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {}
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    sk_memset32((uint32_t*)pixels, data, bytes >> 2);
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    sk_memset16((uint16_t*)pixels, data, bytes >> 1);
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memset(pixels, data, bytes);
1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic BitmapXferProc ChooseBitmapXferProc(const SkBitmap& bitmap,
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                           const SkPaint& paint,
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                           uint32_t* data) {
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // todo: we can apply colorfilter up front if no shader, so we wouldn't
1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need to abort this fastpath
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getShader() || paint.getColorFilter()) {
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return NULL;
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
153845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com    SkXfermode::Mode mode;
154845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com    if (!SkXfermode::IsMode(paint.getXfermode(), &mode)) {
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return NULL;
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
157a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkColor color = paint.getColor();
159a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // collaps modes based on color...
161845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com    if (SkXfermode::kSrcOver_Mode == mode) {
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        unsigned alpha = SkColorGetA(color);
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (0 == alpha) {
164845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com            mode = SkXfermode::kDst_Mode;
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else if (0xFF == alpha) {
166845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com            mode = SkXfermode::kSrc_Mode;
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
169a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (mode) {
171845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com        case SkXfermode::kClear_Mode:
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//            SkDebugf("--- D_Clear_BitmapXferProc\n");
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return D_Clear_BitmapXferProc;  // ignore data
174845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com        case SkXfermode::kDst_Mode:
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//            SkDebugf("--- D_Dst_BitmapXferProc\n");
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return D_Dst_BitmapXferProc;    // ignore data
177845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com        case SkXfermode::kSrc_Mode: {
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            /*
179a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com                should I worry about dithering for the lower depths?
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            */
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPMColor pmc = SkPreMultiplyColor(color);
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            switch (bitmap.config()) {
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kARGB_8888_Config:
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = pmc;
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- D32_Src_BitmapXferProc\n");
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return D32_Src_BitmapXferProc;
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kARGB_4444_Config:
1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = SkPixel32ToPixel4444(pmc);
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- D16_Src_BitmapXferProc\n");
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return D16_Src_BitmapXferProc;
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kRGB_565_Config:
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = SkPixel32ToPixel16(pmc);
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- D16_Src_BitmapXferProc\n");
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return D16_Src_BitmapXferProc;
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                case SkBitmap::kA8_Config:
2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (data) {
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        *data = SkGetPackedA32(pmc);
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//                    SkDebugf("--- DA8_Src_BitmapXferProc\n");
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    return DA8_Src_BitmapXferProc;
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                default:
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    break;
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return NULL;
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void CallBitmapXferProc(const SkBitmap& bitmap, const SkIRect& rect,
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                               BitmapXferProc proc, uint32_t procData) {
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int shiftPerPixel;
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (bitmap.config()) {
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kARGB_8888_Config:
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shiftPerPixel = 2;
2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kARGB_4444_Config:
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kRGB_565_Config:
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shiftPerPixel = 1;
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkBitmap::kA8_Config:
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            shiftPerPixel = 0;
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(!"Can't use xferproc on this config");
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint8_t* pixels = (uint8_t*)bitmap.getPixels();
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(pixels);
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const size_t rowBytes = bitmap.rowBytes();
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const int widthBytes = rect.width() << shiftPerPixel;
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // skip down to the first scanline and X position
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel);
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int scans = rect.height() - 1; scans >= 0; --scans) {
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        proc(pixels, widthBytes, procData);
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pixels += rowBytes;
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawPaint(const SkPaint& paint) const {
251f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fClip->isEmpty()) {
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    devRect;
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    devRect.set(0, 0, fBitmap->width(), fBitmap->height());
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder && !fBounder->doIRect(devRect)) {
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
262a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*  If we don't have a shader (i.e. we're just a solid color) we may
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        be faster to operate directly on the device bitmap, rather than invoking
2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a blitter. Esp. true for xfermodes, which require a colorshader to be
2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        present, which is just redundant work. Since we're drawing everywhere
2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        in the clip, we don't have to worry about antialiasing.
2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t procData = 0;  // to avoid the warning
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    BitmapXferProc proc = ChooseBitmapXferProc(*fBitmap, paint, &procData);
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (proc) {
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (D_Dst_BitmapXferProc == proc) { // nothing to do
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
275a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRegion::Iterator iter(*fClip);
2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while (!iter.done()) {
2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            CallBitmapXferProc(*fBitmap, iter.rect(), proc, procData);
2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            iter.next();
2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // normal case: use a blitter
2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScan::FillIRect(devRect, fClip, blitter.get());
2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct PtProcRec {
2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkCanvas::PointMode fMode;
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPaint*  fPaint;
2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkRegion* fClip;
294a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // computed values
2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed fRadius;
297a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count,
2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         SkBlitter*);
3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix,
302a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com              const SkRegion* clip);
3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Proc chooseProc(SkBlitter* blitter);
3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                 int count, SkBlitter* blitter) {
3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(rec.fClip->isRect());
3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkIRect& r = rec.fClip->getBounds();
310a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int x = SkScalarFloor(devPts[i].fX);
3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int y = SkScalarFloor(devPts[i].fY);
3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (r.contains(x, y)) {
3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            blitter->blitH(x, y, 1);
3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_pt_rect_16_hair_proc(const PtProcRec& rec,
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                    const SkPoint devPts[], int count,
3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                    SkBlitter* blitter) {
3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(rec.fClip->isRect());
3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkIRect& r = rec.fClip->getBounds();
3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t value;
3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(bitmap);
328a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint16_t* addr = bitmap->getAddr16(0, 0);
3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int rb = bitmap->rowBytes();
3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int x = SkScalarFloor(devPts[i].fX);
3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int y = SkScalarFloor(devPts[i].fY);
3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (r.contains(x, y)) {
3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//            *bitmap->getAddr16(x, y) = SkToU16(value);
3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value);
3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            int count, SkBlitter* blitter) {
3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int x = SkScalarFloor(devPts[i].fX);
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int y = SkScalarFloor(devPts[i].fY);
3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (rec.fClip->contains(x, y)) {
3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            blitter->blitH(x, y, 1);
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i += 2) {
3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScan::HairLine(devPts[i], devPts[i+1], rec.fClip, blitter);
3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count - 1; i++) {
3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScan::HairLine(devPts[i], devPts[i+1], rec.fClip, blitter);
3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// aa versions
3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i += 2) {
3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScan::AntiHairLine(devPts[i], devPts[i+1], rec.fClip, blitter);
3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              int count, SkBlitter* blitter) {
3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count - 1; i++) {
3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScan::AntiHairLine(devPts[i], devPts[i+1], rec.fClip, blitter);
3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// square procs (strokeWidth > 0 but matrix is square-scale (sx == sy)
3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[],
3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           int count, SkBlitter* blitter) {
3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkFixed radius = rec.fRadius;
3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed x = SkScalarToFixed(devPts[i].fX);
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed y = SkScalarToFixed(devPts[i].fY);
391a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkXRect r;
3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fLeft = x - radius;
3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fTop = y - radius;
3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fRight = x + radius;
3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fBottom = y + radius;
397a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScan::FillXRect(r, rec.fClip, blitter);
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[],
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           int count, SkBlitter* blitter) {
4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkFixed radius = rec.fRadius;
4058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
4068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed x = SkScalarToFixed(devPts[i].fX);
4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkFixed y = SkScalarToFixed(devPts[i].fY);
408a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkXRect r;
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fLeft = x - radius;
4118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fTop = y - radius;
4128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fRight = x + radius;
4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.fBottom = y + radius;
414a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
4158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScan::AntiFillXRect(r, rec.fClip, blitter);
4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
419b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com// If this guy returns true, then chooseProc() must return a valid proc
4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint,
4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                     const SkMatrix* matrix, const SkRegion* clip) {
4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getPathEffect()) {
4238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
4248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar width = paint.getStrokeWidth();
4268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (0 == width) {
4278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fMode = mode;
4288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPaint = &paint;
4298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fClip = clip;
4308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fRadius = SK_Fixed1 >> 1;
4318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return true;
4328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
433b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com    if (paint.getStrokeCap() != SkPaint::kRound_Cap &&
434b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com            matrix->rectStaysRect() && SkCanvas::kPoints_PointMode == mode) {
4358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sx = matrix->get(SkMatrix::kMScaleX);
4368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sy = matrix->get(SkMatrix::kMScaleY);
4378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (SkScalarNearlyZero(sx - sy)) {
4388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (sx < 0) {
4398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                sx = -sx;
4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fMode = mode;
4438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fPaint = &paint;
4448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fClip = clip;
4458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1;
4468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return true;
4478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
4488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return false;
4508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comPtProcRec::Proc PtProcRec::chooseProc(SkBlitter* blitter) {
453b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com    Proc proc = NULL;
454a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
4558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // for our arrays
4568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(0 == SkCanvas::kPoints_PointMode);
4578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == SkCanvas::kLines_PointMode);
4588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(2 == SkCanvas::kPolygon_PointMode);
4598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode);
4608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // first check for hairlines
4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (0 == fPaint->getStrokeWidth()) {
4638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fPaint->isAntiAlias()) {
4648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            static const Proc gAAProcs[] = {
4658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc
4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            };
4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc = gAAProcs[fMode];
4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
4698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) {
4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                uint32_t value;
4718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const SkBitmap* bm = blitter->justAnOpaqueColor(&value);
4728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (bm && bm->config() == SkBitmap::kRGB_565_Config) {
4738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    proc = bw_pt_rect_16_hair_proc;
4748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                } else {
4758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    proc = bw_pt_rect_hair_proc;
4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } else {
4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                static Proc gBWProcs[] = {
4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc
4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                };
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                proc = gBWProcs[fMode];
4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
484b4f404ac4195e5b1f49e49c591bd69f98b246f9breed@android.com    } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) {
4858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(SkCanvas::kPoints_PointMode == fMode);
4868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fPaint->isAntiAlias()) {
4878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc = aa_square_proc;
4888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
4898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc = bw_square_proc;
4908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
4918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return proc;
4938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool bounder_points(SkBounder* bounder, SkCanvas::PointMode mode,
4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           size_t count, const SkPoint pts[],
4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           const SkPaint& paint, const SkMatrix& matrix) {
4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect ibounds;
4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect bounds;
5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar inset = paint.getStrokeWidth();
5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.set(pts, count);
5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.inset(-inset, -inset);
5048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.mapRect(&bounds);
5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.roundOut(&ibounds);
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return bounder->doIRect(ibounds);
5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// each of these costs 8-bytes of stack space, so don't make it too large
5118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// must be even for lines/polygon to work
5128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define MAX_DEV_PTS     32
5138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count,
515f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        const SkPoint pts[], const SkPaint& paint,
516f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        bool forceUseDevice) const {
5178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // if we're in lines mode, force count to be even
5188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkCanvas::kLines_PointMode == mode) {
5198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        count &= ~(size_t)1;
5208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if ((long)count <= 0) {
5238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
5248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
525a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
5268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(pts != NULL);
527f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
528a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
5298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     // nothing to draw
5308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fClip->isEmpty() ||
5318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
5328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
5338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
535fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    if (fBounder) {
536fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        if (!bounder_points(fBounder, mode, count, pts, paint, *fMatrix)) {
537fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com            return;
538fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        }
539fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com
540fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        // clear the bounder and call this again, so we don't invoke the bounder
541fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        // later if we happen to call ourselves for drawRect, drawPath, etc.
542fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        SkDraw noBounder(*this);
543fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        noBounder.fBounder = NULL;
544fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        noBounder.drawPoints(mode, count, pts, paint, forceUseDevice);
545fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com        return;
546fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    }
547fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com
5488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    PtProcRec rec;
549f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (!forceUseDevice && rec.init(mode, paint, fMatrix, fClip)) {
5508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
5518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint             devPts[MAX_DEV_PTS];
5538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkMatrix*     matrix = fMatrix;
5548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkBlitter*          bltr = blitter.get();
5558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        PtProcRec::Proc     proc = rec.chooseProc(bltr);
5568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // we have to back up subsequent passes if we're in polygon mode
5578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const size_t backup = (SkCanvas::kPolygon_PointMode == mode);
558a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
5598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        do {
5608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            size_t n = count;
5618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (n > MAX_DEV_PTS) {
5628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                n = MAX_DEV_PTS;
5638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
5648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix->mapPoints(devPts, pts, n);
5658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            proc(rec, devPts, n, bltr);
5668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pts += n - backup;
5678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(count >= n);
5688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            count -= n;
5698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (count > 0) {
5708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                count += backup;
5718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
5728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } while (count != 0);
5738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
5748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        switch (mode) {
5758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kPoints_PointMode: {
5768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // temporarily mark the paint as filling.
57740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                SkPaint newPaint(paint);
57840c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                newPaint.setStyle(SkPaint::kFill_Style);
5798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
58040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                SkScalar width = newPaint.getStrokeWidth();
5818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkScalar radius = SkScalarHalf(width);
582a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
58340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) {
5848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkPath      path;
5858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkMatrix    preMatrix;
586a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
5878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.addCircle(0, 0, radius);
5888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    for (size_t i = 0; i < count; i++) {
5898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        preMatrix.setTranslate(pts[i].fX, pts[i].fY);
5908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        // pass true for the last point, since we can modify
5918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        // then path then
592f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        if (fDevice) {
59340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            fDevice->drawPath(*this, path, newPaint, &preMatrix,
594f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                              (count-1) == i);
595f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        } else {
59640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            this->drawPath(path, newPaint, &preMatrix,
59740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                                           (count-1) == i);
598f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        }
5998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
6008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                } else {
6018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkRect  r;
602a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
6038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    for (size_t i = 0; i < count; i++) {
6048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fLeft = pts[i].fX - radius;
6058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fTop = pts[i].fY - radius;
6068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fRight = r.fLeft + width;
6078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        r.fBottom = r.fTop + width;
608f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        if (fDevice) {
60940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            fDevice->drawRect(*this, r, newPaint);
610f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        } else {
61140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                            this->drawRect(r, newPaint);
612f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        }
6138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
6148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
6158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
6168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
6178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kLines_PointMode:
6188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkCanvas::kPolygon_PointMode: {
6198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                count -= 1;
6208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkPath path;
6218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkPaint p(paint);
6228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                p.setStyle(SkPaint::kStroke_Style);
6238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
6248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                for (size_t i = 0; i < count; i += inc) {
6258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.moveTo(pts[i]);
6268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.lineTo(pts[i+1]);
627f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    if (fDevice) {
628f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        fDevice->drawPath(*this, path, p, NULL, true);
629f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    } else {
630f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        this->drawPath(path, p, NULL, true);
631f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    }
6328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    path.rewind();
6338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
6348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
6358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
6368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
6378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
6388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic inline SkPoint* as_lefttop(SkRect* r) {
6418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (SkPoint*)(void*)r;
6428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic inline SkPoint* as_rightbottom(SkRect* r) {
6458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return ((SkPoint*)(void*)r) + 1;
6468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
648761fb62b0eb174783316d2a8b933fba896ca6355reed@google.comstatic bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix,
649761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                           SkPoint* strokeSize) {
650761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    if (SkPaint::kMiter_Join != paint.getStrokeJoin() ||
651761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com        paint.getStrokeMiter() < SK_ScalarSqrt2) {
652761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com        return false;
653761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    }
65462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
655761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    SkASSERT(matrix.rectStaysRect());
656761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() };
657761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    matrix.mapVectors(strokeSize, &pt, 1);
6586115338c59799b4ef09cda187f23867dea093f6ereed@google.com    strokeSize->fX = SkScalarAbs(strokeSize->fX);
6596115338c59799b4ef09cda187f23867dea093f6ereed@google.com    strokeSize->fY = SkScalarAbs(strokeSize->fY);
660761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com    return true;
6617ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org}
6627ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org
66362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.comSkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint,
66462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com                                         const SkMatrix& matrix,
66562ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com                                         SkPoint* strokeSize) {
6667ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    RectType rtype;
6677ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    const SkScalar width = paint.getStrokeWidth();
66862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    const bool zeroWidth = (0 == width);
6697ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    SkPaint::Style style = paint.getStyle();
67062ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
6717ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) {
6727ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        style = SkPaint::kFill_Style;
6737ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    }
67462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
6758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getPathEffect() || paint.getMaskFilter() ||
67662ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        paint.getRasterizer() || !matrix.rectStaysRect() ||
6777ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        SkPaint::kStrokeAndFill_Style == style) {
67862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        rtype = kPath_RectType;
67962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    } else if (SkPaint::kFill_Style == style) {
6807ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kFill_RectType;
6817ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    } else if (zeroWidth) {
6827ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kHair_RectType;
68362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    } else if (easy_rect_join(paint, matrix, strokeSize)) {
6847ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kStroke_RectType;
6857ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    } else {
6867ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        rtype = kPath_RectType;
6877ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    }
68862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    return rtype;
68962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com}
69062ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
69140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.comstatic SkPoint* rect_points(SkRect& r, int index) {
69240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkASSERT((unsigned)index < 2);
69340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    return &((SkPoint*)(void*)&r)[index];
69440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com}
69540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com
69662ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.comvoid SkDraw::drawRect(const SkRect& rect, const SkPaint& paint) const {
69762ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    SkDEBUGCODE(this->validate();)
6987ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org
69962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    // nothing to draw
70062ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    if (fClip->isEmpty() ||
70162ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
70262ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        return;
70362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    }
70462ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
70562ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    SkPoint strokeSize;
70662ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize);
70762ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
70862ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com#ifdef SK_DISABLE_FAST_AA_STROKE_RECT
70962ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    if (kStroke_RectType == rtype && paint.isAntiAlias()) {
71062ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com        rtype = kPath_RectType;
71162ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com    }
71262ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com#endif
71362ab7addb06bbc5b93460eaf2f70a9f8399308d3reed@google.com
7147ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    if (kPath_RectType == rtype) {
7158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPath  tmp;
7168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tmp.addRect(rect);
7178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tmp.setFillType(SkPath::kWinding_FillType);
718187d5595901d1120d9425851e5afdd773f574502reed@android.com        this->drawPath(tmp, paint, NULL, true);
7198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
7208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix& matrix = *fMatrix;
7238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect          devRect;
7248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform rect into devRect
7268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
7278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        matrix.mapXY(rect.fLeft, rect.fTop, rect_points(devRect, 0));
7288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        matrix.mapXY(rect.fRight, rect.fBottom, rect_points(devRect, 1));
7298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        devRect.sort();
7308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder && !fBounder->doRect(devRect, paint)) {
7338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
7348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // look for the quick exit, before we build a blitter
7378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
7388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkIRect ir;
7398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        devRect.roundOut(&ir);
74055e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        if (paint.getStyle() != SkPaint::kFill_Style) {
74155e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            // extra space for hairlines
74255e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            ir.inset(-1, -1);
74355e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        }
7448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fClip->quickReject(ir))
7458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
7468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose blitterStorage(*fBitmap, matrix, paint);
7498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBlitter*          blitter = blitterStorage.get();
7508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkRegion*     clip = fClip;
7518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
752b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
753b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // case we are also hairline (if we've gotten to here), which devolves to
754b641c9f20beb36dbf4e8df53a199c84cef3be9b2reed@android.com    // effectively just kFill
7557ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org    switch (rtype) {
7567ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kFill_RectType:
7577ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
7587ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::AntiFillRect(devRect, clip, blitter);
7597ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
7607ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::FillRect(devRect, clip, blitter);
7617ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
7627ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
7637ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kStroke_RectType:
7647ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
765761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                SkScan::AntiFrameRect(devRect, strokeSize, clip, blitter);
7667ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
767761fb62b0eb174783316d2a8b933fba896ca6355reed@google.com                SkScan::FrameRect(devRect, strokeSize, clip, blitter);
7687ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
7697ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
7707ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        case kHair_RectType:
7717ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            if (paint.isAntiAlias()) {
7727ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::AntiHairRect(devRect, clip, blitter);
7737ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            } else {
7747ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org                SkScan::HairRect(devRect, clip, blitter);
7757ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            }
7767ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            break;
7777ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org        default:
7787ff678bc1618dc669648198a7bdca8adfb189505mike@reedtribe.org            SkASSERT(!"bad rtype");
7798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const {
7838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (srcM.fBounds.isEmpty()) {
7848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
7858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMask          dstM;
7888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMask*   mask = &srcM;
7898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dstM.fImage = NULL;
7918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoMaskImage ami(&dstM, false);
7928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getMaskFilter() &&
7948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, NULL)) {
7958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask = &dstM;
7968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder && !fBounder->doIRect(mask->fBounds)) {
7998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
8008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
8038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    blitter->blitMaskRegion(*mask, *fClip);
8058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
807ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.comstatic SkScalar fast_len(const SkVector& vec) {
808ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar x = SkScalarAbs(vec.fX);
809ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar y = SkScalarAbs(vec.fY);
810ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    if (x < y) {
811ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        SkTSwap(x, y);
812ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
813ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    return x + SkScalarHalf(y);
814ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com}
815ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com
816ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com// our idea is to return true if there is no appreciable skew or non-square scale
817ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com// for that we'll transform (0,1) and (1,0), and check that the resulting dot-prod
818ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com// is nearly one
819ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.comstatic bool map_radius(const SkMatrix& matrix, SkScalar* value) {
8208d430185e08d2067584837a76b7193b803fee7a0tomhudson@google.com    if (matrix.hasPerspective()) {
821ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        return false;
822ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
823ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkVector src[2], dst[2];
824ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    src[0].set(*value, 0);
825ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    src[1].set(0, *value);
826ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    matrix.mapVectors(dst, src, 2);
827ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar len0 = fast_len(dst[0]);
828ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    SkScalar len1 = fast_len(dst[1]);
829652807bbc8c57e5fa9622126b51fd369f5c67935agl@chromium.org    if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) {
830ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        *value = SkScalarAve(len0, len1);
831ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        return true;
832ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    }
833ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    return false;
834ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com}
835ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com
83632e5d97ccf60f859db063ebd6e903c362e625767reed@google.comvoid SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint,
8378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      const SkMatrix* prePathMatrix, bool pathIsMutable) const {
838f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
8398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
8418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fClip->isEmpty() ||
84232e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        (origPaint.getAlpha() == 0 && origPaint.getXfermode() == NULL)) {
8438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
8448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath*         pathPtr = (SkPath*)&origSrcPath;
8478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool            doFill = true;
8488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath          tmpPath;
8498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        tmpMatrix;
8508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix* matrix = fMatrix;
8518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (prePathMatrix) {
85332e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style ||
85432e5d97ccf60f859db063ebd6e903c362e625767reed@google.com                origPaint.getRasterizer()) {
8558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPath* result = pathPtr;
856a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
8578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!pathIsMutable) {
8588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                result = &tmpPath;
8598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pathIsMutable = true;
8608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
8618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathPtr->transform(*prePathMatrix, result);
8628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathPtr = result;
8638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
8648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!tmpMatrix.setConcat(*matrix, *prePathMatrix)) {
8658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // overflow
8668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
8678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
8688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix = &tmpMatrix;
8698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
8708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // at this point we're done with prePathMatrix
8728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
873a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
8748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*
8758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If the device thickness < 1.0, then make it a hairline, and
8768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        modulate alpha if the thickness is even smaller (e.g. thickness == 0.5
8778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        should modulate the alpha by 1/2)
87832e5d97ccf60f859db063ebd6e903c362e625767reed@google.com     */
87932e5d97ccf60f859db063ebd6e903c362e625767reed@google.com
88032e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    const SkPaint* paint = &origPaint;
88132e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    SkTLazy<SkPaint> lazyPaint;
882a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
883ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    // can we approximate a thin (but not hairline) stroke with an alpha-modulated
884ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    // hairline? Only if the matrix scales evenly in X and Y, and the device-width is
885ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com    // less than a pixel
88632e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (origPaint.isAntiAlias() &&
88732e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            origPaint.getStyle() == SkPaint::kStroke_Style &&
88832e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            origPaint.getXfermode() == NULL) {
88932e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        SkScalar width = origPaint.getStrokeWidth();
890ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com        if (width > 0 && map_radius(*matrix, &width)) {
891ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com            int scale = (int)SkScalarMul(width, 256);
89232e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            int alpha = origPaint.getAlpha() * scale >> 8;
893a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
894ebdeeb8a018f2df01e190fd961d68a94f0e0fcb9reed@android.com            // pretend to be a hairline, with a modulated alpha
89532e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            lazyPaint.set(origPaint);
89632e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            lazyPaint.get()->setAlpha(alpha);
89732e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            lazyPaint.get()->setStrokeWidth(0);
89832e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            paint = lazyPaint.get();
8998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
9008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
901a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
90232e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) {
90332e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        doFill = paint->getFillPath(*pathPtr, &tmpPath);
9048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathPtr = &tmpPath;
9058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
906a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
90732e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (paint->getRasterizer()) {
9088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
90932e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->getRasterizer()->rasterize(*pathPtr, *matrix,
91032e5d97ccf60f859db063ebd6e903c362e625767reed@google.com                            &fClip->getBounds(), paint->getMaskFilter(), &mask,
9118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
91232e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            this->drawDevMask(mask, *paint);
9138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkMask::FreeImage(mask.fImage);
9148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
9158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
9168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // avoid possibly allocating a new path in transform if we can
9198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
9208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform the path into device space
9228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pathPtr->transform(*matrix, devPathPtr);
9238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
92432e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, *paint);
9258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // how does filterPath() know to fill or hairline the path??? <mrr>
92732e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (paint->getMaskFilter() &&
92832e5d97ccf60f859db063ebd6e903c362e625767reed@google.com            paint->getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fClip,
92932e5d97ccf60f859db063ebd6e903c362e625767reed@google.com                                               fBounder, blitter.get())) {
9308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return; // filterPath() called the blitter, so we're done
9318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
93332e5d97ccf60f859db063ebd6e903c362e625767reed@google.com    if (fBounder && !fBounder->doPath(*devPathPtr, *paint, doFill)) {
9348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
9358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (doFill) {
93832e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->isAntiAlias()) {
9398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkScan::AntiFillPath(*devPathPtr, *fClip, blitter.get());
9408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
9418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkScan::FillPath(*devPathPtr, *fClip, blitter.get());
9428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
9438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // hairline
94432e5d97ccf60f859db063ebd6e903c362e625767reed@google.com        if (paint->isAntiAlias()) {
9458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkScan::AntiHairPath(*devPathPtr, fClip, blitter.get());
9468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
9478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkScan::HairPath(*devPathPtr, fClip, blitter.get());
9488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
9498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9520baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com/** For the purposes of drawing bitmaps, if a matrix is "almost" translate
9530baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    go ahead and treat it as if it were, so that subsequent code can go fast.
9540baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com */
9550baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.comstatic bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) {
9560baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    SkMatrix::TypeMask mask = matrix.getType();
9570baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com
9580baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (mask & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
9590baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        return false;
9600baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    }
9610baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (mask & SkMatrix::kScale_Mask) {
9620baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        SkScalar sx = matrix[SkMatrix::kMScaleX];
9630baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        SkScalar sy = matrix[SkMatrix::kMScaleY];
9640baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int w = bitmap.width();
9650baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int h = bitmap.height();
9660baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int sw = SkScalarRound(SkScalarMul(sx, SkIntToScalar(w)));
9670baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        int sh = SkScalarRound(SkScalarMul(sy, SkIntToScalar(h)));
9680baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        return sw == w && sh == h;
9690baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    }
9700baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    // if we got here, we're either kTranslate_Mask or identity
9710baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    return true;
9728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawBitmapAsMask(const SkBitmap& bitmap,
9758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint) const {
9768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(bitmap.getConfig() == SkBitmap::kA8_Config);
9778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
978a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    if (just_translate(*fMatrix, bitmap)) {
9798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int ix = SkScalarRound(fMatrix->getTranslateX());
9808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int iy = SkScalarRound(fMatrix->getTranslateY());
9818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
9838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fBounds.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
9848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fFormat = SkMask::kA8_Format;
9858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fRowBytes = bitmap.rowBytes();
9868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fImage = bitmap.getAddr8(0, 0);
987a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
9888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawDevMask(mask, paint);
9898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // need to xform the bitmap first
9908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect  r;
9918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  mask;
992a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
9938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.set(0, 0,
9948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com              SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
9958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fMatrix->mapRect(&r);
9968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.round(&mask.fBounds);
997a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
9988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // set the mask's bounds to the transformed bitmap-bounds,
9998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // clipped to the actual device
10008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
10018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkIRect    devBounds;
10028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            devBounds.set(0, 0, fBitmap->width(), fBitmap->height());
10038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // need intersect(l, t, r, b) on irect
10048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!mask.fBounds.intersect(devBounds)) {
10058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
10068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
10078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1008543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com
10098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fFormat = SkMask::kA8_Format;
10108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fRowBytes = SkAlign4(mask.fBounds.width());
1011543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        size_t size = mask.computeImageSize();
1012543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        if (0 == size) {
1013543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            // the mask is too big to allocated, draw nothing
1014543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            return;
1015543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        }
10168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // allocate (and clear) our temp buffer to hold the transformed bitmap
10188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAutoMalloc    storage(size);
10198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask.fImage = (uint8_t*)storage.get();
10208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        memset(mask.fImage, 0, size);
1021a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // now draw our bitmap(src) into mask(dst), transformed by the matrix
10238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
10248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkBitmap    device;
10258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            device.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(),
10268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             mask.fBounds.height(), mask.fRowBytes);
10278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            device.setPixels(mask.fImage);
1028a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkCanvas c(device);
10308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // need the unclipped top/left for the translate
10318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            c.translate(-SkIntToScalar(mask.fBounds.fLeft),
10328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        -SkIntToScalar(mask.fBounds.fTop));
10338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            c.concat(*fMatrix);
10343469c76c40790b409621fd7eff34f56240718549reed@android.com
10353469c76c40790b409621fd7eff34f56240718549reed@android.com            // We can't call drawBitmap, or we'll infinitely recurse. Instead
1036fb12c3e6ba84f95dc15fbaddc239dede0ba1d60ereed@android.com            // we manually build a shader and draw that into our new mask
10373469c76c40790b409621fd7eff34f56240718549reed@android.com            SkPaint tmpPaint;
10383469c76c40790b409621fd7eff34f56240718549reed@android.com            tmpPaint.setFlags(paint.getFlags());
103940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            SkAutoBitmapShaderInstall install(bitmap, tmpPaint);
10403469c76c40790b409621fd7eff34f56240718549reed@android.com            SkRect rr;
10413469c76c40790b409621fd7eff34f56240718549reed@android.com            rr.set(0, 0, SkIntToScalar(bitmap.width()),
10423469c76c40790b409621fd7eff34f56240718549reed@android.com                   SkIntToScalar(bitmap.height()));
104340c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            c.drawRect(rr, install.paintWithShader());
10448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
10458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawDevMask(mask, paint);
10468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool clipped_out(const SkMatrix& m, const SkRegion& c,
10508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        const SkRect& srcR) {
10518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect  dstR;
10528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect devIR;
1053a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.mapRect(&dstR, srcR);
1055a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    dstR.roundOut(&devIR);
10568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return c.quickReject(devIR);
10578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool clipped_out(const SkMatrix& matrix, const SkRegion& clip,
10608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        int width, int height) {
10618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect  r;
10628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
10638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return clipped_out(matrix, clip, r);
10648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
106740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                        const SkPaint& origPaint) const {
1068f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
10698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
10718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fClip->isEmpty() ||
10728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bitmap.width() == 0 || bitmap.height() == 0 ||
10738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bitmap.getConfig() == SkBitmap::kNo_Config ||
107440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            (origPaint.getAlpha() == 0 && origPaint.getXfermode() == NULL)) {
10758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
10768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1077a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1078a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#ifndef SK_ALLOW_OVER_32K_BITMAPS
10798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // run away on too-big bitmaps for now (exceed 16.16)
10808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (bitmap.width() > 32767 || bitmap.height() > 32767) {
10818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
10828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1083a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#endif
1084a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
108540c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkPaint paint(origPaint);
108640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    paint.setStyle(SkPaint::kFill_Style);
1087a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
10888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix matrix;
10898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (!matrix.setConcat(*fMatrix, prematrix)) {
10908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
10918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (clipped_out(matrix, *fClip, bitmap.width(), bitmap.height())) {
10948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
10958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1097218521e15706c4377b1be49d931c4d7c8d597445reed@android.com    if (fBounder && just_translate(matrix, bitmap)) {
1098218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        SkIRect ir;
1099218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        int32_t ix = SkScalarRound(matrix.getTranslateX());
1100218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        int32_t iy = SkScalarRound(matrix.getTranslateY());
1101218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1102218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        if (!fBounder->doIRect(ir)) {
1103218521e15706c4377b1be49d931c4d7c8d597445reed@android.com            return;
1104218521e15706c4377b1be49d931c4d7c8d597445reed@android.com        }
1105218521e15706c4377b1be49d931c4d7c8d597445reed@android.com    }
1106218521e15706c4377b1be49d931c4d7c8d597445reed@android.com
1107218521e15706c4377b1be49d931c4d7c8d597445reed@android.com    // only lock the pixels if we passed the clip and bounder tests
11088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoLockPixels alp(bitmap);
11098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // after the lock, check if we are valid
11108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (!bitmap.readyToDraw()) {
11118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
11128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11140baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com    if (bitmap.getConfig() != SkBitmap::kA8_Config &&
11150baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com            just_translate(matrix, bitmap)) {
11168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int         ix = SkScalarRound(matrix.getTranslateX());
11178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int         iy = SkScalarRound(matrix.getTranslateY());
11188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        uint32_t    storage[kBlitterStorageLongCount];
11198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkBlitter*  blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
11208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                            ix, iy, storage, sizeof(storage));
11218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (blitter) {
11228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkAutoTPlacementDelete<SkBlitter>   ad(blitter, storage);
11238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkIRect    ir;
11258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
11268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkRegion::Cliperator iter(*fClip, ir);
11288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            const SkIRect&       cr = iter.rect();
11298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            for (; !iter.done(); iter.next()) {
11318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkASSERT(!cr.isEmpty());
11328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                blitter->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
11338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
11348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
11358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
11368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0
11378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDebugf("---- MISSING sprite case: config=%d [%d %d], device=%d, xfer=%p, alpha=0x%X colorFilter=%p\n",
11388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                bitmap.config(), bitmap.width(), bitmap.height(), fBitmap->config(),
11398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                paint.getXfermode(), paint.getAlpha(), paint.getColorFilter());
11408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
11418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1142a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // now make a temp draw on the stack, and use it
11448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //
11458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDraw draw(*this);
11468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix = &matrix;
1147a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (bitmap.getConfig() == SkBitmap::kA8_Config) {
11498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        draw.drawBitmapAsMask(bitmap, paint);
11508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
115140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        SkAutoBitmapShaderInstall install(bitmap, paint);
11528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect  r;
11548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.set(0, 0, SkIntToScalar(bitmap.width()),
11558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com              SkIntToScalar(bitmap.height()));
1156a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com        // is this ok if paint has a rasterizer?
115740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com        draw.drawRect(r, install.paintWithShader());
11588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y,
116240c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com                        const SkPaint& origPaint) const {
1163f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
1164a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
11658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
11668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fClip->isEmpty() ||
11678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bitmap.width() == 0 || bitmap.height() == 0 ||
11688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bitmap.getConfig() == SkBitmap::kNo_Config ||
116940c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com            (origPaint.getAlpha() == 0 && origPaint.getXfermode() == NULL)) {
11708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
11718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    bounds;
11748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bounds.set(x, y, x + bitmap.width(), y + bitmap.height());
11758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fClip->quickReject(bounds)) {
11778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return; // nothing to draw
11788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
118040c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkPaint paint(origPaint);
118140c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    paint.setStyle(SkPaint::kFill_Style);
11828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == paint.getColorFilter()) {
11848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        uint32_t    storage[kBlitterStorageLongCount];
11858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkBlitter*  blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
11868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                                x, y, storage, sizeof(storage));
11878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (blitter) {
11898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkAutoTPlacementDelete<SkBlitter> ad(blitter, storage);
11908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (fBounder && !fBounder->doIRect(bounds)) {
11928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
11938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
11948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkRegion::Cliperator iter(*fClip, bounds);
11968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            const SkIRect&       cr = iter.rect();
11978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            for (; !iter.done(); iter.next()) {
11998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkASSERT(!cr.isEmpty());
12008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                blitter->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
12018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
12028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
12038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
12048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
120640c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    SkAutoBitmapShaderInstall install(bitmap, paint);
120740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    const SkPaint& shaderPaint = install.paintWithShader();
12088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        matrix;
12108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect          r;
12118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // get a scalar version of our rect
12138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.set(bounds);
12148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // tell the shader our offset
12168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setTranslate(r.fLeft, r.fTop);
121740c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    shaderPaint.getShader()->setLocalMatrix(matrix);
1218a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDraw draw(*this);
12208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.reset();
12218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix = &matrix;
12228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // call ourself with a rect
1223a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    // is this OK if paint has a rasterizer?
122440c2ba27b6e5c6a4b6c073264f8c0a7c86355abereed@google.com    draw.drawRect(r, shaderPaint);
12258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
12288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkScalerContext.h"
12308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGlyphCache.h"
12318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h"
12328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void measure_text(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
12348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const char text[], size_t byteLength, SkVector* stopVector) {
12358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed     x = 0, y = 0;
12368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* stop = text + byteLength;
12378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoKern  autokern;
1239a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
12408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (text < stop) {
12418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // don't need x, y here, since all subpixel variants will have the
12428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // same advance
12438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
12448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x += autokern.adjust(glyph) + glyph.fAdvanceX;
12468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y += glyph.fAdvanceY;
12478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
12498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(text == stop);
12518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawText_asPaths(const char text[], size_t byteLength,
12548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              SkScalar x, SkScalar y,
12558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint) const {
1256f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
12578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTextToPathIter iter(text, byteLength, paint, true, true);
12598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    matrix;
12618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setScale(iter.getPathScale(), iter.getPathScale());
12628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.postTranslate(x, y);
12638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPath* iterPath;
12658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar xpos, prevXPos = 0;
12668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while ((iterPath = iter.next(&xpos)) != NULL) {
12688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        matrix.postTranslate(xpos - prevXPos, 0);
1269f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        const SkPaint& pnt = iter.getPaint();
1270f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (fDevice) {
1271f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fDevice->drawPath(*this, *iterPath, pnt, &matrix, false);
1272f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        } else {
1273f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            this->drawPath(*iterPath, pnt, &matrix, false);
1274f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
12758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        prevXPos = xpos;
12768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// disable warning : local variable used without having been initialized
1280a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com#if defined _WIN32 && _MSC_VER >= 1300
12818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( push )
12828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( disable : 4701 )
12838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
12848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
12868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_NoBounder_RectClip(const SkDraw1Glyph& state,
1288f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                   SkFixed fx, SkFixed fy,
1289f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                   const SkGlyph& glyph) {
1290f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1291f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
12928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
12938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkASSERT(state.fClip->isRect());
12948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkASSERT(NULL == state.fBounder);
12958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkASSERT(state.fClipBounds == state.fClip->getBounds());
12968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
12988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
12998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int right   = left + glyph.fWidth;
13018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int bottom  = top + glyph.fHeight;
13028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkMask		mask;
13048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkIRect		storage;
13058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkIRect*	bounds = &mask.fBounds;
13068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	mask.fBounds.set(left, top, right, bottom);
13088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	// this extra test is worth it, assuming that most of the time it succeeds
13108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	// since we can avoid writing to storage
13118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) {
13128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds))
13138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			return;
13148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		bounds = &storage;
13158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	}
1316a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1317a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com	uint8_t* aa = (uint8_t*)glyph.fImage;
13188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	if (NULL == aa) {
13198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		aa = (uint8_t*)state.fCache->findImage(glyph);
13208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		if (NULL == aa) {
13218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			return; // can't rasterize glyph
13228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
13238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	}
13248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	mask.fRowBytes = glyph.rowBytes();
13266c14b43a840c791699747ba4cc0ed5abf2bda218reed@android.com	mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
13278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	mask.fImage = aa;
13288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	state.fBlitter->blitMask(mask, *bounds);
13298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_NoBounder_RgnClip(const SkDraw1Glyph& state,
1332f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                  SkFixed fx, SkFixed fy,
1333f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com								  const SkGlyph& glyph) {
1334f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1335f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
13368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
13378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkASSERT(!state.fClip->isRect());
13388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkASSERT(NULL == state.fBounder);
13398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMask  mask;
13418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
13438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
13448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
13468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
13478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	if (!clipper.done()) {
13498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		const SkIRect&  cr = clipper.rect();
13508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		const uint8_t*  aa = (const uint8_t*)glyph.fImage;
13518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		if (NULL == aa) {
13528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			aa = (uint8_t*)state.fCache->findImage(glyph);
13538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			if (NULL == aa) {
13548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com				return;
13558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
13568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		}
1357a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
13588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		mask.fRowBytes = glyph.rowBytes();
13596c14b43a840c791699747ba4cc0ed5abf2bda218reed@android.com		mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
13608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		mask.fImage = (uint8_t*)aa;
13618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		do {
13628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			state.fBlitter->blitMask(mask, cr);
13638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			clipper.next();
13648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		} while (!clipper.done());
13658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	}
13668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void D1G_Bounder(const SkDraw1Glyph& state,
1369f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                        SkFixed fx, SkFixed fy,
1370f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com						const SkGlyph& glyph) {
1371f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int left = SkFixedFloor(fx);
1372f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int top = SkFixedFloor(fy);
13738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
13748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMask  mask;
13768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    left += glyph.fLeft;
13788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    top  += glyph.fTop;
13798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
13818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
13828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	if (!clipper.done()) {
13848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		const SkIRect&  cr = clipper.rect();
13858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		const uint8_t*  aa = (const uint8_t*)glyph.fImage;
13868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		if (NULL == aa) {
13878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			aa = (uint8_t*)state.fCache->findImage(glyph);
13888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			if (NULL == aa) {
13898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com				return;
13908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
13918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		}
1392d055c1fde2128514167b315f4d104b177e04a3dereed@android.com
1393d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        // we need to pass the origin, which we approximate with our
1394d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        // (unadjusted) left,top coordinates (the caller called fixedfloor)
1395d055c1fde2128514167b315f4d104b177e04a3dereed@android.com		if (state.fBounder->doIRectGlyph(cr,
1396d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                                         left - glyph.fLeft,
1397d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                                         top - glyph.fTop, glyph)) {
13988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			mask.fRowBytes = glyph.rowBytes();
13996c14b43a840c791699747ba4cc0ed5abf2bda218reed@android.com			mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
14008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			mask.fImage = (uint8_t*)aa;
14018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			do {
14028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com				state.fBlitter->blitMask(mask, cr);
14038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com				clipper.next();
14048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			} while (!clipper.done());
14058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		}
14068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	}
14078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1409fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.comstatic bool hasCustomD1GProc(const SkDraw& draw) {
1410fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    return draw.fProcs && draw.fProcs->fD1GProc;
1411fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com}
1412fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com
1413fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.comstatic bool needsRasterTextBlit(const SkDraw& draw) {
1414fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com    return !hasCustomD1GProc(draw);
1415fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com}
1416fd4236ecc1d5eb1bb48ca9dc33df5e9051c3036ereed@google.com
14178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter,
14188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                      SkGlyphCache* cache) {
14198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDraw = draw;
14208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	fBounder = draw->fBounder;
14218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	fClip = draw->fClip;
14228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fClipBounds = fClip->getBounds();
14238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	fBlitter = blitter;
14248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	fCache = cache;
14258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14261d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (hasCustomD1GProc(*draw)) {
14278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return draw->fProcs->fD1GProc;
14288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == fBounder) {
14318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fClip->isRect()) {
14328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return D1G_NoBounder_RectClip;
14338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
14348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return D1G_NoBounder_RgnClip;
14358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
14368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
14378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return D1G_Bounder;
14388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
14428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawText(const char text[], size_t byteLength,
14448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      SkScalar x, SkScalar y, const SkPaint& paint) const {
14458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
14468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1447f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
14488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
14508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (text == NULL || byteLength == 0 ||
14518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fClip->isEmpty() ||
14528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
14538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
14548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (/*paint.isLinearText() ||*/
14578d430185e08d2067584837a76b7193b803fee7a0tomhudson@google.com        (fMatrix->hasPerspective())) {
14588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawText_asPaths(text, byteLength, x, y, paint);
14598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
14608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
14638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1464f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkMatrix* matrix = fMatrix;
14651d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (hasCustomD1GProc(*this)) {
1466f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        // only support the fMVMatrix (for now) for the GPU case, which also
1467f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        // sets the fD1GProc
1468f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (fMVMatrix) {
1469f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            matrix = fMVMatrix;
1470f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
1471f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
1472f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1473f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkAutoGlyphCache    autoCache(paint, matrix);
14748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkGlyphCache*       cache = autoCache.getCache();
1475a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
14768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform our starting point
14778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
14788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint loc;
1479f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        matrix->mapXY(x, y, &loc);
14808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x = loc.fX;
14818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y = loc.fY;
14828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need to measure first
14858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getTextAlign() != SkPaint::kLeft_Align) {
14868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkVector    stop;
14878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        measure_text(cache, glyphCacheProc, text, byteLength, &stop);
14898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar    stopX = stop.fX;
14918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar    stopY = stop.fY;
14928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.getTextAlign() == SkPaint::kCenter_Align) {
14948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            stopX = SkScalarHalf(stopX);
14958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            stopY = SkScalarHalf(stopY);
14968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
14978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x -= stopX;
14988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        y -= stopY;
14998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1500a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
15018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed fx = SkScalarToFixed(x);
15028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFixed fy = SkScalarToFixed(y);
15038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* stop = text + byteLength;
15048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1505f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkFixed fxMask = ~0;
1506f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkFixed fyMask = ~0;
15078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.isSubpixelText()) {
1508cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*matrix);
1509cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        if (kX_SkAxisAlignment == baseline) {
1510f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fyMask = 0;
1511cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        } else if (kY_SkAxisAlignment == baseline) {
1512f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fxMask = 0;
15138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
15148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1515f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    // apply the bias here, so we don't have to add 1/2 in the loop
1516f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    fx += SK_FixedHalf;
1517f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    fy += SK_FixedHalf;
15188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15191d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    SkAutoBlitterChoose blitter;
15201d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (needsRasterTextBlit(*this)) {
15211d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com        blitter.choose(*fBitmap, *matrix, paint);
15221d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
15231d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com
15248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoKern          autokern;
152552c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com    SkDraw1Glyph        d1g;
152652c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com    SkDraw1Glyph::Proc  proc = d1g.init(this, blitter.get(), cache);
15278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (text < stop) {
1529f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        const SkGlyph& glyph  = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
15308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fx += autokern.adjust(glyph);
15328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (glyph.fWidth) {
153452c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com            proc(d1g, fx, fy, glyph);
15358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
15368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fx += glyph.fAdvanceX;
15378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fy += glyph.fAdvanceY;
15388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
15398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// last parameter is interpreted as SkFixed [x, y]
15428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// return the fixed position, which may be rounded or not by the caller
15438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//   e.g. subpixel doesn't round
15448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef void (*AlignProc)(const SkPoint&, const SkGlyph&, SkIPoint*);
15458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void leftAlignProc(const SkPoint& loc, const SkGlyph& glyph,
15478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          SkIPoint* dst) {
15488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX), SkScalarToFixed(loc.fY));
15498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void centerAlignProc(const SkPoint& loc, const SkGlyph& glyph,
15528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            SkIPoint* dst) {
15538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX) - (glyph.fAdvanceX >> 1),
15548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com             SkScalarToFixed(loc.fY) - (glyph.fAdvanceY >> 1));
15558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void rightAlignProc(const SkPoint& loc, const SkGlyph& glyph,
15588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkIPoint* dst) {
15598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst->set(SkScalarToFixed(loc.fX) - glyph.fAdvanceX,
15608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com             SkScalarToFixed(loc.fY) - glyph.fAdvanceY);
15618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic AlignProc pick_align_proc(SkPaint::Align align) {
15648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static const AlignProc gProcs[] = {
15658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        leftAlignProc, centerAlignProc, rightAlignProc
15668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
1567a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
15688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((unsigned)align < SK_ARRAY_COUNT(gProcs));
15698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return gProcs[align];
15718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass TextMapState {
15748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
15758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mutable SkPoint fLoc;
1576a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
15778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    TextMapState(const SkMatrix& matrix, SkScalar y)
15788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        : fMatrix(matrix), fProc(matrix.getMapXYProc()), fY(y) {}
15798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef void (*Proc)(const TextMapState&, const SkScalar pos[]);
1581a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
15828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Proc pickProc(int scalarsPerPosition);
1583a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
15848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
15858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix&     fMatrix;
15868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix::MapXYProc fProc;
15878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            fY; // ignored by MapXYProc
15888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // these are only used by Only... procs
15898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            fScaleX, fTransX, fTransformedY;
15908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapXProc(const TextMapState& state, const SkScalar pos[]) {
15928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fProc(state.fMatrix, *pos, state.fY, &state.fLoc);
15938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1594a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
15958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapXYProc(const TextMapState& state, const SkScalar pos[]) {
15968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fProc(state.fMatrix, pos[0], pos[1], &state.fLoc);
15978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1598a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
15998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapOnlyScaleXProc(const TextMapState& state,
16008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                  const SkScalar pos[]) {
16018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fLoc.set(SkScalarMul(state.fScaleX, *pos) + state.fTransX,
16028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                       state.fTransformedY);
16038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1604a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void MapOnlyTransXProc(const TextMapState& state,
16068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                  const SkScalar pos[]) {
16078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state.fLoc.set(*pos + state.fTransX, state.fTransformedY);
16088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
16108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comTextMapState::Proc TextMapState::pickProc(int scalarsPerPosition) {
16128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1613a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (1 == scalarsPerPosition) {
16158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        unsigned mtype = fMatrix.getType();
16168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (mtype & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
16178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return MapXProc;
16188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
16198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fScaleX = fMatrix.getScaleX();
16208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fTransX = fMatrix.getTranslateX();
16218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fTransformedY = SkScalarMul(fY, fMatrix.getScaleY()) +
16228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            fMatrix.getTranslateY();
16238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return (mtype & SkMatrix::kScale_Mask) ?
16248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        MapOnlyScaleXProc : MapOnlyTransXProc;
16258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
16268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
16278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return MapXYProc;
16288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
16308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
16328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawPosText(const char text[], size_t byteLength,
16348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         const SkScalar pos[], SkScalar constY,
16358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         int scalarsPerPosition, const SkPaint& paint) const {
16368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
16378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
16388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1639f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDEBUGCODE(this->validate();)
16408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
16428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (text == NULL || byteLength == 0 ||
16438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fClip->isEmpty() ||
16448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
16458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
16468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (/*paint.isLinearText() ||*/
16498d430185e08d2067584837a76b7193b803fee7a0tomhudson@google.com        (fMatrix->hasPerspective())) {
16508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // TODO !!!!
16518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//      this->drawText_asPaths(text, byteLength, x, y, paint);
16528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
16538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
16548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1655f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkMatrix* matrix = fMatrix;
16561d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (hasCustomD1GProc(*this)) {
1657f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        // only support the fMVMatrix (for now) for the GPU case, which also
1658f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        // sets the fD1GProc
1659f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (fMVMatrix) {
1660f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            matrix = fMVMatrix;
1661f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
1662f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
1663a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawCacheProc     glyphCacheProc = paint.getDrawCacheProc();
1665f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkAutoGlyphCache    autoCache(paint, matrix);
16668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkGlyphCache*       cache = autoCache.getCache();
1667a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16681d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    SkAutoBlitterChoose blitter;
16691d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    if (needsRasterTextBlit(*this)) {
16701d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com        blitter.choose(*fBitmap, *matrix, paint);
16711d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com    }
16721d6ee0bd4db7f3b676209341214641d9af5a965freed@google.com
16738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char*        stop = text + byteLength;
16748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    AlignProc          alignProc = pick_align_proc(paint.getTextAlign());
16758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkDraw1Glyph	   d1g;
16768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkDraw1Glyph::Proc  proc = d1g.init(this, blitter.get(), cache);
1677f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    TextMapState       tms(*matrix, constY);
16788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    TextMapState::Proc tmsProc = tms.pickProc(scalarsPerPosition);
16798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.isSubpixelText()) {
16818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // maybe we should skip the rounding if linearText is set
1682cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com        SkAxisAlignment roundBaseline = SkComputeAxisAlignmentForHText(*matrix);
16838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
16848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (SkPaint::kLeft_Align == paint.getTextAlign()) {
16858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            while (text < stop) {
1686a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                tmsProc(tms, pos);
1688a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
16898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkFixed fx = SkScalarToFixed(tms.fLoc.fX);
16908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkFixed fy = SkScalarToFixed(tms.fLoc.fY);
1691f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                SkFixed fxMask = ~0;
1692f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                SkFixed fyMask = ~0;
16938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1694cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                if (kX_SkAxisAlignment == roundBaseline) {
1695f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    fyMask = 0;
1696cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                } else if (kY_SkAxisAlignment == roundBaseline) {
1697f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    fxMask = 0;
16988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
1699a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1700f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                const SkGlyph& glyph = glyphCacheProc(cache, &text,
1701f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                                      fx & fxMask, fy & fyMask);
1702a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (glyph.fWidth) {
1704f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    proc(d1g, fx, fy, glyph);
17058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
17068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pos += scalarsPerPosition;
17078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
17088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
17098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            while (text < stop) {
17108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const SkGlyph* glyph = &glyphCacheProc(cache, &text, 0, 0);
1711a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (glyph->fWidth) {
17138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkDEBUGCODE(SkFixed prevAdvX = glyph->fAdvanceX;)
17148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkDEBUGCODE(SkFixed prevAdvY = glyph->fAdvanceY;)
17158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkFixed fx, fy;
1717f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    SkFixed fxMask = ~0;
1718f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    SkFixed fyMask = ~0;
17198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    tmsProc(tms, pos);
1720a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    {
17228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkIPoint fixedLoc;
17238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        alignProc(tms.fLoc, *glyph, &fixedLoc);
17248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        fx = fixedLoc.fX;
17258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        fy = fixedLoc.fY;
17268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1727cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                        if (kX_SkAxisAlignment == roundBaseline) {
1728f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            fyMask = 0;
1729cb6ccdde5120ec45df208c0b958b263d8252a505reed@google.com                        } else if (kY_SkAxisAlignment == roundBaseline) {
1730f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            fxMask = 0;
17318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        }
17328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
1733a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // have to call again, now that we've been "aligned"
1735f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    glyph = &glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
17368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // the assumption is that the advance hasn't changed
17378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkASSERT(prevAdvX == glyph->fAdvanceX);
17388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkASSERT(prevAdvY == glyph->fAdvanceY);
1739a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1740f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    proc(d1g, fx, fy, *glyph);
17418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
17428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                pos += scalarsPerPosition;
17438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
17448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
17458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // not subpixel
17468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while (text < stop) {
17478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // the last 2 parameters are ignored
17488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1749a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (glyph.fWidth) {
17518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                tmsProc(tms, pos);
1752a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
17538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkIPoint fixedLoc;
17548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                alignProc(tms.fLoc, glyph, &fixedLoc);
1755a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1756f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                proc(d1g, fixedLoc.fX + SK_FixedHalf,
1757f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                     fixedLoc.fY + SK_FixedHalf, glyph);
17588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
17598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pos += scalarsPerPosition;
17608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
17618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
17628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if defined _WIN32 && _MSC_VER >= 1300
17658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#pragma warning ( pop )
17668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
17678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
17698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPathMeasure.h"
17718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void morphpoints(SkPoint dst[], const SkPoint src[], int count,
17738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkPathMeasure& meas, const SkMatrix& matrix) {
17748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix::MapXYProc proc = matrix.getMapXYProc();
17758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
17778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint pos;
17788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkVector tangent;
17798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        proc(matrix, src[i].fX, src[i].fY, &pos);
17818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sx = pos.fX;
17828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar sy = pos.fY;
17838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        meas.getPosTan(sx, &pos, &tangent);
17858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        /*  This is the old way (that explains our approach but is way too slow
17878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkMatrix    matrix;
17888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPoint     pt;
17898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pt.set(sx, sy);
17918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.setSinCos(tangent.fY, tangent.fX);
17928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.preTranslate(-sx, 0);
17938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.postTranslate(pos.fX, pos.fY);
17948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            matrix.mapPoints(&dst[i], &pt, 1);
17958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        */
17968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy),
17978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                   pos.fY + SkScalarMul(tangent.fX, sy));
17988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
17998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
18008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  TODO
18028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Need differentially more subdivisions when the follow-path is curvy. Not sure how to
18048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    determine that, but we need it. I guess a cheap answer is let the caller tell us,
18058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
18068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
18078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
18088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      const SkMatrix& matrix) {
18098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath::Iter    iter(src, false);
18108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint         srcP[4], dstP[3];
18118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath::Verb    verb;
18128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
18148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        switch (verb) {
18158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kMove_Verb:
18168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, srcP, 1, meas, matrix);
18178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->moveTo(dstP[0]);
18188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
18198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kLine_Verb:
18208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                // turn lines into quads to look bendy
18218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX);
18228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY);
18238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, srcP, 2, meas, matrix);
18248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->quadTo(dstP[0], dstP[1]);
18258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
18268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kQuad_Verb:
18278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, &srcP[1], 2, meas, matrix);
18288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->quadTo(dstP[0], dstP[1]);
18298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
18308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kCubic_Verb:
18318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                morphpoints(dstP, &srcP[1], 3, meas, matrix);
18328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->cubicTo(dstP[0], dstP[1], dstP[2]);
18338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
18348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            case SkPath::kClose_Verb:
18358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst->close();
18368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
18378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            default:
18388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkASSERT(!"unknown verb");
18398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                break;
18408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
18418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
18438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawTextOnPath(const char text[], size_t byteLength,
18458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPath& follow, const SkMatrix* matrix,
18468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPaint& paint) const {
18478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(byteLength == 0 || text != NULL);
18488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // nothing to draw
18508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (text == NULL || byteLength == 0 ||
18518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fClip->isEmpty() ||
18528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
18538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
18548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTextToPathIter    iter(text, byteLength, paint, true, true);
18578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPathMeasure       meas(follow, false);
18588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar            hOffset = 0;
18598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need to measure first
18618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getTextAlign() != SkPaint::kLeft_Align) {
18628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar pathLen = meas.getLength();
18638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.getTextAlign() == SkPaint::kCenter_Align) {
18648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pathLen = SkScalarHalf(pathLen);
18658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
18668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hOffset += pathLen;
18678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPath*   iterPath;
18708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar        xpos;
18718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix        scaledMatrix;
18728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar        scale = iter.getPathScale();
18738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    scaledMatrix.setScale(scale, scale);
1875a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
18768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while ((iterPath = iter.next(&xpos)) != NULL) {
18778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPath      tmp;
18788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMatrix    m(scaledMatrix);
18798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
18808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        m.postTranslate(xpos + hOffset, 0);
18818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (matrix) {
18828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            m.postConcat(*matrix);
18838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
18848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        morphpath(&tmp, *iterPath, meas, m);
1885f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (fDevice) {
1886f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fDevice->drawPath(*this, tmp, iter.getPaint(), NULL, true);
1887f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        } else {
1888f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            this->drawPath(tmp, iter.getPaint(), NULL, true);
1889f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
18908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
18928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1893cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com#ifdef ANDROID
1894cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.comvoid SkDraw::drawPosTextOnPath(const char text[], size_t byteLength,
1895cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                               const SkPoint pos[], const SkPaint& paint,
1896cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                               const SkPath& path, const SkMatrix* matrix) const {
1897cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // nothing to draw
1898cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    if (text == NULL || byteLength == 0 || fClip->isEmpty() ||
1899cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
1900cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        return;
1901cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
1902cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1903cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkMatrix scaledMatrix;
1904cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkPathMeasure meas(path, false);
1905cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1906cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkMeasureCacheProc glyphCacheProc = paint.getMeasureCacheProc(
1907cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            SkPaint::kForward_TextBufferDirection, true);
1908cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1909cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Copied (modified) from SkTextToPathIter constructor to setup paint
1910cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkPaint tempPaint(paint);
1911cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1912cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    tempPaint.setLinearText(true);
1913cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    tempPaint.setMaskFilter(NULL); // don't want this affecting our path-cache lookup
1914cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1915cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    if (tempPaint.getPathEffect() == NULL && !(tempPaint.getStrokeWidth() > 0
1916cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            && tempPaint.getStyle() != SkPaint::kFill_Style)) {
1917cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        tempPaint.setStyle(SkPaint::kFill_Style);
1918cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        tempPaint.setPathEffect(NULL);
1919cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
1920cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // End copied from SkTextToPathIter constructor
1921cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1922cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // detach cache
1923cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkGlyphCache* cache = tempPaint.detachCache(NULL);
1924cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1925cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Must set scale, even if 1
1926cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkScalar scale = SK_Scalar1;
1927cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    scaledMatrix.setScale(scale, scale);
1928cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1929cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // Loop over all glyph ids
1930cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    for (const char* stop = text + byteLength; text < stop; pos++) {
1931cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1932cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        const SkGlyph& glyph = glyphCacheProc(cache, &text);
1933cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        SkPath tmp;
1934cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1935cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        const SkPath* glyphPath = cache->findPath(glyph);
1936cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        if (glyphPath == NULL) {
1937cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            continue;
1938cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        }
1939cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1940cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        SkMatrix m(scaledMatrix);
1941cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        m.postTranslate(pos->fX, 0);
1942cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1943cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        if (matrix) {
1944cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com            m.postConcat(*matrix);
1945cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        }
1946cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1947cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        morphpath(&tmp, *glyphPath, meas, m);
1948cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com        this->drawPath(tmp, tempPaint);
1949cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1950cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    }
1951cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
1952cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    // re-attach cache
1953cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    SkGlyphCache::AttachCache(cache);
1954cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com}
1955cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com#endif
1956cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com
19578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
19588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct VertState {
19608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int f0, f1, f2;
19618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState(int vCount, const uint16_t indices[], int indexCount)
19638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            : fIndices(indices) {
19648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCurrIndex = 0;
19658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (indices) {
19668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fCount = indexCount;
19678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
19688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fCount = vCount;
19698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
19708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1971a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
1972a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com    typedef bool (*Proc)(VertState*);
19738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Proc chooseProc(SkCanvas::VertexMode mode);
19748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
19768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int             fCount;
19778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int             fCurrIndex;
19788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* fIndices;
1979a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
19808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool Triangles(VertState*);
19818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TrianglesX(VertState*);
19828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleStrip(VertState*);
19838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleStripX(VertState*);
19848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleFan(VertState*);
19858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TriangleFanX(VertState*);
19868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
19878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::Triangles(VertState* state) {
19898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
19908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
19918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
19928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
19938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = index + 0;
19948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = index + 1;
19958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
19968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 3;
19978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
19988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
19998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TrianglesX(VertState* state) {
20018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
20028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
20038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
20048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
20058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = indices[index + 0];
20078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = indices[index + 1];
20088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
20098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 3;
20108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
20118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleStrip(VertState* state) {
20148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
20158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
20168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
20178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
20198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index & 1) {
20208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = index + 1;
20218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = index + 0;
20228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
20238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = index + 0;
20248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = index + 1;
20258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
20278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
20288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleStripX(VertState* state) {
20318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
20328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
20338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
20348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
20358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
20378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index & 1) {
20388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = indices[index + 1];
20398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = indices[index + 0];
20408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
20418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f0 = indices[index + 0];
20428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        state->f1 = indices[index + 1];
20438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
20458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
20468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleFan(VertState* state) {
20498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
20508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
20518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
20528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = 0;
20548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = index + 1;
20558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = index + 2;
20568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
20578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
20588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool VertState::TriangleFanX(VertState* state) {
20618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const uint16_t* indices = state->fIndices;
20628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int index = state->fCurrIndex;
20638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index + 3 > state->fCount) {
20648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
20658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f0 = indices[0];
20678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f1 = indices[index + 1];
20688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->f2 = indices[index + 2];
20698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    state->fCurrIndex = index + 1;
20708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
20718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comVertState::Proc VertState::chooseProc(SkCanvas::VertexMode mode) {
20748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (mode) {
20758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangles_VertexMode:
20768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TrianglesX : Triangles;
20778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangleStrip_VertexMode:
20788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TriangleStripX : TriangleStrip;
20798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SkCanvas::kTriangleFan_VertexMode:
20808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fIndices ? TriangleFanX : TriangleFan;
20818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
20828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return NULL;
20838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef void (*HairProc)(const SkPoint&, const SkPoint&, const SkRegion*,
20878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                         SkBlitter*);
20888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic HairProc ChooseHairProc(bool doAntiAlias) {
20908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
20918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool texture_to_matrix(const VertState& state, const SkPoint verts[],
20948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPoint texs[], SkMatrix* matrix) {
20958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint src[3], dst[3];
2096a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
20978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[0] = texs[state.f0];
20988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[1] = texs[state.f1];
20998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    src[2] = texs[state.f2];
21008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[0] = verts[state.f0];
21018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[1] = verts[state.f1];
21028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[2] = verts[state.f2];
21038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return matrix->setPolyToPoly(src, dst, 3);
21048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkTriColorShader : public SkShader {
21078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
21088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader() {}
21098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
2111a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
21128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count);
2113a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
21148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
21158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader(SkFlattenableReadBuffer& buffer) : SkShader(buffer) {}
2116a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
21178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual Factory getFactory() { return CreateProc; }
2118a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
21198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
21208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    fDstToUnit;
21218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPMColor   fColors[3];
2122a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
21238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
21248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return SkNEW_ARGS(SkTriColorShader, (buffer));
21258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkShader INHERITED;
21278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
21288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkTriColorShader::setup(const SkPoint pts[], const SkColor colors[],
21308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             int index0, int index1, int index2) {
2131a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
21328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[0] = SkPreMultiplyColor(colors[index0]);
21338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[1] = SkPreMultiplyColor(colors[index1]);
21348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColors[2] = SkPreMultiplyColor(colors[index2]);
2135a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
21368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix m, im;
21378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.reset();
21388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(0, pts[index1].fX - pts[index0].fX);
21398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(1, pts[index2].fX - pts[index0].fX);
21408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(2, pts[index0].fX);
21418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(3, pts[index1].fY - pts[index0].fY);
21428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(4, pts[index2].fY - pts[index0].fY);
21438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    m.set(5, pts[index0].fY);
21448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (!m.invert(&im)) {
21458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
21468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fDstToUnit.setConcat(im, this->getTotalInverse());
21488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
2151689411a87ca7d19abe04b5c9baff26bd96b2123ereed@android.com#include "SkComposeShader.h"
21528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic int ScalarTo256(SkScalar v) {
21548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int scale = SkScalarToFixed(v) >> 8;
21558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (scale < 0) {
21568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        scale = 0;
21578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (scale > 255) {
21598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        scale = 255;
21608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkAlpha255To256(scale);
21628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkTriColorShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
21658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint src;
2166a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
21678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
21688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
21698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x += 1;
2170a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
21718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale1 = ScalarTo256(src.fX);
21728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale2 = ScalarTo256(src.fY);
21738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int scale0 = 256 - scale1 - scale2;
21748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (scale0 < 0) {
21758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (scale1 > scale2) {
21768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                scale2 = 256 - scale1;
21778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } else {
21788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                scale1 = 256 - scale2;
21798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
21808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            scale0 = 0;
21818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2182a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
21838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        dstC[i] = SkAlphaMulQ(fColors[0], scale0) +
21848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAlphaMulQ(fColors[1], scale1) +
21858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAlphaMulQ(fColors[2], scale2);
21868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
21908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPoint vertices[], const SkPoint textures[],
21918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkColor colors[], SkXfermode* xmode,
21928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const uint16_t indices[], int indexCount,
21938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint& paint) const {
21948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(0 == count || NULL != vertices);
2195a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
21968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // abort early if there is nothing to draw
21978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (count < 3 || (indices && indexCount < 3) || fClip->isEmpty() ||
21988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
21998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
22008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2201a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // transform out vertices into device coordinates
22038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoSTMalloc<16, SkPoint> storage(count);
22048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint* devVerts = storage.get();
22058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMatrix->mapPoints(devVerts, vertices, count);
2206a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fBounder) {
22088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect bounds;
22098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.set(devVerts, count);
22108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!fBounder->doRect(bounds, paint)) {
22118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
22128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
22138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2214a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*
22168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        We can draw the vertices in 1 of 4 ways:
22178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - solid color (no shader/texture[], no colors[])
22198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - just colors (no shader/texture[], has colors[])
22208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - just texture (has shader/texture[], no colors[])
22218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        - colors * texture (has shader/texture[], has colors[])
2222a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Thus for texture drawing, we need both texture[] and a shader.
22248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
22258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTriColorShader triShader; // must be above declaration of p
22278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint p(paint);
22288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkShader* shader = p.getShader();
22308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == shader) {
22318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if we have no shader, we ignore the texture coordinates
22328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        textures = NULL;
22338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else if (NULL == textures) {
22348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if we don't have texture coordinates, ignore the shader
22358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        p.setShader(NULL);
22368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        shader = NULL;
22378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // setup the custom shader (if needed)
22408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL != colors) {
22418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL == textures) {
22428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // just colors (no texture)
22438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            p.setShader(&triShader);
22448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
22458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // colors * texture
22468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(shader);
22478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bool releaseMode = false;
22488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL == xmode) {
2249845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com                xmode = SkXfermode::Create(SkXfermode::kMultiply_Mode);
22508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                releaseMode = true;
22518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
22528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkShader* compose = SkNEW_ARGS(SkComposeShader,
22538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                           (&triShader, shader, xmode));
22548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            p.setShader(compose)->unref();
22558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (releaseMode) {
22568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                xmode->unref();
22578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
22588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
22598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
22628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // setup our state and function pointer for iterating triangles
22638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState       state(count, indices, indexCount);
22648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    VertState::Proc vertProc = state.chooseProc(vmode);
2265a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL != textures || NULL != colors) {
22678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMatrix  localM, tempM;
22688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bool      hasLocalM = shader && shader->getLocalMatrix(&localM);
2269a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != colors) {
22718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (!triShader.setContext(*fBitmap, p, *fMatrix)) {
22728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                colors = NULL;
22738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
22748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2275a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
22768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while (vertProc(&state)) {
22778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL != textures) {
22788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (texture_to_matrix(state, vertices, textures, &tempM)) {
22798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (hasLocalM) {
22808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        tempM.postConcat(localM);
22818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
22828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    shader->setLocalMatrix(tempM);
22838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    // need to recal setContext since we changed the local matrix
22848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (!shader->setContext(*fBitmap, p, *fMatrix)) {
22858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        continue;
22868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    }
22878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
22888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
22898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (NULL != colors) {
22908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (!triShader.setup(vertices, colors,
22918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                     state.f0, state.f1, state.f2)) {
22928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    continue;
22938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
22948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
22958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkScan::FillTriangle(devVerts[state.f0], devVerts[state.f1],
22968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                 devVerts[state.f2], fClip, blitter.get());
22978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
22988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // now restore the shader's original local matrix
22998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != shader) {
23008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (hasLocalM) {
23018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                shader->setLocalMatrix(localM);
23028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } else {
23038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                shader->resetLocalMatrix();
23048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
23058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
23068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
23078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // no colors[] and no texture
23088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        HairProc hairProc = ChooseHairProc(paint.isAntiAlias());
23098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while (vertProc(&state)) {
23108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            hairProc(devVerts[state.f0], devVerts[state.f1], fClip, blitter.get());
23118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            hairProc(devVerts[state.f1], devVerts[state.f2], fClip, blitter.get());
23128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            hairProc(devVerts[state.f2], devVerts[state.f0], fClip, blitter.get());
23138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
23148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23170a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
23180a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
23198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
23218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2322f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comvoid SkDraw::validate() const {
23238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fBitmap != NULL);
23248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fMatrix != NULL);
23258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fClip != NULL);
23268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkIRect&  cr = fClip->getBounds();
23288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect         br;
23298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2330f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    br.set(0, 0, fBitmap->width(), fBitmap->height());
23318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(cr.isEmpty() || br.contains(cr));
2332f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2333f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    // assert that both are null, or both are not-null
2334f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkASSERT(!fMVMatrix == !fExtMatrix);
23358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
23388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23390a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com///////////////////////////////////////////////////////////////////////////////
23400a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com
23410a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.comSkBounder::SkBounder() {
23420a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com    // initialize up front. This gets reset by SkCanvas before each draw call.
23430a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com    fClip = &SkRegion::GetEmptyRegion();
23440a0a236c3ba241046d38caaf78226ec68ff9c998reed@google.com}
23458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doIRect(const SkIRect& r) {
23478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    rr;
23488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return rr.intersect(fClip->getBounds(), r) && this->onIRect(rr);
23498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2351d055c1fde2128514167b315f4d104b177e04a3dereed@android.com// TODO: change the prototype to take fixed, and update the callers
2352d055c1fde2128514167b315f4d104b177e04a3dereed@android.combool SkBounder::doIRectGlyph(const SkIRect& r, int x, int y,
2353d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                             const SkGlyph& glyph) {
2354474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com    SkIRect    rr;
2355d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    if (!rr.intersect(fClip->getBounds(), r)) {
2356d055c1fde2128514167b315f4d104b177e04a3dereed@android.com        return false;
2357d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    }
2358d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    GlyphRec rec;
2359d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fLSB.set(SkIntToFixed(x), SkIntToFixed(y));
2360d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fRSB.set(rec.fLSB.fX + glyph.fAdvanceX,
2361d055c1fde2128514167b315f4d104b177e04a3dereed@android.com                 rec.fLSB.fY + glyph.fAdvanceY);
2362d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fGlyphID = glyph.getGlyphID();
2363d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    rec.fFlags = 0;
2364d055c1fde2128514167b315f4d104b177e04a3dereed@android.com    return this->onIRectGlyph(rr, rec);
2365474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com}
2366474a12c4976b3000174cad5df74c498cd723c5e2reed@android.com
23678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doHairline(const SkPoint& pt0, const SkPoint& pt1,
23688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           const SkPaint& paint) {
23698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect     r;
23708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    v0, v1;
23718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v0 = pt0.fX;
23738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v1 = pt1.fX;
23748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (v0 > v1) {
23758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkTSwap<SkScalar>(v0, v1);
23768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fLeft     = SkScalarFloor(v0);
23788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fRight    = SkScalarCeil(v1);
23798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v0 = pt0.fY;
23818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    v1 = pt1.fY;
23828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (v0 > v1) {
23838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkTSwap<SkScalar>(v0, v1);
23848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fTop      = SkScalarFloor(v0);
23868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.fBottom   = SkScalarCeil(v1);
23878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.isAntiAlias()) {
23898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(-1, -1);
23908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
23928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
23938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doRect(const SkRect& rect, const SkPaint& paint) {
23958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect    r;
23968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.getStyle() == SkPaint::kFill_Style) {
23988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rect.round(&r);
23998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
24008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int rad = -1;
24018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rect.roundOut(&r);
24028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.isAntiAlias()) {
24038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            rad = -2;
24048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
24058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(rad, rad);
24068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
24088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
24098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBounder::doPath(const SkPath& path, const SkPaint& paint, bool doFill) {
2411d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    SkIRect       r;
2412d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    const SkRect& bounds = path.getBounds();
24138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (doFill) {
24158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.round(&r);
24168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // hairline
24178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.roundOut(&r);
24188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.isAntiAlias()) {
24218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.inset(-1, -1);
24228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->doIRect(r);
24248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
24258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkBounder::commit() {
24278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // override in subclass
24288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
24298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////////////////////////
24318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPath.h"
24338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDraw.h"
24348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRegion.h"
24358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBlitter.h"
24368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds,
24388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkMaskFilter* filter, const SkMatrix* filterMatrix,
24398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkIRect* bounds) {
24408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (devPath.isEmpty()) {
24418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
24428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIPoint   margin;
24458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    margin.set(0, 0);
24468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //  init our bounds from the path
24488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
2449d252db03d9650013b545ef9781fe993c07f8f314reed@android.com        SkRect pathBounds = devPath.getBounds();
24508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathBounds.inset(-SK_ScalarHalf, -SK_ScalarHalf);
24518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pathBounds.roundOut(bounds);
24528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2453a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (filter) {
24558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(filterMatrix);
2456a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkMask  srcM, dstM;
2458a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fBounds = *bounds;
24608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fFormat = SkMask::kA8_Format;
24618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        srcM.fImage = NULL;
24628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
24638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return false;
24648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
24658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        *bounds = dstM.fBounds;
24668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (clipBounds && !SkIRect::Intersects(*clipBounds, *bounds)) {
24698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
24708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2471a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
24728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // (possibly) trim the srcM bounds to reflect the clip
24738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // (plus whatever slop the filter needs)
24748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (clipBounds && !clipBounds->contains(*bounds)) {
24758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkIRect tmp = *bounds;
24768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        (void)tmp.intersect(*clipBounds);
24773555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // Ugh. Guard against gigantic margins from wacky filters. Without this
24783555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // check we can request arbitrary amounts of slop beyond our visible
24793555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // clip, and bring down the renderer (at least on finite RAM machines
24803555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // like handsets, etc.). Need to balance this invented value between
24813555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // quality of large filters like blurs, and the corresponding memory
24823555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        // requests.
24833555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        static const int MAX_MARGIN = 128;
24843555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com        tmp.inset(-SkMin32(margin.fX, MAX_MARGIN),
24853555591c158c242b071c7ec92ad75b6e4cb74af2reed@android.com                  -SkMin32(margin.fY, MAX_MARGIN));
24868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        (void)bounds->intersect(tmp);
24878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
24908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
24918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void draw_into_mask(const SkMask& mask, const SkPath& devPath) {
24938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBitmap    bm;
24948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDraw      draw;
24958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRegion    clipRgn;
24968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    matrix;
24978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint     paint;
24988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), mask.fBounds.height(), mask.fRowBytes);
25008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bm.setPixels(mask.fImage);
25018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    clipRgn.setRect(0, 0, mask.fBounds.width(), mask.fBounds.height());
25038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
25048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        -SkIntToScalar(mask.fBounds.fTop));
25058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fBitmap    = &bm;
25078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fClip      = &clipRgn;
25088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fMatrix    = &matrix;
25098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.fBounder   = NULL;
25108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    paint.setAntiAlias(true);
25118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    draw.drawPath(devPath, paint);
25128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
25158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkMaskFilter* filter, const SkMatrix* filterMatrix,
25168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkMask* mask, SkMask::CreateMode mode) {
25178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kJustRenderImage_CreateMode != mode) {
25188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds))
25198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return false;
25208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2521a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
25228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
25238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask->fFormat = SkMask::kA8_Format;
25248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mask->fRowBytes = mask->fBounds.width();
2525543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        size_t size = mask->computeImageSize();
2526543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        if (0 == size) {
2527543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            // we're too big to allocate the mask, abort
2528543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com            return false;
2529543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        }
2530543ed9352c7dfd93071c08b14930cca2e82a08d4reed@android.com        mask->fImage = SkMask::AllocImage(size);
25318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        memset(mask->fImage, 0, mask->computeImageSize());
25328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
25338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
25348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkMask::kJustComputeBounds_CreateMode != mode) {
25358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        draw_into_mask(*mask, devPath);
25368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2537a76de3d1a92134c3e95ad7fce99234f92fa48268reed@google.com
25388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
25398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2540