1 /*
2  * Copyright (c) 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 RUNTIME_EFFECT_H
17 #define RUNTIME_EFFECT_H
18 
19 #include "drawing/engine_adapter/impl_interface/runtime_effect_impl.h"
20 
21 #include "effect/blender.h"
22 #include "effect/shader_effect.h"
23 #include "utils/data.h"
24 #include "utils/matrix.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace Drawing {
29 class RuntimeEffectOptions {
30 public:
31     bool forceNoInline = false;
32     bool useAF = false;
33 };
34 
35 class DRAWING_API RuntimeEffect {
36 public:
37     /**
38      * @brief Shdader described by shading language requires an entry point that looks like:
39      * vec4 main(vec2 inCoords) { ... }
40      * -or-
41      * vec4 main(vec2 inCoords, vec4 inColor) { ... }
42      * Most shaders don't use the input color, so that parameter is optional.
43      *
44      * @param sl       shader sl
45      * @param options  runtime effect options to be set
46      * @return A shared pointer to RuntimeEffect
47      */
48     static std::shared_ptr<RuntimeEffect> CreateForShader(const std::string& sl,
49         const RuntimeEffectOptions&);
50     static std::shared_ptr<RuntimeEffect> CreateForShader(const std::string& sl);
51     static std::shared_ptr<RuntimeEffect> CreateForES3Shader(const std::string& sl);
52 
53     /**
54      * @brief Blend shader desribed by shading language requires an entry point that looks like:
55      * vec4 main(vec4 srcColor, vec4 dstColor) { ... }
56      *
57      * @param sl
58      * @return A shared pointer to RuntimeEffect
59      */
60     static std::shared_ptr<RuntimeEffect> CreateForBlender(const std::string& sl);
61 
62     explicit RuntimeEffect(const std::string& sl) noexcept;
63     RuntimeEffect(const std::string& sl, const RuntimeEffectOptions&) noexcept;
64     RuntimeEffect(const std::string& sl, const RuntimeEffectOptions&, bool isES3) noexcept;
65     RuntimeEffect(const std::string& sl, bool isBlender) noexcept;
66     std::shared_ptr<ShaderEffect> MakeShader(std::shared_ptr<Data> uniforms,
67                                              std::shared_ptr<ShaderEffect> children[],
68                                              size_t childCount, const Matrix* localMatrix,
69                                              bool isOpaque);
70     virtual ~RuntimeEffect() = default;
GetDrawingType()71     virtual DrawingType GetDrawingType() const
72     {
73         return DrawingType::COMMON;
74     }
75 
76     template<typename T>
GetImpl()77     T* GetImpl() const
78     {
79         return impl_->DowncastingTo<T>();
80     }
81 
82 protected:
83     RuntimeEffect() noexcept;
84 
85 private:
86     std::shared_ptr<RuntimeEffectImpl> impl_;
87 };
88 } // namespace Drawing
89 } // namespace Rosen
90 } // namespace OHOS
91 #endif
92