SkLayerDrawLooper.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 SkLayerDrawLooper_DEFINED
  8. #define SkLayerDrawLooper_DEFINED
  9. #include "SkDrawLooper.h"
  10. #include "SkPaint.h"
  11. #include "SkPoint.h"
  12. #include "SkBlendMode.h"
  13. class SK_API SkLayerDrawLooper : public SkDrawLooper {
  14. public:
  15. ~SkLayerDrawLooper() override;
  16. /**
  17. * Bits specifies which aspects of the layer's paint should replace the
  18. * corresponding aspects on the draw's paint.
  19. * kEntirePaint_Bits means use the layer's paint completely.
  20. * 0 means ignore the layer's paint... except for fColorMode, which is
  21. * always applied.
  22. */
  23. enum Bits {
  24. kStyle_Bit = 1 << 0, //!< use this layer's Style/stroke settings
  25. kTextSkewX_Bit = 1 << 1, //!< use this layer's textskewx
  26. kPathEffect_Bit = 1 << 2, //!< use this layer's patheffect
  27. kMaskFilter_Bit = 1 << 3, //!< use this layer's maskfilter
  28. kShader_Bit = 1 << 4, //!< use this layer's shader
  29. kColorFilter_Bit = 1 << 5, //!< use this layer's colorfilter
  30. kXfermode_Bit = 1 << 6, //!< use this layer's xfermode
  31. /**
  32. * Use the layer's paint entirely, with these exceptions:
  33. * - We never override the draw's paint's text_encoding, since that is
  34. * used to interpret the text/len parameters in draw[Pos]Text.
  35. * - Color is always computed using the LayerInfo's fColorMode.
  36. */
  37. kEntirePaint_Bits = -1
  38. };
  39. typedef int32_t BitFlags;
  40. /**
  41. * Info for how to apply the layer's paint and offset.
  42. *
  43. * fColorMode controls how we compute the final color for the layer:
  44. * The layer's paint's color is treated as the SRC
  45. * The draw's paint's color is treated as the DST
  46. * final-color = Mode(layers-color, draws-color);
  47. * Any SkBlendMode will work. Two common choices are:
  48. * kSrc: to use the layer's color, ignoring the draw's
  49. * kDst: to just keep the draw's color, ignoring the layer's
  50. */
  51. struct SK_API LayerInfo {
  52. BitFlags fPaintBits;
  53. SkBlendMode fColorMode;
  54. SkVector fOffset;
  55. bool fPostTranslate; //!< applies to fOffset
  56. /**
  57. * Initial the LayerInfo. Defaults to settings that will draw the
  58. * layer with no changes: e.g.
  59. * fPaintBits == 0
  60. * fColorMode == kDst_Mode
  61. * fOffset == (0, 0)
  62. */
  63. LayerInfo();
  64. };
  65. SkDrawLooper::Context* makeContext(SkCanvas*, SkArenaAlloc*) const override;
  66. bool asABlurShadow(BlurShadowRec* rec) const override;
  67. protected:
  68. sk_sp<SkDrawLooper> onMakeColorSpace(SkColorSpaceXformer*) const override;
  69. SkLayerDrawLooper();
  70. void flatten(SkWriteBuffer&) const override;
  71. private:
  72. SK_FLATTENABLE_HOOKS(SkLayerDrawLooper)
  73. struct Rec {
  74. Rec* fNext;
  75. SkPaint fPaint;
  76. LayerInfo fInfo;
  77. };
  78. Rec* fRecs;
  79. int fCount;
  80. // state-machine during the init/next cycle
  81. class LayerDrawLooperContext : public SkDrawLooper::Context {
  82. public:
  83. explicit LayerDrawLooperContext(const SkLayerDrawLooper* looper);
  84. protected:
  85. bool next(SkCanvas*, SkPaint* paint) override;
  86. private:
  87. Rec* fCurrRec;
  88. static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&);
  89. };
  90. typedef SkDrawLooper INHERITED;
  91. public:
  92. class SK_API Builder {
  93. public:
  94. Builder();
  95. ~Builder();
  96. /**
  97. * Call for each layer you want to add (from top to bottom).
  98. * This returns a paint you can modify, but that ptr is only valid until
  99. * the next call made to addLayer().
  100. */
  101. SkPaint* addLayer(const LayerInfo&);
  102. /**
  103. * This layer will draw with the original paint, at the specified offset
  104. */
  105. void addLayer(SkScalar dx, SkScalar dy);
  106. /**
  107. * This layer will with the original paint and no offset.
  108. */
  109. void addLayer() { this->addLayer(0, 0); }
  110. /// Similar to addLayer, but adds a layer to the top.
  111. SkPaint* addLayerOnTop(const LayerInfo&);
  112. /**
  113. * Pass list of layers on to newly built looper and return it. This will
  114. * also reset the builder, so it can be used to build another looper.
  115. */
  116. sk_sp<SkDrawLooper> detach();
  117. private:
  118. Rec* fRecs;
  119. Rec* fTopRec;
  120. int fCount;
  121. };
  122. };
  123. #endif