SkBitmapDevice.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #ifndef SkBitmapDevice_DEFINED
  8. #define SkBitmapDevice_DEFINED
  9. #include "include/core/SkBitmap.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.h"
  12. #include "include/core/SkImageInfo.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkScalar.h"
  15. #include "include/core/SkSize.h"
  16. #include "include/core/SkSurfaceProps.h"
  17. #include "src/core/SkDevice.h"
  18. #include "src/core/SkGlyphRunPainter.h"
  19. #include "src/core/SkRasterClip.h"
  20. #include "src/core/SkRasterClipStack.h"
  21. class SkImageFilterCache;
  22. class SkMatrix;
  23. class SkPaint;
  24. class SkPath;
  25. class SkPixmap;
  26. class SkRasterHandleAllocator;
  27. class SkRRect;
  28. class SkSurface;
  29. struct SkPoint;
  30. ///////////////////////////////////////////////////////////////////////////////
  31. class SkBitmapDevice : public SkBaseDevice {
  32. public:
  33. /**
  34. * Construct a new device with the specified bitmap as its backend. It is
  35. * valid for the bitmap to have no pixels associated with it. In that case,
  36. * any drawing to this device will have no effect.
  37. */
  38. SkBitmapDevice(const SkBitmap& bitmap);
  39. /**
  40. * Create a new device along with its requisite pixel memory using
  41. * default SkSurfaceProps (i.e., kLegacyFontHost_InitType-style).
  42. * Note: this entry point is slated for removal - no one should call it.
  43. */
  44. static SkBitmapDevice* Create(const SkImageInfo& info);
  45. /**
  46. * Construct a new device with the specified bitmap as its backend. It is
  47. * valid for the bitmap to have no pixels associated with it. In that case,
  48. * any drawing to this device will have no effect.
  49. */
  50. SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps,
  51. void* externalHandle, const SkBitmap* coverage);
  52. static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&,
  53. bool trackCoverage,
  54. SkRasterHandleAllocator*);
  55. static SkBitmapDevice* Create(const SkImageInfo& info, const SkSurfaceProps& props) {
  56. return Create(info, props, false, nullptr);
  57. }
  58. const SkPixmap* accessCoverage() const {
  59. return fCoverage ? &fCoverage->pixmap() : nullptr;
  60. }
  61. protected:
  62. void* getRasterHandle() const override { return fRasterHandle; }
  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.
  66. */
  67. void drawPaint(const SkPaint& paint) override;
  68. void drawPoints(SkCanvas::PointMode mode, size_t count,
  69. const SkPoint[], const SkPaint& paint) override;
  70. void drawRect(const SkRect& r, const SkPaint& paint) override;
  71. void drawOval(const SkRect& oval, const SkPaint& paint) override;
  72. void drawRRect(const SkRRect& rr, const SkPaint& paint) override;
  73. /**
  74. * If pathIsMutable, then the implementation is allowed to cast path to a
  75. * non-const pointer and modify it in place (as an optimization). Canvas
  76. * may do this to implement helpers such as drawOval, by placing a temp
  77. * path on the stack to hold the representation of the oval.
  78. */
  79. void drawPath(const SkPath&, const SkPaint&, bool pathIsMutable) override;
  80. void drawSprite(const SkBitmap&, int x, int y, const SkPaint&) override;
  81. /**
  82. * The default impl. will create a bitmap-shader from the bitmap,
  83. * and call drawRect with it.
  84. */
  85. void drawBitmapRect(const SkBitmap&, const SkRect*, const SkRect&,
  86. const SkPaint&, SkCanvas::SrcRectConstraint) override;
  87. void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override;
  88. void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode,
  89. const SkPaint& paint) override;
  90. void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override;
  91. ///////////////////////////////////////////////////////////////////////////
  92. void drawSpecial(SkSpecialImage*, int x, int y, const SkPaint&,
  93. SkImage*, const SkMatrix&) override;
  94. sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
  95. sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
  96. sk_sp<SkSpecialImage> snapSpecial() override;
  97. void setImmutable() override { fBitmap.setImmutable(); }
  98. sk_sp<SkSpecialImage> snapBackImage(const SkIRect&) override;
  99. ///////////////////////////////////////////////////////////////////////////
  100. bool onReadPixels(const SkPixmap&, int x, int y) override;
  101. bool onWritePixels(const SkPixmap&, int, int) override;
  102. bool onPeekPixels(SkPixmap*) override;
  103. bool onAccessPixels(SkPixmap*) override;
  104. void onSave() override;
  105. void onRestore() override;
  106. void onClipRect(const SkRect& rect, SkClipOp, bool aa) override;
  107. void onClipRRect(const SkRRect& rrect, SkClipOp, bool aa) override;
  108. void onClipPath(const SkPath& path, SkClipOp, bool aa) override;
  109. void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override;
  110. void onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) override;
  111. bool onClipIsAA() const override;
  112. void onAsRgnClip(SkRegion*) const override;
  113. void validateDevBounds(const SkIRect& r) override;
  114. ClipType onGetClipType() const override;
  115. virtual void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
  116. const SkPaint&);
  117. private:
  118. friend class SkCanvas;
  119. friend struct DeviceCM; //for setMatrixClip
  120. friend class SkDraw;
  121. friend class SkDrawIter;
  122. friend class SkDrawTiler;
  123. friend class SkSurface_Raster;
  124. class BDDraw;
  125. // used to change the backend's pixels (and possibly config/rowbytes)
  126. // but cannot change the width/height, so there should be no change to
  127. // any clip information.
  128. void replaceBitmapBackendForRasterSurface(const SkBitmap&) override;
  129. SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
  130. sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
  131. SkImageFilterCache* getImageFilterCache() override;
  132. SkBitmap fBitmap;
  133. void* fRasterHandle = nullptr;
  134. SkRasterClipStack fRCStack;
  135. std::unique_ptr<SkBitmap> fCoverage; // if non-null, will have the same dimensions as fBitmap
  136. SkGlyphRunListPainter fGlyphPainter;
  137. typedef SkBaseDevice INHERITED;
  138. };
  139. class SkBitmapDeviceFilteredSurfaceProps {
  140. public:
  141. SkBitmapDeviceFilteredSurfaceProps(const SkBitmap& bitmap, const SkPaint& paint,
  142. const SkSurfaceProps& surfaceProps)
  143. : fSurfaceProps((kN32_SkColorType != bitmap.colorType() || !paint.isSrcOver())
  144. ? fLazy.init(surfaceProps.flags(), kUnknown_SkPixelGeometry)
  145. : &surfaceProps)
  146. { }
  147. SkBitmapDeviceFilteredSurfaceProps(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
  148. SkBitmapDeviceFilteredSurfaceProps& operator=(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
  149. SkBitmapDeviceFilteredSurfaceProps(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
  150. SkBitmapDeviceFilteredSurfaceProps& operator=(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
  151. const SkSurfaceProps& operator()() const { return *fSurfaceProps; }
  152. private:
  153. SkTLazy<SkSurfaceProps> fLazy;
  154. SkSurfaceProps const * const fSurfaceProps;
  155. };
  156. #endif // SkBitmapDevice_DEFINED