robertphillips@google.com | 9241e33 | 2013-08-21 13:54:44 +0000 | [diff] [blame] | 1 | |
| 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 | |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 12 | #include "SkBitmap.h" |
| 13 | #include "SkCanvas.h" |
| 14 | #include "SkColor.h" |
robertphillips@google.com | 9241e33 | 2013-08-21 13:54:44 +0000 | [diff] [blame] | 15 | #include "SkDevice.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 16 | #include "SkImageInfo.h" |
| 17 | #include "SkRect.h" |
| 18 | #include "SkScalar.h" |
| 19 | #include "SkSize.h" |
| 20 | #include "SkSurfaceProps.h" |
| 21 | #include "SkTypes.h" |
| 22 | |
| 23 | class SkDraw; |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 24 | class SkImageFilterCache; |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 25 | class SkMatrix; |
| 26 | class SkPaint; |
| 27 | class SkPath; |
| 28 | class SkPixelRef; |
| 29 | class SkPixmap; |
| 30 | class SkRRect; |
| 31 | class SkSurface; |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 32 | struct SkPoint; |
robertphillips@google.com | 9241e33 | 2013-08-21 13:54:44 +0000 | [diff] [blame] | 33 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | class SK_API SkBitmapDevice : public SkBaseDevice { |
| 36 | public: |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 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. |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 41 | */ |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 42 | SkBitmapDevice(const SkBitmap& bitmap); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 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 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 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. |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 55 | */ |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 56 | SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 57 | |
| 58 | static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 59 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 60 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | bool onShouldDisableLCD(const SkPaint&) const override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 62 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 63 | /** These are called inside the per-device-layer loop for each draw call. |
| 64 | When these are called, we have already applied any saveLayer operations, |
| 65 | and are handling any looping from the paint, and any effects from the |
| 66 | DrawFilter. |
| 67 | */ |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 68 | void drawPaint(const SkDraw&, const SkPaint& paint) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 69 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 70 | const SkPoint[], const SkPaint& paint) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 71 | virtual void drawRect(const SkDraw&, const SkRect& r, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | const SkPaint& paint) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 73 | virtual void drawOval(const SkDraw&, const SkRect& oval, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 74 | const SkPaint& paint) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 75 | virtual void drawRRect(const SkDraw&, const SkRRect& rr, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | const SkPaint& paint) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * If pathIsMutable, then the implementation is allowed to cast path to a |
| 80 | * non-const pointer and modify it in place (as an optimization). Canvas |
| 81 | * may do this to implement helpers such as drawOval, by placing a temp |
| 82 | * path on the stack to hold the representation of the oval. |
| 83 | * |
| 84 | * If prePathMatrix is not null, it should logically be applied before any |
| 85 | * stroking or other effects. If there are no effects on the paint that |
| 86 | * affect the geometry/rasterization, then the pre matrix can just be |
| 87 | * pre-concated with the current matrix. |
| 88 | */ |
| 89 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| 90 | const SkPaint& paint, |
| 91 | const SkMatrix* prePathMatrix = NULL, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 92 | bool pathIsMutable = false) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 93 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 94 | const SkMatrix& matrix, const SkPaint& paint) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 95 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 96 | int x, int y, const SkPaint& paint) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * The default impl. will create a bitmap-shader from the bitmap, |
| 100 | * and call drawRect with it. |
| 101 | */ |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 102 | void drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect*, const SkRect&, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 103 | const SkPaint&, SkCanvas::SrcRectConstraint) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Does not handle text decoration. |
| 107 | * Decorations (underline and stike-thru) will be handled by SkCanvas. |
| 108 | */ |
| 109 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 110 | SkScalar x, SkScalar y, const SkPaint& paint) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 111 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 112 | const SkScalar pos[], int scalarsPerPos, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 113 | const SkPoint& offset, const SkPaint& paint) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 114 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, |
| 115 | const SkPoint verts[], const SkPoint texs[], |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 116 | const SkColor colors[], SkBlendMode, |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 117 | const uint16_t indices[], int indexCount, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 118 | const SkPaint& paint) override; |
| 119 | virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, const SkPaint&) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 120 | |
| 121 | /////////////////////////////////////////////////////////////////////////// |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 122 | |
| 123 | void drawSpecial(const SkDraw&, SkSpecialImage*, int x, int y, const SkPaint&) override; |
| 124 | sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override; |
| 125 | sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override; |
| 126 | sk_sp<SkSpecialImage> snapSpecial() override; |
| 127 | |
| 128 | /////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 129 | |
| 130 | /** Update as needed the pixel value in the bitmap, so that the caller can |
| 131 | access the pixels directly. Note: only the pixels field should be |
| 132 | altered. The config/width/height/rowbytes must remain unchanged. |
| 133 | @return the device contents as a bitmap |
| 134 | */ |
robertphillips | 1f3923e | 2016-07-21 07:17:54 -0700 | [diff] [blame] | 135 | #ifdef SK_SUPPORT_LEGACY_ACCESSBITMAP |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 136 | const SkBitmap& onAccessBitmap() override; |
robertphillips | 1f3923e | 2016-07-21 07:17:54 -0700 | [diff] [blame] | 137 | #else |
| 138 | const SkBitmap& onAccessBitmap(); |
| 139 | #endif |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 140 | |
| 141 | SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } |
| 142 | // just for subclasses, to assign a custom pixelref |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 143 | SkPixelRef* setPixelRef(SkPixelRef* pr) { |
| 144 | fBitmap.setPixelRef(pr); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 145 | return pr; |
| 146 | } |
| 147 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 148 | bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) override; |
| 149 | bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) override; |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 150 | bool onPeekPixels(SkPixmap*) override; |
| 151 | bool onAccessPixels(SkPixmap*) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 152 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 153 | private: |
| 154 | friend class SkCanvas; |
| 155 | friend struct DeviceCM; //for setMatrixClip |
| 156 | friend class SkDraw; |
| 157 | friend class SkDrawIter; |
| 158 | friend class SkDeviceFilteredPaint; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 159 | |
| 160 | friend class SkSurface_Raster; |
| 161 | |
| 162 | // used to change the backend's pixels (and possibly config/rowbytes) |
| 163 | // but cannot change the width/height, so there should be no change to |
| 164 | // any clip information. |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 165 | void replaceBitmapBackendForRasterSurface(const SkBitmap&) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 166 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 167 | SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 168 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 169 | sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override; |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 170 | |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 171 | SkImageFilterCache* getImageFilterCache() override; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 172 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 173 | SkBitmap fBitmap; |
| 174 | |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 175 | void setNewSize(const SkISize&); // Used by SkCanvas for resetForNextPicture(). |
| 176 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 177 | typedef SkBaseDevice INHERITED; |
| 178 | }; |
robertphillips@google.com | 9241e33 | 2013-08-21 13:54:44 +0000 | [diff] [blame] | 179 | |
| 180 | #endif // SkBitmapDevice_DEFINED |