SkDrawLooper.h 4.6 KB

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