3 * Copyright 2013 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #ifndef SkBitmapDevice_DEFINED
10 #define SkBitmapDevice_DEFINED
14 ///////////////////////////////////////////////////////////////////////////////
15 class SK_API SkBitmapDevice : public SkBaseDevice {
17 SK_DECLARE_INST_COUNT(SkBitmapDevice)
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.
24 SkBitmapDevice(const SkBitmap& bitmap);
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.
31 SkBitmapDevice(const SkBitmap& bitmap, const SkDeviceProperties& deviceProperties);
32 static SkBitmapDevice* Create(const SkImageInfo&, const SkDeviceProperties*);
34 static SkBitmapDevice* Create(const SkImageInfo& info) {
35 return Create(info, NULL);
38 virtual SkImageInfo imageInfo() const SK_OVERRIDE;
41 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE;
43 /** Clears the entire device to the specified color (including alpha).
46 virtual void clear(SkColor color) SK_OVERRIDE;
48 /** These are called inside the per-device-layer loop for each draw call.
49 When these are called, we have already applied any saveLayer operations,
50 and are handling any looping from the paint, and any effects from the
53 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
54 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
55 const SkPoint[], const SkPaint& paint) SK_OVERRIDE;
56 virtual void drawRect(const SkDraw&, const SkRect& r,
57 const SkPaint& paint) SK_OVERRIDE;
58 virtual void drawOval(const SkDraw&, const SkRect& oval,
59 const SkPaint& paint) SK_OVERRIDE;
60 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
61 const SkPaint& paint) SK_OVERRIDE;
64 * If pathIsMutable, then the implementation is allowed to cast path to a
65 * non-const pointer and modify it in place (as an optimization). Canvas
66 * may do this to implement helpers such as drawOval, by placing a temp
67 * path on the stack to hold the representation of the oval.
69 * If prePathMatrix is not null, it should logically be applied before any
70 * stroking or other effects. If there are no effects on the paint that
71 * affect the geometry/rasterization, then the pre matrix can just be
72 * pre-concated with the current matrix.
74 virtual void drawPath(const SkDraw&, const SkPath& path,
76 const SkMatrix* prePathMatrix = NULL,
77 bool pathIsMutable = false) SK_OVERRIDE;
78 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
79 const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE;
80 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
81 int x, int y, const SkPaint& paint) SK_OVERRIDE;
84 * The default impl. will create a bitmap-shader from the bitmap,
85 * and call drawRect with it.
87 virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
88 const SkRect* srcOrNull, const SkRect& dst,
90 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
93 * Does not handle text decoration.
94 * Decorations (underline and stike-thru) will be handled by SkCanvas.
96 virtual void drawText(const SkDraw&, const void* text, size_t len,
97 SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE;
98 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
99 const SkScalar pos[], int scalarsPerPos,
100 const SkPoint& offset, const SkPaint& paint) SK_OVERRIDE;
101 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
102 const SkPath& path, const SkMatrix* matrix,
103 const SkPaint& paint) SK_OVERRIDE;
104 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
105 const SkPoint verts[], const SkPoint texs[],
106 const SkColor colors[], SkXfermode* xmode,
107 const uint16_t indices[], int indexCount,
108 const SkPaint& paint) SK_OVERRIDE;
109 /** The SkBaseDevice passed will be an SkBaseDevice which was returned by a call to
110 onCreateDevice on this device with kSaveLayer_Usage.
112 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
113 const SkPaint&) SK_OVERRIDE;
115 ///////////////////////////////////////////////////////////////////////////
117 /** Update as needed the pixel value in the bitmap, so that the caller can
118 access the pixels directly. Note: only the pixels field should be
119 altered. The config/width/height/rowbytes must remain unchanged.
120 @return the device contents as a bitmap
122 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
124 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
125 // just for subclasses, to assign a custom pixelref
126 SkPixelRef* setPixelRef(SkPixelRef* pr) {
127 fBitmap.setPixelRef(pr);
131 virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) SK_OVERRIDE;
132 virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) SK_OVERRIDE;
133 virtual void* onAccessPixels(SkImageInfo* info, size_t* rowBytes) SK_OVERRIDE;
135 /** Called when this device is installed into a Canvas. Balanced by a call
136 to unlockPixels() when the device is removed from a Canvas.
138 virtual void lockPixels() SK_OVERRIDE;
139 virtual void unlockPixels() SK_OVERRIDE;
142 friend class SkCanvas;
143 friend struct DeviceCM; //for setMatrixClip
145 friend class SkDrawIter;
146 friend class SkDeviceFilteredPaint;
147 friend class SkDeviceImageFilterProxy;
149 friend class SkSurface_Raster;
151 // used to change the backend's pixels (and possibly config/rowbytes)
152 // but cannot change the width/height, so there should be no change to
153 // any clip information.
154 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE;
156 virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
158 virtual SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&) SK_OVERRIDE;
159 virtual const void* peekPixels(SkImageInfo*, size_t* rowBytes) SK_OVERRIDE;
161 virtual SkImageFilter::Cache* getImageFilterCache() SK_OVERRIDE;
165 typedef SkBaseDevice INHERITED;
168 #endif // SkBitmapDevice_DEFINED