Remove unused program / shader caches
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / shader-cache.h
1 #ifndef DALI_INTERNAL_SHADER_CACHE_H
2 #define DALI_INTERNAL_SHADER_CACHE_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 #include <dali/graphics-api/graphics-controller.h>
21 #include <dali/graphics-api/graphics-shader-create-info.h>
22 #include <dali/graphics-api/graphics-shader.h>
23 #include <memory>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 namespace Render
30 {
31 /**
32  * Caches graphics shaders as they are created by SceneGraph::Shader.
33  */
34 struct ShaderCache
35 {
36   struct Item
37   {
38     Item()            = default;
39     Item(const Item&) = delete;
40     Item(Item&&)      = default;
41     Item& operator=(const Item&) = delete;
42     Item& operator=(Item&&) = default;
43
44     Item(Graphics::UniquePtr<Dali::Graphics::Shader> shader,
45          const std::vector<char>&                    shaderCode,
46          Graphics::PipelineStage                     stage,
47          Graphics::ShaderSourceMode                  type)
48     : shader(std::move(shader)),
49       shaderCode(shaderCode),
50       stage(stage),
51       type(type),
52       refCount{1u}
53     {
54     }
55
56     ~Item() = default;
57
58     Graphics::UniquePtr<Dali::Graphics::Shader> shader{nullptr};
59     std::vector<char>                           shaderCode;
60     Graphics::PipelineStage                     stage;
61     Graphics::ShaderSourceMode                  type;
62     uint16_t                                    refCount;
63   };
64
65   /**
66    * Constructor
67    *
68    * @param[in] controller The graphics controller
69    */
70   explicit ShaderCache(Dali::Graphics::Controller& controller);
71
72   /**
73    * Get a shader from it's source code
74    * It will increate getted shader item reference count.
75    *
76    * @param[in] shaderCode The shader code
77    * @param[in] stage The pipeline stage (e.g. VERTEX_SHADER or FRAGMENT_SHADER etc.)
78    * @param[in] type The type of the shader (i.e. text or binary)
79    * @return the graphics shader
80    */
81   Dali::Graphics::Shader& GetShader(const std::vector<char>& shaderCode, Graphics::PipelineStage stage, Graphics::ShaderSourceMode type);
82
83   /**
84    * @brief Reset all items reference count as 0.
85    */
86   void ResetReferenceCount();
87
88   /**
89    * @brief Clear items who the reference count is 0.
90    */
91   void ClearUnusedCache();
92
93 private:
94   std::vector<Item>           mItems;
95   Dali::Graphics::Controller& mController;
96 };
97
98 } // namespace Render
99 } // namespace Internal
100 } // namespace Dali
101
102 #endif //DALI_INTERNAL_SHADER_CACHE_H