blob: 0ab0234772ca72b0466816d80cd55dc1e1f0ec6a [file] [log] [blame]
robertphillips@google.com9241e332013-08-21 13:54:44 +00001
2/*
3 * Copyright 2013 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef SkBitmapDevice_DEFINED
10#define SkBitmapDevice_DEFINED
11
12#include "SkDevice.h"
13
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000014///////////////////////////////////////////////////////////////////////////////
15class SK_API SkBitmapDevice : public SkBaseDevice {
16public:
17 SK_DECLARE_INST_COUNT(SkBitmapDevice)
18
19 /**
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.
23 */
24 SkBitmapDevice(const SkBitmap& bitmap);
reede010f1c2014-09-17 10:49:38 -070025private:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000026 /**
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.
30 */
31 SkBitmapDevice(const SkBitmap& bitmap, const SkDeviceProperties& deviceProperties);
reede010f1c2014-09-17 10:49:38 -070032 static SkBitmapDevice* Create(const SkImageInfo&, const SkDeviceProperties*);
33public:
34 static SkBitmapDevice* Create(const SkImageInfo& info) {
35 return Create(info, NULL);
36 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000037
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000038 virtual SkImageInfo imageInfo() const SK_OVERRIDE;
39
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000040protected:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000041 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE;
42
43 /** Clears the entire device to the specified color (including alpha).
44 * Ignores the clip.
45 */
46 virtual void clear(SkColor color) SK_OVERRIDE;
47
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
51 DrawFilter.
52 */
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;
62
63 /**
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.
68 *
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.
73 */
74 virtual void drawPath(const SkDraw&, const SkPath& path,
75 const SkPaint& paint,
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;
82
83 /**
84 * The default impl. will create a bitmap-shader from the bitmap,
85 * and call drawRect with it.
86 */
87 virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
88 const SkRect* srcOrNull, const SkRect& dst,
89 const SkPaint& paint,
90 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
91
92 /**
93 * Does not handle text decoration.
94 * Decorations (underline and stike-thru) will be handled by SkCanvas.
95 */
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[], SkScalar constY,
100 int scalarsPerPos, 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;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000104 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
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000110 onCreateDevice on this device with kSaveLayer_Usage.
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000111 */
112 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
113 const SkPaint&) SK_OVERRIDE;
114
115 ///////////////////////////////////////////////////////////////////////////
116
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
121 */
122 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
123
124 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
125 // just for subclasses, to assign a custom pixelref
reed@google.com672588b2014-01-08 15:42:01 +0000126 SkPixelRef* setPixelRef(SkPixelRef* pr) {
127 fBitmap.setPixelRef(pr);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000128 return pr;
129 }
130
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000131 virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) SK_OVERRIDE;
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000132 virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) SK_OVERRIDE;
reed@google.com9c135db2014-03-12 18:28:35 +0000133 virtual void* onAccessPixels(SkImageInfo* info, size_t* rowBytes) SK_OVERRIDE;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000134
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.
137 */
138 virtual void lockPixels() SK_OVERRIDE;
139 virtual void unlockPixels() SK_OVERRIDE;
140
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000141private:
142 friend class SkCanvas;
143 friend struct DeviceCM; //for setMatrixClip
144 friend class SkDraw;
145 friend class SkDrawIter;
146 friend class SkDeviceFilteredPaint;
147 friend class SkDeviceImageFilterProxy;
148
149 friend class SkSurface_Raster;
150
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;
155
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000156 virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000157
reed3716fd02014-09-21 09:39:55 -0700158 virtual SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&) SK_OVERRIDE;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000159 virtual const void* peekPixels(SkImageInfo*, size_t* rowBytes) SK_OVERRIDE;
reed@google.com76f10a32014-02-05 15:32:21 +0000160
senorblancobe129b22014-08-08 07:14:35 -0700161 virtual SkImageFilter::Cache* getImageFilterCache() SK_OVERRIDE;
senorblanco55b6d8b2014-07-30 11:26:46 -0700162
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000163 SkBitmap fBitmap;
164
165 typedef SkBaseDevice INHERITED;
166};
robertphillips@google.com9241e332013-08-21 13:54:44 +0000167
168#endif // SkBitmapDevice_DEFINED