SkColorFilter.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2006 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 SkColorFilter_DEFINED
  8. #define SkColorFilter_DEFINED
  9. #include "include/core/SkBlendMode.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFlattenable.h"
  12. #include "include/core/SkRefCnt.h"
  13. class GrColorSpaceInfo;
  14. class GrFragmentProcessor;
  15. class GrRecordingContext;
  16. class SkBitmap;
  17. class SkColorMatrix;
  18. class SkColorSpace;
  19. struct SkStageRec;
  20. class SkString;
  21. /**
  22. * ColorFilters are optional objects in the drawing pipeline. When present in
  23. * a paint, they are called with the "src" colors, and return new colors, which
  24. * are then passed onto the next stage (either ImageFilter or Xfermode).
  25. *
  26. * All subclasses are required to be reentrant-safe : it must be legal to share
  27. * the same instance between several threads.
  28. */
  29. class SK_API SkColorFilter : public SkFlattenable {
  30. public:
  31. // DEPRECATED. skbug.com/8941
  32. bool asColorMode(SkColor* color, SkBlendMode* mode) const {
  33. return this->onAsAColorMode(color, mode);
  34. }
  35. /** If the filter can be represented by a source color plus Mode, this
  36. * returns true, and sets (if not NULL) the color and mode appropriately.
  37. * If not, this returns false and ignores the parameters.
  38. */
  39. bool asAColorMode(SkColor* color, SkBlendMode* mode) const {
  40. return this->onAsAColorMode(color, mode);
  41. }
  42. /** If the filter can be represented by a 5x4 matrix, this
  43. * returns true, and sets the matrix appropriately.
  44. * If not, this returns false and ignores the parameter.
  45. */
  46. bool asAColorMatrix(float matrix[20]) const {
  47. return this->onAsAColorMatrix(matrix);
  48. }
  49. bool appendStages(const SkStageRec& rec, bool shaderIsOpaque) const;
  50. enum Flags {
  51. /** If set the filter methods will not change the alpha channel of the colors.
  52. */
  53. kAlphaUnchanged_Flag = 1 << 0,
  54. };
  55. /** Returns the flags for this filter. Override in subclasses to return custom flags.
  56. */
  57. virtual uint32_t getFlags() const { return 0; }
  58. SkColor filterColor(SkColor) const;
  59. SkColor4f filterColor4f(const SkColor4f&, SkColorSpace*) const;
  60. /** Construct a colorfilter whose effect is to first apply the inner filter and then apply
  61. * this filter, applied to the output of the inner filter.
  62. *
  63. * result = this(inner(...))
  64. *
  65. * Due to internal limits, it is possible that this will return NULL, so the caller must
  66. * always check.
  67. */
  68. sk_sp<SkColorFilter> makeComposed(sk_sp<SkColorFilter> inner) const;
  69. #if SK_SUPPORT_GPU
  70. /**
  71. * A subclass may implement this factory function to work with the GPU backend. It returns
  72. * a GrFragmentProcessor that implemets the color filter in GPU shader code.
  73. *
  74. * The fragment processor receives a premultiplied input color and produces a premultiplied
  75. * output color.
  76. *
  77. * A null return indicates that the color filter isn't implemented for the GPU backend.
  78. */
  79. virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(
  80. GrRecordingContext*, const GrColorSpaceInfo& dstColorSpaceInfo) const;
  81. #endif
  82. bool affectsTransparentBlack() const {
  83. return this->filterColor(SK_ColorTRANSPARENT) != SK_ColorTRANSPARENT;
  84. }
  85. static void RegisterFlattenables();
  86. static SkFlattenable::Type GetFlattenableType() {
  87. return kSkColorFilter_Type;
  88. }
  89. SkFlattenable::Type getFlattenableType() const override {
  90. return kSkColorFilter_Type;
  91. }
  92. static sk_sp<SkColorFilter> Deserialize(const void* data, size_t size,
  93. const SkDeserialProcs* procs = nullptr) {
  94. return sk_sp<SkColorFilter>(static_cast<SkColorFilter*>(
  95. SkFlattenable::Deserialize(
  96. kSkColorFilter_Type, data, size, procs).release()));
  97. }
  98. protected:
  99. SkColorFilter() {}
  100. virtual bool onAsAColorMatrix(float[20]) const;
  101. virtual bool onAsAColorMode(SkColor* color, SkBlendMode* bmode) const;
  102. private:
  103. /*
  104. * Returns 1 if this is a single filter (not a composition of other filters), otherwise it
  105. * reutrns the number of leaf-node filters in a composition. This should be the same value
  106. * as the number of GrFragmentProcessors returned by asFragmentProcessors's array parameter.
  107. *
  108. * e.g. compose(filter, compose(compose(filter, filter), filter)) --> 4
  109. */
  110. virtual int privateComposedFilterCount() const { return 1; }
  111. virtual bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const = 0;
  112. friend class SkComposeColorFilter;
  113. typedef SkFlattenable INHERITED;
  114. };
  115. class SK_API SkColorFilters {
  116. public:
  117. static sk_sp<SkColorFilter> Compose(sk_sp<SkColorFilter> outer, sk_sp<SkColorFilter> inner) {
  118. return outer ? outer->makeComposed(inner) : inner;
  119. }
  120. static sk_sp<SkColorFilter> Blend(SkColor c, SkBlendMode mode);
  121. static sk_sp<SkColorFilter> Matrix(const SkColorMatrix&);
  122. static sk_sp<SkColorFilter> Matrix(const float rowMajor[20]);
  123. static sk_sp<SkColorFilter> LinearToSRGBGamma();
  124. static sk_sp<SkColorFilter> SRGBToLinearGamma();
  125. static sk_sp<SkColorFilter> Lerp(float t, sk_sp<SkColorFilter> dst, sk_sp<SkColorFilter> src);
  126. private:
  127. SkColorFilters() = delete;
  128. };
  129. #endif