robertphillips@google.com | 9241e33 | 2013-08-21 13:54:44 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #ifndef SkBitmapDevice_DEFINED |
| 10 | #define SkBitmapDevice_DEFINED |
| 11 | |
| 12 | #include "SkDevice.h" |
| 13 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 14 | /////////////////////////////////////////////////////////////////////////////// |
| 15 | class SK_API SkBitmapDevice : public SkBaseDevice { |
| 16 | public: |
| 17 | SK_DECLARE_INST_COUNT(SkBitmapDevice) |
| 18 | |
| 19 | /** |
| 20 | * Construct a new device with the specified bitmap as its backend. It is |
| 21 | * valid for the bitmap to have no pixels associated with it. In that case, |
| 22 | * any drawing to this device will have no effect. |
| 23 | */ |
| 24 | SkBitmapDevice(const SkBitmap& bitmap); |
| 25 | |
| 26 | /** |
| 27 | * Construct a new device with the specified bitmap as its backend. It is |
| 28 | * valid for the bitmap to have no pixels associated with it. In that case, |
| 29 | * any drawing to this device will have no effect. |
| 30 | */ |
| 31 | SkBitmapDevice(const SkBitmap& bitmap, const SkDeviceProperties& deviceProperties); |
| 32 | |
| 33 | /** |
| 34 | * Create a new raster device and have the pixels be automatically |
| 35 | * allocated. The rowBytes of the device will be computed automatically |
| 36 | * based on the config and the width. |
| 37 | * |
| 38 | * @param config The desired config for the pixels. If the request cannot |
| 39 | * be met, the closest matching support config will be used. |
| 40 | * @param width width (in pixels) of the device |
| 41 | * @param height height (in pixels) of the device |
| 42 | * @param isOpaque Set to true if it is known that all of the pixels will |
| 43 | * be drawn to opaquely. Used as an accelerator when drawing |
| 44 | * these pixels to another device. |
| 45 | */ |
| 46 | SkBitmapDevice(SkBitmap::Config config, int width, int height, bool isOpaque = false); |
| 47 | |
| 48 | /** |
| 49 | * Create a new raster device and have the pixels be automatically |
| 50 | * allocated. The rowBytes of the device will be computed automatically |
| 51 | * based on the config and the width. |
| 52 | * |
| 53 | * @param config The desired config for the pixels. If the request cannot |
| 54 | * be met, the closest matching support config will be used. |
| 55 | * @param width width (in pixels) of the device |
| 56 | * @param height height (in pixels) of the device |
| 57 | * @param isOpaque Set to true if it is known that all of the pixels will |
| 58 | * be drawn to opaquely. Used as an accelerator when drawing |
| 59 | * these pixels to another device. |
| 60 | * @param deviceProperties Properties which affect compositing. |
| 61 | */ |
| 62 | SkBitmapDevice(SkBitmap::Config config, int width, int height, bool isOpaque, |
| 63 | const SkDeviceProperties& deviceProperties); |
| 64 | |
| 65 | virtual ~SkBitmapDevice(); |
| 66 | |
| 67 | virtual uint32_t getDeviceCapabilities() SK_OVERRIDE { return 0; } |
| 68 | |
| 69 | /** Return the width of the device (in pixels). |
| 70 | */ |
| 71 | virtual int width() const SK_OVERRIDE { return fBitmap.width(); } |
| 72 | /** Return the height of the device (in pixels). |
| 73 | */ |
| 74 | virtual int height() const SK_OVERRIDE { return fBitmap.height(); } |
| 75 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 76 | /** Returns true if the device's bitmap's config treats every pixels as |
| 77 | implicitly opaque. |
| 78 | */ |
| 79 | virtual bool isOpaque() const SK_OVERRIDE { return fBitmap.isOpaque(); } |
| 80 | |
| 81 | /** Return the bitmap config of the device's pixels |
| 82 | */ |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 83 | virtual SkBitmap::Config config() const SK_OVERRIDE { return fBitmap.config(); } |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * DEPRECATED: This will be made protected once WebKit stops using it. |
| 87 | * Instead use Canvas' writePixels method. |
| 88 | * |
| 89 | * Similar to draw sprite, this method will copy the pixels in bitmap onto |
| 90 | * the device, with the top/left corner specified by (x, y). The pixel |
| 91 | * values in the device are completely replaced: there is no blending. |
| 92 | * |
| 93 | * Currently if bitmap is backed by a texture this is a no-op. This may be |
| 94 | * relaxed in the future. |
| 95 | * |
| 96 | * If the bitmap has config kARGB_8888_Config then the config8888 param |
| 97 | * will determines how the pixel valuess are intepreted. If the bitmap is |
| 98 | * not kARGB_8888_Config then this parameter is ignored. |
| 99 | */ |
| 100 | virtual void writePixels(const SkBitmap& bitmap, int x, int y, |
| 101 | SkCanvas::Config8888 config8888) SK_OVERRIDE; |
| 102 | |
| 103 | /** |
| 104 | * Return the device's associated gpu render target, or NULL. |
| 105 | */ |
| 106 | virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; } |
| 107 | |
| 108 | protected: |
| 109 | /** |
| 110 | * Device may filter the text flags for drawing text here. If it wants to |
| 111 | * make a change to the specified values, it should write them into the |
| 112 | * textflags parameter (output) and return true. If the paint is fine as |
| 113 | * is, then ignore the textflags parameter and return false. |
| 114 | * |
| 115 | * The baseclass SkDevice filters based on its depth and blitters. |
| 116 | */ |
| 117 | virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE; |
| 118 | |
| 119 | /** Clears the entire device to the specified color (including alpha). |
| 120 | * Ignores the clip. |
| 121 | */ |
| 122 | virtual void clear(SkColor color) SK_OVERRIDE; |
| 123 | |
| 124 | /** These are called inside the per-device-layer loop for each draw call. |
| 125 | When these are called, we have already applied any saveLayer operations, |
| 126 | and are handling any looping from the paint, and any effects from the |
| 127 | DrawFilter. |
| 128 | */ |
| 129 | virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE; |
| 130 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, |
| 131 | const SkPoint[], const SkPaint& paint) SK_OVERRIDE; |
| 132 | virtual void drawRect(const SkDraw&, const SkRect& r, |
| 133 | const SkPaint& paint) SK_OVERRIDE; |
| 134 | virtual void drawOval(const SkDraw&, const SkRect& oval, |
| 135 | const SkPaint& paint) SK_OVERRIDE; |
| 136 | virtual void drawRRect(const SkDraw&, const SkRRect& rr, |
| 137 | const SkPaint& paint) SK_OVERRIDE; |
| 138 | |
| 139 | /** |
| 140 | * If pathIsMutable, then the implementation is allowed to cast path to a |
| 141 | * non-const pointer and modify it in place (as an optimization). Canvas |
| 142 | * may do this to implement helpers such as drawOval, by placing a temp |
| 143 | * path on the stack to hold the representation of the oval. |
| 144 | * |
| 145 | * If prePathMatrix is not null, it should logically be applied before any |
| 146 | * stroking or other effects. If there are no effects on the paint that |
| 147 | * affect the geometry/rasterization, then the pre matrix can just be |
| 148 | * pre-concated with the current matrix. |
| 149 | */ |
| 150 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| 151 | const SkPaint& paint, |
| 152 | const SkMatrix* prePathMatrix = NULL, |
| 153 | bool pathIsMutable = false) SK_OVERRIDE; |
| 154 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
| 155 | const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE; |
| 156 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 157 | int x, int y, const SkPaint& paint) SK_OVERRIDE; |
| 158 | |
| 159 | /** |
| 160 | * The default impl. will create a bitmap-shader from the bitmap, |
| 161 | * and call drawRect with it. |
| 162 | */ |
| 163 | virtual void drawBitmapRect(const SkDraw&, const SkBitmap&, |
| 164 | const SkRect* srcOrNull, const SkRect& dst, |
| 165 | const SkPaint& paint, |
| 166 | SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE; |
| 167 | |
| 168 | /** |
| 169 | * Does not handle text decoration. |
| 170 | * Decorations (underline and stike-thru) will be handled by SkCanvas. |
| 171 | */ |
| 172 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 173 | SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE; |
| 174 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 175 | const SkScalar pos[], SkScalar constY, |
| 176 | int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE; |
| 177 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 178 | const SkPath& path, const SkMatrix* matrix, |
| 179 | const SkPaint& paint) SK_OVERRIDE; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 180 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, |
| 181 | const SkPoint verts[], const SkPoint texs[], |
| 182 | const SkColor colors[], SkXfermode* xmode, |
| 183 | const uint16_t indices[], int indexCount, |
| 184 | const SkPaint& paint) SK_OVERRIDE; |
| 185 | /** The SkBaseDevice passed will be an SkBaseDevice which was returned by a call to |
| 186 | onCreateCompatibleDevice on this device with kSaveLayer_Usage. |
| 187 | */ |
| 188 | virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, |
| 189 | const SkPaint&) SK_OVERRIDE; |
| 190 | |
| 191 | /////////////////////////////////////////////////////////////////////////// |
| 192 | |
| 193 | /** Update as needed the pixel value in the bitmap, so that the caller can |
| 194 | access the pixels directly. Note: only the pixels field should be |
| 195 | altered. The config/width/height/rowbytes must remain unchanged. |
| 196 | @return the device contents as a bitmap |
| 197 | */ |
| 198 | virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE; |
| 199 | |
| 200 | SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } |
| 201 | // just for subclasses, to assign a custom pixelref |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 202 | SkPixelRef* setPixelRef(SkPixelRef* pr) { |
| 203 | fBitmap.setPixelRef(pr); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 204 | return pr; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Implements readPixels API. The caller will ensure that: |
| 209 | * 1. bitmap has pixel config kARGB_8888_Config. |
| 210 | * 2. bitmap has pixels. |
| 211 | * 3. The rectangle (x, y, x + bitmap->width(), y + bitmap->height()) is |
| 212 | * contained in the device bounds. |
| 213 | */ |
| 214 | virtual bool onReadPixels(const SkBitmap& bitmap, |
| 215 | int x, int y, |
| 216 | SkCanvas::Config8888 config8888) SK_OVERRIDE; |
| 217 | |
| 218 | /** Called when this device is installed into a Canvas. Balanced by a call |
| 219 | to unlockPixels() when the device is removed from a Canvas. |
| 220 | */ |
| 221 | virtual void lockPixels() SK_OVERRIDE; |
| 222 | virtual void unlockPixels() SK_OVERRIDE; |
| 223 | |
| 224 | /** |
| 225 | * Returns true if the device allows processing of this imagefilter. If |
| 226 | * false is returned, then the filter is ignored. This may happen for |
| 227 | * some subclasses that do not support pixel manipulations after drawing |
| 228 | * has occurred (e.g. printing). The default implementation returns true. |
| 229 | */ |
| 230 | virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE; |
| 231 | |
| 232 | /** |
| 233 | * Override and return true for filters that the device can handle |
| 234 | * intrinsically. Doing so means that SkCanvas will pass-through this |
| 235 | * filter to drawSprite and drawDevice (and potentially filterImage). |
| 236 | * Returning false means the SkCanvas will have apply the filter itself, |
| 237 | * and just pass the resulting image to the device. |
| 238 | */ |
| 239 | virtual bool canHandleImageFilter(SkImageFilter*) SK_OVERRIDE; |
| 240 | |
| 241 | /** |
| 242 | * Related (but not required) to canHandleImageFilter, this method returns |
| 243 | * true if the device could apply the filter to the src bitmap and return |
| 244 | * the result (and updates offset as needed). |
| 245 | * If the device does not recognize or support this filter, |
| 246 | * it just returns false and leaves result and offset unchanged. |
| 247 | */ |
| 248 | virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&, |
| 249 | SkBitmap* result, SkIPoint* offset) SK_OVERRIDE; |
| 250 | |
| 251 | private: |
| 252 | friend class SkCanvas; |
| 253 | friend struct DeviceCM; //for setMatrixClip |
| 254 | friend class SkDraw; |
| 255 | friend class SkDrawIter; |
| 256 | friend class SkDeviceFilteredPaint; |
| 257 | friend class SkDeviceImageFilterProxy; |
| 258 | |
| 259 | friend class SkSurface_Raster; |
| 260 | |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 261 | void init(SkBitmap::Config config, int width, int height, bool isOpaque); |
reed@google.com | 9271389 | 2014-01-03 17:58:57 +0000 | [diff] [blame] | 262 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 263 | // used to change the backend's pixels (and possibly config/rowbytes) |
| 264 | // but cannot change the width/height, so there should be no change to |
| 265 | // any clip information. |
| 266 | virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE; |
| 267 | |
| 268 | /** |
| 269 | * Subclasses should override this to implement createCompatibleDevice. |
| 270 | */ |
| 271 | virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config, |
| 272 | int width, int height, |
| 273 | bool isOpaque, |
| 274 | Usage usage) SK_OVERRIDE; |
| 275 | |
| 276 | /** Causes any deferred drawing to the device to be completed. |
| 277 | */ |
| 278 | virtual void flush() SK_OVERRIDE {} |
| 279 | |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame^] | 280 | virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE; |
| 281 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 282 | SkBitmap fBitmap; |
| 283 | |
| 284 | typedef SkBaseDevice INHERITED; |
| 285 | }; |
robertphillips@google.com | 9241e33 | 2013-08-21 13:54:44 +0000 | [diff] [blame] | 286 | |
| 287 | #endif // SkBitmapDevice_DEFINED |