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 "SkBitmap.h" 12 #include "SkCanvas.h" 13 #include "SkColor.h" 14 #include "SkDevice.h" 15 #include "SkGlyphRunPainter.h" 16 #include "SkImageInfo.h" 17 #include "SkRasterClip.h" 18 #include "SkRasterClipStack.h" 19 #include "SkRect.h" 20 #include "SkScalar.h" 21 #include "SkSize.h" 22 #include "SkSurfaceProps.h" 23 24 class SkImageFilterCache; 25 class SkMatrix; 26 class SkPaint; 27 class SkPath; 28 class SkPixmap; 29 class SkRasterHandleAllocator; 30 class SkRRect; 31 class SkSurface; 32 struct SkPoint; 33 34 /////////////////////////////////////////////////////////////////////////////// 35 class SkBitmapDevice : public SkBaseDevice { 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, 57 void* externalHandle, const SkBitmap* coverage); 58 59 static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&, 60 bool trackCoverage, 61 SkRasterHandleAllocator*); 62 Create(const SkImageInfo & info,const SkSurfaceProps & props)63 static SkBitmapDevice* Create(const SkImageInfo& info, const SkSurfaceProps& props) { 64 return Create(info, props, false, nullptr); 65 } 66 accessCoverage()67 const SkPixmap* accessCoverage() const { 68 return fCoverage ? &fCoverage->pixmap() : nullptr; 69 } 70 71 protected: getRasterHandle()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 void drawBitmap(const SkBitmap&, SkScalar x, SkScalar y, const SkPaint&) override; 93 void drawSprite(const SkBitmap&, int x, int y, const SkPaint&) override; 94 95 /** 96 * The default impl. will create a bitmap-shader from the bitmap, 97 * and call drawRect with it. 98 */ 99 void drawBitmapRect(const SkBitmap&, const SkRect*, const SkRect&, 100 const SkPaint&, SkCanvas::SrcRectConstraint) override; 101 102 void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override; 103 void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode, 104 const SkPaint& paint) override; 105 void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override; 106 107 /////////////////////////////////////////////////////////////////////////// 108 109 void drawSpecial(SkSpecialImage*, int x, int y, const SkPaint&, 110 SkImage*, const SkMatrix&) override; 111 sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override; 112 sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override; 113 sk_sp<SkSpecialImage> snapSpecial() override; setImmutable()114 void setImmutable() override { fBitmap.setImmutable(); } 115 116 sk_sp<SkSpecialImage> snapBackImage(const SkIRect&) override; 117 118 /////////////////////////////////////////////////////////////////////////// 119 120 bool onReadPixels(const SkPixmap&, int x, int y) override; 121 bool onWritePixels(const SkPixmap&, int, int) override; 122 bool onPeekPixels(SkPixmap*) override; 123 bool onAccessPixels(SkPixmap*) override; 124 125 void onSave() override; 126 void onRestore() override; 127 void onClipRect(const SkRect& rect, SkClipOp, bool aa) override; 128 void onClipRRect(const SkRRect& rrect, SkClipOp, bool aa) override; 129 void onClipPath(const SkPath& path, SkClipOp, bool aa) override; 130 void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override; 131 void onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) override; 132 bool onClipIsAA() const override; 133 void onAsRgnClip(SkRegion*) const override; 134 void validateDevBounds(const SkIRect& r) override; 135 ClipType onGetClipType() const override; 136 137 virtual void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull, 138 const SkPaint&); 139 140 private: 141 friend class SkCanvas; 142 friend struct DeviceCM; //for setMatrixClip 143 friend class SkDraw; 144 friend class SkDrawIter; 145 friend class SkDrawTiler; 146 friend class SkSurface_Raster; 147 148 class BDDraw; 149 150 // used to change the backend's pixels (and possibly config/rowbytes) 151 // but cannot change the width/height, so there should be no change to 152 // any clip information. 153 void replaceBitmapBackendForRasterSurface(const SkBitmap&) override; 154 155 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override; 156 157 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override; 158 159 SkImageFilterCache* getImageFilterCache() override; 160 161 SkBitmap fBitmap; 162 void* fRasterHandle = nullptr; 163 SkRasterClipStack fRCStack; 164 std::unique_ptr<SkBitmap> fCoverage; // if non-null, will have the same dimensions as fBitmap 165 SkGlyphRunListPainter fGlyphPainter; 166 167 168 typedef SkBaseDevice INHERITED; 169 }; 170 171 class SkBitmapDeviceFilteredSurfaceProps { 172 public: SkBitmapDeviceFilteredSurfaceProps(const SkBitmap & bitmap,const SkPaint & paint,const SkSurfaceProps & surfaceProps)173 SkBitmapDeviceFilteredSurfaceProps(const SkBitmap& bitmap, const SkPaint& paint, 174 const SkSurfaceProps& surfaceProps) 175 : fSurfaceProps((kN32_SkColorType != bitmap.colorType() || !paint.isSrcOver()) 176 ? fLazy.init(surfaceProps.flags(), kUnknown_SkPixelGeometry) 177 : &surfaceProps) 178 { } 179 180 SkBitmapDeviceFilteredSurfaceProps(const SkBitmapDeviceFilteredSurfaceProps&) = delete; 181 SkBitmapDeviceFilteredSurfaceProps& operator=(const SkBitmapDeviceFilteredSurfaceProps&) = delete; 182 SkBitmapDeviceFilteredSurfaceProps(SkBitmapDeviceFilteredSurfaceProps&&) = delete; 183 SkBitmapDeviceFilteredSurfaceProps& operator=(SkBitmapDeviceFilteredSurfaceProps&&) = delete; 184 operator()185 const SkSurfaceProps& operator()() const { return *fSurfaceProps; } 186 187 private: 188 SkTLazy<SkSurfaceProps> fLazy; 189 SkSurfaceProps const * const fSurfaceProps; 190 }; 191 192 #endif // SkBitmapDevice_DEFINED 193