SkDrawLooper.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2011 The Android Open Source Project
  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 SkDrawLooper_DEFINED
  8. #define SkDrawLooper_DEFINED
  9. #include "include/core/SkBlurTypes.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFlattenable.h"
  12. #include "include/core/SkPoint.h"
  13. class SkArenaAlloc;
  14. class SkCanvas;
  15. class SkPaint;
  16. struct SkRect;
  17. class SkString;
  18. /** \class SkDrawLooper
  19. Subclasses of SkDrawLooper can be attached to a SkPaint. Where they are,
  20. and something is drawn to a canvas with that paint, the looper subclass will
  21. be called, allowing it to modify the canvas and/or paint for that draw call.
  22. More than that, via the next() method, the looper can modify the draw to be
  23. invoked multiple times (hence the name loop-er), allow it to perform effects
  24. like shadows or frame/fills, that require more than one pass.
  25. */
  26. class SK_API SkDrawLooper : public SkFlattenable {
  27. public:
  28. /**
  29. * Holds state during a draw. Users call next() until it returns false.
  30. *
  31. * Subclasses of SkDrawLooper should create a subclass of this object to
  32. * hold state specific to their subclass.
  33. */
  34. class SK_API Context {
  35. public:
  36. Context() {}
  37. virtual ~Context() {}
  38. /**
  39. * Called in a loop on objects returned by SkDrawLooper::createContext().
  40. * Each time true is returned, the object is drawn (possibly with a modified
  41. * canvas and/or paint). When false is finally returned, drawing for the object
  42. * stops.
  43. *
  44. * On each call, the paint will be in its original state, but the
  45. * canvas will be as it was following the previous call to next() or
  46. * createContext().
  47. *
  48. * The implementation must ensure that, when next() finally returns
  49. * false, the canvas has been restored to the state it was
  50. * initially, before createContext() was first called.
  51. */
  52. virtual bool next(SkCanvas* canvas, SkPaint* paint) = 0;
  53. private:
  54. Context(const Context&) = delete;
  55. Context& operator=(const Context&) = delete;
  56. };
  57. /**
  58. * Called right before something is being drawn. Returns a Context
  59. * whose next() method should be called until it returns false.
  60. */
  61. virtual Context* makeContext(SkCanvas*, SkArenaAlloc*) const = 0;
  62. /**
  63. * The fast bounds functions are used to enable the paint to be culled early
  64. * in the drawing pipeline. If a subclass can support this feature it must
  65. * return true for the canComputeFastBounds() function. If that function
  66. * returns false then computeFastBounds behavior is undefined otherwise it
  67. * is expected to have the following behavior. Given the parent paint and
  68. * the parent's bounding rect the subclass must fill in and return the
  69. * storage rect, where the storage rect is with the union of the src rect
  70. * and the looper's bounding rect.
  71. */
  72. bool canComputeFastBounds(const SkPaint& paint) const;
  73. void computeFastBounds(const SkPaint& paint, const SkRect& src, SkRect* dst) const;
  74. struct BlurShadowRec {
  75. SkScalar fSigma;
  76. SkVector fOffset;
  77. SkColor fColor;
  78. SkBlurStyle fStyle;
  79. };
  80. /**
  81. * If this looper can be interpreted as having two layers, such that
  82. * 1. The first layer (bottom most) just has a blur and translate
  83. * 2. The second layer has no modifications to either paint or canvas
  84. * 3. No other layers.
  85. * then return true, and if not null, fill out the BlurShadowRec).
  86. *
  87. * If any of the above are not met, return false and ignore the BlurShadowRec parameter.
  88. */
  89. virtual bool asABlurShadow(BlurShadowRec*) const;
  90. static SkFlattenable::Type GetFlattenableType() {
  91. return kSkDrawLooper_Type;
  92. }
  93. SkFlattenable::Type getFlattenableType() const override {
  94. return kSkDrawLooper_Type;
  95. }
  96. static sk_sp<SkDrawLooper> Deserialize(const void* data, size_t size,
  97. const SkDeserialProcs* procs = nullptr) {
  98. return sk_sp<SkDrawLooper>(static_cast<SkDrawLooper*>(
  99. SkFlattenable::Deserialize(
  100. kSkDrawLooper_Type, data, size, procs).release()));
  101. }
  102. void apply(SkCanvas* canvas, const SkPaint& paint,
  103. std::function<void(SkCanvas*, const SkPaint&)>);
  104. protected:
  105. SkDrawLooper() {}
  106. private:
  107. typedef SkFlattenable INHERITED;
  108. };
  109. #endif