robertphillips@google.com | 9241e33 | 2013-08-21 13:54:44 +0000 | [diff] [blame] | 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 | |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 11 | #include "SkBitmap.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkColor.h" |
| 14 | #include "SkDevice.h" |
Herb Derby | 8378dfb | 2018-08-30 14:50:04 -0400 | [diff] [blame] | 15 | #include "SkGlyphRunPainter.h" |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 16 | #include "SkImageInfo.h" |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 17 | #include "SkRasterClip.h" |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 18 | #include "SkRasterClipStack.h" |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 19 | #include "SkRect.h" |
| 20 | #include "SkScalar.h" |
| 21 | #include "SkSize.h" |
| 22 | #include "SkSurfaceProps.h" |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 23 | |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 24 | class SkImageFilterCache; |
| 25 | class SkMatrix; |
| 26 | class SkPaint; |
| 27 | class SkPath; |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 28 | class SkPixmap; |
| 29 | class SkRasterHandleAllocator; |
| 30 | class SkRRect; |
| 31 | class SkSurface; |
| 32 | struct SkPoint; |
| 33 | |
| 34 | /////////////////////////////////////////////////////////////////////////////// |
Mike Reed | 57c2b8b | 2017-12-31 15:23:54 -0500 | [diff] [blame] | 35 | class SkBitmapDevice : public SkBaseDevice { |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 36 | public: |
| 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 Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 57 | void* externalHandle, const SkBitmap* coverage); |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 58 | |
| 59 | static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&, |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 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 | } |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 70 | |
| 71 | protected: |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 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, |
Ben Wagner | 2c312c4 | 2018-06-27 14:46:46 -0400 | [diff] [blame] | 76 | and are handling any looping from the paint. |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 77 | */ |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 78 | void drawPaint(const SkPaint& paint) override; |
| 79 | void drawPoints(SkCanvas::PointMode mode, size_t count, |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 80 | const SkPoint[], const SkPaint& paint) override; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 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; |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 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. |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 90 | */ |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 91 | void drawPath(const SkPath&, const SkPaint&, bool pathIsMutable) override; |
Hal Canary | b964238 | 2017-06-27 09:58:56 -0400 | [diff] [blame] | 92 | void drawBitmap(const SkBitmap&, SkScalar x, SkScalar y, const SkPaint&) override; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 93 | void drawSprite(const SkBitmap&, int x, int y, const SkPaint&) override; |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * The default impl. will create a bitmap-shader from the bitmap, |
| 97 | * and call drawRect with it. |
| 98 | */ |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 99 | void drawBitmapRect(const SkBitmap&, const SkRect*, const SkRect&, |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 100 | const SkPaint&, SkCanvas::SrcRectConstraint) override; |
| 101 | |
Herb Derby | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 102 | void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override; |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 103 | void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode, |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 104 | const SkPaint& paint) override; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 105 | void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override; |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 106 | |
| 107 | /////////////////////////////////////////////////////////////////////////// |
Yuqian Li | b55dd55 | 2017-04-03 10:52:48 -0400 | [diff] [blame] | 108 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 109 | void drawSpecial(SkSpecialImage*, int x, int y, const SkPaint&, |
| 110 | SkImage*, const SkMatrix&) override; |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 111 | sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override; |
| 112 | sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override; |
| 113 | sk_sp<SkSpecialImage> snapSpecial() override; |
Lee Salzman | 5d8949c | 2018-09-19 15:36:54 -0400 | [diff] [blame] | 114 | void setImmutable() override { fBitmap.setImmutable(); } |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 115 | |
| 116 | /////////////////////////////////////////////////////////////////////////// |
| 117 | |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 118 | bool onReadPixels(const SkPixmap&, int x, int y) override; |
| 119 | bool onWritePixels(const SkPixmap&, int, int) override; |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 120 | bool onPeekPixels(SkPixmap*) override; |
| 121 | bool onAccessPixels(SkPixmap*) override; |
| 122 | |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 123 | 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 Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 129 | void onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) override; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 130 | bool onClipIsAA() const override; |
| 131 | void onAsRgnClip(SkRegion*) const override; |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 132 | void validateDevBounds(const SkIRect& r) override; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 133 | ClipType onGetClipType() const override; |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 134 | |
Yuqian Li | bf74a46 | 2018-03-22 15:00:01 -0400 | [diff] [blame] | 135 | virtual void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull, |
| 136 | const SkPaint&); |
| 137 | |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 138 | private: |
| 139 | friend class SkCanvas; |
| 140 | friend struct DeviceCM; //for setMatrixClip |
| 141 | friend class SkDraw; |
| 142 | friend class SkDrawIter; |
Mike Reed | b5e1f75 | 2018-03-07 14:16:52 -0500 | [diff] [blame] | 143 | friend class SkDrawTiler; |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 144 | friend class SkSurface_Raster; |
Mike Reed | 025e244 | 2017-03-04 19:43:23 +0000 | [diff] [blame] | 145 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 146 | class BDDraw; |
| 147 | |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 148 | // 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 Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 161 | SkRasterClipStack fRCStack; |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 162 | std::unique_ptr<SkBitmap> fCoverage; // if non-null, will have the same dimensions as fBitmap |
Herb Derby | 6595687 | 2018-08-21 16:55:04 -0400 | [diff] [blame] | 163 | SkGlyphRunListPainter fGlyphPainter; |
Herb Derby | 1347144 | 2018-07-23 16:20:53 -0400 | [diff] [blame] | 164 | |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 165 | |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 166 | typedef SkBaseDevice INHERITED; |
| 167 | }; |
robertphillips@google.com | 9241e33 | 2013-08-21 13:54:44 +0000 | [diff] [blame] | 168 | |
Yuqian Li | 5c91fa4 | 2018-05-04 14:19:56 -0400 | [diff] [blame] | 169 | class SkBitmapDeviceFilteredSurfaceProps { |
| 170 | public: |
| 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 | |
| 185 | private: |
| 186 | SkTLazy<SkSurfaceProps> fLazy; |
| 187 | SkSurfaceProps const * const fSurfaceProps; |
| 188 | }; |
| 189 | |
robertphillips@google.com | 9241e33 | 2013-08-21 13:54:44 +0000 | [diff] [blame] | 190 | #endif // SkBitmapDevice_DEFINED |