/* * 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" #include "SkImageFilter.h" class SkClipStack; class SkDraw; class SkDrawFilter; struct SkIRect; class SkMatrix; class SkMetaData; class SkRegion; struct SkDeviceProperties; class GrRenderTarget; class SK_API SkBaseDevice : public SkRefCnt { public: SK_DECLARE_INST_COUNT(SkBaseDevice) /** * Construct a new device. */ SkBaseDevice(); explicit SkBaseDevice(const SkDeviceProperties&); virtual ~SkBaseDevice(); SkMetaData& getMetaData(); /** * Return ImageInfo for this device. If the canvas is not backed by pixels * (cpu or gpu), then the info's ColorType will be kUnknown_SkColorType. */ virtual SkImageInfo imageInfo() const; /** * Return the bounds of the device in the coordinate space of the root * canvas. The root device will have its top-left at 0,0, but other devices * such as those associated with saveLayer may have a non-zero origin. */ void getGlobalBounds(SkIRect* bounds) const { SkASSERT(bounds); const SkIPoint& origin = this->getOrigin(); bounds->setXYWH(origin.x(), origin.y(), this->width(), this->height()); } SkIRect getGlobalBounds() const { SkIRect bounds; this->getGlobalBounds(&bounds); return bounds; } int width() const { return this->imageInfo().width(); } int height() const { return this->imageInfo().height(); } bool isOpaque() const { return this->imageInfo().isOpaque(); } /** 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); bool writePixels(const SkImageInfo&, const void*, size_t rowBytes, int x, int y); void* accessPixels(SkImageInfo* info, size_t* rowBytes); /** * Return the device's associated gpu render target, or NULL. */ virtual GrRenderTarget* accessRenderTarget() { return NULL; } /** * Return the device's origin: its offset in device coordinates from * the default origin in its canvas' matrix/clip */ const SkIPoint& getOrigin() const { return fOrigin; } /** * onAttachToCanvas is invoked whenever a device is installed in a canvas * (i.e., setDevice, saveLayer (for the new device created by the save), * and SkCanvas' SkBaseDevice & SkBitmap -taking ctors). It allows the * devices to prepare for drawing (e.g., locking their pixels, etc.) */ virtual void onAttachToCanvas(SkCanvas*) { SkASSERT(!fAttachedToCanvas); this->lockPixels(); #ifdef SK_DEBUG fAttachedToCanvas = true; #endif }; /** * onDetachFromCanvas notifies a device that it will no longer be drawn to. * It gives the device a chance to clean up (e.g., unlock its pixels). It * is invoked from setDevice (for the displaced device), restore and * possibly from SkCanvas' dtor. */ virtual void onDetachFromCanvas() { SkASSERT(fAttachedToCanvas); this->unlockPixels(); #ifdef SK_DEBUG fAttachedToCanvas = false; #endif }; protected: enum Usage { kGeneral_Usage, kSaveLayer_Usage, //