• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "drawing/engine_adapter/impl_interface/shader_effect_impl.h"
20 #include "include/core/SkShader.h"
21 #include "utils/drawing_macros.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_EFFECT,
38         BLEND,
39         IMAGE,
40         PICTURE,
41         LINEAR_GRADIENT,
42         RADIAL_GRADIENT,
43         CONICAL_GRADIENT,
44         SWEEP_GRADIENT,
45     };
46 
47     static std::shared_ptr<ShaderEffect> CreateColorShader(ColorQuad color);
48     static std::shared_ptr<ShaderEffect> CreateBlendShader(ShaderEffect& dst, ShaderEffect& src, BlendMode mode);
49     static std::shared_ptr<ShaderEffect> CreateImageShader(
50         const Image& image, TileMode tileX, TileMode tileY, const SamplingOptions& sampling, const Matrix& matrix);
51     static std::shared_ptr<ShaderEffect> CreatePictureShader(const Picture& picture, TileMode tileX, TileMode tileY,
52         FilterMode mode, const Matrix& matrix, const Rect& rect);
53     static std::shared_ptr<ShaderEffect> CreateLinearGradient(const Point& startPt, const Point& endPt,
54         const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode);
55     static std::shared_ptr<ShaderEffect> CreateRadialGradient(const Point& centerPt, scalar radius,
56         const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode);
57     static std::shared_ptr<ShaderEffect> CreateTwoPointConical(const Point& startPt, scalar startRadius,
58         const Point& endPt, scalar endRadius, const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos,
59         TileMode mode, const Matrix *matrix);
60     static std::shared_ptr<ShaderEffect> CreateSweepGradient(const Point& centerPt,
61         const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, scalar startAngle,
62         scalar endAngle, const Matrix *matrix);
63 
64     virtual ~ShaderEffect() = default;
65     ShaderEffectType GetType() const;
GetDrawingType()66     virtual DrawingType GetDrawingType() const
67     {
68         return DrawingType::COMMON;
69     }
70 
71     template<typename T>
GetImpl()72     T* GetImpl() const
73     {
74         return impl_->DowncastingTo<T>();
75     }
76 
77     ShaderEffect(ShaderEffectType t, ColorQuad color) noexcept;
78     ShaderEffect(ShaderEffectType t, ShaderEffect& dst, ShaderEffect& src, BlendMode mode) noexcept;
79     ShaderEffect(ShaderEffectType t, const Image& image, TileMode tileX, TileMode tileY,
80         const SamplingOptions& sampling, const Matrix& matrix) noexcept;
81     ShaderEffect(ShaderEffectType t, const Picture& picture, TileMode tileX, TileMode tileY, FilterMode mode,
82         const Matrix& matrix, const Rect& rect) noexcept;
83     ShaderEffect(ShaderEffectType t, const Point& startPt, const Point& endPt, const std::vector<ColorQuad>& colors,
84         const std::vector<scalar>& pos, TileMode mode) noexcept;
85     ShaderEffect(ShaderEffectType t, const Point& centerPt, scalar radius, const std::vector<ColorQuad>& colors,
86         const std::vector<scalar>& pos, TileMode mode) noexcept;
87     ShaderEffect(ShaderEffectType t, const Point& startPt, scalar startRadius, const Point& endPt, scalar endRadius,
88         const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode,
89         const Matrix *matrix) noexcept;
90     ShaderEffect(ShaderEffectType t, const Point& centerPt, const std::vector<ColorQuad>& colors,
91         const std::vector<scalar>& pos, TileMode mode, scalar startAngle, scalar endAngle,
92         const Matrix *matrix) noexcept;
93     ShaderEffect(ShaderEffectType t) noexcept;
94     ShaderEffect() noexcept;
95 
96     std::shared_ptr<Data> Serialize() const;
97     bool Deserialize(std::shared_ptr<Data> data);
98 
99     const sk_sp<SkShader> ExportSkShader();
100     void SetSkShader(sk_sp<SkShader> shader);
101 private:
102     ShaderEffectType type_;
103     std::shared_ptr<ShaderEffectImpl> impl_;
104 };
105 } // namespace Drawing
106 } // namespace Rosen
107 } // namespace OHOS
108 #endif
109