1/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkBitmapDevice_DEFINED
9#define SkBitmapDevice_DEFINED
10
11#include "include/core/SkBitmap.h"
12#include "include/core/SkCanvas.h"
13#include "include/core/SkColor.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkSurfaceProps.h"
19#include "src/core/SkDevice.h"
20#include "src/core/SkGlyphRunPainter.h"
21#include "src/core/SkRasterClip.h"
22#include "src/core/SkRasterClipStack.h"
23
24class SkImageFilterCache;
25class SkMatrix;
26class SkPaint;
27class SkPath;
28class SkPixmap;
29class SkRasterHandleAllocator;
30class SkRRect;
31class SkSurface;
32struct SkPoint;
33
34///////////////////////////////////////////////////////////////////////////////
35class SkBitmapDevice : public SkBaseDevice {
36public:
37 /**
38 * Construct a new device with the specified bitmap as its backend. It is
39 * valid for the bitmap to have no pixels associated with it. In that case,
40 * any drawing to this device will have no effect.
41 */
42 SkBitmapDevice(const SkBitmap& bitmap);
43
44 /**
45 * Create a new device along with its requisite pixel memory using
46 * default SkSurfaceProps (i.e., kLegacyFontHost_InitType-style).
47 * Note: this entry point is slated for removal - no one should call it.
48 */
49 static SkBitmapDevice* Create(const SkImageInfo& info);
50
51 /**
52 * Construct a new device with the specified bitmap as its backend. It is
53 * valid for the bitmap to have no pixels associated with it. In that case,
54 * any drawing to this device will have no effect.
55 */
56 SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps,
57 void* externalHandle, const SkBitmap* coverage);
58
59 static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&,
60 bool trackCoverage,
61 SkRasterHandleAllocator*);
62
63 static SkBitmapDevice* Create(const SkImageInfo& info, const SkSurfaceProps& props) {
64 return Create(info, props, false, nullptr);
65 }
66
67 const SkPixmap* accessCoverage() const {
68 return fCoverage ? &fCoverage->pixmap() : nullptr;
69 }
70
71protected:
72 void* getRasterHandle() const override { return fRasterHandle; }
73
74 /** These are called inside the per-device-layer loop for each draw call.
75 When these are called, we have already applied any saveLayer operations,
76 and are handling any looping from the paint.
77 */
78 void drawPaint(const SkPaint& paint) override;
79 void drawPoints(SkCanvas::PointMode mode, size_t count,
80 const SkPoint[], const SkPaint& paint) override;
81 void drawRect(const SkRect& r, const SkPaint& paint) override;
82 void drawOval(const SkRect& oval, const SkPaint& paint) override;
83 void drawRRect(const SkRRect& rr, const SkPaint& paint) override;
84
85 /**
86 * If pathIsMutable, then the implementation is allowed to cast path to a
87 * non-const pointer and modify it in place (as an optimization). Canvas
88 * may do this to implement helpers such as drawOval, by placing a temp
89 * path on the stack to hold the representation of the oval.
90 */
91 void drawPath(const SkPath&, const SkPaint&, bool pathIsMutable) override;
92
93 void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
94 const SkPaint&, SkCanvas::SrcRectConstraint) override;
95
96 void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override;
97 void drawVertices(const SkVertices*, SkBlendMode, const SkPaint&) override;
98 void drawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int count,
99 SkBlendMode, const SkPaint&) override;
100 void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override;
101
102 ///////////////////////////////////////////////////////////////////////////
103
104 void drawSpecial(SkSpecialImage*, int x, int y, const SkPaint&,
105 SkImage*, const SkMatrix&) override;
106 sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
107 sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
108 sk_sp<SkSpecialImage> snapSpecial(const SkIRect&, bool = false) override;
109 void setImmutable() override { fBitmap.setImmutable(); }
110
111 ///////////////////////////////////////////////////////////////////////////
112
113 bool onReadPixels(const SkPixmap&, int x, int y) override;
114 bool onWritePixels(const SkPixmap&, int, int) override;
115 bool onPeekPixels(SkPixmap*) override;
116 bool onAccessPixels(SkPixmap*) override;
117
118 void onSave() override;
119 void onRestore() override;
120 void onClipRect(const SkRect& rect, SkClipOp, bool aa) override;
121 void onClipRRect(const SkRRect& rrect, SkClipOp, bool aa) override;
122 void onClipPath(const SkPath& path, SkClipOp, bool aa) override;
123 void onClipShader(sk_sp<SkShader>) override;
124 void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override;
125 void onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) override;
126 bool onClipIsAA() const override;
127 bool onClipIsWideOpen() const override;
128 void onAsRgnClip(SkRegion*) const override;
129 void validateDevBounds(const SkIRect& r) override;
130 ClipType onGetClipType() const override;
131 SkIRect onDevClipBounds() const override;
132
133 void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
134 const SkPaint&);
135
136private:
137 friend class SkCanvas;
138 friend struct DeviceCM; //for setMatrixClip
139 friend class SkDraw;
140 friend class SkDrawIter;
141 friend class SkDrawTiler;
142 friend class SkSurface_Raster;
143
144 class BDDraw;
145
146 // used to change the backend's pixels (and possibly config/rowbytes)
147 // but cannot change the width/height, so there should be no change to
148 // any clip information.
149 void replaceBitmapBackendForRasterSurface(const SkBitmap&) override;
150
151 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
152
153 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
154
155 SkImageFilterCache* getImageFilterCache() override;
156
157 SkBitmap fBitmap;
158 void* fRasterHandle = nullptr;
159 SkRasterClipStack fRCStack;
160 std::unique_ptr<SkBitmap> fCoverage; // if non-null, will have the same dimensions as fBitmap
161 SkGlyphRunListPainter fGlyphPainter;
162
163
164 typedef SkBaseDevice INHERITED;
165};
166
167class SkBitmapDeviceFilteredSurfaceProps {
168public:
169 SkBitmapDeviceFilteredSurfaceProps(const SkBitmap& bitmap, const SkPaint& paint,
170 const SkSurfaceProps& surfaceProps)
171 : fSurfaceProps((kN32_SkColorType != bitmap.colorType() || !paint.isSrcOver())
172 ? fLazy.init(surfaceProps.flags(), kUnknown_SkPixelGeometry)
173 : &surfaceProps)
174 { }
175
176 SkBitmapDeviceFilteredSurfaceProps(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
177 SkBitmapDeviceFilteredSurfaceProps& operator=(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
178 SkBitmapDeviceFilteredSurfaceProps(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
179 SkBitmapDeviceFilteredSurfaceProps& operator=(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
180
181 const SkSurfaceProps& operator()() const { return *fSurfaceProps; }
182
183private:
184 SkTLazy<SkSurfaceProps> fLazy;
185 SkSurfaceProps const * const fSurfaceProps;
186};
187
188#endif // SkBitmapDevice_DEFINED
189