1 /* 2 * Copyright (c) 2021-2025 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 #include "draw/sdf_shaper_base.h" 23 24 #ifdef ROSEN_OHOS 25 #include <parcel.h> 26 #endif 27 28 namespace OHOS { 29 namespace Rosen { 30 namespace Drawing { 31 enum class TileMode { 32 CLAMP, 33 REPEAT, 34 MIRROR, 35 DECAL, 36 }; 37 38 class DRAWING_API ShaderEffect { 39 public: 40 enum class ShaderEffectType : int32_t { 41 NO_TYPE, 42 COLOR_SHADER, 43 BLEND, 44 IMAGE, 45 PICTURE, 46 LINEAR_GRADIENT, 47 RADIAL_GRADIENT, 48 CONICAL_GRADIENT, 49 SWEEP_GRADIENT, 50 LIGHT_UP, 51 EXTEND_SHADER, 52 SDF_SHADER, 53 LAZY_SHADER 54 }; 55 56 /** 57 * @brief Create a ShaderEffect that ignores the color in the paint, and uses the 58 * specified color. Note: like all shaders, at draw time the paint's alpha 59 * will be respected, and is applied to the specified color. 60 * 61 * @param color the specified color 62 * @return A shared pointer to ShaderEffect 63 */ 64 static std::shared_ptr<ShaderEffect> CreateColorShader(ColorQuad color); 65 66 static std::shared_ptr<ShaderEffect> CreateColorSpaceShader(const Color4f& color, 67 std::shared_ptr<ColorSpace> colorSpace); 68 69 static std::shared_ptr<ShaderEffect> CreateBlendShader(ShaderEffect& dst, ShaderEffect& src, BlendMode mode); 70 71 static std::shared_ptr<ShaderEffect> CreateImageShader( 72 const Image& image, TileMode tileX, TileMode tileY, const SamplingOptions& sampling, const Matrix& matrix); 73 74 static std::shared_ptr<ShaderEffect> CreatePictureShader(const Picture& picture, TileMode tileX, TileMode tileY, 75 FilterMode mode, const Matrix& matrix, const Rect& rect); 76 77 static std::shared_ptr<ShaderEffect> CreateLinearGradient(const Point& startPt, const Point& endPt, 78 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, 79 const Matrix *matrix = nullptr); 80 81 static std::shared_ptr<ShaderEffect> CreateLinearGradient(const Point& startPt, const Point& endPt, 82 const std::vector<Color4f>& colors, std::shared_ptr<ColorSpace> colorSpace, 83 const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix = nullptr); 84 85 static std::shared_ptr<ShaderEffect> CreateRadialGradient(const Point& centerPt, scalar radius, 86 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, 87 const Matrix *matrix = nullptr); 88 89 static std::shared_ptr<ShaderEffect> CreateRadialGradient(const Point& centerPt, scalar radius, 90 const std::vector<Color4f>& colors, std::shared_ptr<ColorSpace> colorSpace, const std::vector<scalar>& pos, 91 TileMode mode, const Matrix *matrix = nullptr); 92 93 static std::shared_ptr<ShaderEffect> CreateTwoPointConical(const Point& startPt, scalar startRadius, 94 const Point& endPt, scalar endRadius, const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, 95 TileMode mode, const Matrix *matrix = nullptr); 96 97 static std::shared_ptr<ShaderEffect> CreateTwoPointConical(const Point& startPt, scalar startRadius, 98 const Point& endPt, scalar endRadius, const std::vector<Color4f>& colors, std::shared_ptr<ColorSpace> colorSpace, 99 const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix = nullptr); 100 101 static std::shared_ptr<ShaderEffect> CreateSweepGradient(const Point& centerPt, 102 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, scalar startAngle, 103 scalar endAngle, const Matrix* matrix = nullptr); 104 105 static std::shared_ptr<ShaderEffect> CreateSweepGradient(const Point& centerPt, 106 const std::vector<Color4f>& colors, std::shared_ptr<ColorSpace> colorSpace,const std::vector<scalar>& pos, 107 TileMode mode, scalar startAngle, scalar endAngle, const Matrix* matrix = nullptr); 108 109 static std::shared_ptr<ShaderEffect> CreateLightUp(const float& lightUpDeg, ShaderEffect& imageShader); 110 111 static std::shared_ptr<ShaderEffect> CreateExtendShader(std::shared_ptr<ExtendObject>); 112 113 static std::shared_ptr<ShaderEffect> CreateSdfShader(const SDFShapeBase& shape); 114 115 virtual ~ShaderEffect() = default; 116 117 ShaderEffectType GetType() const; IsLazy()118 virtual bool IsLazy() const { return false; }; 119 120 #ifdef ROSEN_OHOS 121 virtual bool Marshalling(Parcel& parcel); 122 static std::shared_ptr<ShaderEffect> Unmarshalling(Parcel& parcel, bool& isValid); 123 #endif 124 GetDrawingType()125 virtual DrawingType GetDrawingType() const 126 { 127 return DrawingType::COMMON; 128 } 129 130 template<typename T> GetImpl()131 T* GetImpl() const 132 { 133 return impl_->DowncastingTo<T>(); 134 } 135 136 /* ColorShader */ 137 ShaderEffect(ShaderEffectType t, ColorQuad color) noexcept; 138 139 /* ColorShader With ColorSpace */ 140 ShaderEffect(ShaderEffectType t, const Color4f& color, std::shared_ptr<ColorSpace> colorSpace) noexcept; 141 142 /* BlendShader */ 143 ShaderEffect(ShaderEffectType t, ShaderEffect& dst, ShaderEffect& src, BlendMode mode) noexcept; 144 145 /* ImageShader */ 146 ShaderEffect(ShaderEffectType t, const Image& image, TileMode tileX, TileMode tileY, 147 const SamplingOptions& sampling, const Matrix& matrix) noexcept; 148 149 /* PictureShader */ 150 ShaderEffect(ShaderEffectType t, const Picture& picture, TileMode tileX, TileMode tileY, FilterMode mode, 151 const Matrix& matrix, const Rect& rect) noexcept; 152 153 /* LinearGradient */ 154 ShaderEffect(ShaderEffectType t, const Point& startPt, const Point& endPt, const std::vector<ColorQuad>& colors, 155 const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix = nullptr) noexcept; 156 157 /* LinearGradient */ 158 ShaderEffect(ShaderEffectType t, const Point& startPt, const Point& endPt, const std::vector<Color4f>& colors, 159 std::shared_ptr<ColorSpace> colorSpace, const std::vector<scalar>& pos, TileMode mode, 160 const Matrix *matrix = nullptr) noexcept; 161 162 /* RadialGradient */ 163 ShaderEffect(ShaderEffectType t, const Point& centerPt, scalar radius, const std::vector<ColorQuad>& colors, 164 const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix = nullptr) noexcept; 165 166 /* RadialGradient */ 167 ShaderEffect(ShaderEffectType t, const Point& centerPt, scalar radius, const std::vector<Color4f>& colors, 168 std::shared_ptr<ColorSpace> colorSpace, const std::vector<scalar>& pos, TileMode mode, 169 const Matrix *matrix = nullptr) noexcept; 170 171 /* TwoPointConicalGradient */ 172 ShaderEffect(ShaderEffectType t, const Point& startPt, scalar startRadius, const Point& endPt, scalar endRadius, 173 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, 174 const Matrix *matrix = nullptr) noexcept; 175 176 /* TwoPointConicalGradient */ 177 ShaderEffect(ShaderEffectType t, const Point& startPt, scalar startRadius, const Point& endPt, scalar endRadius, 178 const std::vector<Color4f>& colors, std::shared_ptr<ColorSpace> colorSpace, const std::vector<scalar>& pos, 179 TileMode mode, const Matrix *matrix = nullptr) noexcept; 180 181 /* SweepGradient */ 182 ShaderEffect(ShaderEffectType t, const Point& centerPt, const std::vector<ColorQuad>& colors, 183 const std::vector<scalar>& pos, TileMode mode, scalar startAngle, scalar endAngle, 184 const Matrix *matrix = nullptr) noexcept; 185 186 /* SweepGradient */ 187 ShaderEffect(ShaderEffectType t, const Point& centerPt, const std::vector<Color4f>& colors, 188 std::shared_ptr<ColorSpace> colorSpace, const std::vector<scalar>& pos, TileMode mode, 189 scalar startAngle, scalar endAngle, const Matrix *matrix = nullptr) noexcept; 190 191 /* LightUpShader */ 192 ShaderEffect(ShaderEffectType t, const float& lightUpDeg, ShaderEffect& imageShader) noexcept; 193 194 /* ExtendShader */ 195 ShaderEffect(ShaderEffectType t, std::shared_ptr<ExtendObject> object) noexcept; 196 197 ShaderEffect(ShaderEffectType t) noexcept; 198 199 /* SdfShader*/ 200 ShaderEffect(ShaderEffectType t, const SDFShapeBase& shape) noexcept; 201 202 ShaderEffect() noexcept; 203 GetExtendObject()204 std::shared_ptr<ExtendObject> GetExtendObject() { return object_; } 205 206 #ifdef RS_ENABLE_GPU 207 void SetGPUContext(std::shared_ptr<GPUContext> gpuContext) const; 208 #endif 209 210 virtual std::shared_ptr<Data> Serialize() const; 211 virtual bool Deserialize(std::shared_ptr<Data> data); 212 protected: 213 ShaderEffectType type_; 214 private: 215 std::shared_ptr<ShaderEffectImpl> impl_; 216 std::shared_ptr<ExtendObject> object_; 217 }; 218 } // namespace Drawing 219 } // namespace Rosen 220 } // namespace OHOS 221 #endif 222