SkShader.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 SkShader_DEFINED
  8. #define SkShader_DEFINED
  9. #include "include/core/SkBlendMode.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFilterQuality.h"
  12. #include "include/core/SkFlattenable.h"
  13. #include "include/core/SkImageInfo.h"
  14. #include "include/core/SkMatrix.h"
  15. #include "include/core/SkTileMode.h"
  16. class SkArenaAlloc;
  17. class SkBitmap;
  18. class SkColorFilter;
  19. class SkColorSpace;
  20. class SkImage;
  21. class SkPath;
  22. class SkPicture;
  23. class SkRasterPipeline;
  24. class GrContext;
  25. class GrFragmentProcessor;
  26. /** \class SkShader
  27. *
  28. * Shaders specify the source color(s) for what is being drawn. If a paint
  29. * has no shader, then the paint's color is used. If the paint has a
  30. * shader, then the shader's color(s) are use instead, but they are
  31. * modulated by the paint's alpha. This makes it easy to create a shader
  32. * once (e.g. bitmap tiling or gradient) and then change its transparency
  33. * w/o having to modify the original shader... only the paint's alpha needs
  34. * to be modified.
  35. */
  36. class SK_API SkShader : public SkFlattenable {
  37. public:
  38. /**
  39. * Returns true if the shader is guaranteed to produce only opaque
  40. * colors, subject to the SkPaint using the shader to apply an opaque
  41. * alpha value. Subclasses should override this to allow some
  42. * optimizations.
  43. */
  44. virtual bool isOpaque() const { return false; }
  45. /**
  46. * Iff this shader is backed by a single SkImage, return its ptr (the caller must ref this
  47. * if they want to keep it longer than the lifetime of the shader). If not, return nullptr.
  48. */
  49. SkImage* isAImage(SkMatrix* localMatrix, SkTileMode xy[2]) const;
  50. bool isAImage() const {
  51. return this->isAImage(nullptr, (SkTileMode*)nullptr) != nullptr;
  52. }
  53. /**
  54. * If the shader subclass can be represented as a gradient, asAGradient
  55. * returns the matching GradientType enum (or kNone_GradientType if it
  56. * cannot). Also, if info is not null, asAGradient populates info with
  57. * the relevant (see below) parameters for the gradient. fColorCount
  58. * is both an input and output parameter. On input, it indicates how
  59. * many entries in fColors and fColorOffsets can be used, if they are
  60. * non-NULL. After asAGradient has run, fColorCount indicates how
  61. * many color-offset pairs there are in the gradient. If there is
  62. * insufficient space to store all of the color-offset pairs, fColors
  63. * and fColorOffsets will not be altered. fColorOffsets specifies
  64. * where on the range of 0 to 1 to transition to the given color.
  65. * The meaning of fPoint and fRadius is dependant on the type of gradient.
  66. *
  67. * None:
  68. * info is ignored.
  69. * Color:
  70. * fColorOffsets[0] is meaningless.
  71. * Linear:
  72. * fPoint[0] and fPoint[1] are the end-points of the gradient
  73. * Radial:
  74. * fPoint[0] and fRadius[0] are the center and radius
  75. * Conical:
  76. * fPoint[0] and fRadius[0] are the center and radius of the 1st circle
  77. * fPoint[1] and fRadius[1] are the center and radius of the 2nd circle
  78. * Sweep:
  79. * fPoint[0] is the center of the sweep.
  80. */
  81. enum GradientType {
  82. kNone_GradientType,
  83. kColor_GradientType,
  84. kLinear_GradientType,
  85. kRadial_GradientType,
  86. kSweep_GradientType,
  87. kConical_GradientType,
  88. kLast_GradientType = kConical_GradientType,
  89. };
  90. struct GradientInfo {
  91. int fColorCount; //!< In-out parameter, specifies passed size
  92. // of fColors/fColorOffsets on input, and
  93. // actual number of colors/offsets on
  94. // output.
  95. SkColor* fColors; //!< The colors in the gradient.
  96. SkScalar* fColorOffsets; //!< The unit offset for color transitions.
  97. SkPoint fPoint[2]; //!< Type specific, see above.
  98. SkScalar fRadius[2]; //!< Type specific, see above.
  99. SkTileMode fTileMode;
  100. uint32_t fGradientFlags; //!< see SkGradientShader::Flags
  101. };
  102. // DEPRECATED. skbug.com/8941
  103. virtual GradientType asAGradient(GradientInfo* info) const;
  104. //////////////////////////////////////////////////////////////////////////
  105. // Methods to create combinations or variants of shaders
  106. /**
  107. * Return a shader that will apply the specified localMatrix to this shader.
  108. * The specified matrix will be applied before any matrix associated with this shader.
  109. */
  110. sk_sp<SkShader> makeWithLocalMatrix(const SkMatrix&) const;
  111. /**
  112. * Create a new shader that produces the same colors as invoking this shader and then applying
  113. * the colorfilter.
  114. */
  115. sk_sp<SkShader> makeWithColorFilter(sk_sp<SkColorFilter>) const;
  116. private:
  117. SkShader() = default;
  118. friend class SkShaderBase;
  119. typedef SkFlattenable INHERITED;
  120. };
  121. class SK_API SkShaders {
  122. public:
  123. static sk_sp<SkShader> Empty();
  124. static sk_sp<SkShader> Color(SkColor);
  125. static sk_sp<SkShader> Color(const SkColor4f&, sk_sp<SkColorSpace>);
  126. static sk_sp<SkShader> Blend(SkBlendMode mode, sk_sp<SkShader> dst, sk_sp<SkShader> src,
  127. const SkMatrix* localMatrix = nullptr);
  128. static sk_sp<SkShader> Lerp(float t, sk_sp<SkShader> dst, sk_sp<SkShader> src,
  129. const SkMatrix* localMatrix = nullptr);
  130. static sk_sp<SkShader> Lerp(sk_sp<SkShader> red, sk_sp<SkShader> dst, sk_sp<SkShader> src,
  131. const SkMatrix* localMatrix = nullptr);
  132. private:
  133. SkShaders() = delete;
  134. };
  135. #endif