SkAAClip.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2011 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 SkAAClip_DEFINED
  8. #define SkAAClip_DEFINED
  9. #include "include/core/SkRegion.h"
  10. #include "src/core/SkAutoMalloc.h"
  11. #include "src/core/SkBlitter.h"
  12. class SkAAClip {
  13. public:
  14. SkAAClip();
  15. SkAAClip(const SkAAClip&);
  16. ~SkAAClip();
  17. SkAAClip& operator=(const SkAAClip&);
  18. friend bool operator==(const SkAAClip&, const SkAAClip&);
  19. friend bool operator!=(const SkAAClip& a, const SkAAClip& b) {
  20. return !(a == b);
  21. }
  22. void swap(SkAAClip&);
  23. bool isEmpty() const { return nullptr == fRunHead; }
  24. const SkIRect& getBounds() const { return fBounds; }
  25. // Returns true iff the clip is not empty, and is just a hard-edged rect (no partial alpha).
  26. // If true, getBounds() can be used in place of this clip.
  27. bool isRect() const;
  28. bool setEmpty();
  29. bool setRect(const SkIRect&);
  30. bool setRect(const SkRect&, bool doAA = true);
  31. bool setPath(const SkPath&, const SkRegion* clip = nullptr, bool doAA = true);
  32. bool setRegion(const SkRegion&);
  33. bool set(const SkAAClip&);
  34. bool op(const SkAAClip&, const SkAAClip&, SkRegion::Op);
  35. // Helpers for op()
  36. bool op(const SkIRect&, SkRegion::Op);
  37. bool op(const SkRect&, SkRegion::Op, bool doAA);
  38. bool op(const SkAAClip&, SkRegion::Op);
  39. bool translate(int dx, int dy, SkAAClip* dst) const;
  40. bool translate(int dx, int dy) {
  41. return this->translate(dx, dy, this);
  42. }
  43. /**
  44. * Allocates a mask the size of the aaclip, and expands its data into
  45. * the mask, using kA8_Format
  46. */
  47. void copyToMask(SkMask*) const;
  48. // called internally
  49. bool quickContains(int left, int top, int right, int bottom) const;
  50. bool quickContains(const SkIRect& r) const {
  51. return this->quickContains(r.fLeft, r.fTop, r.fRight, r.fBottom);
  52. }
  53. const uint8_t* findRow(int y, int* lastYForRow = nullptr) const;
  54. const uint8_t* findX(const uint8_t data[], int x, int* initialCount = nullptr) const;
  55. class Iter;
  56. struct RunHead;
  57. struct YOffset;
  58. class Builder;
  59. #ifdef SK_DEBUG
  60. void validate() const;
  61. void debug(bool compress_y=false) const;
  62. #else
  63. void validate() const {}
  64. void debug(bool compress_y=false) const {}
  65. #endif
  66. private:
  67. SkIRect fBounds;
  68. RunHead* fRunHead;
  69. void freeRuns();
  70. bool trimBounds();
  71. bool trimTopBottom();
  72. bool trimLeftRight();
  73. friend class Builder;
  74. class BuilderBlitter;
  75. friend class BuilderBlitter;
  76. };
  77. ///////////////////////////////////////////////////////////////////////////////
  78. class SkAAClipBlitter : public SkBlitter {
  79. public:
  80. SkAAClipBlitter() : fScanlineScratch(nullptr) {}
  81. ~SkAAClipBlitter() override;
  82. void init(SkBlitter* blitter, const SkAAClip* aaclip) {
  83. SkASSERT(aaclip && !aaclip->isEmpty());
  84. fBlitter = blitter;
  85. fAAClip = aaclip;
  86. fAAClipBounds = aaclip->getBounds();
  87. }
  88. void blitH(int x, int y, int width) override;
  89. void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override;
  90. void blitV(int x, int y, int height, SkAlpha alpha) override;
  91. void blitRect(int x, int y, int width, int height) override;
  92. void blitMask(const SkMask&, const SkIRect& clip) override;
  93. const SkPixmap* justAnOpaqueColor(uint32_t* value) override;
  94. private:
  95. SkBlitter* fBlitter;
  96. const SkAAClip* fAAClip;
  97. SkIRect fAAClipBounds;
  98. // point into fScanlineScratch
  99. int16_t* fRuns;
  100. SkAlpha* fAA;
  101. enum {
  102. kSize = 32 * 32
  103. };
  104. SkAutoSMalloc<kSize> fGrayMaskScratch; // used for blitMask
  105. void* fScanlineScratch; // enough for a mask at 32bit, or runs+aa
  106. void ensureRunsAndAA();
  107. };
  108. #endif