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