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/SkFlattenable.h" 14 #include "include/core/SkImageInfo.h" 15 #include "include/core/SkMatrix.h" 16 #include "include/core/SkTileMode.h" 17 18 class SkArenaAlloc; 19 class SkBitmap; 20 class SkBlender; 21 class SkColorFilter; 22 class SkColorSpace; 23 class SkImage; 24 class SkPath; 25 class SkPicture; 26 class SkRasterPipeline; 27 class GrFragmentProcessor; 28 29 /** \class SkShader 30 * 31 * Shaders specify the source color(s) for what is being drawn. If a paint 32 * has no shader, then the paint's color is used. If the paint has a 33 * shader, then the shader's color(s) are use instead, but they are 34 * modulated by the paint's alpha. This makes it easy to create a shader 35 * once (e.g. bitmap tiling or gradient) and then change its transparency 36 * w/o having to modify the original shader... only the paint's alpha needs 37 * to be modified. 38 */ 39 class SK_API SkShader : public SkFlattenable { 40 public: 41 /** 42 * Returns true if the shader is guaranteed to produce only opaque 43 * colors, subject to the SkPaint using the shader to apply an opaque 44 * alpha value. Subclasses should override this to allow some 45 * optimizations. 46 */ isOpaque()47 virtual bool isOpaque() const { return false; } 48 49 /** 50 * Iff this shader is backed by a single SkImage, return its ptr (the caller must ref this 51 * if they want to keep it longer than the lifetime of the shader). If not, return nullptr. 52 */ 53 SkImage* isAImage(SkMatrix* localMatrix, SkTileMode xy[2]) const; 54 isAImage()55 bool isAImage() const { 56 return this->isAImage(nullptr, (SkTileMode*)nullptr) != nullptr; 57 } 58 59 ////////////////////////////////////////////////////////////////////////// 60 // Methods to create combinations or variants of shaders 61 62 /** 63 * Return a shader that will apply the specified localMatrix to this shader. 64 * The specified matrix will be applied before any matrix associated with this shader. 65 */ 66 sk_sp<SkShader> makeWithLocalMatrix(const SkMatrix&) const; 67 68 /** 69 * Create a new shader that produces the same colors as invoking this shader and then applying 70 * the colorfilter. 71 */ 72 sk_sp<SkShader> makeWithColorFilter(sk_sp<SkColorFilter>) const; 73 74 private: 75 SkShader() = default; 76 friend class SkShaderBase; 77 78 using INHERITED = SkFlattenable; 79 }; 80 81 class SK_API SkShaders { 82 public: 83 static sk_sp<SkShader> Empty(); 84 static sk_sp<SkShader> Color(SkColor); 85 static sk_sp<SkShader> Color(const SkColor4f&, sk_sp<SkColorSpace>); 86 static sk_sp<SkShader> Blend(SkBlendMode mode, sk_sp<SkShader> dst, sk_sp<SkShader> src); 87 static sk_sp<SkShader> Blend(sk_sp<SkBlender>, sk_sp<SkShader> dst, sk_sp<SkShader> src); 88 static sk_sp<SkShader> CoordClamp(sk_sp<SkShader>, const SkRect& subset); 89 private: 90 SkShaders() = delete; 91 }; 92 93 #endif 94