SkCanvas.cpp revision f2b98d67dcb6fcb3120feede9c72016fc7b3ead8
18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Copyright (C) 2006-2008 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Licensed under the Apache License, Version 2.0 (the "License");
58a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * you may not use this file except in compliance with the License.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * You may obtain a copy of the License at
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *      http://www.apache.org/licenses/LICENSE-2.0
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Unless required by applicable law or agreed to in writing, software
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * distributed under the License is distributed on an "AS IS" BASIS,
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * See the License for the specific language governing permissions and
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * limitations under the License.
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBounder.h"
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDevice.h"
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDraw.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDrawFilter.h"
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDrawLooper.h"
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPicture.h"
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkScalarCompare.h"
25f76bacff7f66724072c67edb185abf9e3add11a0reed@android.com#include "SkShape.h"
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTemplates.h"
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h"
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include <new>
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//#define SK_TRACE_SAVERESTORE
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_TRACE_SAVERESTORE
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static int gLayerCounter;
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void inc_layer() { ++gLayerCounter; printf("----- inc layer %d\n", gLayerCounter); }
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void dec_layer() { --gLayerCounter; printf("----- dec layer %d\n", gLayerCounter); }
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static int gRecCounter;
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void inc_rec() { ++gRecCounter; printf("----- inc rec %d\n", gRecCounter); }
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void dec_rec() { --gRecCounter; printf("----- dec rec %d\n", gRecCounter); }
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static int gCanvasCounter;
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void inc_canvas() { ++gCanvasCounter; printf("----- inc canvas %d\n", gCanvasCounter); }
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void dec_canvas() { --gCanvasCounter; printf("----- dec canvas %d\n", gCanvasCounter); }
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define inc_layer()
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define dec_layer()
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define inc_rec()
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define dec_rec()
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define inc_canvas()
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define dec_canvas()
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// Helpers for computing fast bounds for quickReject tests
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkCanvas::EdgeType paint2EdgeType(const SkPaint* paint) {
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return paint != NULL && paint->isAntiAlias() ?
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkCanvas::kAA_EdgeType : SkCanvas::kBW_EdgeType;
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  This is the record we keep for each SkDevice that the user installs.
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The clip/matrix/proc are fields that reflect the top of the save/restore
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    stack. Whenever the canvas changes, it marks a dirty flag, and then before
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    these are used (assuming we're not on a layer) we rebuild these cache
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    values: they reflect the top of the save stack, but translated and clipped
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    by the device's XY offset and bitmap-bounds.
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct DeviceCM {
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    DeviceCM*           fNext;
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDevice*           fDevice;
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRegion            fClip;
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix*     fMatrix;
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	SkPaint*			fPaint;	// may be null (in the future)
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int16_t             fX, fY; // relative to base matrix/clip
77f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    // optional, related to canvas' external matrix
78f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkMatrix*     fMVMatrix;
79f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkMatrix*     fExtMatrix;
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	DeviceCM(SkDevice* device, int x, int y, const SkPaint* paint)
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            : fNext(NULL) {
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != device) {
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            device->ref();
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            device->lockPixels();
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fDevice = device;
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fX = SkToS16(x);
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fY = SkToS16(y);
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPaint = paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL;
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	}
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	~DeviceCM() {
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != fDevice) {
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fDevice->unlockPixels();
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fDevice->unref();
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		SkDELETE(fPaint);
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	}
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void updateMC(const SkMatrix& totalMatrix, const SkRegion& totalClip,
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                  SkRegion* updateClip) {
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int x = fX;
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int y = fY;
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int width = fDevice->width();
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int height = fDevice->height();
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if ((x | y) == 0) {
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fMatrix = &totalMatrix;
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fClip = totalClip;
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fMatrixStorage = totalMatrix;
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fMatrixStorage.postTranslate(SkIntToScalar(-x),
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                         SkIntToScalar(-y));
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fMatrix = &fMatrixStorage;
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            totalClip.translate(-x, -y, &fClip);
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fClip.op(0, 0, width, height, SkRegion::kIntersect_Op);
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // intersect clip, but don't translate it (yet)
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (updateClip) {
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            updateClip->op(x, y, x + width, y + height,
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           SkRegion::kDifference_Op);
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fDevice->setMatrixClip(*fMatrix, fClip);
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!fClip.isEmpty()) {
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkIRect deviceR;
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            deviceR.set(0, 0, width, height);
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(deviceR.contains(fClip.getBounds()));
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
138f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        // default is to assume no external matrix
139f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fMVMatrix = NULL;
140f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fExtMatrix = NULL;
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
142f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
143f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    // can only be called after calling updateMC()
144f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    void updateExternalMatrix(const SkMatrix& extM, const SkMatrix& extI) {
145f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fMVMatrixStorage.setConcat(extI, *fMatrix);
146f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fMVMatrix = &fMVMatrixStorage;
147f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fExtMatrix = &extM; // assumes extM has long life-time (owned by canvas)
148f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
149f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void translateClip() {
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fX | fY) {
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fClip.translate(fX, fY);
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
157f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkMatrix    fMatrixStorage, fMVMatrixStorage;
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  This is the record we keep for each save/restore level in the stack.
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Since a level optionally copies the matrix and/or stack, we have pointers
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for these fields. If the value is copied for this level, the copy is
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    stored in the ...Storage field, and the pointer points to that. If the
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    value is not copied for this level, we ignore ...Storage, and just point
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    at the corresponding value in the previous level in the stack.
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkCanvas::MCRec {
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    MCRec*          fNext;
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix*       fMatrix;    // points to either fMatrixStorage or prev MCRec
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRegion*       fRegion;    // points to either fRegionStorage or prev MCRec
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawFilter*   fFilter;    // the current filter (or null)
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    DeviceCM*   fLayer;
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*  If there are any layers in the stack, this points to the top-most
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        one that is at or below this level in the stack (so we know what
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bitmap/device to draw into from this level. This value is NOT
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        reference counted, since the real owner is either our fLayer field,
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        or a previous one in a lower level.)
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    DeviceCM*	fTopLayer;
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    MCRec(const MCRec* prev, int flags) {
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != prev) {
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (flags & SkCanvas::kMatrix_SaveFlag) {
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                fMatrixStorage = *prev->fMatrix;
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                fMatrix = &fMatrixStorage;
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } else {
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                fMatrix = prev->fMatrix;
1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (flags & SkCanvas::kClip_SaveFlag) {
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                fRegionStorage = *prev->fRegion;
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                fRegion = &fRegionStorage;
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } else {
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                fRegion = prev->fRegion;
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fFilter = prev->fFilter;
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fFilter->safeRef();
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fTopLayer = prev->fTopLayer;
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {   // no prev
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fMatrixStorage.reset();
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fMatrix     = &fMatrixStorage;
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fRegion     = &fRegionStorage;
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fFilter     = NULL;
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fTopLayer   = NULL;
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fLayer = NULL;
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // don't bother initializing fNext
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        inc_rec();
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~MCRec() {
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fFilter->safeUnref();
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDELETE(fLayer);
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        dec_rec();
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    fMatrixStorage;
2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRegion    fRegionStorage;
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkDrawIter : public SkDraw {
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawIter(SkCanvas* canvas, bool skipEmptyClips = true) {
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCanvas = canvas;
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->updateDeviceCMCache();
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fBounder = canvas->getBounder();
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCurrLayer = canvas->fMCRec->fTopLayer;
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fSkipEmptyClips = skipEmptyClips;
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool next() {
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // skip over recs with empty clips
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fSkipEmptyClips) {
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            while (fCurrLayer && fCurrLayer->fClip.isEmpty()) {
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                fCurrLayer = fCurrLayer->fNext;
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != fCurrLayer) {
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            const DeviceCM* rec = fCurrLayer;
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fMatrix = rec->fMatrix;
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fClip   = &rec->fClip;
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fDevice = rec->fDevice;
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fBitmap = &fDevice->accessBitmap(true);
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fLayerX = rec->fX;
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fLayerY = rec->fY;
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fPaint  = rec->fPaint;
256f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fMVMatrix = rec->fMVMatrix;
257f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fExtMatrix = rec->fExtMatrix;
258f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            SkDEBUGCODE(this->validate();)
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fCurrLayer = rec->fNext;
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (fBounder) {
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                fBounder->setClip(fClip);
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // fCurrLayer may be NULL now
265199f108f14a5f60a9c2205ffa79b26102a206ad0reed@android.com
266f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fCanvas->prepareForDeviceDraw(fDevice, *fMatrix, *fClip);
2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return true;
2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int getX() const { return fLayerX; }
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int getY() const { return fLayerY; }
2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDevice* getDevice() const { return fDevice; }
2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkMatrix& getMatrix() const { return *fMatrix; }
2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkRegion& getClip() const { return *fClip; }
2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPaint* getPaint() const { return fPaint; }
2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkCanvas*       fCanvas;
2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const DeviceCM* fCurrLayer;
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPaint*  fPaint;     // May be null.
2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int             fLayerX;
2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int             fLayerY;
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBool8         fSkipEmptyClips;
2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkDraw INHERITED;
2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////////////
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass AutoDrawLooper {
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    AutoDrawLooper(SkCanvas* canvas, const SkPaint& paint, SkDrawFilter::Type t)
2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            : fCanvas(canvas), fPaint((SkPaint*)&paint), fType(t) {
2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if ((fLooper = paint.getLooper()) != NULL) {
2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fLooper->init(canvas, (SkPaint*)&paint);
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fOnce = true;
2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fFilter = canvas->getDrawFilter();
3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fNeedFilterRestore = false;
3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~AutoDrawLooper() {
3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fNeedFilterRestore) {
3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(fFilter);
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fFilter->restore(fCanvas, fPaint, fType);
3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != fLooper) {
3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fLooper->restore();
3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool next() {
3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDrawFilter* filter = fFilter;
3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if we drew earlier with a filter, then we need to restore first
3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fNeedFilterRestore) {
3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(filter);
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            filter->restore(fCanvas, fPaint, fType);
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fNeedFilterRestore = false;
3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bool result;
3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != fLooper) {
3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            result = fLooper->next();
3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            result = fOnce;
3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fOnce = false;
3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if we're gonna draw, give the filter a chance to do its work
3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (result && NULL != filter) {
3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fNeedFilterRestore = result = filter->filter(fCanvas, fPaint,
3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                                         fType);
3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return result;
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawLooper*   fLooper;
3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawFilter*   fFilter;
3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkCanvas*       fCanvas;
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint*        fPaint;
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawFilter::Type  fType;
3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool            fOnce;
3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool            fNeedFilterRestore;
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  Stack helper for managing a SkBounder. In the destructor, if we were
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    given a bounder, we call its commit() method, signifying that we are
3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    done accumulating bounds for that draw.
3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoBounderCommit {
3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoBounderCommit(SkBounder* bounder) : fBounder(bounder) {}
3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoBounderCommit() {
3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL != fBounder) {
3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fBounder->commit();
3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBounder*  fBounder;
3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass AutoValidator {
3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    AutoValidator(SkDevice* device) : fDevice(device) {}
3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~AutoValidator() {
3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkBitmap& bm = fDevice->accessBitmap(false);
3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (bm.config() == SkBitmap::kARGB_4444_Config) {
3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            for (int y = 0; y < bm.height(); y++) {
3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                const SkPMColor16* p = bm.getAddr16(0, y);
3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                for (int x = 0; x < bm.width(); x++) {
3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkPMColor16 c = p[x];
3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    SkPMColor16Assert(c);
3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDevice* fDevice;
3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////// macros to place around the internal draw calls //////////////////
3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define ITER_BEGIN(paint, type)                                     \
3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*    AutoValidator   validator(fMCRec->fTopLayer->fDevice); */     \
3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    AutoDrawLooper  looper(this, paint, type);                      \
3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (looper.next()) {                                         \
3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAutoBounderCommit ac(fBounder);                           \
3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDrawIter          iter(this);
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define ITER_END    }
4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDevice* SkCanvas::init(SkDevice* device) {
4058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fBounder = NULL;
4068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fLocalBoundsCompareTypeDirty = true;
407ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    fLocalBoundsCompareTypeDirtyBW = true;
408199f108f14a5f60a9c2205ffa79b26102a206ad0reed@android.com    fLastDeviceToGainFocus = NULL;
409447bcfa8898ce10e7b6493ba9e3e23e08bd13f01agl@chromium.org    fDeviceCMDirty = false;
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMCRec = (MCRec*)fMCStack.push_back();
4128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    new (fMCRec) MCRec(NULL, 0);
4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMCRec->fLayer = SkNEW_ARGS(DeviceCM, (NULL, 0, 0, NULL));
4158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMCRec->fTopLayer = fMCRec->fLayer;
4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMCRec->fNext = NULL;
4178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
418f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    fUseExternalMatrix = false;
419f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->setDevice(device);
4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4238d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.orgSkCanvas::SkCanvas(SkDeviceFactory* factory)
4248d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org        : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)),
4258d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org          fDeviceFactory(factory) {
4268d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    inc_canvas();
4278d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org
4288d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    if (!factory)
4298d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org        fDeviceFactory = SkNEW(SkRasterDeviceFactory);
4308d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org
4318d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    this->init(NULL);
4328d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org}
4338d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org
4348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkCanvas::SkCanvas(SkDevice* device)
4358d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org        : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)),
4368d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org          fDeviceFactory(device->getDeviceFactory()) {
4378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inc_canvas();
4388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->init(device);
4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkCanvas::SkCanvas(const SkBitmap& bitmap)
4438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) {
4448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inc_canvas();
4458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
446f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDevice* device = SkNEW_ARGS(SkDevice, (this, bitmap, false));
4478d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    fDeviceFactory = device->getDeviceFactory();
4488d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    this->init(device)->unref();
4498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkCanvas::~SkCanvas() {
4528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // free up the contents of our deque
4538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->restoreToCount(1);    // restore everything but the last
4548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->internalRestore();    // restore the last, since we're going away
4558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4568d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    SkSafeUnref(fBounder);
457b70ae310bbdaa1b26786773aabce5548c1f48563vandebo@chromium.org    SkDELETE(fDeviceFactory);
458b70ae310bbdaa1b26786773aabce5548c1f48563vandebo@chromium.org
4598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dec_canvas();
4608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkBounder* SkCanvas::setBounder(SkBounder* bounder) {
4638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRefCnt_SafeAssign(fBounder, bounder);
4648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return bounder;
4658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDrawFilter* SkCanvas::getDrawFilter() const {
4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fMCRec->fFilter;
4698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDrawFilter* SkCanvas::setDrawFilter(SkDrawFilter* filter) {
4728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRefCnt_SafeAssign(fMCRec->fFilter, filter);
4738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return filter;
4748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDevice* SkCanvas::getDevice() const {
4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // return root device
4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDeque::Iter   iter(fMCStack);
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    MCRec*          rec = (MCRec*)iter.next();
4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(rec && rec->fLayer);
4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return rec->fLayer->fDevice;
4848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDevice* SkCanvas::setDevice(SkDevice* device) {
4878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // return root device
4888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDeque::Iter   iter(fMCStack);
4898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    MCRec*          rec = (MCRec*)iter.next();
4908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(rec && rec->fLayer);
4918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDevice*       rootDevice = rec->fLayer->fDevice;
4928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (rootDevice == device) {
4948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return device;
4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /* Notify the devices that they are going in/out of scope, so they can do
4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com       things like lock/unlock their pixels, etc.
4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (device) {
5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        device->lockPixels();
5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (rootDevice) {
5048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rootDevice->unlockPixels();
5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRefCnt_SafeAssign(rec->fLayer->fDevice, device);
5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    rootDevice = device;
5098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
5118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*  Now we update our initial region to have the bounds of the new device,
5138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        and then intersect all of the clips in our stack with these bounds,
5148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        to ensure that we can't draw outside of the device's bounds (and trash
5158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                                                     memory).
5168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    NOTE: this is only a partial-fix, since if the new device is larger than
5188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        the previous one, we don't know how to "enlarge" the clips in our stack,
5198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        so drawing may be artificially restricted. Without keeping a history of
5208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        all calls to canvas->clipRect() and canvas->clipPath(), we can't exactly
5218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        reconstruct the correct clips, so this approximation will have to do.
5228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        The caller really needs to restore() back to the base if they want to
5238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        accurately take advantage of the new device bounds.
5248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
5258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == device) {
5278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rec->fRegion->setEmpty();
5288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while ((rec = (MCRec*)iter.next()) != NULL) {
5298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            (void)rec->fRegion->setEmpty();
5308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
5318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
5328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // compute our total bounds for all devices
5338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkIRect bounds;
5348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bounds.set(0, 0, device->width(), device->height());
5368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // now jam our 1st clip to be bounds, and intersect the rest with that
5388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rec->fRegion->setRect(bounds);
5398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        while ((rec = (MCRec*)iter.next()) != NULL) {
5408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            (void)rec->fRegion->op(bounds, SkRegion::kIntersect_Op);
5418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
5428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return device;
5448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
546f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comSkDevice* SkCanvas::setBitmapDevice(const SkBitmap& bitmap, bool forLayer) {
547f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDevice* device = this->setDevice(SkNEW_ARGS(SkDevice, (this, bitmap, forLayer)));
5488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    device->unref();
5498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return device;
5508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
5538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::getViewport(SkIPoint* size) const {
55535fc62b960db6739b19c59576085663796951e47vandebo@chromium.org    if ((getDevice()->getDeviceCapabilities() & SkDevice::kGL_Capability) == 0)
5568d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org        return false;
5578d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    if (size)
5588d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org        size->set(getDevice()->width(), getDevice()->height());
5598d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    return true;
5608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::setViewport(int width, int height) {
56335fc62b960db6739b19c59576085663796951e47vandebo@chromium.org    if ((getDevice()->getDeviceCapabilities() & SkDevice::kGL_Capability) == 0)
5648d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org        return false;
565f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
566f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    this->setDevice(this->createDevice(SkBitmap::kARGB_8888_Config, width, height,
567f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                       false, false))->unref();
5688d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    return true;
5698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::updateDeviceCMCache() {
5728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fDeviceCMDirty) {
5738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkMatrix& totalMatrix = this->getTotalMatrix();
5748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkRegion& totalClip = this->getTotalClip();
5758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        DeviceCM*       layer = fMCRec->fTopLayer;
5768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (NULL == layer->fNext) {   // only one layer
5788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            layer->updateMC(totalMatrix, totalClip, NULL);
579f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            if (fUseExternalMatrix) {
580f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                layer->updateExternalMatrix(fExternalMatrix,
581f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                            fExternalInverse);
582f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            }
5838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
5848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkRegion clip;
5858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            clip = totalClip;  // make a copy
5868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            do {
5878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                layer->updateMC(totalMatrix, clip, &clip);
588f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                if (fUseExternalMatrix) {
589f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                    layer->updateExternalMatrix(fExternalMatrix,
590f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                                fExternalInverse);
591f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                }
5928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            } while ((layer = layer->fNext) != NULL);
5938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
5948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fDeviceCMDirty = false;
5958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
598f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comvoid SkCanvas::prepareForDeviceDraw(SkDevice* device, const SkMatrix& matrix,
599f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                    const SkRegion& clip) {
6008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(device);
601199f108f14a5f60a9c2205ffa79b26102a206ad0reed@android.com    if (fLastDeviceToGainFocus != device) {
602f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        device->gainFocus(this, matrix, clip);
603199f108f14a5f60a9c2205ffa79b26102a206ad0reed@android.com        fLastDeviceToGainFocus = device;
604199f108f14a5f60a9c2205ffa79b26102a206ad0reed@android.com    }
6058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
6088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkCanvas::internalSave(SaveFlags flags) {
6108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int saveCount = this->getSaveCount(); // record this before the actual save
6118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    MCRec* newTop = (MCRec*)fMCStack.push_back();
6138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    new (newTop) MCRec(fMCRec, flags);    // balanced in restore()
6148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    newTop->fNext = fMCRec;
6168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMCRec = newTop;
6178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return saveCount;
6198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkCanvas::save(SaveFlags flags) {
6228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // call shared impl
6238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->internalSave(flags);
6248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define C32MASK (1 << SkBitmap::kARGB_8888_Config)
6278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define C16MASK (1 << SkBitmap::kRGB_565_Config)
6288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define C8MASK  (1 << SkBitmap::kA8_Config)
6298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkBitmap::Config resolve_config(SkCanvas* canvas,
6318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                       const SkIRect& bounds,
6328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                       SkCanvas::SaveFlags flags,
6338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                       bool* isOpaque) {
6348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    *isOpaque = (flags & SkCanvas::kHasAlphaLayer_SaveFlag) == 0;
6358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0
6378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // loop through and union all the configs we may draw into
6388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t configMask = 0;
6398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = canvas->countLayerDevices() - 1; i >= 0; --i)
6408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
6418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDevice* device = canvas->getLayerDevice(i);
6428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (device->intersects(bounds))
6438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            configMask |= 1 << device->config();
6448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
6458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // if the caller wants alpha or fullcolor, we can't return 565
6478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (flags & (SkCanvas::kFullColorLayer_SaveFlag |
6488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                 SkCanvas::kHasAlphaLayer_SaveFlag))
6498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        configMask &= ~C16MASK;
6508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (configMask) {
6528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case C8MASK:    // if we only have A8, return that
6538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return SkBitmap::kA8_Config;
6548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case C16MASK:   // if we only have 565, return that
6568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return SkBitmap::kRGB_565_Config;
6578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    default:
6598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return SkBitmap::kARGB_8888_Config; // default answer
6608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
6618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
6628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkBitmap::kARGB_8888_Config; // default answer
6638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
6648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool bounds_affects_clip(SkCanvas::SaveFlags flags) {
6678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (flags & SkCanvas::kClipToLayer_SaveFlag) != 0;
6688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
6718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SaveFlags flags) {
6728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // do this before we create the layer. We don't call the public save() since
6738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // that would invoke a possibly overridden virtual
6748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int count = this->internalSave(flags);
6758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
6778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkIRect         ir;
6798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkIRect&  clipBounds = this->getTotalClip().getBounds();
680f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (clipBounds.isEmpty()) {
681f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        return count;
682f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
6838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL != bounds) {
6858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect r;
6868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->getTotalMatrix().mapRect(&r, *bounds);
6888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.roundOut(&ir);
6898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // early exit if the layer's bounds are clipped out
6908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (!ir.intersect(clipBounds)) {
6918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (bounds_affects_clip(flags))
6928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                fMCRec->fRegion->setEmpty();
6938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return count;
6948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
6958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {    // no user bounds, so just use the clip
6968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        ir = clipBounds;
6978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
6988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // early exit if the clip is now empty
7008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (bounds_affects_clip(flags) &&
7018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        !fMCRec->fRegion->op(ir, SkRegion::kIntersect_Op)) {
7028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return count;
7038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool isOpaque;
7068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBitmap::Config config = resolve_config(this, ir, flags, &isOpaque);
7078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDevice* device = this->createDevice(config, ir.width(), ir.height(),
7098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                          isOpaque, true);
7108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    DeviceCM* layer = SkNEW_ARGS(DeviceCM, (device, ir.fLeft, ir.fTop, paint));
7118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    device->unref();
7128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    layer->fNext = fMCRec->fTopLayer;
7148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMCRec->fLayer = layer;
7158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMCRec->fTopLayer = layer;    // this field is NOT an owner of layer
7168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return count;
7188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha,
7218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             SaveFlags flags) {
7228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (0xFF == alpha) {
7238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return this->saveLayer(bounds, NULL, flags);
7248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
7258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPaint tmpPaint;
7268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tmpPaint.setAlpha(alpha);
7278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return this->saveLayer(bounds, &tmpPaint, flags);
7288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::restore() {
7328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // check for underflow
7338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fMCStack.count() > 1) {
7348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->internalRestore();
7358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::internalRestore() {
7398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(fMCStack.count() != 0);
7408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
7428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fLocalBoundsCompareTypeDirty = true;
743ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    fLocalBoundsCompareTypeDirtyBW = true;
7448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	// reserve our layer (if any)
7468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    DeviceCM* layer = fMCRec->fLayer;   // may be null
7478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // now detach it from fMCRec so we can pop(). Gets freed after its drawn
7488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMCRec->fLayer = NULL;
7498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // now do the normal restore()
7518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMCRec->~MCRec();       // balanced in save()
7528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMCStack.pop_back();
7538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fMCRec = (MCRec*)fMCStack.back();
7548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*  Time to draw the layer's offscreen. We can't call the public drawSprite,
7568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        since if we're being recorded, we don't want to record this (the
7578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        recorder will have already recorded the restore).
7588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
7598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL != layer) {
7608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (layer->fNext) {
7618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            this->drawDevice(layer->fDevice, layer->fX, layer->fY,
7628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             layer->fPaint);
7638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // reset this, since drawDevice will have set it to true
7648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fDeviceCMDirty = true;
7658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
7668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDELETE(layer);
7678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	}
7688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkCanvas::getSaveCount() const {
7718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fMCStack.count();
7728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::restoreToCount(int count) {
7758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // sanity check
7768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (count < 1) {
7778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        count = 1;
7788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (fMCStack.count() > count) {
7808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->restore();
7818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////////////
7858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// can't draw it if its empty, or its too big for a fixed-point width or height
7878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool reject_bitmap(const SkBitmap& bitmap) {
7888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return  bitmap.width() <= 0 || bitmap.height() <= 0 ||
7898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bitmap.width() > 32767 || bitmap.height() > 32767;
7908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
792f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comvoid SkCanvas::internalDrawBitmap(const SkBitmap& bitmap, const SkIRect* srcRect,
7938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                const SkMatrix& matrix, const SkPaint* paint) {
7948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (reject_bitmap(bitmap)) {
7958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
7968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == paint) {
7998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPaint tmpPaint;
800f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        this->commonDrawBitmap(bitmap, srcRect, matrix, tmpPaint);
8018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
802f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        this->commonDrawBitmap(bitmap, srcRect, matrix, *paint);
8038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawDevice(SkDevice* device, int x, int y,
8078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint* paint) {
8088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint tmp;
8098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == paint) {
8108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tmp.setDither(true);
8118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint = &tmp;
8128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(*paint, SkDrawFilter::kBitmap_Type)
8158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
8168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        iter.fDevice->drawDevice(iter, device, x - iter.getX(), y - iter.getY(),
8178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                 *paint);
8188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
8208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////////////
8238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::translate(SkScalar dx, SkScalar dy) {
8258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
8268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fLocalBoundsCompareTypeDirty = true;
827ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    fLocalBoundsCompareTypeDirtyBW = true;
8288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fMCRec->fMatrix->preTranslate(dx, dy);
8298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::scale(SkScalar sx, SkScalar sy) {
8328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
8338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fLocalBoundsCompareTypeDirty = true;
834ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    fLocalBoundsCompareTypeDirtyBW = true;
8358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fMCRec->fMatrix->preScale(sx, sy);
8368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::rotate(SkScalar degrees) {
8398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
8408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fLocalBoundsCompareTypeDirty = true;
841ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    fLocalBoundsCompareTypeDirtyBW = true;
8428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fMCRec->fMatrix->preRotate(degrees);
8438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::skew(SkScalar sx, SkScalar sy) {
8468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
8478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fLocalBoundsCompareTypeDirty = true;
848ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    fLocalBoundsCompareTypeDirtyBW = true;
8498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fMCRec->fMatrix->preSkew(sx, sy);
8508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::concat(const SkMatrix& matrix) {
8538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
8548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fLocalBoundsCompareTypeDirty = true;
855ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    fLocalBoundsCompareTypeDirtyBW = true;
8568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fMCRec->fMatrix->preConcat(matrix);
8578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::setMatrix(const SkMatrix& matrix) {
8608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
8618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fLocalBoundsCompareTypeDirty = true;
862ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    fLocalBoundsCompareTypeDirtyBW = true;
8638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    *fMCRec->fMatrix = matrix;
8648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// this is not virtual, so it must call a virtual method so that subclasses
8678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// will see its action
8688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::resetMatrix() {
8698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix matrix;
8708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.reset();
8728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->setMatrix(matrix);
8738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
8768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::clipRect(const SkRect& rect, SkRegion::Op op) {
8788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
8798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fLocalBoundsCompareTypeDirty = true;
880ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    fLocalBoundsCompareTypeDirtyBW = true;
8818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fMCRec->fMatrix->rectStaysRect()) {
88398de2bdbd12a01aaf347ca2549801b5940613f3freed@android.com        // for these simpler matrices, we can stay a rect ever after applying
88498de2bdbd12a01aaf347ca2549801b5940613f3freed@android.com        // the matrix. This means we don't have to a) make a path, and b) tell
88598de2bdbd12a01aaf347ca2549801b5940613f3freed@android.com        // the region code to scan-convert the path, only to discover that it
88698de2bdbd12a01aaf347ca2549801b5940613f3freed@android.com        // is really just a rect.
8878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect      r;
8888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkIRect     ir;
8898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fMCRec->fMatrix->mapRect(&r, rect);
8918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.round(&ir);
8928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return fMCRec->fRegion->op(ir, op);
8938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
89498de2bdbd12a01aaf347ca2549801b5940613f3freed@android.com        // since we're rotate or some such thing, we convert the rect to a path
89598de2bdbd12a01aaf347ca2549801b5940613f3freed@android.com        // and clip against that, since it can handle any matrix. However, to
89698de2bdbd12a01aaf347ca2549801b5940613f3freed@android.com        // avoid recursion in the case where we are subclassed (e.g. Pictures)
89798de2bdbd12a01aaf347ca2549801b5940613f3freed@android.com        // we explicitly call "our" version of clipPath.
8988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPath  path;
8998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        path.addRect(rect);
90198de2bdbd12a01aaf347ca2549801b5940613f3freed@android.com        return this->SkCanvas::clipPath(path, op);
9028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::clipPath(const SkPath& path, SkRegion::Op op) {
9068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
9078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fLocalBoundsCompareTypeDirty = true;
908ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    fLocalBoundsCompareTypeDirtyBW = true;
9098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath devPath;
9118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    path.transform(*fMCRec->fMatrix, &devPath);
9128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkRegion::kIntersect_Op == op) {
9148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return fMCRec->fRegion->setPath(devPath, *fMCRec->fRegion);
9158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
9168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRegion base;
9178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkBitmap& bm = this->getDevice()->accessBitmap(false);
9188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        base.setRect(0, 0, bm.width(), bm.height());
9198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (SkRegion::kReplace_Op == op) {
9218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fMCRec->fRegion->setPath(devPath, base);
9228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
9238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkRegion rgn;
9248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            rgn.setPath(devPath, base);
9258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return fMCRec->fRegion->op(rgn, op);
9268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
9278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::clipRegion(const SkRegion& rgn, SkRegion::Op op) {
9318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDeviceCMDirty = true;
9328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fLocalBoundsCompareTypeDirty = true;
933ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    fLocalBoundsCompareTypeDirtyBW = true;
9348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fMCRec->fRegion->op(rgn, op);
9368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
938ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.comvoid SkCanvas::computeLocalClipBoundsCompareType(EdgeType et) const {
9398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect r;
940ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    SkRectCompareType& rCompare = et == kAA_EdgeType ? fLocalBoundsCompareType :
941ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com            fLocalBoundsCompareTypeBW;
942ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com
943ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com    if (!this->getClipBounds(&r, et)) {
944ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com        rCompare.setEmpty();
9458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
946ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com        rCompare.set(SkScalarToCompareType(r.fLeft),
947ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com                     SkScalarToCompareType(r.fTop),
948ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com                     SkScalarToCompareType(r.fRight),
949ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com                     SkScalarToCompareType(r.fBottom));
9508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
953d252db03d9650013b545ef9781fe993c07f8f314reed@android.com/*  current impl ignores edgetype, and relies on
954d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    getLocalClipBoundsCompareType(), which always returns a value assuming
955d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    antialiasing (worst case)
956d252db03d9650013b545ef9781fe993c07f8f314reed@android.com */
957ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.combool SkCanvas::quickReject(const SkRect& rect, EdgeType et) const {
9588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fMCRec->fRegion->isEmpty()) {
9598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return true;
9608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
962a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com    if (fMCRec->fMatrix->getType() & SkMatrix::kPerspective_Mask) {
963a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        SkRect dst;
964a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        fMCRec->fMatrix->mapRect(&dst, rect);
965a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        SkIRect idst;
966a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        dst.roundOut(&idst);
967a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        return !SkIRect::Intersects(idst, fMCRec->fRegion->getBounds());
968a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com    } else {
969ba09de4c4be66cc07790f23b0f3a925f47340e3ereed@android.com        const SkRectCompareType& clipR = this->getLocalClipBoundsCompareType(et);
970d252db03d9650013b545ef9781fe993c07f8f314reed@android.com
971a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        // for speed, do the most likely reject compares first
972a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        SkScalarCompareType userT = SkScalarToCompareType(rect.fTop);
973a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        SkScalarCompareType userB = SkScalarToCompareType(rect.fBottom);
974a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        if (userT >= clipR.fBottom || userB <= clipR.fTop) {
975a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com            return true;
976a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        }
977a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        SkScalarCompareType userL = SkScalarToCompareType(rect.fLeft);
978a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        SkScalarCompareType userR = SkScalarToCompareType(rect.fRight);
979a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        if (userL >= clipR.fRight || userR <= clipR.fLeft) {
980a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com            return true;
981a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        }
982a380ae4a9ac209f5676c06aeaceacc1b08817edareed@android.com        return false;
9838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::quickReject(const SkPath& path, EdgeType et) const {
987d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    return path.isEmpty() || this->quickReject(path.getBounds(), et);
9888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::quickRejectY(SkScalar top, SkScalar bottom, EdgeType et) const {
9918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*  current impl ignores edgetype, and relies on
9928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        getLocalClipBoundsCompareType(), which always returns a value assuming
9938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        antialiasing (worst case)
9948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
9958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fMCRec->fRegion->isEmpty()) {
9978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return true;
9988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1000aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com    SkScalarCompareType userT = SkScalarToCompareType(top);
1001aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com    SkScalarCompareType userB = SkScalarToCompareType(bottom);
10028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // check for invalid user Y coordinates (i.e. empty)
1004d252db03d9650013b545ef9781fe993c07f8f314reed@android.com    // reed: why do we need to do this check, since it slows us down?
10058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (userT >= userB) {
10068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return true;
10078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // check if we are above or below the local clip bounds
10108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkRectCompareType& clipR = this->getLocalClipBoundsCompareType();
10118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return userT >= clipR.fBottom || userB <= clipR.fTop;
10128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkCanvas::getClipBounds(SkRect* bounds, EdgeType et) const {
10158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkRegion& clip = *fMCRec->fRegion;
10168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (clip.isEmpty()) {
10178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (bounds) {
10188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bounds->setEmpty();
10198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
10208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
10218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1023d9c0f0b57affec7a472879c5919acac6637d926areed@android.com    SkMatrix inverse;
1024d9c0f0b57affec7a472879c5919acac6637d926areed@android.com    // if we can't invert the CTM, we can't return local clip bounds
1025d9c0f0b57affec7a472879c5919acac6637d926areed@android.com    if (!fMCRec->fMatrix->invert(&inverse)) {
102672dcd3a3c16a68f98bc345a4263678d43bc3daebreed@android.com        if (bounds) {
102772dcd3a3c16a68f98bc345a4263678d43bc3daebreed@android.com            bounds->setEmpty();
102872dcd3a3c16a68f98bc345a4263678d43bc3daebreed@android.com        }
1029d9c0f0b57affec7a472879c5919acac6637d926areed@android.com        return false;
1030d9c0f0b57affec7a472879c5919acac6637d926areed@android.com    }
1031d9c0f0b57affec7a472879c5919acac6637d926areed@android.com
10328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL != bounds) {
10338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect   r;
10348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // get the clip's bounds
10358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkIRect& ibounds = clip.getBounds();
10368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // adjust it outwards if we are antialiasing
10378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int inset = (kAA_EdgeType == et);
10388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.iset(ibounds.fLeft - inset,  ibounds.fTop - inset,
10398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com               ibounds.fRight + inset, ibounds.fBottom + inset);
10408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // invert into local coordinates
10428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        inverse.mapRect(bounds, r);
10438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
10458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst SkMatrix& SkCanvas::getTotalMatrix() const {
10488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return *fMCRec->fMatrix;
10498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst SkRegion& SkCanvas::getTotalClip() const {
10528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return *fMCRec->fRegion;
10538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1055f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comvoid SkCanvas::setExternalMatrix(const SkMatrix* matrix) {
1056f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (NULL == matrix || matrix->isIdentity()) {
1057f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (fUseExternalMatrix) {
1058f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fDeviceCMDirty = true;
1059f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
1060f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fUseExternalMatrix = false;
1061f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    } else {
1062f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fUseExternalMatrix = true;
1063f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fDeviceCMDirty = true;  // |= (fExternalMatrix != *matrix)
1064f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1065f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fExternalMatrix = *matrix;
1066f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        matrix->invert(&fExternalInverse);
1067f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
1068f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1069f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    static bool gUseExt;
1070f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (gUseExt != fUseExternalMatrix && false) {
1071f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        gUseExt = fUseExternalMatrix;
1072f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        printf("---- fUseExternalMatrix = %d\n", fUseExternalMatrix);
1073f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
1074f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
10758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1076f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comSkDevice* SkCanvas::createDevice(SkBitmap::Config config, int width, int height,
1077f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                 bool isOpaque, bool forLayer) {
1078f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    return fDeviceFactory->newDevice(this, config, width, height, isOpaque, forLayer);
10798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
10828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  These are the virtual drawing methods
10838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
10848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawPaint(const SkPaint& paint) {
10868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(paint, SkDrawFilter::kPaint_Type)
10878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
10898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        iter.fDevice->drawPaint(iter, paint);
10908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
10918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
10938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[],
10968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint& paint) {
10978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if ((long)count <= 0) {
10988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
10998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(pts != NULL);
11028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(paint, SkDrawFilter::kPoint_Type)
11048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
11068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        iter.fDevice->drawPoints(iter, mode, count, pts, paint);
11078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
11108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) {
11138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.canComputeFastBounds()) {
11148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect storage;
11158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (this->quickReject(paint.computeFastBounds(r, &storage),
11168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              paint2EdgeType(&paint))) {
11178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
11188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
11198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(paint, SkDrawFilter::kRect_Type)
11228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
11248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        iter.fDevice->drawRect(iter, r, paint);
11258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
11288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
11318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.canComputeFastBounds()) {
1132d252db03d9650013b545ef9781fe993c07f8f314reed@android.com        SkRect storage;
1133d252db03d9650013b545ef9781fe993c07f8f314reed@android.com        const SkRect& bounds = path.getBounds();
1134d252db03d9650013b545ef9781fe993c07f8f314reed@android.com        if (this->quickReject(paint.computeFastBounds(bounds, &storage),
11358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              paint2EdgeType(&paint))) {
11368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
11378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
11388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(paint, SkDrawFilter::kPath_Type)
11418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
11438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        iter.fDevice->drawPath(iter, path, paint);
11448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
11478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
11508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint* paint) {
11518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDEBUGCODE(bitmap.validate();)
11528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == paint || (paint->getMaskFilter() == NULL)) {
11548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect fastBounds;
11558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fastBounds.set(x, y,
11568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                       x + SkIntToScalar(bitmap.width()),
11578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                       y + SkIntToScalar(bitmap.height()));
11588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (this->quickReject(fastBounds, paint2EdgeType(paint))) {
11598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
11608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
11618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix matrix;
11648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setTranslate(x, y);
1165f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    this->internalDrawBitmap(bitmap, NULL, matrix, paint);
11668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
11698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkRect& dst, const SkPaint* paint) {
11708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (bitmap.width() == 0 || bitmap.height() == 0 || dst.isEmpty()) {
11718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
11728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // do this now, to avoid the cost of calling extract for RLE bitmaps
11758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (this->quickReject(dst, paint2EdgeType(paint))) {
11768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
11778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkBitmap* bitmapPtr = &bitmap;
11808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix matrix;
1182878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com    SkRect tmpSrc;
1183878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com    if (src) {
1184878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com        tmpSrc.set(*src);
1185878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com        // if the extract process clipped off the top or left of the
1186878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com        // original, we adjust for that here to get the position right.
1187878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com        if (tmpSrc.fLeft > 0) {
1188878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com            tmpSrc.fRight -= tmpSrc.fLeft;
1189878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com            tmpSrc.fLeft = 0;
1190fead49e3c43e67cf9648ec1999b34da959e1e36breed@android.com        }
1191878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com        if (tmpSrc.fTop > 0) {
1192878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com            tmpSrc.fBottom -= tmpSrc.fTop;
1193878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com            tmpSrc.fTop = 0;
1194878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com        }
1195878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com    } else {
1196878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com        tmpSrc.set(0, 0, SkIntToScalar(bitmap.width()),
1197878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com                   SkIntToScalar(bitmap.height()));
11988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1199878999965b977c4ed771c3d655f9e23ef9b5adb1reed@android.com    matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1200f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1201f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    // ensure that src is "valid" before we pass it to our internal routines
1202f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    // and to SkDevice. i.e. sure it is contained inside the original bitmap.
1203f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkIRect tmpISrc;
1204f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (src) {
1205f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        tmpISrc.set(0, 0, bitmap.width(), bitmap.height());
1206f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        tmpISrc.intersect(*src);
1207f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        src = &tmpISrc;
1208f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
1209f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    this->internalDrawBitmap(*bitmapPtr, src, matrix, paint);
12108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& matrix,
12138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                const SkPaint* paint) {
12148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDEBUGCODE(bitmap.validate();)
1215f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    this->internalDrawBitmap(bitmap, NULL, matrix, paint);
12168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1218f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comvoid SkCanvas::commonDrawBitmap(const SkBitmap& bitmap, const SkIRect* srcRect,
1219f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                const SkMatrix& matrix, const SkPaint& paint) {
12208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDEBUGCODE(bitmap.validate();)
12219b0390626f73cc88c05c90de64bfe0481e808f14reed@android.com
12228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(paint, SkDrawFilter::kBitmap_Type)
12239b0390626f73cc88c05c90de64bfe0481e808f14reed@android.com
12248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
1225f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        iter.fDevice->drawBitmap(iter, bitmap, srcRect, matrix, paint);
12268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12279b0390626f73cc88c05c90de64bfe0481e808f14reed@android.com
12288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
12298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
12328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint* paint) {
12338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDEBUGCODE(bitmap.validate();)
12348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (reject_bitmap(bitmap)) {
12368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return;
12378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint tmp;
12408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == paint) {
12418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint = &tmp;
12428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(*paint, SkDrawFilter::kBitmap_Type)
12458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
12478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        iter.fDevice->drawSprite(iter, bitmap, x - iter.getX(), y - iter.getY(),
12488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                 *paint);
12498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
12518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawText(const void* text, size_t byteLength,
12548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkScalar x, SkScalar y, const SkPaint& paint) {
12558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(paint, SkDrawFilter::kText_Type)
12568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
12588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        iter.fDevice->drawText(iter, text, byteLength, x, y, paint);
12598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
12628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawPosText(const void* text, size_t byteLength,
12658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                           const SkPoint pos[], const SkPaint& paint) {
12668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(paint, SkDrawFilter::kText_Type)
12678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
12698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        iter.fDevice->drawPosText(iter, text, byteLength, &pos->fX, 0, 2,
12708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                  paint);
12718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
12748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawPosTextH(const void* text, size_t byteLength,
12778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkScalar xpos[], SkScalar constY,
12788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPaint& paint) {
12798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(paint, SkDrawFilter::kText_Type)
12808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
12828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        iter.fDevice->drawPosText(iter, text, byteLength, xpos, constY, 1,
12838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                  paint);
12848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
12878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
12888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawTextOnPath(const void* text, size_t byteLength,
12908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPath& path, const SkMatrix* matrix,
12918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint) {
12928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(paint, SkDrawFilter::kText_Type)
12938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
12958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        iter.fDevice->drawTextOnPath(iter, text, byteLength, path,
12968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                     matrix, paint);
12978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
13008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawVertices(VertexMode vmode, int vertexCount,
13038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPoint verts[], const SkPoint texs[],
13048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkColor colors[], SkXfermode* xmode,
13058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const uint16_t indices[], int indexCount,
13068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPaint& paint) {
13078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_BEGIN(paint, SkDrawFilter::kPath_Type)
13088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (iter.next()) {
13108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        iter.fDevice->drawVertices(iter, vmode, vertexCount, verts, texs,
13118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                   colors, xmode, indices, indexCount, paint);
13128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ITER_END
13158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1317cb60844b34766aad4151df5e87c144d4a57e9abereed@android.comvoid SkCanvas::drawData(const void* data, size_t length) {
1318cb60844b34766aad4151df5e87c144d4a57e9abereed@android.com    // do nothing. Subclasses may do something with the data
1319cb60844b34766aad4151df5e87c144d4a57e9abereed@android.com}
1320cb60844b34766aad4151df5e87c144d4a57e9abereed@android.com
13218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
13228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// These methods are NOT virtual, and therefore must call back into virtual
13238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// methods, rather than actually drawing themselves.
13248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
13258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b,
1327845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com                        SkXfermode::Mode mode) {
13288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint paint;
13298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    paint.setARGB(a, r, g, b);
1331845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com    if (SkXfermode::kSrcOver_Mode != mode) {
13320baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        paint.setXfermodeMode(mode);
13338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->drawPaint(paint);
13358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1337845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.comvoid SkCanvas::drawColor(SkColor c, SkXfermode::Mode mode) {
13388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint paint;
13398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    paint.setColor(c);
1341845fdaca174f4675e9acc164b510e3a5ffa9053creed@android.com    if (SkXfermode::kSrcOver_Mode != mode) {
13420baf19375466cfc24c96532df406e7c5b1d1aae8reed@android.com        paint.setXfermodeMode(mode);
13438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->drawPaint(paint);
13458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) {
13488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint pt;
13498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pt.set(x, y);
13518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->drawPoints(kPoints_PointMode, 1, &pt, paint);
13528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawPoint(SkScalar x, SkScalar y, SkColor color) {
13558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint pt;
13568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint paint;
13578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pt.set(x, y);
13598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    paint.setColor(color);
13608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->drawPoints(kPoints_PointMode, 1, &pt, paint);
13618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1,
13648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        const SkPaint& paint) {
13658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint pts[2];
13668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pts[0].set(x0, y0);
13688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pts[1].set(x1, y1);
13698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->drawPoints(kLines_PointMode, 2, pts, paint);
13708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawRectCoords(SkScalar left, SkScalar top,
13738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              SkScalar right, SkScalar bottom,
13748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint) {
13758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect  r;
13768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.set(left, top, right, bottom);
13788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->drawRect(r, paint);
13798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius,
13828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint& paint) {
13838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (radius < 0) {
13848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        radius = 0;
13858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRect  r;
13888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r.set(cx - radius, cy - radius, cx + radius, cy + radius);
13898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.canComputeFastBounds()) {
13918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect storage;
13928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (this->quickReject(paint.computeFastBounds(r, &storage),
13938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              paint2EdgeType(&paint))) {
13948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
13958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
13968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath  path;
13998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    path.addOval(r);
14008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->drawPath(path, paint);
14018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry,
14048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             const SkPaint& paint) {
14058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (rx > 0 && ry > 0) {
14068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (paint.canComputeFastBounds()) {
14078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkRect storage;
14088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (this->quickReject(paint.computeFastBounds(r, &storage),
14098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                  paint2EdgeType(&paint))) {
14108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return;
14118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
14128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
14138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPath  path;
14158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        path.addRoundRect(r, rx, ry, SkPath::kCW_Direction);
14168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawPath(path, paint);
14178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
14188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawRect(r, paint);
14198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawOval(const SkRect& oval, const SkPaint& paint) {
14238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (paint.canComputeFastBounds()) {
14248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect storage;
14258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (this->quickReject(paint.computeFastBounds(oval, &storage),
14268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              paint2EdgeType(&paint))) {
14278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
14288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
14298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath  path;
14328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    path.addOval(oval);
14338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->drawPath(path, paint);
14348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle,
14378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                       SkScalar sweepAngle, bool useCenter,
14388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                       const SkPaint& paint) {
14398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkScalarAbs(sweepAngle) >= SkIntToScalar(360)) {
14408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawOval(oval, paint);
14418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
14428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPath  path;
14438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (useCenter) {
14448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            path.moveTo(oval.centerX(), oval.centerY());
14458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
14468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        path.arcTo(oval, startAngle, sweepAngle, !useCenter);
14478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (useCenter) {
14488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            path.close();
14498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
14508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->drawPath(path, paint);
14518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawTextOnPathHV(const void* text, size_t byteLength,
14558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                const SkPath& path, SkScalar hOffset,
14568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                SkScalar vOffset, const SkPaint& paint) {
14578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    matrix;
14588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setTranslate(hOffset, vOffset);
14608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->drawTextOnPath(text, byteLength, path, &matrix, paint);
14618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1463f76bacff7f66724072c67edb185abf9e3add11a0reed@android.com///////////////////////////////////////////////////////////////////////////////
1464f76bacff7f66724072c67edb185abf9e3add11a0reed@android.com
14658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::drawPicture(SkPicture& picture) {
14668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int saveCount = save();
14678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    picture.draw(this);
14688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    restoreToCount(saveCount);
14698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1471f76bacff7f66724072c67edb185abf9e3add11a0reed@android.comvoid SkCanvas::drawShape(SkShape* shape) {
1472f76bacff7f66724072c67edb185abf9e3add11a0reed@android.com    // shape baseclass takes care of save/restore
1473f76bacff7f66724072c67edb185abf9e3add11a0reed@android.com    shape->draw(this);
1474f76bacff7f66724072c67edb185abf9e3add11a0reed@android.com}
1475f76bacff7f66724072c67edb185abf9e3add11a0reed@android.com
14768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
14778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
14788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkCanvas::LayerIter::LayerIter(SkCanvas* canvas, bool skipEmptyClips) {
14808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // need COMPILE_TIME_ASSERT
14818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(sizeof(fStorage) >= sizeof(SkDrawIter));
14828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(canvas);
14848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fImpl = new (fStorage) SkDrawIter(canvas, skipEmptyClips);
14868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDone = !fImpl->next();
14878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkCanvas::LayerIter::~LayerIter() {
14908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fImpl->~SkDrawIter();
14918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkCanvas::LayerIter::next() {
14948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDone = !fImpl->next();
14958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDevice* SkCanvas::LayerIter::device() const {
14988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fImpl->getDevice();
14998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst SkMatrix& SkCanvas::LayerIter::matrix() const {
15028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fImpl->getMatrix();
15038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst SkPaint& SkCanvas::LayerIter::paint() const {
15068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkPaint* paint = fImpl->getPaint();
15078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (NULL == paint) {
15088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint = &fDefaultPaint;
15098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
15108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return *paint;
15118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst SkRegion& SkCanvas::LayerIter::clip() const { return fImpl->getClip(); }
15148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkCanvas::LayerIter::x() const { return fImpl->getX(); }
15158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkCanvas::LayerIter::y() const { return fImpl->getY(); }
15168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1517