2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2010-11-11 00:49:41 +00:00
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkDevice_DEFINED
|
|
|
|
#define SkDevice_DEFINED
|
|
|
|
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColor.h"
|
2011-01-25 23:50:57 +00:00
|
|
|
#include "SkRefDict.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-02-22 20:56:26 +00:00
|
|
|
class SkClipStack;
|
2010-10-13 22:13:05 +00:00
|
|
|
class SkDevice;
|
2008-12-17 15:59:43 +00:00
|
|
|
class SkDraw;
|
|
|
|
struct SkIRect;
|
|
|
|
class SkMatrix;
|
|
|
|
class SkRegion;
|
|
|
|
|
2010-10-13 22:13:05 +00:00
|
|
|
/** \class SkDeviceFactory
|
|
|
|
|
|
|
|
Devices that extend SkDevice should also provide a SkDeviceFactory class
|
|
|
|
to pass into SkCanvas. Doing so will eliminate the need to extend
|
|
|
|
SkCanvas as well.
|
|
|
|
*/
|
2011-03-19 17:27:49 +00:00
|
|
|
class SK_API SkDeviceFactory {
|
2010-10-13 22:13:05 +00:00
|
|
|
public:
|
2010-11-11 00:49:41 +00:00
|
|
|
virtual ~SkDeviceFactory();
|
2010-12-20 18:26:13 +00:00
|
|
|
virtual SkDevice* newDevice(SkCanvas*, SkBitmap::Config, int width,
|
|
|
|
int height, bool isOpaque, bool isLayer) = 0;
|
2010-10-13 22:13:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SkRasterDeviceFactory : public SkDeviceFactory {
|
|
|
|
public:
|
2010-12-20 18:26:13 +00:00
|
|
|
virtual SkDevice* newDevice(SkCanvas*, SkBitmap::Config, int width,
|
|
|
|
int height, bool isOpaque, bool isLayer);
|
2010-10-13 22:13:05 +00:00
|
|
|
};
|
|
|
|
|
2011-03-15 21:27:08 +00:00
|
|
|
class SK_API SkDevice : public SkRefCnt {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2010-12-20 18:26:13 +00:00
|
|
|
SkDevice(SkCanvas*);
|
|
|
|
/** Construct a new device, extracting the width/height/config/isOpaque values from
|
|
|
|
the bitmap. If transferPixelOwnership is true, and the bitmap claims to own its
|
|
|
|
own pixels (getOwnsPixels() == true), then transfer this responsibility to the
|
|
|
|
device, and call setOwnsPixels(false) on the bitmap.
|
2011-02-07 22:44:43 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
Subclasses may override the destructor, which is virtual, even though this class
|
|
|
|
doesn't have one. SkRefCnt does.
|
2011-02-07 22:44:43 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
@param bitmap A copy of this bitmap is made and stored in the device
|
|
|
|
*/
|
2010-12-20 18:26:13 +00:00
|
|
|
SkDevice(SkCanvas*, const SkBitmap& bitmap, bool forOffscreen);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2010-10-13 22:13:05 +00:00
|
|
|
virtual SkDeviceFactory* getDeviceFactory() {
|
|
|
|
return SkNEW(SkRasterDeviceFactory);
|
|
|
|
}
|
|
|
|
|
2010-10-26 19:47:30 +00:00
|
|
|
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; }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the width of the device (in pixels).
|
|
|
|
*/
|
2010-10-20 22:23:29 +00:00
|
|
|
virtual int width() const { return fBitmap.width(); }
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the height of the device (in pixels).
|
|
|
|
*/
|
2010-10-20 22:23:29 +00:00
|
|
|
virtual int height() const { return fBitmap.height(); }
|
2011-03-04 22:27:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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; }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the bitmap config of the device's pixels
|
|
|
|
*/
|
|
|
|
SkBitmap::Config config() const { return fBitmap.getConfig(); }
|
|
|
|
/** Returns true if the device's bitmap's config treats every pixels as
|
|
|
|
implicitly opaque.
|
|
|
|
*/
|
|
|
|
bool isOpaque() const { return fBitmap.isOpaque(); }
|
2011-02-07 22:44:43 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the bounds of the device
|
|
|
|
*/
|
|
|
|
void getBounds(SkIRect* bounds) const;
|
2011-02-07 22:44:43 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return true if the specified rectangle intersects the bounds of the
|
|
|
|
device. If sect is not NULL and there is an intersection, sect returns
|
|
|
|
the intersection.
|
|
|
|
*/
|
|
|
|
bool intersects(const SkIRect& r, SkIRect* sect = NULL) const;
|
|
|
|
|
|
|
|
/** 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);
|
|
|
|
|
|
|
|
/** Helper to erase the entire device to the specified color (including
|
|
|
|
alpha).
|
|
|
|
*/
|
|
|
|
void eraseColor(SkColor eraseColor);
|
|
|
|
|
|
|
|
/** 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();
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
/** Return the device's associated texture, or NULL. If returned, it may be
|
|
|
|
drawn into another device
|
|
|
|
*/
|
|
|
|
virtual SkGpuTexture* accessTexture() { return NULL; }
|
|
|
|
|
2011-02-22 20:56:26 +00:00
|
|
|
/**
|
|
|
|
* Called with the correct matrix and clip before this device is drawn
|
|
|
|
* to using those settings. If your subclass overrides this, be sure to
|
|
|
|
* call through to the base class as well.
|
|
|
|
*
|
|
|
|
* The clipstack is another view of the clip. It records the actual
|
|
|
|
* geometry that went into building the region. It is present for devices
|
|
|
|
* that want to parse it, but is not required: the region is a complete
|
|
|
|
* picture of the current clip. (i.e. if you regionize all of the geometry
|
|
|
|
* in the clipstack, you will arrive at an equivalent region to the one
|
|
|
|
* passed in).
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2011-02-22 20:56:26 +00:00
|
|
|
virtual void setMatrixClip(const SkMatrix&, const SkRegion&,
|
|
|
|
const SkClipStack&);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** Called when this device gains focus (i.e becomes the current device
|
|
|
|
for drawing).
|
|
|
|
*/
|
2011-03-03 13:54:13 +00:00
|
|
|
virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&,
|
|
|
|
const SkClipStack&) {}
|
2011-02-07 22:44:43 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
/** Causes any deferred drawing to the device to be completed.
|
|
|
|
*/
|
|
|
|
virtual void flush() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy the pixels from the device into bitmap. Returns true on success.
|
|
|
|
* If false is returned, then the bitmap parameter is left unchanged.
|
2011-02-07 22:44:43 +00:00
|
|
|
* The bitmap parameter is treated as output-only, and will be completely
|
|
|
|
* overwritten (if the method returns true).
|
2010-12-20 18:26:13 +00:00
|
|
|
*/
|
|
|
|
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);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** 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);
|
|
|
|
virtual void drawPath(const SkDraw&, const SkPath& path,
|
2010-12-20 18:26:13 +00:00
|
|
|
const SkPaint& paint,
|
|
|
|
const SkMatrix* prePathMatrix = NULL,
|
|
|
|
bool pathIsMutable = false);
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
|
2010-12-20 18:26:13 +00:00
|
|
|
const SkIRect* srcRectOrNull,
|
2008-12-17 15:59:43 +00:00
|
|
|
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);
|
2011-03-14 20:30:14 +00:00
|
|
|
#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
|
2008-12-17 15:59:43 +00:00
|
|
|
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);
|
|
|
|
virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
|
|
|
|
const SkPaint&);
|
|
|
|
|
2011-01-25 23:50:57 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
SkRefDict& getRefDict() { return fRefDict; }
|
|
|
|
|
2011-03-15 20:56:58 +00:00
|
|
|
struct TextFlags {
|
|
|
|
uint32_t fFlags; // SkPaint::getFlags()
|
|
|
|
SkPaint::Hinting fHinting;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Device may filter the text flags for drawing text here. If it wants to
|
|
|
|
* make a change to the specified values, it should write them into the
|
|
|
|
* textflags parameter (output) and return true. If the paint is fine as
|
|
|
|
* is, then ignore the textflags parameter and return false.
|
|
|
|
*
|
|
|
|
* The baseclass SkDevice filters based on its depth and blitters.
|
|
|
|
*/
|
|
|
|
virtual bool filterTextFlags(const SkPaint& paint, TextFlags*);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
|
|
|
/** 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*);
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2011-03-04 22:27:10 +00:00
|
|
|
friend class SkCanvas;
|
|
|
|
// just called by SkCanvas when built as a layer
|
|
|
|
void setOrigin(int x, int y) { fOrigin.set(x, y); }
|
|
|
|
|
2011-01-25 23:50:57 +00:00
|
|
|
SkCanvas* fCanvas;
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
SkRefDict fRefDict;
|
2011-03-04 22:27:10 +00:00
|
|
|
SkIPoint fOrigin;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|