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/SkColor.h" 12 #include "include/core/SkFlattenable.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/private/base/SkAPI.h" 15 16 class SkBlender; 17 class SkColorFilter; 18 class SkColorSpace; 19 class SkImage; 20 class SkMatrix; 21 enum class SkBlendMode; 22 enum class SkTileMode; 23 struct SkRect; 24 struct SkSamplingOptions; 25 26 /** \class SkShader 27 * 28 * Shaders specify the premultiplied source color(s) for what is being drawn. 29 * If a paint has no shader, then the paint's color is used. If the paint has a 30 * shader, then the shader's color(s) are used 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 */ isOpaque()44 virtual bool isOpaque() const { return false; } 45 46 /** 47 * Iff this shader is backed by a single SkImage, return its ptr (the caller must ref this 48 * if they want to keep it longer than the lifetime of the shader). If not, return nullptr. 49 */ 50 SkImage* isAImage(SkMatrix* localMatrix, SkTileMode xy[2]) const; 51 isAImage()52 bool isAImage() const { 53 return this->isAImage(nullptr, (SkTileMode*)nullptr) != nullptr; 54 } 55 56 ////////////////////////////////////////////////////////////////////////// 57 // Methods to create combinations or variants of shaders 58 59 /** 60 * Return a shader that will apply the specified localMatrix to this shader. 61 * The specified matrix will be applied before any matrix associated with this shader. 62 */ 63 sk_sp<SkShader> makeWithLocalMatrix(const SkMatrix&) const; 64 65 /** 66 * Create a new shader that produces the same colors as invoking this shader and then applying 67 * the colorfilter. 68 */ 69 sk_sp<SkShader> makeWithColorFilter(sk_sp<SkColorFilter>) const; 70 71 /** 72 * Return a shader that will compute this shader in a specific color space. 73 * By default, all shaders operate in the destination (surface) color space. 74 * The results of a shader are still always converted to the destination - this 75 * API has no impact on simple shaders or images. Primarily, it impacts shaders 76 * that perform mathematical operations, like Blend shaders, or runtime shaders. 77 */ 78 sk_sp<SkShader> makeWithWorkingColorSpace(sk_sp<SkColorSpace>) const; 79 80 private: 81 SkShader() = default; 82 friend class SkShaderBase; 83 84 using INHERITED = SkFlattenable; 85 }; 86 87 namespace SkShaders { 88 SK_API sk_sp<SkShader> Empty(); 89 SK_API sk_sp<SkShader> Color(SkColor); 90 SK_API sk_sp<SkShader> Color(const SkColor4f&, sk_sp<SkColorSpace>); 91 SK_API sk_sp<SkShader> Blend(SkBlendMode mode, sk_sp<SkShader> dst, sk_sp<SkShader> src); 92 SK_API sk_sp<SkShader> Blend(sk_sp<SkBlender>, sk_sp<SkShader> dst, sk_sp<SkShader> src); 93 SK_API sk_sp<SkShader> CoordClamp(sk_sp<SkShader>, const SkRect& subset); 94 95 /* 96 * Create an SkShader that will sample the 'image'. This is equivalent to SkImage::makeShader. 97 */ 98 SK_API sk_sp<SkShader> Image(sk_sp<SkImage> image, 99 SkTileMode tmx, SkTileMode tmy, 100 const SkSamplingOptions& options, 101 const SkMatrix* localMatrix = nullptr); 102 /* 103 * Create an SkShader that will sample 'image' with minimal processing. This is equivalent to 104 * SkImage::makeRawShader. 105 */ 106 SK_API sk_sp<SkShader> RawImage(sk_sp<SkImage> image, 107 SkTileMode tmx, SkTileMode tmy, 108 const SkSamplingOptions& options, 109 const SkMatrix* localMatrix = nullptr); 110 } 111 112 #endif 113