Apply Precompile shader
[platform/core/uifw/dali-adaptor.git] / dali / integration-api / adaptor-framework / shader-precompiler.h
1 #ifndef DALI_INTEGRATION_SHADER_PRECOMPILER_H
2 #define DALI_INTEGRATION_SHADER_PRECOMPILER_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL HEADER
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/public-api/object/base-handle.h>
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/devel-api/threading/conditional-wait.h>
26 #include <dali/devel-api/threading/mutex.h>
27
28 // EXTERNAL HEDAER
29 #include <string_view>
30 #include <memory>
31 #include <mutex>
32 #include <dali/graphics-api/graphics-program.h>
33 #include <dali/graphics-api/graphics-types.h>
34
35 namespace Dali
36 {
37 struct RawShaderData
38 {
39   int shaderCount;
40   std::vector<std::string_view> vertexPrefix;
41   std::vector<std::string_view> fragmentPrefix;
42   std::string_view vertexShader;
43   std::string_view fragmentShader;
44 };
45
46 /**
47  * ShaderPreCompiler  is used to precompile shaders.
48  * The precompiled shaders are stored in a file.
49  * @SINCE_2_2.45
50  */
51 class DALI_CORE_API ShaderPreCompiler : public BaseHandle
52 {
53 public:
54   /**
55    * @brief Gets the singleton of ShaderPreCompiler object.
56    *
57    * @return A handle to the ShaderPreCompiler
58    */
59   static ShaderPreCompiler& Get();
60
61   /**
62    * @brief Add a precompiled program to the ShaderPreCompiler
63    *
64    * @param[in] program precompiled program
65    */
66   void AddPreCompiledProgram(Graphics::UniquePtr<Dali::Graphics::Program> program);
67
68   /**
69    * @brief Gets the shader list to be precompiled
70    *
71    *  @param[in] shaders shader data for precompile
72    */
73   void GetPreCompileShaderList(std::vector<RawShaderData>& shaders);
74
75   /**
76    * @brief Save the shader list to be precompiled
77    *
78    * @param[in] shaders shader data for precompile
79    */
80   void SavePreCompileShaderList(std::vector<RawShaderData>& shaders);
81
82   /**
83    * @brief Checks whether the precompiled list is ready or not
84    *
85    * @return true if precompile list is ready
86    */
87   bool IsReady() const;
88
89   /**
90    * @brief Enable the feature of precompile
91    *
92    */
93   void Enable();
94
95   /**
96    * @brief Check the feature of precompile is enabled or not
97    *
98    * @return true if the feature of precompile is enabled
99   */
100   bool IsEnable();
101
102   /**
103    * @brief Waiting for a list of shaders to be precompiled
104    *
105    */
106   void Wait();
107
108   /**
109    * @brief Awake waiting for a list of shaders to be precompiled
110    *
111    */
112   void Awake();
113
114 private:
115   /**
116    * Construct a new ShaderPreCompiler.
117    */
118   ShaderPreCompiler();
119
120   // Undefined
121   ShaderPreCompiler(const ShaderPreCompiler&) = delete;
122
123   // Undefined
124   ShaderPreCompiler& operator=(const ShaderPreCompiler& rhs) = delete;
125
126 private:
127   std::vector<Graphics::UniquePtr<Dali::Graphics::Program>> mProgram;
128   static std::unique_ptr<ShaderPreCompiler> mInstance;
129   static std::once_flag mOnceFlag;
130   std::vector<RawShaderData> mRawShaderList;
131   ConditionalWait mConditionalWait;
132   Dali::Mutex mMutex;
133   bool mPrecompiled;
134   bool mEnabled;
135   bool mNeedsSleep{true};
136 };
137
138 } // namespace Dali
139
140 #endif // DALI_INTEGRATION_SHADER_PRECOMPILER_H