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