1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef SHADER_EFFECT_H 17 #define SHADER_EFFECT_H 18 19 #include "impl_interface/shader_effect_impl.h" 20 #include "utils/drawing_macros.h" 21 #include "utils/extend_object.h" 22 23 namespace OHOS { 24 namespace Rosen { 25 namespace Drawing { 26 enum class TileMode { 27 CLAMP, 28 REPEAT, 29 MIRROR, 30 DECAL, 31 }; 32 33 class DRAWING_API ShaderEffect { 34 public: 35 enum class ShaderEffectType { 36 NO_TYPE, 37 COLOR_SHADER, 38 BLEND, 39 IMAGE, 40 PICTURE, 41 LINEAR_GRADIENT, 42 RADIAL_GRADIENT, 43 CONICAL_GRADIENT, 44 SWEEP_GRADIENT, 45 LIGHT_UP, 46 EXTEND_SHADER, 47 }; 48 49 /** 50 * @brief Create a ShaderEffect that ignores the color in the paint, and uses the 51 * specified color. Note: like all shaders, at draw time the paint's alpha 52 * will be respected, and is applied to the specified color. 53 * 54 * @param color the specified color 55 * @return A shared pointer to ShaderEffect 56 */ 57 static std::shared_ptr<ShaderEffect> CreateColorShader(ColorQuad color); 58 59 static std::shared_ptr<ShaderEffect> CreateColorSpaceShader(const Color4f& color, 60 std::shared_ptr<ColorSpace> colorSpace); 61 62 static std::shared_ptr<ShaderEffect> CreateBlendShader(ShaderEffect& dst, ShaderEffect& src, BlendMode mode); 63 64 static std::shared_ptr<ShaderEffect> CreateImageShader( 65 const Image& image, TileMode tileX, TileMode tileY, const SamplingOptions& sampling, const Matrix& matrix); 66 67 static std::shared_ptr<ShaderEffect> CreatePictureShader(const Picture& picture, TileMode tileX, TileMode tileY, 68 FilterMode mode, const Matrix& matrix, const Rect& rect); 69 70 static std::shared_ptr<ShaderEffect> CreateLinearGradient(const Point& startPt, const Point& endPt, 71 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, 72 const Matrix *matrix = nullptr); 73 74 static std::shared_ptr<ShaderEffect> CreateRadialGradient(const Point& centerPt, scalar radius, 75 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, 76 const Matrix *matrix = nullptr); 77 78 static std::shared_ptr<ShaderEffect> CreateTwoPointConical(const Point& startPt, scalar startRadius, 79 const Point& endPt, scalar endRadius, const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, 80 TileMode mode, const Matrix *matrix = nullptr); 81 82 static std::shared_ptr<ShaderEffect> CreateSweepGradient(const Point& centerPt, 83 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, scalar startAngle, 84 scalar endAngle, const Matrix* matrix = nullptr); 85 86 static std::shared_ptr<ShaderEffect> CreateExtendShader(std::shared_ptr<ExtendObject>); 87 88 virtual ~ShaderEffect() = default; 89 90 ShaderEffectType GetType() const; 91 GetDrawingType()92 virtual DrawingType GetDrawingType() const 93 { 94 return DrawingType::COMMON; 95 } 96 97 template<typename T> GetImpl()98 T* GetImpl() const 99 { 100 return impl_->DowncastingTo<T>(); 101 } 102 103 /* ColorShader */ 104 ShaderEffect(ShaderEffectType t, ColorQuad color) noexcept; 105 106 /* ColorShader With ColorSpace */ 107 ShaderEffect(ShaderEffectType t, const Color4f& color, std::shared_ptr<ColorSpace> colorSpace) noexcept; 108 109 /* BlendShader */ 110 ShaderEffect(ShaderEffectType t, ShaderEffect& dst, ShaderEffect& src, BlendMode mode) noexcept; 111 112 /* ImageShader */ 113 ShaderEffect(ShaderEffectType t, const Image& image, TileMode tileX, TileMode tileY, 114 const SamplingOptions& sampling, const Matrix& matrix) noexcept; 115 116 /* PictureShader */ 117 ShaderEffect(ShaderEffectType t, const Picture& picture, TileMode tileX, TileMode tileY, FilterMode mode, 118 const Matrix& matrix, const Rect& rect) noexcept; 119 120 /* LinearGradient */ 121 ShaderEffect(ShaderEffectType t, const Point& startPt, const Point& endPt, const std::vector<ColorQuad>& colors, 122 const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix = nullptr) noexcept; 123 124 /* RadialGradient */ 125 ShaderEffect(ShaderEffectType t, const Point& centerPt, scalar radius, const std::vector<ColorQuad>& colors, 126 const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix = nullptr) noexcept; 127 128 /* TwoPointConicalGradient */ 129 ShaderEffect(ShaderEffectType t, const Point& startPt, scalar startRadius, const Point& endPt, scalar endRadius, 130 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, 131 const Matrix *matrix = nullptr) noexcept; 132 133 /* SweepGradient */ 134 ShaderEffect(ShaderEffectType t, const Point& centerPt, const std::vector<ColorQuad>& colors, 135 const std::vector<scalar>& pos, TileMode mode, scalar startAngle, scalar endAngle, 136 const Matrix *matrix = nullptr) noexcept; 137 138 /* LightUpShader */ 139 ShaderEffect(ShaderEffectType t, const float& lightUpDeg, ShaderEffect& imageShader) noexcept; 140 141 /* ExtendShader */ 142 ShaderEffect(ShaderEffectType t, std::shared_ptr<ExtendObject> object) noexcept; 143 144 ShaderEffect(ShaderEffectType t) noexcept; 145 146 ShaderEffect() noexcept; 147 GetExtendObject()148 std::shared_ptr<ExtendObject> GetExtendObject() { return object_; } 149 150 std::shared_ptr<Data> Serialize() const; 151 bool Deserialize(std::shared_ptr<Data> data); 152 private: 153 ShaderEffectType type_; 154 std::shared_ptr<ShaderEffectImpl> impl_; 155 std::shared_ptr<ExtendObject> object_; 156 }; 157 } // namespace Drawing 158 } // namespace Rosen 159 } // namespace OHOS 160 #endif 161