SkDevice.h revision 1f90287df3129cb267422e482c52ebeca6a8990f
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2010 The Android Open Source Project
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDevice_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkDevice_DEFINED
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRefCnt.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBitmap.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColor.h"
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1846799cd9f0bded51a189d77731b25af159ab4609reed@google.comclass SkClipStack;
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkDraw;
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct SkIRect;
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkMatrix;
22a7d948523dce6044fc02db90726e5f971a93628breed@google.comclass SkMetaData;
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkRegion;
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2588edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com// This is an opaque class, not interpreted by skia
26d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.comclass SkGpuRenderTarget;
27d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
287ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkDevice : public SkRefCnt {
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
30af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com    /**
31af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *  Construct a new device with the specified bitmap as its backend. It is
32af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *  valid for the bitmap to have no pixels associated with it. In that case,
33af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *  any drawing to this device will have no effect.
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
35af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com    SkDevice(const SkBitmap& bitmap);
36af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com
37af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com    /**
38af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *  Create a new raster device and have the pixels be automatically
39af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *  allocated. The rowBytes of the device will be computed automatically
40af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *  based on the config and the width.
41af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *
42af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *  @param config   The desired config for the pixels. If the request cannot
43af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *                  be met, the closest matching support config will be used.
44af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *  @param width    width (in pixels) of the device
45af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *  @param height   height (in pixels) of the device
46af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *  @param isOpaque Set to true if it is known that all of the pixels will
47af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *                  be drawn to opaquely. Used as an accelerator when drawing
48af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     *                  these pixels to another device.
49af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com     */
50af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com    SkDevice(SkBitmap::Config config, int width, int height, bool isOpaque = false);
51af951c9bc4cbb6e60b430194fe5127ebe99c53fbreed@google.com
52a7d948523dce6044fc02db90726e5f971a93628breed@google.com    virtual ~SkDevice();
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
54ea4ac97dec2eb291139bd906939e0d2e05cdd7efmike@reedtribe.org    /**
552d54d06adc58f0a6372fb2a1b2eca832965b863creed@google.com     *  Creates a device that is of the same type as this device (e.g. SW-raster,
562d54d06adc58f0a6372fb2a1b2eca832965b863creed@google.com     *  GPU, or PDF). The backing store for this device is created automatically
572d54d06adc58f0a6372fb2a1b2eca832965b863creed@google.com     *  (e.g. offscreen pixels or FBO or whatever is appropriate).
58e97f0856a8044866b12527819d14cdfbcdfd96f2bsalomon@google.com     *
592d54d06adc58f0a6372fb2a1b2eca832965b863creed@google.com     *  @param width    width of the device to create
602d54d06adc58f0a6372fb2a1b2eca832965b863creed@google.com     *  @param height   height of the device to create
612d54d06adc58f0a6372fb2a1b2eca832965b863creed@google.com     *  @param isOpaque performance hint, set to true if you know that you will
622d54d06adc58f0a6372fb2a1b2eca832965b863creed@google.com     *                  draw into this device such that all of the pixels will
632d54d06adc58f0a6372fb2a1b2eca832965b863creed@google.com     *                  be opaque.
64e97f0856a8044866b12527819d14cdfbcdfd96f2bsalomon@google.com     */
6574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org    SkDevice* createCompatibleDevice(SkBitmap::Config config,
66e97f0856a8044866b12527819d14cdfbcdfd96f2bsalomon@google.com                                     int width, int height,
67e97f0856a8044866b12527819d14cdfbcdfd96f2bsalomon@google.com                                     bool isOpaque);
68e97f0856a8044866b12527819d14cdfbcdfd96f2bsalomon@google.com
6988edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    SkMetaData& getMetaData();
7088edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
7135fc62b960db6739b19c59576085663796951e47vandebo@chromium.org    enum Capabilities {
7235fc62b960db6739b19c59576085663796951e47vandebo@chromium.org        kGL_Capability     = 0x1,  //!< mask indicating GL support
7335fc62b960db6739b19c59576085663796951e47vandebo@chromium.org        kVector_Capability = 0x2,  //!< mask indicating a vector representation
7435fc62b960db6739b19c59576085663796951e47vandebo@chromium.org        kAll_Capabilities  = 0x3
7535fc62b960db6739b19c59576085663796951e47vandebo@chromium.org    };
768a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com    virtual uint32_t getDeviceCapabilities() { return 0; }
7735fc62b960db6739b19c59576085663796951e47vandebo@chromium.org
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the width of the device (in pixels).
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
808a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com    virtual int width() const { return fBitmap.width(); }
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the height of the device (in pixels).
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
838a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com    virtual int height() const { return fBitmap.height(); }
846f8f292aa768869a9e85c314b124875f57504f2creed@google.com
85d51bfa0583c82c7b731b993e56159fbf815dd2e0reed@google.com    /**
86d51bfa0583c82c7b731b993e56159fbf815dd2e0reed@google.com     *  Return the bounds of the device in the coordinate space of the root
87d51bfa0583c82c7b731b993e56159fbf815dd2e0reed@google.com     *  canvas. The root device will have its top-left at 0,0, but other devices
88d51bfa0583c82c7b731b993e56159fbf815dd2e0reed@google.com     *  such as those associated with saveLayer may have a non-zero origin.
89d51bfa0583c82c7b731b993e56159fbf815dd2e0reed@google.com     */
90d51bfa0583c82c7b731b993e56159fbf815dd2e0reed@google.com    void getGlobalBounds(SkIRect* bounds) const;
9188edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if the device's bitmap's config treats every pixels as
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        implicitly opaque.
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool isOpaque() const { return fBitmap.isOpaque(); }
963dd42b3c87299ad17b4df93fe251f6f9362495dcreed@google.com
9788edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    /** Return the bitmap config of the device's pixels
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
9988edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    SkBitmap::Config config() const { return fBitmap.getConfig(); }
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the bitmap associated with this device. Call this each time you need
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        to access the bitmap, as it notifies the subclass to perform any flushing
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        etc. before you examine the pixels.
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param changePixels set to true if the caller plans to change the pixels
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return the device's bitmap
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkBitmap& accessBitmap(bool changePixels);
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10988edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    /**
110ae0fb058dd9a11171ae4b807ce8a65017e52d5e1bsalomon@google.com     *  DEPRECATED: This will be made protected once WebKit stops using it.
1116850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *              Instead use Canvas' writePixels method.
112d58a1cd00b969a7755c375f55cf80f4d49d3047bbsalomon@google.com     *
11388edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  Similar to draw sprite, this method will copy the pixels in bitmap onto
11488edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  the device, with the top/left corner specified by (x, y). The pixel
11588edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  values in the device are completely replaced: there is no blending.
116d58a1cd00b969a7755c375f55cf80f4d49d3047bbsalomon@google.com     *
117d58a1cd00b969a7755c375f55cf80f4d49d3047bbsalomon@google.com     *  Currently if bitmap is backed by a texture this is a no-op. This may be
118d58a1cd00b969a7755c375f55cf80f4d49d3047bbsalomon@google.com     *  relaxed in the future.
119d58a1cd00b969a7755c375f55cf80f4d49d3047bbsalomon@google.com     *
120d58a1cd00b969a7755c375f55cf80f4d49d3047bbsalomon@google.com     *  If the bitmap has config kARGB_8888_Config then the config8888 param
121d58a1cd00b969a7755c375f55cf80f4d49d3047bbsalomon@google.com     *  will determines how the pixel valuess are intepreted. If the bitmap is
122d58a1cd00b969a7755c375f55cf80f4d49d3047bbsalomon@google.com     *  not kARGB_8888_Config then this parameter is ignored.
123398109cc3e56b8122ad21c4d43a74a83be5b42a8bsalomon@google.com     */
124d58a1cd00b969a7755c375f55cf80f4d49d3047bbsalomon@google.com    virtual void writePixels(const SkBitmap& bitmap, int x, int y,
125ae0fb058dd9a11171ae4b807ce8a65017e52d5e1bsalomon@google.com                             SkCanvas::Config8888 config8888 = SkCanvas::kNative_Premul_Config8888);
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
127d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com    /**
128d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com     * Return the device's associated gpu render target, or NULL.
129f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
130d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com    virtual SkGpuRenderTarget* accessRenderTarget() { return NULL; }
131f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
13288edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
13388edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    /**
13488edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  Return the device's origin: its offset in device coordinates from
13588edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  the default origin in its canvas' matrix/clip
13688edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     */
13788edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    const SkIPoint& getOrigin() const { return fOrigin; }
13888edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
139d53f7c28bf8aecd5b7f0ee2189ac97e309a1b06dcaryclark@google.comprotected:
140d53f7c28bf8aecd5b7f0ee2189ac97e309a1b06dcaryclark@google.com    enum Usage {
141d53f7c28bf8aecd5b7f0ee2189ac97e309a1b06dcaryclark@google.com       kGeneral_Usage,
1421f90287df3129cb267422e482c52ebeca6a8990ftomhudson@google.com       kSaveLayer_Usage  // <! internal use only
143d53f7c28bf8aecd5b7f0ee2189ac97e309a1b06dcaryclark@google.com    };
144d53f7c28bf8aecd5b7f0ee2189ac97e309a1b06dcaryclark@google.com
14588edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    struct TextFlags {
14688edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com        uint32_t            fFlags;     // SkPaint::getFlags()
14788edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com        SkPaint::Hinting    fHinting;
14888edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    };
14988edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
15088edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    /**
15188edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  Device may filter the text flags for drawing text here. If it wants to
15288edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  make a change to the specified values, it should write them into the
15388edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  textflags parameter (output) and return true. If the paint is fine as
15488edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  is, then ignore the textflags parameter and return false.
15588edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *
15688edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  The baseclass SkDevice filters based on its depth and blitters.
15788edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     */
15888edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    virtual bool filterTextFlags(const SkPaint& paint, TextFlags*);
15988edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
16046799cd9f0bded51a189d77731b25af159ab4609reed@google.com    /**
1618a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com     *  Called with the correct matrix and clip before this device is drawn
1628a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com     *  to using those settings. If your subclass overrides this, be sure to
1638a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com     *  call through to the base class as well.
1648a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com     *
1658a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com     *  The clipstack is another view of the clip. It records the actual
1668a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com     *  geometry that went into building the region. It is present for devices
1678a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com     *  that want to parse it, but is not required: the region is a complete
1688a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com     *  picture of the current clip. (i.e. if you regionize all of the geometry
1698a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com     *  in the clipstack, you will arrive at an equivalent region to the one
1708a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com     *  passed in).
1718a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com    */
1728a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com    virtual void setMatrixClip(const SkMatrix&, const SkRegion&,
1738a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com                               const SkClipStack&);
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Called when this device gains focus (i.e becomes the current device
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        for drawing).
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
178d302f1401b3c9aea094804bad4e76de98782cfe8bsalomon@google.com    virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&,
1798a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com                           const SkClipStack&) {}
1803dd42b3c87299ad17b4df93fe251f6f9362495dcreed@google.com
18188edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    /** Clears the entire device to the specified color (including alpha).
18288edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     *  Ignores the clip.
183f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
18488edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    virtual void clear(SkColor color);
185f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
186f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    /**
18788edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     * Deprecated name for clear.
188f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
18988edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    void eraseColor(SkColor eraseColor) { this->clear(eraseColor); }
1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** These are called inside the per-device-layer loop for each draw call.
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     When these are called, we have already applied any saveLayer operations,
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     and are handling any looping from the paint, and any effects from the
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     DrawFilter.
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawPaint(const SkDraw&, const SkPaint& paint);
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPoint[], const SkPaint& paint);
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawRect(const SkDraw&, const SkRect& r,
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint& paint);
2017ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com    /**
2027ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  If pathIsMutable, then the implementation is allowed to cast path to a
2037ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  non-const pointer and modify it in place (as an optimization). Canvas
2047ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  may do this to implement helpers such as drawOval, by placing a temp
2057ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  path on the stack to hold the representation of the oval.
2067ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *
2077ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  If prePathMatrix is not null, it should logically be applied before any
2087ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  stroking or other effects. If there are no effects on the paint that
2097ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  affect the geometry/rasterization, then the pre matrix can just be
2107ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     *  pre-concated with the current matrix.
2117ff8d81290c3d9d461f2e304f4fd7846275f956dreed@google.com     */
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawPath(const SkDraw&, const SkPath& path,
213f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                          const SkPaint& paint,
214f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                          const SkMatrix* prePathMatrix = NULL,
215f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                          bool pathIsMutable = false);
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
217f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            const SkIRect* srcRectOrNull,
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkMatrix& matrix, const SkPaint& paint);
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            int x, int y, const SkPaint& paint);
22152c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com    /**
22252c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com     *  Does not handle text decoration.
22352c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com     *  Decorations (underline and stike-thru) will be handled by SkCanvas.
22452c748b1691f02f90b27c35bc05074fcef709e66bungeman@google.com     */
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawText(const SkDraw&, const void* text, size_t len,
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          SkScalar x, SkScalar y, const SkPaint& paint);
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawPosText(const SkDraw&, const void* text, size_t len,
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             const SkScalar pos[], SkScalar constY,
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             int scalarsPerPos, const SkPaint& paint);
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                const SkPath& path, const SkMatrix* matrix,
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                const SkPaint& paint);
23356c69773aea56c6c6bd47bc7e7970dd081205184djsollen@google.com#ifdef SK_BUILD_FOR_ANDROID
234cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com    virtual void drawPosTextOnPath(const SkDraw& draw, const void* text, size_t len,
235cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                                   const SkPoint pos[], const SkPaint& paint,
236cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com                                   const SkPath& path, const SkMatrix* matrix);
237cd9d69b9ce7eb301a9fd8d91b9f95fd99b07bae5djsollen@google.com#endif
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPoint verts[], const SkPoint texs[],
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkColor colors[], SkXfermode* xmode,
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const uint16_t indices[], int indexCount,
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint);
24388edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    /** The SkDevice passed will be an SkDevice which was returned by a call to
24488edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com        onCreateCompatibleDevice on this device with kSaveLayer_Usage.
24588edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     */
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPaint&);
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2496850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com    /**
2506850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  On success (returns true), copy the device pixels into the bitmap.
2516850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  On failure, the bitmap parameter is left unchanged and false is
2526850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  returned.
2536850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *
2546850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  The device's pixels are converted to the bitmap's config. The only
2556850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  supported config is kARGB_8888_Config, though this is likely to be
2566850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  relaxed in  the future. The meaning of config kARGB_8888_Config is
2576850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  modified by the enum param config8888. The default value interprets
2586850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  kARGB_8888_Config as SkPMColor
2596850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *
2606850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  If the bitmap has pixels already allocated, the device pixels will be
26174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org     *  written there. If not, bitmap->allocPixels() will be called
2626850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  automatically. If the bitmap is backed by a texture readPixels will
2636850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  fail.
2646850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *
2656850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  The actual pixels written is the intersection of the device's bounds,
2666850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  and the rectangle formed by the bitmap's width,height and the specified
2676850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  x,y. If bitmap pixels extend outside of that intersection, they will not
2686850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  be modified.
2696850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *
2706850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *  Other failure conditions:
2716850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *    * If the device is not a raster device (e.g. PDF) then readPixels will
2726850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *      fail.
2736850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *    * If bitmap is texture-backed then readPixels will fail. (This may be
2746850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     *      relaxed in the future.)
2756850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com     */
2766850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com    bool readPixels(SkBitmap* bitmap,
2776850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com                    int x, int y,
2786850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com                    SkCanvas::Config8888 config8888);
2796850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com
2803636ed558fb2af5a48a9634efec55fd8a87c88d7reed@google.com    ///////////////////////////////////////////////////////////////////////////
2813636ed558fb2af5a48a9634efec55fd8a87c88d7reed@google.com
2821f9767c03bad1ef85e5388d84e23e4b5dff4bc1ajunov@chromium.org    /** Update as needed the pixel value in the bitmap, so that the caller can
2831f9767c03bad1ef85e5388d84e23e4b5dff4bc1ajunov@chromium.org        access the pixels directly. Note: only the pixels field should be
2841f9767c03bad1ef85e5388d84e23e4b5dff4bc1ajunov@chromium.org        altered. The config/width/height/rowbytes must remain unchanged.
2851f9767c03bad1ef85e5388d84e23e4b5dff4bc1ajunov@chromium.org        @param bitmap The device's bitmap
2861f9767c03bad1ef85e5388d84e23e4b5dff4bc1ajunov@chromium.org        @return Echo the bitmap parameter, or an alternate (shadow) bitmap
2871f9767c03bad1ef85e5388d84e23e4b5dff4bc1ajunov@chromium.org            maintained by the subclass.
2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2891f9767c03bad1ef85e5388d84e23e4b5dff4bc1ajunov@chromium.org    virtual const SkBitmap& onAccessBitmap(SkBitmap*);
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
291f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
292f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    // just for subclasses, to assign a custom pixelref
293f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkPixelRef* setPixelRef(SkPixelRef* pr, size_t offset) {
294f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fBitmap.setPixelRef(pr, offset);
295f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        return pr;
296f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
29774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
298c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com    /**
299c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com     * Implements readPixels API. The caller will ensure that:
300c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com     *  1. bitmap has pixel config kARGB_8888_Config.
301c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com     *  2. bitmap has pixels.
302c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com     *  3. The rectangle (x, y, x + bitmap->width(), y + bitmap->height()) is
303c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com     *     contained in the device bounds.
304c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com     */
3056850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com    virtual bool onReadPixels(const SkBitmap& bitmap,
3066850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com                              int x, int y,
3076850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com                              SkCanvas::Config8888 config8888);
308f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
30988edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    /** Called when this device is installed into a Canvas. Balanaced by a call
31088edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com        to unlockPixels() when the device is removed from a Canvas.
31188edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    */
31288edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    virtual void lockPixels();
31388edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    virtual void unlockPixels();
31488edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
31576dd277b1fa021c42fc3acdd8d61e7dc05f9c267reed@google.com    /**
316b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com     *  Returns true if the device allows processing of this imagefilter. If
317b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com     *  false is returned, then the filter is ignored. This may happen for
318b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com     *  some subclasses that do not support pixel manipulations after drawing
319b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com     *  has occurred (e.g. printing). The default implementation returns true.
320b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com     */
321b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com    virtual bool allowImageFilter(SkImageFilter*);
322b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com
323b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com    /**
3248926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  Override and return true for filters that the device can handle
3258926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  intrinsically. Doing so means that SkCanvas will pass-through this
3268926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  filter to drawSprite and drawDevice (and potentially filterImage).
3278926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  Returning false means the SkCanvas will have apply the filter itself,
3288926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  and just pass the resulting image to the device.
32976dd277b1fa021c42fc3acdd8d61e7dc05f9c267reed@google.com     */
3308926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com    virtual bool canHandleImageFilter(SkImageFilter*);
3318926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com
3328926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com    /**
3338926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  Related (but not required) to canHandleImageFilter, this method returns
3348926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  true if the device could apply the filter to the src bitmap and return
3358926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  the result (and updates offset as needed).
3368926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  If the device does not recognize or support this filter,
3378926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     *  it just returns false and leaves result and offset unchanged.
3388926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com     */
3398926b169f6a0dfa4c2129a98ec2aee205f0c8527reed@google.com    virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&,
34076dd277b1fa021c42fc3acdd8d61e7dc05f9c267reed@google.com                             SkBitmap* result, SkIPoint* offset);
34176dd277b1fa021c42fc3acdd8d61e7dc05f9c267reed@google.com
34274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org    // This is equal kBGRA_Premul_Config8888 or kRGBA_Premul_Config8888 if
3436850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com    // either is identical to kNative_Premul_Config8888. Otherwise, -1.
3446850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com    static const SkCanvas::Config8888 kPMColorAlias;
3456850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
3476f8f292aa768869a9e85c314b124875f57504f2creed@google.com    friend class SkCanvas;
34888edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    friend struct DeviceCM; //for setMatrixClip
34988edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    friend class SkDraw;
35088edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    friend class SkDrawIter;
35188edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    friend class SkDeviceFilteredPaint;
35276dd277b1fa021c42fc3acdd8d61e7dc05f9c267reed@google.com    friend class DeviceImageFilterProxy;
35388edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
35449d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com    /**
35549d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com     * postSave is called by SkCanvas to inform the device that it has
35649d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com     * just completed a save operation. This allows derived
35749d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com     * classes to initialize their state-dependent caches.
35849d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com     */
35949d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com    virtual void postSave() {};
36049d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com
36149d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com    /**
36249d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com     * preRestore is called by SkCanvas right before it executes a restore
36349d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com     * operation. As the partner of postSave, it allows
36449d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com     * derived classes to clear their state-dependent caches.
36549d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com     */
36649d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com    virtual void preRestore() {};
36749d9fd5fdb9ffd48538c8fc5a2f6f2d43a2e4fe9robertphillips@google.com
3685676b4a23a190b9a0e2f4d803fe17dbfa47caf7evandebo@chromium.org    // just called by SkCanvas when built as a layer
3695676b4a23a190b9a0e2f4d803fe17dbfa47caf7evandebo@chromium.org    void setOrigin(int x, int y) { fOrigin.set(x, y); }
370e97f0856a8044866b12527819d14cdfbcdfd96f2bsalomon@google.com    // just called by SkCanvas for saveLayer
37174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org    SkDevice* createCompatibleDeviceForSaveLayer(SkBitmap::Config config,
372e97f0856a8044866b12527819d14cdfbcdfd96f2bsalomon@google.com                                                 int width, int height,
373e97f0856a8044866b12527819d14cdfbcdfd96f2bsalomon@google.com                                                 bool isOpaque);
3746f8f292aa768869a9e85c314b124875f57504f2creed@google.com
37588edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    /**
37688edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     * Subclasses should override this to implement createCompatibleDevice.
37788edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     */
37874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org    virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
37974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org                                               int width, int height,
38088edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com                                               bool isOpaque,
38188edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com                                               Usage usage);
38288edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
38388edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com    /** Causes any deferred drawing to the device to be completed.
38488edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com     */
3858a0b0291ae4260ef2a46f4341c18a702c0ce3f8btomhudson@google.com    virtual void flush() {}
38688edf1e50794e6d8cd7cc671ffce4f5e329ef888bungeman@google.com
3873636ed558fb2af5a48a9634efec55fd8a87c88d7reed@google.com    SkBitmap    fBitmap;
3886f8f292aa768869a9e85c314b124875f57504f2creed@google.com    SkIPoint    fOrigin;
389a7d948523dce6044fc02db90726e5f971a93628breed@google.com    SkMetaData* fMetaData;
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
393