epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkDevice_DEFINED |
| 11 | #define SkDevice_DEFINED |
| 12 | |
| 13 | #include "SkRefCnt.h" |
| 14 | #include "SkBitmap.h" |
| 15 | #include "SkCanvas.h" |
| 16 | #include "SkColor.h" |
| 17 | |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 18 | class SkClipStack; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | class SkDraw; |
| 20 | struct SkIRect; |
| 21 | class SkMatrix; |
reed@google.com | a7d9485 | 2011-03-30 21:23:07 +0000 | [diff] [blame] | 22 | class SkMetaData; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | class SkRegion; |
| 24 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 25 | // This is an opaque class, not interpreted by skia |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 26 | class SkGpuRenderTarget; |
| 27 | |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 28 | class SK_API SkDevice : public SkRefCnt { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 30 | SK_DECLARE_INST_COUNT(SkDevice) |
| 31 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 32 | /** |
| 33 | * Construct a new device with the specified bitmap as its backend. It is |
| 34 | * valid for the bitmap to have no pixels associated with it. In that case, |
| 35 | * any drawing to this device will have no effect. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | */ |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 37 | SkDevice(const SkBitmap& bitmap); |
| 38 | |
| 39 | /** |
| 40 | * Create a new raster device and have the pixels be automatically |
| 41 | * allocated. The rowBytes of the device will be computed automatically |
| 42 | * based on the config and the width. |
| 43 | * |
| 44 | * @param config The desired config for the pixels. If the request cannot |
| 45 | * be met, the closest matching support config will be used. |
| 46 | * @param width width (in pixels) of the device |
| 47 | * @param height height (in pixels) of the device |
| 48 | * @param isOpaque Set to true if it is known that all of the pixels will |
| 49 | * be drawn to opaquely. Used as an accelerator when drawing |
| 50 | * these pixels to another device. |
| 51 | */ |
| 52 | SkDevice(SkBitmap::Config config, int width, int height, bool isOpaque = false); |
| 53 | |
reed@google.com | a7d9485 | 2011-03-30 21:23:07 +0000 | [diff] [blame] | 54 | virtual ~SkDevice(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | |
mike@reedtribe.org | ea4ac97 | 2011-04-26 11:48:33 +0000 | [diff] [blame] | 56 | /** |
reed@google.com | 2d54d06 | 2011-06-21 12:19:28 +0000 | [diff] [blame] | 57 | * Creates a device that is of the same type as this device (e.g. SW-raster, |
| 58 | * GPU, or PDF). The backing store for this device is created automatically |
| 59 | * (e.g. offscreen pixels or FBO or whatever is appropriate). |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 60 | * |
reed@google.com | 2d54d06 | 2011-06-21 12:19:28 +0000 | [diff] [blame] | 61 | * @param width width of the device to create |
| 62 | * @param height height of the device to create |
| 63 | * @param isOpaque performance hint, set to true if you know that you will |
| 64 | * draw into this device such that all of the pixels will |
| 65 | * be opaque. |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 66 | */ |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 67 | SkDevice* createCompatibleDevice(SkBitmap::Config config, |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 68 | int width, int height, |
| 69 | bool isOpaque); |
| 70 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 71 | SkMetaData& getMetaData(); |
| 72 | |
vandebo@chromium.org | 35fc62b | 2010-10-26 19:47:30 +0000 | [diff] [blame] | 73 | enum Capabilities { |
| 74 | kGL_Capability = 0x1, //!< mask indicating GL support |
| 75 | kVector_Capability = 0x2, //!< mask indicating a vector representation |
| 76 | kAll_Capabilities = 0x3 |
| 77 | }; |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 78 | virtual uint32_t getDeviceCapabilities() { return 0; } |
vandebo@chromium.org | 35fc62b | 2010-10-26 19:47:30 +0000 | [diff] [blame] | 79 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | /** Return the width of the device (in pixels). |
| 81 | */ |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 82 | virtual int width() const { return fBitmap.width(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | /** Return the height of the device (in pixels). |
| 84 | */ |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 85 | virtual int height() const { return fBitmap.height(); } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 86 | |
reed@google.com | d51bfa0 | 2011-08-30 15:56:11 +0000 | [diff] [blame] | 87 | /** |
| 88 | * Return the bounds of the device in the coordinate space of the root |
| 89 | * canvas. The root device will have its top-left at 0,0, but other devices |
| 90 | * such as those associated with saveLayer may have a non-zero origin. |
| 91 | */ |
| 92 | void getGlobalBounds(SkIRect* bounds) const; |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 93 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | /** Returns true if the device's bitmap's config treats every pixels as |
| 95 | implicitly opaque. |
| 96 | */ |
| 97 | bool isOpaque() const { return fBitmap.isOpaque(); } |
reed@google.com | 3dd42b3 | 2011-02-07 22:44:43 +0000 | [diff] [blame] | 98 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 99 | /** Return the bitmap config of the device's pixels |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | */ |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 101 | SkBitmap::Config config() const { return fBitmap.getConfig(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | |
| 103 | /** Return the bitmap associated with this device. Call this each time you need |
| 104 | to access the bitmap, as it notifies the subclass to perform any flushing |
| 105 | etc. before you examine the pixels. |
| 106 | @param changePixels set to true if the caller plans to change the pixels |
| 107 | @return the device's bitmap |
| 108 | */ |
| 109 | const SkBitmap& accessBitmap(bool changePixels); |
| 110 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 111 | /** |
bsalomon@google.com | ae0fb05 | 2011-11-10 22:34:56 +0000 | [diff] [blame] | 112 | * DEPRECATED: This will be made protected once WebKit stops using it. |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 113 | * Instead use Canvas' writePixels method. |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 114 | * |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 115 | * Similar to draw sprite, this method will copy the pixels in bitmap onto |
| 116 | * the device, with the top/left corner specified by (x, y). The pixel |
| 117 | * values in the device are completely replaced: there is no blending. |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 118 | * |
| 119 | * Currently if bitmap is backed by a texture this is a no-op. This may be |
| 120 | * relaxed in the future. |
| 121 | * |
| 122 | * If the bitmap has config kARGB_8888_Config then the config8888 param |
| 123 | * will determines how the pixel valuess are intepreted. If the bitmap is |
| 124 | * not kARGB_8888_Config then this parameter is ignored. |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 125 | */ |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 126 | virtual void writePixels(const SkBitmap& bitmap, int x, int y, |
bsalomon@google.com | ae0fb05 | 2011-11-10 22:34:56 +0000 | [diff] [blame] | 127 | SkCanvas::Config8888 config8888 = SkCanvas::kNative_Premul_Config8888); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 129 | /** |
| 130 | * Return the device's associated gpu render target, or NULL. |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 131 | */ |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 132 | virtual SkGpuRenderTarget* accessRenderTarget() { return NULL; } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 133 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Return the device's origin: its offset in device coordinates from |
| 137 | * the default origin in its canvas' matrix/clip |
| 138 | */ |
| 139 | const SkIPoint& getOrigin() const { return fOrigin; } |
| 140 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 141 | /** |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 142 | * onAttachToCanvas is invoked whenever a device is installed in a canvas |
| 143 | * (i.e., setDevice, saveLayer (for the new device created by the save), |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 144 | * and SkCanvas' SkDevice & SkBitmap -taking ctors). It allows the |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 145 | * devices to prepare for drawing (e.g., locking their pixels, etc.) |
| 146 | */ |
| 147 | virtual void onAttachToCanvas(SkCanvas* canvas) { |
bsalomon@google.com | 405d0f4 | 2012-08-29 21:26:13 +0000 | [diff] [blame] | 148 | SkASSERT(!fAttachedToCanvas); |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 149 | this->lockPixels(); |
| 150 | #ifdef SK_DEBUG |
| 151 | fAttachedToCanvas = true; |
| 152 | #endif |
| 153 | }; |
| 154 | |
| 155 | /** |
| 156 | * onDetachFromCanvas notifies a device that it will no longer be drawn to. |
| 157 | * It gives the device a chance to clean up (e.g., unlock its pixels). It |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 158 | * is invoked from setDevice (for the displaced device), restore and |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 159 | * possibly from SkCanvas' dtor. |
| 160 | */ |
| 161 | virtual void onDetachFromCanvas() { |
bsalomon@google.com | 405d0f4 | 2012-08-29 21:26:13 +0000 | [diff] [blame] | 162 | SkASSERT(fAttachedToCanvas); |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 163 | this->unlockPixels(); |
| 164 | #ifdef SK_DEBUG |
| 165 | fAttachedToCanvas = false; |
| 166 | #endif |
| 167 | }; |
| 168 | |
caryclark@google.com | d53f7c2 | 2012-01-05 17:05:05 +0000 | [diff] [blame] | 169 | protected: |
| 170 | enum Usage { |
| 171 | kGeneral_Usage, |
tomhudson@google.com | 1f90287 | 2012-06-01 13:15:47 +0000 | [diff] [blame] | 172 | kSaveLayer_Usage // <! internal use only |
caryclark@google.com | d53f7c2 | 2012-01-05 17:05:05 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 175 | struct TextFlags { |
| 176 | uint32_t fFlags; // SkPaint::getFlags() |
| 177 | SkPaint::Hinting fHinting; |
| 178 | }; |
| 179 | |
| 180 | /** |
| 181 | * Device may filter the text flags for drawing text here. If it wants to |
| 182 | * make a change to the specified values, it should write them into the |
| 183 | * textflags parameter (output) and return true. If the paint is fine as |
| 184 | * is, then ignore the textflags parameter and return false. |
| 185 | * |
| 186 | * The baseclass SkDevice filters based on its depth and blitters. |
| 187 | */ |
| 188 | virtual bool filterTextFlags(const SkPaint& paint, TextFlags*); |
| 189 | |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 190 | /** |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 191 | * Called with the correct matrix and clip before this device is drawn |
| 192 | * to using those settings. If your subclass overrides this, be sure to |
| 193 | * call through to the base class as well. |
| 194 | * |
| 195 | * The clipstack is another view of the clip. It records the actual |
| 196 | * geometry that went into building the region. It is present for devices |
| 197 | * that want to parse it, but is not required: the region is a complete |
| 198 | * picture of the current clip. (i.e. if you regionize all of the geometry |
| 199 | * in the clipstack, you will arrive at an equivalent region to the one |
| 200 | * passed in). |
| 201 | */ |
| 202 | virtual void setMatrixClip(const SkMatrix&, const SkRegion&, |
| 203 | const SkClipStack&); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | |
| 205 | /** Called when this device gains focus (i.e becomes the current device |
| 206 | for drawing). |
| 207 | */ |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 208 | virtual void gainFocus(const SkMatrix&, const SkRegion&) { |
| 209 | SkASSERT(fAttachedToCanvas); |
| 210 | } |
reed@google.com | 3dd42b3 | 2011-02-07 22:44:43 +0000 | [diff] [blame] | 211 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 212 | /** Clears the entire device to the specified color (including alpha). |
| 213 | * Ignores the clip. |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 214 | */ |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 215 | virtual void clear(SkColor color); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 216 | |
| 217 | /** |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 218 | * Deprecated name for clear. |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 219 | */ |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 220 | void eraseColor(SkColor eraseColor) { this->clear(eraseColor); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | |
| 222 | /** These are called inside the per-device-layer loop for each draw call. |
| 223 | When these are called, we have already applied any saveLayer operations, |
| 224 | and are handling any looping from the paint, and any effects from the |
| 225 | DrawFilter. |
| 226 | */ |
| 227 | virtual void drawPaint(const SkDraw&, const SkPaint& paint); |
| 228 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, |
| 229 | const SkPoint[], const SkPaint& paint); |
| 230 | virtual void drawRect(const SkDraw&, const SkRect& r, |
| 231 | const SkPaint& paint); |
reed@google.com | 7ff8d81 | 2011-03-25 15:08:16 +0000 | [diff] [blame] | 232 | /** |
| 233 | * If pathIsMutable, then the implementation is allowed to cast path to a |
| 234 | * non-const pointer and modify it in place (as an optimization). Canvas |
| 235 | * may do this to implement helpers such as drawOval, by placing a temp |
| 236 | * path on the stack to hold the representation of the oval. |
| 237 | * |
| 238 | * If prePathMatrix is not null, it should logically be applied before any |
| 239 | * stroking or other effects. If there are no effects on the paint that |
| 240 | * affect the geometry/rasterization, then the pre matrix can just be |
| 241 | * pre-concated with the current matrix. |
| 242 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | virtual void drawPath(const SkDraw&, const SkPath& path, |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 244 | const SkPaint& paint, |
| 245 | const SkMatrix* prePathMatrix = NULL, |
| 246 | bool pathIsMutable = false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 248 | const SkIRect* srcRectOrNull, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 249 | const SkMatrix& matrix, const SkPaint& paint); |
| 250 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 251 | int x, int y, const SkPaint& paint); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame^] | 252 | |
| 253 | /** |
| 254 | * The default impl. will create a bitmap-shader from the bitmap, |
| 255 | * and call drawRect with it. |
| 256 | */ |
| 257 | virtual void drawBitmapRect(const SkDraw&, const SkBitmap&, |
| 258 | const SkRect* srcOrNull, const SkRect& dst, |
| 259 | const SkPaint& paint); |
| 260 | |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 261 | /** |
| 262 | * Does not handle text decoration. |
| 263 | * Decorations (underline and stike-thru) will be handled by SkCanvas. |
| 264 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 265 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 266 | SkScalar x, SkScalar y, const SkPaint& paint); |
| 267 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 268 | const SkScalar pos[], SkScalar constY, |
| 269 | int scalarsPerPos, const SkPaint& paint); |
| 270 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 271 | const SkPath& path, const SkMatrix* matrix, |
| 272 | const SkPaint& paint); |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 273 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 274 | virtual void drawPosTextOnPath(const SkDraw& draw, const void* text, size_t len, |
| 275 | const SkPoint pos[], const SkPaint& paint, |
| 276 | const SkPath& path, const SkMatrix* matrix); |
| 277 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 278 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, |
| 279 | const SkPoint verts[], const SkPoint texs[], |
| 280 | const SkColor colors[], SkXfermode* xmode, |
| 281 | const uint16_t indices[], int indexCount, |
| 282 | const SkPaint& paint); |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 283 | /** The SkDevice passed will be an SkDevice which was returned by a call to |
| 284 | onCreateCompatibleDevice on this device with kSaveLayer_Usage. |
| 285 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 286 | virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, |
| 287 | const SkPaint&); |
| 288 | |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 289 | /** |
| 290 | * On success (returns true), copy the device pixels into the bitmap. |
| 291 | * On failure, the bitmap parameter is left unchanged and false is |
| 292 | * returned. |
| 293 | * |
| 294 | * The device's pixels are converted to the bitmap's config. The only |
| 295 | * supported config is kARGB_8888_Config, though this is likely to be |
| 296 | * relaxed in the future. The meaning of config kARGB_8888_Config is |
| 297 | * modified by the enum param config8888. The default value interprets |
| 298 | * kARGB_8888_Config as SkPMColor |
| 299 | * |
| 300 | * If the bitmap has pixels already allocated, the device pixels will be |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 301 | * written there. If not, bitmap->allocPixels() will be called |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 302 | * automatically. If the bitmap is backed by a texture readPixels will |
| 303 | * fail. |
| 304 | * |
| 305 | * The actual pixels written is the intersection of the device's bounds, |
| 306 | * and the rectangle formed by the bitmap's width,height and the specified |
| 307 | * x,y. If bitmap pixels extend outside of that intersection, they will not |
| 308 | * be modified. |
| 309 | * |
| 310 | * Other failure conditions: |
| 311 | * * If the device is not a raster device (e.g. PDF) then readPixels will |
| 312 | * fail. |
| 313 | * * If bitmap is texture-backed then readPixels will fail. (This may be |
| 314 | * relaxed in the future.) |
| 315 | */ |
| 316 | bool readPixels(SkBitmap* bitmap, |
| 317 | int x, int y, |
| 318 | SkCanvas::Config8888 config8888); |
| 319 | |
reed@google.com | 3636ed5 | 2011-01-25 23:50:57 +0000 | [diff] [blame] | 320 | /////////////////////////////////////////////////////////////////////////// |
| 321 | |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 322 | /** Update as needed the pixel value in the bitmap, so that the caller can |
| 323 | access the pixels directly. Note: only the pixels field should be |
| 324 | altered. The config/width/height/rowbytes must remain unchanged. |
| 325 | @param bitmap The device's bitmap |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 326 | @return Echo the bitmap parameter, or an alternate (shadow) bitmap |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 327 | maintained by the subclass. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 328 | */ |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 329 | virtual const SkBitmap& onAccessBitmap(SkBitmap*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 330 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 331 | SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } |
| 332 | // just for subclasses, to assign a custom pixelref |
| 333 | SkPixelRef* setPixelRef(SkPixelRef* pr, size_t offset) { |
| 334 | fBitmap.setPixelRef(pr, offset); |
| 335 | return pr; |
| 336 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 337 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 338 | /** |
| 339 | * Implements readPixels API. The caller will ensure that: |
| 340 | * 1. bitmap has pixel config kARGB_8888_Config. |
| 341 | * 2. bitmap has pixels. |
| 342 | * 3. The rectangle (x, y, x + bitmap->width(), y + bitmap->height()) is |
| 343 | * contained in the device bounds. |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 344 | */ |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 345 | virtual bool onReadPixels(const SkBitmap& bitmap, |
| 346 | int x, int y, |
| 347 | SkCanvas::Config8888 config8888); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 348 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 349 | /** Called when this device is installed into a Canvas. Balanaced by a call |
| 350 | to unlockPixels() when the device is removed from a Canvas. |
| 351 | */ |
| 352 | virtual void lockPixels(); |
| 353 | virtual void unlockPixels(); |
| 354 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 355 | /** |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 356 | * Returns true if the device allows processing of this imagefilter. If |
| 357 | * false is returned, then the filter is ignored. This may happen for |
| 358 | * some subclasses that do not support pixel manipulations after drawing |
| 359 | * has occurred (e.g. printing). The default implementation returns true. |
| 360 | */ |
| 361 | virtual bool allowImageFilter(SkImageFilter*); |
| 362 | |
| 363 | /** |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 364 | * Override and return true for filters that the device can handle |
| 365 | * intrinsically. Doing so means that SkCanvas will pass-through this |
| 366 | * filter to drawSprite and drawDevice (and potentially filterImage). |
| 367 | * Returning false means the SkCanvas will have apply the filter itself, |
| 368 | * and just pass the resulting image to the device. |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 369 | */ |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 370 | virtual bool canHandleImageFilter(SkImageFilter*); |
| 371 | |
| 372 | /** |
| 373 | * Related (but not required) to canHandleImageFilter, this method returns |
| 374 | * true if the device could apply the filter to the src bitmap and return |
| 375 | * the result (and updates offset as needed). |
| 376 | * If the device does not recognize or support this filter, |
| 377 | * it just returns false and leaves result and offset unchanged. |
| 378 | */ |
| 379 | virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&, |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 380 | SkBitmap* result, SkIPoint* offset); |
| 381 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 382 | // This is equal kBGRA_Premul_Config8888 or kRGBA_Premul_Config8888 if |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 383 | // either is identical to kNative_Premul_Config8888. Otherwise, -1. |
| 384 | static const SkCanvas::Config8888 kPMColorAlias; |
| 385 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 386 | private: |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 387 | friend class SkCanvas; |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 388 | friend struct DeviceCM; //for setMatrixClip |
| 389 | friend class SkDraw; |
| 390 | friend class SkDrawIter; |
| 391 | friend class SkDeviceFilteredPaint; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 392 | friend class DeviceImageFilterProxy; |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 393 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 394 | friend class SkSurface_Raster; |
| 395 | // used to change the backend's pixels (and possibly config/rowbytes) |
| 396 | // but cannot change the width/height, so there should be no change to |
| 397 | // any clip information. |
| 398 | void replaceBitmapBackendForRasterSurface(const SkBitmap&); |
| 399 | |
vandebo@chromium.org | 5676b4a | 2011-10-03 19:03:48 +0000 | [diff] [blame] | 400 | // just called by SkCanvas when built as a layer |
| 401 | void setOrigin(int x, int y) { fOrigin.set(x, y); } |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 402 | // just called by SkCanvas for saveLayer |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 403 | SkDevice* createCompatibleDeviceForSaveLayer(SkBitmap::Config config, |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 404 | int width, int height, |
| 405 | bool isOpaque); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 406 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 407 | /** |
| 408 | * Subclasses should override this to implement createCompatibleDevice. |
| 409 | */ |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 410 | virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config, |
| 411 | int width, int height, |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 412 | bool isOpaque, |
| 413 | Usage usage); |
| 414 | |
| 415 | /** Causes any deferred drawing to the device to be completed. |
| 416 | */ |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 417 | virtual void flush() {} |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 418 | |
reed@google.com | 3636ed5 | 2011-01-25 23:50:57 +0000 | [diff] [blame] | 419 | SkBitmap fBitmap; |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 420 | SkIPoint fOrigin; |
reed@google.com | a7d9485 | 2011-03-30 21:23:07 +0000 | [diff] [blame] | 421 | SkMetaData* fMetaData; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 422 | |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 423 | #ifdef SK_DEBUG |
| 424 | bool fAttachedToCanvas; |
| 425 | #endif |
| 426 | |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 427 | typedef SkRefCnt INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | #endif |