/* * Copyright 2010 The Android Open Source Project * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef SkDevice_DEFINED #define SkDevice_DEFINED #include "SkRefCnt.h" #include "SkBitmap.h" #include "SkCanvas.h" #include "SkColor.h" class SkClipStack; class SkDraw; struct SkIRect; class SkMatrix; class SkMetaData; class SkRegion; // This is an opaque class, not interpreted by skia class SkGpuRenderTarget; class SK_API SkDevice : public SkRefCnt { public: /** * Construct a new device with the specified bitmap as its backend. It is * valid for the bitmap to have no pixels associated with it. In that case, * any drawing to this device will have no effect. */ SkDevice(const SkBitmap& bitmap); /** * Create a new raster device and have the pixels be automatically * allocated. The rowBytes of the device will be computed automatically * based on the config and the width. * * @param config The desired config for the pixels. If the request cannot * be met, the closest matching support config will be used. * @param width width (in pixels) of the device * @param height height (in pixels) of the device * @param isOpaque Set to true if it is known that all of the pixels will * be drawn to opaquely. Used as an accelerator when drawing * these pixels to another device. */ SkDevice(SkBitmap::Config config, int width, int height, bool isOpaque = false); virtual ~SkDevice(); /** * Creates a device that is of the same type as this device (e.g. SW-raster, * GPU, or PDF). The backing store for this device is created automatically * (e.g. offscreen pixels or FBO or whatever is appropriate). * * @param width width of the device to create * @param height height of the device to create * @param isOpaque performance hint, set to true if you know that you will * draw into this device such that all of the pixels will * be opaque. */ SkDevice* createCompatibleDevice(SkBitmap::Config config, int width, int height, bool isOpaque); SkMetaData& getMetaData(); enum Capabilities { kGL_Capability = 0x1, //!< mask indicating GL support kVector_Capability = 0x2, //!< mask indicating a vector representation kAll_Capabilities = 0x3 }; virtual uint32_t getDeviceCapabilities() { return 0; } /** Return the width of the device (in pixels). */ virtual int width() const { return fBitmap.width(); } /** Return the height of the device (in pixels). */ virtual int height() const { return fBitmap.height(); } /** Return the bounds of the device */ void getBounds(SkIRect* bounds) const; /** Returns true if the device's bitmap's config treats every pixels as implicitly opaque. */ bool isOpaque() const { return fBitmap.isOpaque(); } /** Return the bitmap config of the device's pixels */ SkBitmap::Config config() const { return fBitmap.getConfig(); } /** Return the bitmap associated with this device. Call this each time you need to access the bitmap, as it notifies the subclass to perform any flushing etc. before you examine the pixels. @param changePixels set to true if the caller plans to change the pixels @return the device's bitmap */ const SkBitmap& accessBitmap(bool changePixels); /** * Copy the pixels from the device into bitmap. Returns true on success. * If false is returned, then the bitmap parameter is left unchanged. * The bitmap parameter is treated as output-only, and will be completely * overwritten (if the method returns true). */ virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap); /** * Similar to draw sprite, this method will copy the pixels in bitmap onto * the device, with the top/left corner specified by (x, y). The pixel * values in the device are completely replaced: there is no blending. */ virtual void writePixels(const SkBitmap& bitmap, int x, int y); /** * Return the device's associated gpu render target, or NULL. */ virtual SkGpuRenderTarget* accessRenderTarget() { return NULL; } protected: enum Usage { kGeneral_Usage, kSaveLayer_Usage, // clear(eraseColor); } /** These are called inside the per-device-layer loop for each draw call. When these are called, we have already applied any saveLayer operations, and are handling any looping from the paint, and any effects from the DrawFilter. */ virtual void drawPaint(const SkDraw&, const SkPaint& paint); virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, const SkPoint[], const SkPaint& paint); virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint); /** * If pathIsMutable, then the implementation is allowed to cast path to a * non-const pointer and modify it in place (as an optimization). Canvas * may do this to implement helpers such as drawOval, by placing a temp * path on the stack to hold the representation of the oval. * * If prePathMatrix is not null, it should logically be applied before any * stroking or other effects. If there are no effects on the paint that * affect the geometry/rasterization, then the pre matrix can just be * pre-concated with the current matrix. */ virtual void drawPath(const SkDraw&, const SkPath& path, const SkPaint& paint, const SkMatrix* prePathMatrix = NULL, bool pathIsMutable = false); virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, const SkIRect* srcRectOrNull, const SkMatrix& matrix, const SkPaint& paint); virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y, const SkPaint& paint); virtual void drawText(const SkDraw&, const void* text, size_t len, SkScalar x, SkScalar y, const SkPaint& paint); virtual void drawPosText(const SkDraw&, const void* text, size_t len, const SkScalar pos[], SkScalar constY, int scalarsPerPos, const SkPaint& paint); virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, const SkPath& path, const SkMatrix* matrix, const SkPaint& paint); #ifdef ANDROID virtual void drawPosTextOnPath(const SkDraw& draw, const void* text, size_t len, const SkPoint pos[], const SkPaint& paint, const SkPath& path, const SkMatrix* matrix); #endif virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, const SkPoint verts[], const SkPoint texs[], const SkColor colors[], SkXfermode* xmode, const uint16_t indices[], int indexCount, const SkPaint& paint); /** The SkDevice passed will be an SkDevice which was returned by a call to onCreateCompatibleDevice on this device with kSaveLayer_Usage. */ virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, const SkPaint&); /////////////////////////////////////////////////////////////////////////// /** Update as needed the pixel value in the bitmap, so that the caller can access the pixels directly. Note: only the pixels field should be altered. The config/width/height/rowbytes must remain unchanged. */ virtual void onAccessBitmap(SkBitmap*); SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } // just for subclasses, to assign a custom pixelref SkPixelRef* setPixelRef(SkPixelRef* pr, size_t offset) { fBitmap.setPixelRef(pr, offset); return pr; } /** Called when this device is installed into a Canvas. Balanaced by a call to unlockPixels() when the device is removed from a Canvas. */ virtual void lockPixels(); virtual void unlockPixels(); private: friend class SkCanvas; friend struct DeviceCM; //for setMatrixClip friend class SkDraw; friend class SkDrawIter; friend class SkDeviceFilteredPaint; // just called by SkCanvas when built as a layer void setOrigin(int x, int y) { fOrigin.set(x, y); } // just called by SkCanvas for saveLayer SkDevice* createCompatibleDeviceForSaveLayer(SkBitmap::Config config, int width, int height, bool isOpaque); /** * Subclasses should override this to implement createCompatibleDevice. */ virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config, int width, int height, bool isOpaque, Usage usage); /** Causes any deferred drawing to the device to be completed. */ virtual void flush() {} SkBitmap fBitmap; SkIPoint fOrigin; SkMetaData* fMetaData; }; #endif