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