SkShaderBase.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2017 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 SkShaderBase_DEFINED
  8. #define SkShaderBase_DEFINED
  9. #include "include/core/SkFilterQuality.h"
  10. #include "include/core/SkMatrix.h"
  11. #include "include/core/SkShader.h"
  12. #include "include/private/SkNoncopyable.h"
  13. #include "src/core/SkEffectPriv.h"
  14. #include "src/core/SkMask.h"
  15. #include "src/core/SkTLazy.h"
  16. #if SK_SUPPORT_GPU
  17. #include "src/gpu/GrFPArgs.h"
  18. #endif
  19. class GrContext;
  20. class GrFragmentProcessor;
  21. class SkArenaAlloc;
  22. class SkColorSpace;
  23. class SkImage;
  24. struct SkImageInfo;
  25. class SkPaint;
  26. class SkRasterPipeline;
  27. class SkShaderBase : public SkShader {
  28. public:
  29. ~SkShaderBase() override;
  30. /**
  31. * Returns true if the shader is guaranteed to produce only a single color.
  32. * Subclasses can override this to allow loop-hoisting optimization.
  33. */
  34. virtual bool isConstant() const { return false; }
  35. const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
  36. enum Flags {
  37. //!< set if all of the colors will be opaque
  38. kOpaqueAlpha_Flag = 1 << 0,
  39. /** set if the spans only vary in X (const in Y).
  40. e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
  41. that varies from left-to-right. This flag specifies this for
  42. shadeSpan().
  43. */
  44. kConstInY32_Flag = 1 << 1,
  45. /** hint for the blitter that 4f is the preferred shading mode.
  46. */
  47. kPrefers4f_Flag = 1 << 2,
  48. };
  49. /**
  50. * ContextRec acts as a parameter bundle for creating Contexts.
  51. */
  52. struct ContextRec {
  53. ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM,
  54. SkColorType dstColorType, SkColorSpace* dstColorSpace)
  55. : fPaint(&paint)
  56. , fMatrix(&matrix)
  57. , fLocalMatrix(localM)
  58. , fDstColorType(dstColorType)
  59. , fDstColorSpace(dstColorSpace) {}
  60. const SkPaint* fPaint; // the current paint associated with the draw
  61. const SkMatrix* fMatrix; // the current matrix in the canvas
  62. const SkMatrix* fLocalMatrix; // optional local matrix
  63. SkColorType fDstColorType; // the color type of the dest surface
  64. SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any)
  65. bool isLegacyCompatible(SkColorSpace* shadersColorSpace) const;
  66. };
  67. class Context : public ::SkNoncopyable {
  68. public:
  69. Context(const SkShaderBase& shader, const ContextRec&);
  70. virtual ~Context();
  71. /**
  72. * Called sometimes before drawing with this shader. Return the type of
  73. * alpha your shader will return. The default implementation returns 0.
  74. * Your subclass should override if it can (even sometimes) report a
  75. * non-zero value, since that will enable various blitters to perform
  76. * faster.
  77. */
  78. virtual uint32_t getFlags() const { return 0; }
  79. /**
  80. * Called for each span of the object being drawn. Your subclass should
  81. * set the appropriate colors (with premultiplied alpha) that correspond
  82. * to the specified device coordinates.
  83. */
  84. virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
  85. protected:
  86. // Reference to shader, so we don't have to dupe information.
  87. const SkShaderBase& fShader;
  88. uint8_t getPaintAlpha() const { return fPaintAlpha; }
  89. const SkMatrix& getTotalInverse() const { return fTotalInverse; }
  90. const SkMatrix& getCTM() const { return fCTM; }
  91. private:
  92. SkMatrix fCTM;
  93. SkMatrix fTotalInverse;
  94. uint8_t fPaintAlpha;
  95. typedef SkNoncopyable INHERITED;
  96. };
  97. /**
  98. * Make a context using the memory provided by the arena.
  99. *
  100. * @return pointer to context or nullptr if can't be created
  101. */
  102. Context* makeContext(const ContextRec&, SkArenaAlloc*) const;
  103. #if SK_SUPPORT_GPU
  104. /**
  105. * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is
  106. * returned if there is no GPU implementation.
  107. *
  108. * The GPU device does not call SkShader::createContext(), instead we pass the view matrix,
  109. * local matrix, and filter quality directly.
  110. *
  111. * The GrContext may be used by the to create textures that are required by the returned
  112. * processor.
  113. *
  114. * The returned GrFragmentProcessor should expect an unpremultiplied input color and
  115. * produce a premultiplied output.
  116. */
  117. virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const;
  118. #endif
  119. /**
  120. * If the shader can represent its "average" luminance in a single color, return true and
  121. * if color is not NULL, return that color. If it cannot, return false and ignore the color
  122. * parameter.
  123. *
  124. * Note: if this returns true, the returned color will always be opaque, as only the RGB
  125. * components are used to compute luminance.
  126. */
  127. bool asLuminanceColor(SkColor*) const;
  128. // If this returns false, then we draw nothing (do not fall back to shader context)
  129. bool appendStages(const SkStageRec&) const;
  130. bool SK_WARN_UNUSED_RESULT computeTotalInverse(const SkMatrix& ctm,
  131. const SkMatrix* outerLocalMatrix,
  132. SkMatrix* totalInverse) const;
  133. // Returns the total local matrix for this shader:
  134. //
  135. // M = postLocalMatrix x shaderLocalMatrix x preLocalMatrix
  136. //
  137. SkTCopyOnFirstWrite<SkMatrix> totalLocalMatrix(const SkMatrix* preLocalMatrix,
  138. const SkMatrix* postLocalMatrix = nullptr) const;
  139. virtual SkImage* onIsAImage(SkMatrix*, SkTileMode[2]) const {
  140. return nullptr;
  141. }
  142. virtual SkPicture* isAPicture(SkMatrix*, SkTileMode[2], SkRect* tile) const { return nullptr; }
  143. static Type GetFlattenableType() { return kSkShaderBase_Type; }
  144. Type getFlattenableType() const override { return GetFlattenableType(); }
  145. static sk_sp<SkShaderBase> Deserialize(const void* data, size_t size,
  146. const SkDeserialProcs* procs = nullptr) {
  147. return sk_sp<SkShaderBase>(static_cast<SkShaderBase*>(
  148. SkFlattenable::Deserialize(GetFlattenableType(), data, size, procs).release()));
  149. }
  150. static void RegisterFlattenables();
  151. /** DEPRECATED. skbug.com/8941
  152. * If this shader can be represented by another shader + a localMatrix, return that shader and
  153. * the localMatrix. If not, return nullptr and ignore the localMatrix parameter.
  154. */
  155. virtual sk_sp<SkShader> makeAsALocalMatrixShader(SkMatrix* localMatrix) const;
  156. protected:
  157. SkShaderBase(const SkMatrix* localMatrix = nullptr);
  158. void flatten(SkWriteBuffer&) const override;
  159. #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
  160. /**
  161. * Specialize creating a SkShader context using the supplied allocator.
  162. * @return pointer to context owned by the arena allocator.
  163. */
  164. virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const {
  165. return nullptr;
  166. }
  167. #endif
  168. virtual bool onAsLuminanceColor(SkColor*) const {
  169. return false;
  170. }
  171. // Default impl creates shadercontext and calls that (not very efficient)
  172. virtual bool onAppendStages(const SkStageRec&) const;
  173. private:
  174. // This is essentially const, but not officially so it can be modified in constructors.
  175. SkMatrix fLocalMatrix;
  176. typedef SkShader INHERITED;
  177. };
  178. inline SkShaderBase* as_SB(SkShader* shader) {
  179. return static_cast<SkShaderBase*>(shader);
  180. }
  181. inline const SkShaderBase* as_SB(const SkShader* shader) {
  182. return static_cast<const SkShaderBase*>(shader);
  183. }
  184. inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) {
  185. return static_cast<SkShaderBase*>(shader.get());
  186. }
  187. #endif // SkShaderBase_DEFINED