blob: 7a0d947bd74b202dfbdf6164aee97c6296811491 [file] [log] [blame]
robertphillips@google.com9241e332013-08-21 13:54:44 +00001/*
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
Mike Reed986480a2017-01-13 22:43:16 +000011#include "SkBitmap.h"
12#include "SkCanvas.h"
13#include "SkColor.h"
14#include "SkDevice.h"
Herb Derby8378dfb2018-08-30 14:50:04 -040015#include "SkGlyphRunPainter.h"
Mike Reed986480a2017-01-13 22:43:16 +000016#include "SkImageInfo.h"
Mike Reed7627fa52017-02-08 10:07:53 -050017#include "SkRasterClip.h"
Mike Reedc42a1cd2017-02-14 14:25:14 -050018#include "SkRasterClipStack.h"
Mike Reed986480a2017-01-13 22:43:16 +000019#include "SkRect.h"
20#include "SkScalar.h"
21#include "SkSize.h"
22#include "SkSurfaceProps.h"
Mike Reed986480a2017-01-13 22:43:16 +000023
Mike Reed986480a2017-01-13 22:43:16 +000024class SkImageFilterCache;
25class SkMatrix;
26class SkPaint;
27class SkPath;
Mike Reed986480a2017-01-13 22:43:16 +000028class SkPixmap;
29class SkRasterHandleAllocator;
30class SkRRect;
31class SkSurface;
32struct SkPoint;
33
34///////////////////////////////////////////////////////////////////////////////
Mike Reed57c2b8b2017-12-31 15:23:54 -050035class SkBitmapDevice : public SkBaseDevice {
Mike Reed986480a2017-01-13 22:43:16 +000036public:
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,
Mike Reed910ca0f2018-04-25 13:04:05 -040057 void* externalHandle, const SkBitmap* coverage);
Mike Reed986480a2017-01-13 22:43:16 +000058
59 static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&,
Mike Reed910ca0f2018-04-25 13:04:05 -040060 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 }
Mike Reed986480a2017-01-13 22:43:16 +000070
71protected:
Mike Reed986480a2017-01-13 22:43:16 +000072 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,
Ben Wagner2c312c42018-06-27 14:46:46 -040076 and are handling any looping from the paint.
Mike Reed986480a2017-01-13 22:43:16 +000077 */
Mike Reeda1361362017-03-07 09:37:29 -050078 void drawPaint(const SkPaint& paint) override;
79 void drawPoints(SkCanvas::PointMode mode, size_t count,
Mike Reed986480a2017-01-13 22:43:16 +000080 const SkPoint[], const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -050081 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;
Mike Reed986480a2017-01-13 22:43:16 +000084
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.
Mike Reed986480a2017-01-13 22:43:16 +000090 */
Robert Phillips137ca522018-08-15 10:14:33 -040091 void drawPath(const SkPath&, const SkPaint&, bool pathIsMutable) override;
Hal Canaryb9642382017-06-27 09:58:56 -040092 void drawBitmap(const SkBitmap&, SkScalar x, SkScalar y, const SkPaint&) override;
Mike Reeda1361362017-03-07 09:37:29 -050093 void drawSprite(const SkBitmap&, int x, int y, const SkPaint&) override;
Mike Reed986480a2017-01-13 22:43:16 +000094
95 /**
96 * The default impl. will create a bitmap-shader from the bitmap,
97 * and call drawRect with it.
98 */
Mike Reeda1361362017-03-07 09:37:29 -050099 void drawBitmapRect(const SkBitmap&, const SkRect*, const SkRect&,
Mike Reed986480a2017-01-13 22:43:16 +0000100 const SkPaint&, SkCanvas::SrcRectConstraint) override;
101
Herb Derbyb935cf82018-07-26 16:54:18 -0400102 void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override;
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400103 void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode,
Ruiqi Maof5101492018-06-29 14:32:21 -0400104 const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -0500105 void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override;
Mike Reed986480a2017-01-13 22:43:16 +0000106
107 ///////////////////////////////////////////////////////////////////////////
Yuqian Lib55dd552017-04-03 10:52:48 -0400108
Florin Malita53f77bd2017-04-28 13:48:37 -0400109 void drawSpecial(SkSpecialImage*, int x, int y, const SkPaint&,
110 SkImage*, const SkMatrix&) override;
Mike Reed986480a2017-01-13 22:43:16 +0000111 sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
112 sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
113 sk_sp<SkSpecialImage> snapSpecial() override;
Lee Salzman5d8949c2018-09-19 15:36:54 -0400114 void setImmutable() override { fBitmap.setImmutable(); }
Mike Reed986480a2017-01-13 22:43:16 +0000115
116 ///////////////////////////////////////////////////////////////////////////
117
Mike Reed353196f2017-07-21 11:01:18 -0400118 bool onReadPixels(const SkPixmap&, int x, int y) override;
119 bool onWritePixels(const SkPixmap&, int, int) override;
Mike Reed986480a2017-01-13 22:43:16 +0000120 bool onPeekPixels(SkPixmap*) override;
121 bool onAccessPixels(SkPixmap*) override;
122
Mike Reed7627fa52017-02-08 10:07:53 -0500123 void onSave() override;
124 void onRestore() override;
125 void onClipRect(const SkRect& rect, SkClipOp, bool aa) override;
126 void onClipRRect(const SkRRect& rrect, SkClipOp, bool aa) override;
127 void onClipPath(const SkPath& path, SkClipOp, bool aa) override;
128 void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override;
Mike Reedc42a1cd2017-02-14 14:25:14 -0500129 void onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) override;
Mike Reeda1361362017-03-07 09:37:29 -0500130 bool onClipIsAA() const override;
131 void onAsRgnClip(SkRegion*) const override;
Mike Reedc42a1cd2017-02-14 14:25:14 -0500132 void validateDevBounds(const SkIRect& r) override;
Mike Reeda1361362017-03-07 09:37:29 -0500133 ClipType onGetClipType() const override;
Mike Reed7627fa52017-02-08 10:07:53 -0500134
Yuqian Libf74a462018-03-22 15:00:01 -0400135 virtual void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
136 const SkPaint&);
137
Mike Reed986480a2017-01-13 22:43:16 +0000138private:
139 friend class SkCanvas;
140 friend struct DeviceCM; //for setMatrixClip
141 friend class SkDraw;
142 friend class SkDrawIter;
Mike Reedb5e1f752018-03-07 14:16:52 -0500143 friend class SkDrawTiler;
Mike Reed27d07f02017-03-04 21:47:47 +0000144 friend class SkSurface_Raster;
Mike Reed025e2442017-03-04 19:43:23 +0000145
Mike Reeda1361362017-03-07 09:37:29 -0500146 class BDDraw;
147
Mike Reed986480a2017-01-13 22:43:16 +0000148 // used to change the backend's pixels (and possibly config/rowbytes)
149 // but cannot change the width/height, so there should be no change to
150 // any clip information.
151 void replaceBitmapBackendForRasterSurface(const SkBitmap&) override;
152
153 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
154
155 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
156
157 SkImageFilterCache* getImageFilterCache() override;
158
159 SkBitmap fBitmap;
160 void* fRasterHandle = nullptr;
Mike Reedc42a1cd2017-02-14 14:25:14 -0500161 SkRasterClipStack fRCStack;
Mike Reed910ca0f2018-04-25 13:04:05 -0400162 std::unique_ptr<SkBitmap> fCoverage; // if non-null, will have the same dimensions as fBitmap
Herb Derby65956872018-08-21 16:55:04 -0400163 SkGlyphRunListPainter fGlyphPainter;
Herb Derby13471442018-07-23 16:20:53 -0400164
Mike Reed986480a2017-01-13 22:43:16 +0000165
Mike Reed986480a2017-01-13 22:43:16 +0000166 typedef SkBaseDevice INHERITED;
167};
robertphillips@google.com9241e332013-08-21 13:54:44 +0000168
Yuqian Li5c91fa42018-05-04 14:19:56 -0400169class SkBitmapDeviceFilteredSurfaceProps {
170public:
171 SkBitmapDeviceFilteredSurfaceProps(const SkBitmap& bitmap, const SkPaint& paint,
172 const SkSurfaceProps& surfaceProps)
173 : fSurfaceProps((kN32_SkColorType != bitmap.colorType() || !paint.isSrcOver())
174 ? fLazy.init(surfaceProps.flags(), kUnknown_SkPixelGeometry)
175 : &surfaceProps)
176 { }
177
178 SkBitmapDeviceFilteredSurfaceProps(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
179 SkBitmapDeviceFilteredSurfaceProps& operator=(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
180 SkBitmapDeviceFilteredSurfaceProps(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
181 SkBitmapDeviceFilteredSurfaceProps& operator=(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
182
183 const SkSurfaceProps& operator()() const { return *fSurfaceProps; }
184
185private:
186 SkTLazy<SkSurfaceProps> fLazy;
187 SkSurfaceProps const * const fSurfaceProps;
188};
189
robertphillips@google.com9241e332013-08-21 13:54:44 +0000190#endif // SkBitmapDevice_DEFINED