18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2010 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDevice_DEFINED
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkDevice_DEFINED
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRefCnt.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBitmap.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColor.h"
154cb543d6057b692e1099e9f115155f0bf323a0c8senorblanco@chromium.org#include "SkImageFilter.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1746799cd9f0bded51a189d77731b25af159ab4609reed@google.comclass SkClipStack;
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkDraw;
19024f996c1daac32ecad1f9ffec15f0e54fb207f4fmalitaclass SkDrawFilter;
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct SkIRect;
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkMatrix;
22a7d948523dce6044fc02db90726e5f971a93628breed@google.comclass SkMetaData;
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkRegion;
24e010f1c2a0e6fe9cbaa73be01c7d878308b5166creedstruct SkDeviceProperties;
25b8d00db075b5ea09e353508a26ef5ced50722a6ccommit-bot@chromium.orgclass GrRenderTarget;
26d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
271f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.comclass SK_API SkBaseDevice : public SkRefCnt {
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
291f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com    SK_DECLARE_INST_COUNT(SkBaseDevice)
3015e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com
31af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com    /**
321f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com     *  Construct a new device.
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
341f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com    SkBaseDevice();
35b2db898573e3cdcc8234eebf51961bfc4977ebbcreed    explicit SkBaseDevice(const SkDeviceProperties&);
361f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com    virtual ~SkBaseDevice();
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3888edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    SkMetaData& getMetaData();
3988edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
40d51bfa0583c82c7b731b993e56159fbf815dd2e0reed@google.com    /**
41c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org     *  Return ImageInfo for this device. If the canvas is not backed by pixels
42c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org     *  (cpu or gpu), then the info's ColorType will be kUnknown_SkColorType.
43c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org     */
44c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    virtual SkImageInfo imageInfo() const;
45c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org
46c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    /**
47d51bfa0583c82c7b731b993e56159fbf815dd2e0reed@google.com     *  Return the bounds of the device in the coordinate space of the root
48d51bfa0583c82c7b731b993e56159fbf815dd2e0reed@google.com     *  canvas. The root device will have its top-left at 0,0, but other devices
49d51bfa0583c82c7b731b993e56159fbf815dd2e0reed@google.com     *  such as those associated with saveLayer may have a non-zero origin.
50d51bfa0583c82c7b731b993e56159fbf815dd2e0reed@google.com     */
51ec3ca87d331c4b7058a0fff376a05e84ae0619careed@google.com    void getGlobalBounds(SkIRect* bounds) const {
52ec3ca87d331c4b7058a0fff376a05e84ae0619careed@google.com        SkASSERT(bounds);
53ec3ca87d331c4b7058a0fff376a05e84ae0619careed@google.com        const SkIPoint& origin = this->getOrigin();
54ec3ca87d331c4b7058a0fff376a05e84ae0619careed@google.com        bounds->setXYWH(origin.x(), origin.y(), this->width(), this->height());
55ec3ca87d331c4b7058a0fff376a05e84ae0619careed@google.com    }
5673a5d53d96cb7a0b4214e273ed147d401e31ea16skia.committer@gmail.com
5778e276889795454891cbba48ab11927968114953reed    SkIRect getGlobalBounds() const {
5878e276889795454891cbba48ab11927968114953reed        SkIRect bounds;
5978e276889795454891cbba48ab11927968114953reed        this->getGlobalBounds(&bounds);
6078e276889795454891cbba48ab11927968114953reed        return bounds;
6178e276889795454891cbba48ab11927968114953reed    }
6278e276889795454891cbba48ab11927968114953reed
63f252f64f17efddf7235682a96d155d7c86c898e3reed    int width() const {
64f252f64f17efddf7235682a96d155d7c86c898e3reed        return this->imageInfo().width();
65f252f64f17efddf7235682a96d155d7c86c898e3reed    }
66620fc60b0486765d5bf9d9d3ea50dabe4f358307reed
67f252f64f17efddf7235682a96d155d7c86c898e3reed    int height() const {
68f252f64f17efddf7235682a96d155d7c86c898e3reed        return this->imageInfo().height();
69f252f64f17efddf7235682a96d155d7c86c898e3reed    }
70620fc60b0486765d5bf9d9d3ea50dabe4f358307reed
71f252f64f17efddf7235682a96d155d7c86c898e3reed    bool isOpaque() const {
72f252f64f17efddf7235682a96d155d7c86c898e3reed        return this->imageInfo().isOpaque();
73f252f64f17efddf7235682a96d155d7c86c898e3reed    }
74772c4e6d09f5e0971a584d2035ee789483d6f47askia.committer@gmail.com
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the bitmap associated with this device. Call this each time you need
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        to access the bitmap, as it notifies the subclass to perform any flushing
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        etc. before you examine the pixels.
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param changePixels set to true if the caller plans to change the pixels
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return the device's bitmap
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkBitmap& accessBitmap(bool changePixels);
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
834ef54f8d72420a521d4aae04ff5cd438810eca7fcommit-bot@chromium.org    bool writePixels(const SkImageInfo&, const void*, size_t rowBytes, int x, int y);
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
859c135db83d198e7d8200027c7d2cf60f38517ee3reed@google.com    void* accessPixels(SkImageInfo* info, size_t* rowBytes);
869c135db83d198e7d8200027c7d2cf60f38517ee3reed@google.com
87d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com    /**
88d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com     * Return the device's associated gpu render target, or NULL.
89f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
9089443aba5bfa2b040dc9fd24938b7d0b3decd737reed    virtual GrRenderTarget* accessRenderTarget() { return NULL; }
91f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
9288edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
9388edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    /**
9488edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  Return the device's origin: its offset in device coordinates from
9588edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  the default origin in its canvas' matrix/clip
9688edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     */
9788edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    const SkIPoint& getOrigin() const { return fOrigin; }
9888edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
99fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    /**
10040a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com     * onAttachToCanvas is invoked whenever a device is installed in a canvas
10140a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com     * (i.e., setDevice, saveLayer (for the new device created by the save),
1021f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com     * and SkCanvas' SkBaseDevice & SkBitmap -taking ctors). It allows the
10340a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com     * devices to prepare for drawing (e.g., locking their pixels, etc.)
10440a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com     */
105e0e385c1d4171e065348ba17c546b3463a0bd651sugoi@google.com    virtual void onAttachToCanvas(SkCanvas*) {
106405d0f43d8fa03874aee23669e5fdb38f21cc3e7bsalomon@google.com        SkASSERT(!fAttachedToCanvas);
10740a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com        this->lockPixels();
10840a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com#ifdef SK_DEBUG
10940a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com        fAttachedToCanvas = true;
11040a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com#endif
11140a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com    };
11240a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com
11340a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com    /**
11440a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com     * onDetachFromCanvas notifies a device that it will no longer be drawn to.
11540a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com     * It gives the device a chance to clean up (e.g., unlock its pixels). It
116fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * is invoked from setDevice (for the displaced device), restore and
11740a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com     * possibly from SkCanvas' dtor.
11840a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com     */
11940a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com    virtual void onDetachFromCanvas() {
120405d0f43d8fa03874aee23669e5fdb38f21cc3e7bsalomon@google.com        SkASSERT(fAttachedToCanvas);
12140a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com        this->unlockPixels();
12240a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com#ifdef SK_DEBUG
12340a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com        fAttachedToCanvas = false;
12440a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com#endif
12540a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com    };
12640a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com
127d53f7c28bf8aecd5b7f0ee2189ac97e309a1b06dcaryclark@google.comprotected:
12876033be81b82c44fd5d4fdf2672eb22e505da1f0reed    enum TileUsage {
12976033be81b82c44fd5d4fdf2672eb22e505da1f0reed        kPossible_TileUsage,    //!< the created device may be drawn tiled
13076033be81b82c44fd5d4fdf2672eb22e505da1f0reed        kNever_TileUsage,       //!< the created device will never be drawn tiled
131d53f7c28bf8aecd5b7f0ee2189ac97e309a1b06dcaryclark@google.com    };
132d53f7c28bf8aecd5b7f0ee2189ac97e309a1b06dcaryclark@google.com
13388edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    struct TextFlags {
1343375c8047e7f10e6bf9a2ce1c2e8b0df08c56024reed        uint32_t    fFlags;     // SkPaint::getFlags()
13588edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    };
13688edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
137112e7e277702e104357f2d44742253ee1b0109acfmalita    /**
138112e7e277702e104357f2d44742253ee1b0109acfmalita     * Returns the text-related flags, possibly modified based on the state of the
139112e7e277702e104357f2d44742253ee1b0109acfmalita     * device (e.g. support for LCD).
140112e7e277702e104357f2d44742253ee1b0109acfmalita     */
141112e7e277702e104357f2d44742253ee1b0109acfmalita    uint32_t filterTextFlags(const SkPaint&) const;
142112e7e277702e104357f2d44742253ee1b0109acfmalita
143b2db898573e3cdcc8234eebf51961bfc4977ebbcreed    virtual bool onShouldDisableLCD(const SkPaint&) const { return false; }
14488edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
1453fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com    /**
146a6926b1f0352916c31cda66f2239a25d52f09315bsalomon@google.com     *
147a6926b1f0352916c31cda66f2239a25d52f09315bsalomon@google.com     *  DEPRECATED: This will be removed in a future change. Device subclasses
148a6926b1f0352916c31cda66f2239a25d52f09315bsalomon@google.com     *  should use the matrix and clip from the SkDraw passed to draw functions.
149a6926b1f0352916c31cda66f2239a25d52f09315bsalomon@google.com     *
1503fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com     *  Called with the correct matrix and clip before this device is drawn
1513fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com     *  to using those settings. If your subclass overrides this, be sure to
1523fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com     *  call through to the base class as well.
1533fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com     *
1543fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com     *  The clipstack is another view of the clip. It records the actual
1553fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com     *  geometry that went into building the region. It is present for devices
1563fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com     *  that want to parse it, but is not required: the region is a complete
1573fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com     *  picture of the current clip. (i.e. if you regionize all of the geometry
1583fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com     *  in the clipstack, you will arrive at an equivalent region to the one
1593fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com     *  passed in).
160a6926b1f0352916c31cda66f2239a25d52f09315bsalomon@google.com     */
161a6926b1f0352916c31cda66f2239a25d52f09315bsalomon@google.com     virtual void setMatrixClip(const SkMatrix&, const SkRegion&,
1621f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                                const SkClipStack&) {};
1633fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** These are called inside the per-device-layer loop for each draw call.
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     When these are called, we have already applied any saveLayer operations,
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     and are handling any looping from the paint, and any effects from the
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     DrawFilter.
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
1691f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com    virtual void drawPaint(const SkDraw&, const SkPaint& paint) = 0;
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
1711f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                            const SkPoint[], const SkPaint& paint) = 0;
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawRect(const SkDraw&, const SkRect& r,
1731f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                          const SkPaint& paint) = 0;
17446d3d39e65e0b3ea2ad7c91c176ccafb4df0fa24jvanverth@google.com    virtual void drawOval(const SkDraw&, const SkRect& oval,
1751f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                          const SkPaint& paint) = 0;
176f2bfd54de32ffbcf90ddcd0e249aaebb1559d9c2commit-bot@chromium.org    virtual void drawRRect(const SkDraw&, const SkRRect& rr,
1771f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                           const SkPaint& paint) = 0;
178f2bfd54de32ffbcf90ddcd0e249aaebb1559d9c2commit-bot@chromium.org
179ed9806f5c972513d4141c9d1b5a04ab78b3af4cbcommit-bot@chromium.org    // Default impl calls drawPath()
180ed9806f5c972513d4141c9d1b5a04ab78b3af4cbcommit-bot@chromium.org    virtual void drawDRRect(const SkDraw&, const SkRRect& outer,
181ed9806f5c972513d4141c9d1b5a04ab78b3af4cbcommit-bot@chromium.org                            const SkRRect& inner, const SkPaint&);
182ed9806f5c972513d4141c9d1b5a04ab78b3af4cbcommit-bot@chromium.org
1837ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com    /**
1847ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  If pathIsMutable, then the implementation is allowed to cast path to a
1857ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  non-const pointer and modify it in place (as an optimization). Canvas
1867ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  may do this to implement helpers such as drawOval, by placing a temp
1877ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  path on the stack to hold the representation of the oval.
1887ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *
1897ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  If prePathMatrix is not null, it should logically be applied before any
1907ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  stroking or other effects. If there are no effects on the paint that
1917ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  affect the geometry/rasterization, then the pre matrix can just be
1927ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  pre-concated with the current matrix.
1937ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     */
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawPath(const SkDraw&, const SkPath& path,
195f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                          const SkPaint& paint,
196f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                          const SkMatrix* prePathMatrix = NULL,
1971f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                          bool pathIsMutable = false) = 0;
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
1991f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                            const SkMatrix& matrix, const SkPaint& paint) = 0;
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
2011f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                            int x, int y, const SkPaint& paint) = 0;
20233535f3c48bf723c46f334a93d4a06d782dad30ereed@google.com
20333535f3c48bf723c46f334a93d4a06d782dad30ereed@google.com    /**
20433535f3c48bf723c46f334a93d4a06d782dad30ereed@google.com     *  The default impl. will create a bitmap-shader from the bitmap,
20533535f3c48bf723c46f334a93d4a06d782dad30ereed@google.com     *  and call drawRect with it.
20633535f3c48bf723c46f334a93d4a06d782dad30ereed@google.com     */
20733535f3c48bf723c46f334a93d4a06d782dad30ereed@google.com    virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
20833535f3c48bf723c46f334a93d4a06d782dad30ereed@google.com                                const SkRect* srcOrNull, const SkRect& dst,
209eed779d866e1e239bfb9ebc6a225b7345a41adf9commit-bot@chromium.org                                const SkPaint& paint,
2101f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                                SkCanvas::DrawBitmapRectFlags flags) = 0;
21133535f3c48bf723c46f334a93d4a06d782dad30ereed@google.com
212a85d4d0814818e4ddabb9237da209d61d6cd5854reed    virtual void drawImage(const SkDraw&, const SkImage*, SkScalar x, SkScalar y, const SkPaint&);
213a85d4d0814818e4ddabb9237da209d61d6cd5854reed    virtual void drawImageRect(const SkDraw&, const SkImage*, const SkRect* src, const SkRect& dst,
214a85d4d0814818e4ddabb9237da209d61d6cd5854reed                               const SkPaint&);
215a85d4d0814818e4ddabb9237da209d61d6cd5854reed
21652c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com    /**
21752c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com     *  Does not handle text decoration.
21852c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com     *  Decorations (underline and stike-thru) will be handled by SkCanvas.
21952c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com     */
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawText(const SkDraw&, const void* text, size_t len,
2211f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                          SkScalar x, SkScalar y, const SkPaint& paint) = 0;
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawPosText(const SkDraw&, const void* text, size_t len,
22305c4a4322e7d4f3417b7df33825bab8603d52051fmalita                             const SkScalar pos[], int scalarsPerPos,
22405c4a4322e7d4f3417b7df33825bab8603d52051fmalita                             const SkPoint& offset, const SkPaint& paint) = 0;
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPoint verts[], const SkPoint texs[],
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkColor colors[], SkXfermode* xmode,
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const uint16_t indices[], int indexCount,
2291f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                              const SkPaint& paint) = 0;
230aa1b9120463fc69be7a5545eb83e6fbe6955a424fmalita    // default implementation unrolls the blob runs.
231aa1b9120463fc69be7a5545eb83e6fbe6955a424fmalita    virtual void drawTextBlob(const SkDraw&, const SkTextBlob*, SkScalar x, SkScalar y,
232024f996c1daac32ecad1f9ffec15f0e54fb207f4fmalita                              const SkPaint& paint, SkDrawFilter* drawFilter);
233ecfff21bde1f0ca3c36533eded325066b5f2d42ddandov    // default implementation calls drawVertices
234b3c9d1c33caf325aada244204215eb790c228c12dandov    virtual void drawPatch(const SkDraw&, const SkPoint cubics[12], const SkColor colors[4],
235b3c9d1c33caf325aada244204215eb790c228c12dandov                           const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint);
2369b051a375ba6d6b61cea98f35834cd032aaa5347robertphillips@google.com    /** The SkDevice passed will be an SkDevice which was returned by a call to
23776033be81b82c44fd5d4fdf2672eb22e505da1f0reed        onCreateDevice on this device with kNeverTile_TileExpectation.
23888edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     */
2391f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com    virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
2401f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com                            const SkPaint&) = 0;
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
242f87fe78bc36c94143625fc38cc848d0da86eb60dreed    virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, const SkPath&,
243f87fe78bc36c94143625fc38cc848d0da86eb60dreed                                const SkMatrix*, const SkPaint&);
244a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org    bool readPixels(const SkImageInfo&, void* dst, size_t rowBytes, int x, int y);
2456850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com
2463636ed558fb2af5a48a9634efec55fd8a87c88d7reed@google.com    ///////////////////////////////////////////////////////////////////////////
2473636ed558fb2af5a48a9634efec55fd8a87c88d7reed@google.com
2481f9767c03bad1ef85e5388d84e23e4b5dff4bc1ajunov@chromium.org    /** Update as needed the pixel value in the bitmap, so that the caller can
249772c4e6d09f5e0971a584d2035ee789483d6f47askia.committer@gmail.com        access the pixels directly.
2501f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com        @return The device contents as a bitmap
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2521f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com    virtual const SkBitmap& onAccessBitmap() = 0;
25374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
25407f81a57ba5720fd65563077114bd9e5ba840151robertphillips@google.com    /** Called when this device is installed into a Canvas. Balanced by a call
25588edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com        to unlockPixels() when the device is removed from a Canvas.
25688edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    */
25789443aba5bfa2b040dc9fd24938b7d0b3decd737reed    virtual void lockPixels() {}
25889443aba5bfa2b040dc9fd24938b7d0b3decd737reed    virtual void unlockPixels() {}
25988edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
26076dd277b1fa021c42fc3acdd8d61e7dc05f9c267reed@google.com    /**
2618926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  Override and return true for filters that the device can handle
2628926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  intrinsically. Doing so means that SkCanvas will pass-through this
2638926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  filter to drawSprite and drawDevice (and potentially filterImage).
2648926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  Returning false means the SkCanvas will have apply the filter itself,
2658926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  and just pass the resulting image to the device.
26676dd277b1fa021c42fc3acdd8d61e7dc05f9c267reed@google.com     */
26789443aba5bfa2b040dc9fd24938b7d0b3decd737reed    virtual bool canHandleImageFilter(const SkImageFilter*) { return false; }
2688926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com
2698926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com    /**
2708926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  Related (but not required) to canHandleImageFilter, this method returns
2718926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  true if the device could apply the filter to the src bitmap and return
2728926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  the result (and updates offset as needed).
2738926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  If the device does not recognize or support this filter,
2748926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  it just returns false and leaves result and offset unchanged.
2758926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     */
2764cb543d6057b692e1099e9f115155f0bf323a0c8senorblanco@chromium.org    virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
277c87dd2ce965cd1bbc8a74abe0c141658a469d7f2djsollen                             const SkImageFilter::Context&,
278c87dd2ce965cd1bbc8a74abe0c141658a469d7f2djsollen                             SkBitmap* /*result*/, SkIPoint* /*offset*/) {
27989443aba5bfa2b040dc9fd24938b7d0b3decd737reed        return false;
28089443aba5bfa2b040dc9fd24938b7d0b3decd737reed    }
28176dd277b1fa021c42fc3acdd8d61e7dc05f9c267reed@google.com
2828128d8c119382279918b90ad8d80ccb3aaebb8a0commit-bot@chromium.orgprotected:
28376f10a3bd936af7dbe2b5873d5a7eedd73cdc5dareed@google.com    // default impl returns NULL
2844a8126e7f81384526629b1e21bf89b632ea13cd9reed    virtual SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&);
28502d6f546161e2c98d69066373cec3f54f3c46252skia.committer@gmail.com
286c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    // default impl returns NULL
287c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    virtual const void* peekPixels(SkImageInfo*, size_t* rowBytes);
28802d6f546161e2c98d69066373cec3f54f3c46252skia.committer@gmail.com
2898128d8c119382279918b90ad8d80ccb3aaebb8a0commit-bot@chromium.org    /**
290a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org     *  The caller is responsible for "pre-clipping" the dst. The impl can assume that the dst
291a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org     *  image at the specified x,y offset will fit within the device's bounds.
292a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org     *
293a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org     *  This is explicitly asserted in readPixels(), the public way to call this.
294a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org     */
295a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org    virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y);
2962cccf833265a607be62f1d77ee51662b49d9e813commit-bot@chromium.org
2972cccf833265a607be62f1d77ee51662b49d9e813commit-bot@chromium.org    /**
2984cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org     *  The caller is responsible for "pre-clipping" the src. The impl can assume that the src
2994cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org     *  image at the specified x,y offset will fit within the device's bounds.
3004cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org     *
3014cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org     *  This is explicitly asserted in writePixelsDirect(), the public way to call this.
3024cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org     */
3034cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org    virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int x, int y);
3044cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org
3054cd9e2169e35cd67ee7358acea6541245e1d1744commit-bot@chromium.org    /**
3069c135db83d198e7d8200027c7d2cf60f38517ee3reed@google.com     *  Default impl returns NULL.
3079c135db83d198e7d8200027c7d2cf60f38517ee3reed@google.com     */
3089c135db83d198e7d8200027c7d2cf60f38517ee3reed@google.com    virtual void* onAccessPixels(SkImageInfo* info, size_t* rowBytes);
3099c135db83d198e7d8200027c7d2cf60f38517ee3reed@google.com
3109c135db83d198e7d8200027c7d2cf60f38517ee3reed@google.com    /**
3118128d8c119382279918b90ad8d80ccb3aaebb8a0commit-bot@chromium.org     *  Leaky properties are those which the device should be applying but it isn't.
3128128d8c119382279918b90ad8d80ccb3aaebb8a0commit-bot@chromium.org     *  These properties will be applied by the draw, when and as it can.
3138128d8c119382279918b90ad8d80ccb3aaebb8a0commit-bot@chromium.org     *  If the device does handle a property, that property should be set to the identity value
3148128d8c119382279918b90ad8d80ccb3aaebb8a0commit-bot@chromium.org     *  for that property, effectively making it non-leaky.
3158128d8c119382279918b90ad8d80ccb3aaebb8a0commit-bot@chromium.org     */
316e010f1c2a0e6fe9cbaa73be01c7d878308b5166creed    const SkDeviceProperties& getLeakyProperties() const {
317e010f1c2a0e6fe9cbaa73be01c7d878308b5166creed        return *fLeakyProperties;
318e010f1c2a0e6fe9cbaa73be01c7d878308b5166creed    }
3198128d8c119382279918b90ad8d80ccb3aaebb8a0commit-bot@chromium.org
320145d1c0fdcef63991d7f910cc067a653a8aa2c4ccommit-bot@chromium.org    /**
321145d1c0fdcef63991d7f910cc067a653a8aa2c4ccommit-bot@chromium.org     *  PRIVATE / EXPERIMENTAL -- do not call
322145d1c0fdcef63991d7f910cc067a653a8aa2c4ccommit-bot@chromium.org     *  This entry point gives the backend an opportunity to take over the rendering
323145d1c0fdcef63991d7f910cc067a653a8aa2c4ccommit-bot@chromium.org     *  of 'picture'. If optimization data is available (due to an earlier
324145d1c0fdcef63991d7f910cc067a653a8aa2c4ccommit-bot@chromium.org     *  'optimize' call) this entry point should make use of it and return true
325145d1c0fdcef63991d7f910cc067a653a8aa2c4ccommit-bot@chromium.org     *  if all rendering has been done. If false is returned, SkCanvas will
326eb849e5fd10cbe00cbc31307ba97fd9efca0b41bskia.committer@gmail.com     *  perform its own rendering pass. It is acceptable for the backend
327145d1c0fdcef63991d7f910cc067a653a8aa2c4ccommit-bot@chromium.org     *  to perform some device-specific warm up tasks and then let SkCanvas
328145d1c0fdcef63991d7f910cc067a653a8aa2c4ccommit-bot@chromium.org     *  perform the main rendering loop (by return false from here).
329145d1c0fdcef63991d7f910cc067a653a8aa2c4ccommit-bot@chromium.org     */
330d5fa1a455aad61f3e99081fe7a9b065cb3b115c6reed    virtual bool EXPERIMENTAL_drawPicture(SkCanvas*, const SkPicture*, const SkMatrix*,
331d5fa1a455aad61f3e99081fe7a9b065cb3b115c6reed                                          const SkPaint*);
332145d1c0fdcef63991d7f910cc067a653a8aa2c4ccommit-bot@chromium.org
3336987dcaf257dd7c2c8e0014cf7452fde82bcba5bfmalita    struct CreateInfo {
33476033be81b82c44fd5d4fdf2672eb22e505da1f0reed        static SkPixelGeometry AdjustGeometry(const SkImageInfo&, TileUsage, SkPixelGeometry);
335b2db898573e3cdcc8234eebf51961bfc4977ebbcreed
33676033be81b82c44fd5d4fdf2672eb22e505da1f0reed        // The constructor may change the pixel geometry based on other parameters.
33700b7e5eb973a1b1e4d1affa74fc0341e8c41e3dahalcanary        CreateInfo(const SkImageInfo& info,
33800b7e5eb973a1b1e4d1affa74fc0341e8c41e3dahalcanary                   TileUsage tileUsage,
33900b7e5eb973a1b1e4d1affa74fc0341e8c41e3dahalcanary                   SkPixelGeometry geo,
34000b7e5eb973a1b1e4d1affa74fc0341e8c41e3dahalcanary                   bool forImageFilter = false)
341b2db898573e3cdcc8234eebf51961bfc4977ebbcreed            : fInfo(info)
34276033be81b82c44fd5d4fdf2672eb22e505da1f0reed            , fTileUsage(tileUsage)
34376033be81b82c44fd5d4fdf2672eb22e505da1f0reed            , fPixelGeometry(AdjustGeometry(info, tileUsage, geo))
34400b7e5eb973a1b1e4d1affa74fc0341e8c41e3dahalcanary            , fForImageFilter(forImageFilter) {}
345b2db898573e3cdcc8234eebf51961bfc4977ebbcreed
34676033be81b82c44fd5d4fdf2672eb22e505da1f0reed        const SkImageInfo       fInfo;
34776033be81b82c44fd5d4fdf2672eb22e505da1f0reed        const TileUsage         fTileUsage;
34876033be81b82c44fd5d4fdf2672eb22e505da1f0reed        const SkPixelGeometry   fPixelGeometry;
34900b7e5eb973a1b1e4d1affa74fc0341e8c41e3dahalcanary        const bool              fForImageFilter;
3506987dcaf257dd7c2c8e0014cf7452fde82bcba5bfmalita    };
351b2db898573e3cdcc8234eebf51961bfc4977ebbcreed
35276033be81b82c44fd5d4fdf2672eb22e505da1f0reed    /**
35376033be81b82c44fd5d4fdf2672eb22e505da1f0reed     *  Create a new device based on CreateInfo. If the paint is not null, then it represents a
35476033be81b82c44fd5d4fdf2672eb22e505da1f0reed     *  preview of how the new device will be composed with its creator device (this).
35561f501f8c675da8d5915e5e7fd32dfdb113f1cfbreed     *
35661f501f8c675da8d5915e5e7fd32dfdb113f1cfbreed     *  The subclass may be handed this device in drawDevice(), so it must always return
35761f501f8c675da8d5915e5e7fd32dfdb113f1cfbreed     *  a device that it knows how to draw, and that it knows how to identify if it is not of the
35861f501f8c675da8d5915e5e7fd32dfdb113f1cfbreed     *  same subclass (since drawDevice is passed a SkBaseDevice*). If the subclass cannot fulfill
35961f501f8c675da8d5915e5e7fd32dfdb113f1cfbreed     *  that contract (e.g. PDF cannot support some settings on the paint) it should return NULL,
36061f501f8c675da8d5915e5e7fd32dfdb113f1cfbreed     *  and the caller may then decide to explicitly create a bitmapdevice, knowing that later
36161f501f8c675da8d5915e5e7fd32dfdb113f1cfbreed     *  it could not call drawDevice with it (but it could call drawSprite or drawBitmap).
36276033be81b82c44fd5d4fdf2672eb22e505da1f0reed     */
36357a48a73a4252c21a5377826d79044017f3cbf06reed    virtual SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) {
36457a48a73a4252c21a5377826d79044017f3cbf06reed        return NULL;
36557a48a73a4252c21a5377826d79044017f3cbf06reed    }
36657a48a73a4252c21a5377826d79044017f3cbf06reed
367b2db898573e3cdcc8234eebf51961bfc4977ebbcreed    virtual void initForRootLayer(SkPixelGeometry geo);
368b2db898573e3cdcc8234eebf51961bfc4977ebbcreed
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
3706f8f292aa768869a9e85c314b124875f57504f2creed@google.com    friend class SkCanvas;
3713fffb2ee9fa01ae61efb2395973eb7575eda4c96robertphillips@google.com    friend struct DeviceCM; //for setMatrixClip
37288edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    friend class SkDraw;
37388edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    friend class SkDrawIter;
37488edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    friend class SkDeviceFilteredPaint;
3759c39744a00573b7133fc765b0a9d50a0ceace7b8senorblanco@chromium.org    friend class SkDeviceImageFilterProxy;
3769c135db83d198e7d8200027c7d2cf60f38517ee3reed@google.com    friend class SkDeferredDevice;    // for newSurface
37778e276889795454891cbba48ab11927968114953reed    friend class SkNoPixelsBitmapDevice;
37888edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
37997af1a64ae6bdddd346d8babfd9f188279dd6644reed@google.com    friend class SkSurface_Raster;
3801f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com
38197af1a64ae6bdddd346d8babfd9f188279dd6644reed@google.com    // used to change the backend's pixels (and possibly config/rowbytes)
38297af1a64ae6bdddd346d8babfd9f188279dd6644reed@google.com    // but cannot change the width/height, so there should be no change to
38397af1a64ae6bdddd346d8babfd9f188279dd6644reed@google.com    // any clip information.
384ec3ca87d331c4b7058a0fff376a05e84ae0619careed@google.com    // TODO: move to SkBitmapDevice
38589443aba5bfa2b040dc9fd24938b7d0b3decd737reed    virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) {}
38697af1a64ae6bdddd346d8babfd9f188279dd6644reed@google.com
387d954498c01ccf0417feacf89e45d0c62a06a813breed    virtual bool forceConservativeRasterClip() const { return false; }
388d954498c01ccf0417feacf89e45d0c62a06a813breed
3895676b4a23a190b9a0e2f4d803fe17dbfa47caf7evandebo@chromium.org    // just called by SkCanvas when built as a layer
3905676b4a23a190b9a0e2f4d803fe17dbfa47caf7evandebo@chromium.org    void setOrigin(int x, int y) { fOrigin.set(x, y); }
3916f8f292aa768869a9e85c314b124875f57504f2creed@google.com
39288edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    /** Causes any deferred drawing to the device to be completed.
39388edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     */
39489443aba5bfa2b040dc9fd24938b7d0b3decd737reed    virtual void flush() {}
39588edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
396be129b26f13d575fd6b396c6ae759838ecc9bd1asenorblanco    virtual SkImageFilter::Cache* getImageFilterCache() { return NULL; }
39755b6d8be997a447ef9ce0f029697677a940bfc24senorblanco
3986f8f292aa768869a9e85c314b124875f57504f2creed@google.com    SkIPoint    fOrigin;
399a7d948523dce6044fc02db90726e5f971a93628breed@google.com    SkMetaData* fMetaData;
400e010f1c2a0e6fe9cbaa73be01c7d878308b5166creed    SkDeviceProperties* fLeakyProperties;   // will always exist.
40115e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com
40240a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com#ifdef SK_DEBUG
40340a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com    bool        fAttachedToCanvas;
40440a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com#endif
40540a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com
40615e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com    typedef SkRefCnt INHERITED;
4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
410