1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/core/SkBitmapDevice.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,288 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2013 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 +#ifndef SkBitmapDevice_DEFINED 1.13 +#define SkBitmapDevice_DEFINED 1.14 + 1.15 +#include "SkDevice.h" 1.16 + 1.17 +/////////////////////////////////////////////////////////////////////////////// 1.18 +class SK_API SkBitmapDevice : public SkBaseDevice { 1.19 +public: 1.20 + SK_DECLARE_INST_COUNT(SkBitmapDevice) 1.21 + 1.22 + /** 1.23 + * Construct a new device with the specified bitmap as its backend. It is 1.24 + * valid for the bitmap to have no pixels associated with it. In that case, 1.25 + * any drawing to this device will have no effect. 1.26 + */ 1.27 + SkBitmapDevice(const SkBitmap& bitmap); 1.28 + 1.29 + /** 1.30 + * Construct a new device with the specified bitmap as its backend. It is 1.31 + * valid for the bitmap to have no pixels associated with it. In that case, 1.32 + * any drawing to this device will have no effect. 1.33 + */ 1.34 + SkBitmapDevice(const SkBitmap& bitmap, const SkDeviceProperties& deviceProperties); 1.35 + 1.36 +#ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG 1.37 + /** 1.38 + * Create a new raster device and have the pixels be automatically 1.39 + * allocated. The rowBytes of the device will be computed automatically 1.40 + * based on the config and the width. 1.41 + * 1.42 + * @param config The desired config for the pixels. If the request cannot 1.43 + * be met, the closest matching support config will be used. 1.44 + * @param width width (in pixels) of the device 1.45 + * @param height height (in pixels) of the device 1.46 + * @param isOpaque Set to true if it is known that all of the pixels will 1.47 + * be drawn to opaquely. Used as an accelerator when drawing 1.48 + * these pixels to another device. 1.49 + */ 1.50 + SkBitmapDevice(SkBitmap::Config config, int width, int height, bool isOpaque = false); 1.51 + 1.52 + /** 1.53 + * Create a new raster device and have the pixels be automatically 1.54 + * allocated. The rowBytes of the device will be computed automatically 1.55 + * based on the config and the width. 1.56 + * 1.57 + * @param config The desired config for the pixels. If the request cannot 1.58 + * be met, the closest matching support config will be used. 1.59 + * @param width width (in pixels) of the device 1.60 + * @param height height (in pixels) of the device 1.61 + * @param isOpaque Set to true if it is known that all of the pixels will 1.62 + * be drawn to opaquely. Used as an accelerator when drawing 1.63 + * these pixels to another device. 1.64 + * @param deviceProperties Properties which affect compositing. 1.65 + */ 1.66 + SkBitmapDevice(SkBitmap::Config config, int width, int height, bool isOpaque, 1.67 + const SkDeviceProperties& deviceProperties); 1.68 +#endif 1.69 + static SkBitmapDevice* Create(const SkImageInfo&, 1.70 + const SkDeviceProperties* = NULL); 1.71 + 1.72 + /** Return the width of the device (in pixels). 1.73 + */ 1.74 + virtual int width() const SK_OVERRIDE { return fBitmap.width(); } 1.75 + /** Return the height of the device (in pixels). 1.76 + */ 1.77 + virtual int height() const SK_OVERRIDE { return fBitmap.height(); } 1.78 + 1.79 + /** Returns true if the device's bitmap's config treats every pixels as 1.80 + implicitly opaque. 1.81 + */ 1.82 + virtual bool isOpaque() const SK_OVERRIDE { return fBitmap.isOpaque(); } 1.83 + 1.84 + /** Return the bitmap config of the device's pixels 1.85 + */ 1.86 + virtual SkBitmap::Config config() const SK_OVERRIDE { return fBitmap.config(); } 1.87 + 1.88 + virtual SkImageInfo imageInfo() const SK_OVERRIDE; 1.89 + 1.90 +#ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG 1.91 + /** 1.92 + * DEPRECATED: This will be made protected once WebKit stops using it. 1.93 + * Instead use Canvas' writePixels method. 1.94 + * 1.95 + * Similar to draw sprite, this method will copy the pixels in bitmap onto 1.96 + * the device, with the top/left corner specified by (x, y). The pixel 1.97 + * values in the device are completely replaced: there is no blending. 1.98 + * 1.99 + * Currently if bitmap is backed by a texture this is a no-op. This may be 1.100 + * relaxed in the future. 1.101 + * 1.102 + * If the bitmap has config kARGB_8888_Config then the config8888 param 1.103 + * will determines how the pixel valuess are intepreted. If the bitmap is 1.104 + * not kARGB_8888_Config then this parameter is ignored. 1.105 + */ 1.106 + virtual void writePixels(const SkBitmap& bitmap, int x, int y, 1.107 + SkCanvas::Config8888 config8888) SK_OVERRIDE; 1.108 +#endif 1.109 + /** 1.110 + * Return the device's associated gpu render target, or NULL. 1.111 + */ 1.112 + virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; } 1.113 + 1.114 +protected: 1.115 + /** 1.116 + * Device may filter the text flags for drawing text here. If it wants to 1.117 + * make a change to the specified values, it should write them into the 1.118 + * textflags parameter (output) and return true. If the paint is fine as 1.119 + * is, then ignore the textflags parameter and return false. 1.120 + * 1.121 + * The baseclass SkDevice filters based on its depth and blitters. 1.122 + */ 1.123 + virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE; 1.124 + 1.125 + /** Clears the entire device to the specified color (including alpha). 1.126 + * Ignores the clip. 1.127 + */ 1.128 + virtual void clear(SkColor color) SK_OVERRIDE; 1.129 + 1.130 + /** These are called inside the per-device-layer loop for each draw call. 1.131 + When these are called, we have already applied any saveLayer operations, 1.132 + and are handling any looping from the paint, and any effects from the 1.133 + DrawFilter. 1.134 + */ 1.135 + virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE; 1.136 + virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, 1.137 + const SkPoint[], const SkPaint& paint) SK_OVERRIDE; 1.138 + virtual void drawRect(const SkDraw&, const SkRect& r, 1.139 + const SkPaint& paint) SK_OVERRIDE; 1.140 + virtual void drawOval(const SkDraw&, const SkRect& oval, 1.141 + const SkPaint& paint) SK_OVERRIDE; 1.142 + virtual void drawRRect(const SkDraw&, const SkRRect& rr, 1.143 + const SkPaint& paint) SK_OVERRIDE; 1.144 + 1.145 + /** 1.146 + * If pathIsMutable, then the implementation is allowed to cast path to a 1.147 + * non-const pointer and modify it in place (as an optimization). Canvas 1.148 + * may do this to implement helpers such as drawOval, by placing a temp 1.149 + * path on the stack to hold the representation of the oval. 1.150 + * 1.151 + * If prePathMatrix is not null, it should logically be applied before any 1.152 + * stroking or other effects. If there are no effects on the paint that 1.153 + * affect the geometry/rasterization, then the pre matrix can just be 1.154 + * pre-concated with the current matrix. 1.155 + */ 1.156 + virtual void drawPath(const SkDraw&, const SkPath& path, 1.157 + const SkPaint& paint, 1.158 + const SkMatrix* prePathMatrix = NULL, 1.159 + bool pathIsMutable = false) SK_OVERRIDE; 1.160 + virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, 1.161 + const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE; 1.162 + virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, 1.163 + int x, int y, const SkPaint& paint) SK_OVERRIDE; 1.164 + 1.165 + /** 1.166 + * The default impl. will create a bitmap-shader from the bitmap, 1.167 + * and call drawRect with it. 1.168 + */ 1.169 + virtual void drawBitmapRect(const SkDraw&, const SkBitmap&, 1.170 + const SkRect* srcOrNull, const SkRect& dst, 1.171 + const SkPaint& paint, 1.172 + SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE; 1.173 + 1.174 + /** 1.175 + * Does not handle text decoration. 1.176 + * Decorations (underline and stike-thru) will be handled by SkCanvas. 1.177 + */ 1.178 + virtual void drawText(const SkDraw&, const void* text, size_t len, 1.179 + SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE; 1.180 + virtual void drawPosText(const SkDraw&, const void* text, size_t len, 1.181 + const SkScalar pos[], SkScalar constY, 1.182 + int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE; 1.183 + virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, 1.184 + const SkPath& path, const SkMatrix* matrix, 1.185 + const SkPaint& paint) SK_OVERRIDE; 1.186 + virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, 1.187 + const SkPoint verts[], const SkPoint texs[], 1.188 + const SkColor colors[], SkXfermode* xmode, 1.189 + const uint16_t indices[], int indexCount, 1.190 + const SkPaint& paint) SK_OVERRIDE; 1.191 + /** The SkBaseDevice passed will be an SkBaseDevice which was returned by a call to 1.192 + onCreateDevice on this device with kSaveLayer_Usage. 1.193 + */ 1.194 + virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, 1.195 + const SkPaint&) SK_OVERRIDE; 1.196 + 1.197 + /////////////////////////////////////////////////////////////////////////// 1.198 + 1.199 + /** Update as needed the pixel value in the bitmap, so that the caller can 1.200 + access the pixels directly. Note: only the pixels field should be 1.201 + altered. The config/width/height/rowbytes must remain unchanged. 1.202 + @return the device contents as a bitmap 1.203 + */ 1.204 + virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE; 1.205 + 1.206 + SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } 1.207 + // just for subclasses, to assign a custom pixelref 1.208 + SkPixelRef* setPixelRef(SkPixelRef* pr) { 1.209 + fBitmap.setPixelRef(pr); 1.210 + return pr; 1.211 + } 1.212 + 1.213 + /** 1.214 + * Implements readPixels API. The caller will ensure that: 1.215 + * 1. bitmap has pixel config kARGB_8888_Config. 1.216 + * 2. bitmap has pixels. 1.217 + * 3. The rectangle (x, y, x + bitmap->width(), y + bitmap->height()) is 1.218 + * contained in the device bounds. 1.219 + */ 1.220 + virtual bool onReadPixels(const SkBitmap&, int x, int y, SkCanvas::Config8888) SK_OVERRIDE; 1.221 + virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) SK_OVERRIDE; 1.222 + virtual void* onAccessPixels(SkImageInfo* info, size_t* rowBytes) SK_OVERRIDE; 1.223 + 1.224 + /** Called when this device is installed into a Canvas. Balanced by a call 1.225 + to unlockPixels() when the device is removed from a Canvas. 1.226 + */ 1.227 + virtual void lockPixels() SK_OVERRIDE; 1.228 + virtual void unlockPixels() SK_OVERRIDE; 1.229 + 1.230 + /** 1.231 + * Returns true if the device allows processing of this imagefilter. If 1.232 + * false is returned, then the filter is ignored. This may happen for 1.233 + * some subclasses that do not support pixel manipulations after drawing 1.234 + * has occurred (e.g. printing). The default implementation returns true. 1.235 + */ 1.236 + virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE; 1.237 + 1.238 + /** 1.239 + * Override and return true for filters that the device can handle 1.240 + * intrinsically. Doing so means that SkCanvas will pass-through this 1.241 + * filter to drawSprite and drawDevice (and potentially filterImage). 1.242 + * Returning false means the SkCanvas will have apply the filter itself, 1.243 + * and just pass the resulting image to the device. 1.244 + */ 1.245 + virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE; 1.246 + 1.247 + /** 1.248 + * Related (but not required) to canHandleImageFilter, this method returns 1.249 + * true if the device could apply the filter to the src bitmap and return 1.250 + * the result (and updates offset as needed). 1.251 + * If the device does not recognize or support this filter, 1.252 + * it just returns false and leaves result and offset unchanged. 1.253 + */ 1.254 + virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkImageFilter::Context&, 1.255 + SkBitmap* result, SkIPoint* offset) SK_OVERRIDE; 1.256 + 1.257 +private: 1.258 + friend class SkCanvas; 1.259 + friend struct DeviceCM; //for setMatrixClip 1.260 + friend class SkDraw; 1.261 + friend class SkDrawIter; 1.262 + friend class SkDeviceFilteredPaint; 1.263 + friend class SkDeviceImageFilterProxy; 1.264 + 1.265 + friend class SkSurface_Raster; 1.266 + 1.267 + // used to change the backend's pixels (and possibly config/rowbytes) 1.268 + // but cannot change the width/height, so there should be no change to 1.269 + // any clip information. 1.270 + virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE; 1.271 + 1.272 +#ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG 1.273 + // in support of legacy constructors 1.274 + void init(SkBitmap::Config config, int width, int height, bool isOpaque); 1.275 +#endif 1.276 + 1.277 + virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE; 1.278 + 1.279 + /** Causes any deferred drawing to the device to be completed. 1.280 + */ 1.281 + virtual void flush() SK_OVERRIDE {} 1.282 + 1.283 + virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE; 1.284 + virtual const void* peekPixels(SkImageInfo*, size_t* rowBytes) SK_OVERRIDE; 1.285 + 1.286 + SkBitmap fBitmap; 1.287 + 1.288 + typedef SkBaseDevice INHERITED; 1.289 +}; 1.290 + 1.291 +#endif // SkBitmapDevice_DEFINED