[dali_2.3.31] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / effects / shader-factory.h
1 #ifndef DALI_INTERNAL_SHADER_FACTORY_H
2 #define DALI_INTERNAL_SHADER_FACTORY_H
3
4 /*
5  * Copyright (c) 2024 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 INCLUDES
22 #include <dali/internal/common/message.h>
23 #include <dali/internal/common/shader-data.h>
24 #include <dali/internal/common/shader-saver.h>
25 #include <dali/public-api/common/dali-vector.h>
26
27 namespace Dali
28 {
29 namespace Internal
30 {
31 class ShaderData;
32 using ShaderDataPtr = IntrusivePtr<ShaderData>;
33
34 /**
35  * @brief ShaderFactory loads and saves shader binaries synchronously.
36  *
37  * Binaries loaded or saved are also cached by the ShaderFactory.
38  */
39 class ShaderFactory : public ShaderSaver
40 {
41 public:
42   /**
43    * Default constructor
44    */
45   ShaderFactory();
46
47   /**
48    * Destructor
49    */
50   ~ShaderFactory() override;
51
52   /**
53    * @brief Looks for precompiled binary version of shader program in memory and file caches.
54    *
55    * Tries to load a binary version of a shader program identified by a hash over the two source
56    * files, checking an in-memory cache first.
57    * If the cache hits or the load succeeds, the buffer member of the returned ShaderData will
58    * contain a precompiled shader binary program which can be uploaded directly to GLES.
59    *
60    * @param [in] vertexSource   The vertex shader source code
61    * @param [in] fragmentSource The fragment shader source code
62    * @param [in] renderPassTag  RenderPass the shaders are executed
63    * @param [in] name           The name of shaders are executed
64    * @param [out] shaderHash    Hash key created from vertex and fragment shader code
65    * @return                    ShaderData containing the source and hash value, and additionally,
66    *                            a compiled shader program binary if one could be found, else an
67    *                            empty binary buffer cleared to size zero.
68    */
69   Internal::ShaderDataPtr Load(std::string_view vertexSource, std::string_view fragmentSource, const Dali::Shader::Hint::Value hints, uint32_t renderPassTag, std::string_view name, size_t& shaderHash);
70
71   /**
72    * @brief Saves shader to memory cache and filesystem.
73    *
74    * This is called when a shader binary is ready to be saved to the memory cache file system.
75    * Shaders that pass through here become available to subsequent invocations of Load.
76    * @param[in] shader The data to be saved.
77    * @sa Load
78    */
79   void SaveBinary(Internal::ShaderDataPtr shader) override;
80
81 private:
82   void MemoryCacheInsert(Internal::ShaderData& shaderData);
83
84   // Undefined
85   ShaderFactory(const ShaderFactory&);
86
87   // Undefined
88   ShaderFactory& operator=(const ShaderFactory& rhs);
89
90 private:
91   Dali::Vector<Internal::ShaderData*> mShaderBinaryCache; ///< Cache of pre-compiled shaders.
92
93 }; // class ShaderFactory
94
95 inline MessageBase* ShaderCompiledMessage(ShaderSaver& factory, Internal::ShaderDataPtr shaderData)
96 {
97   return new MessageValue1<ShaderSaver, Internal::ShaderDataPtr>(&factory,
98                                                                  &ShaderSaver::SaveBinary,
99                                                                  shaderData);
100 }
101
102 } // namespace Internal
103
104 } // namespace Dali
105
106 #endif // DALI_INTERNAL_SHADER_FACTORY_H