7701e30d0cc382b9ec3477d385045c526b4302cf
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / program-controller.h
1 #ifndef DALI_INTERNAL_PROGRAM_CONTROLLER_H
2 #define DALI_INTERNAL_PROGRAM_CONTROLLER_H
3
4 /*
5  * Copyright (c) 2019 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/devel-api/common/owner-container.h>
23 #include <dali/internal/common/shader-data.h>
24 #include <dali/internal/render/shaders/program.h>
25 #include <dali/internal/render/shaders/program-cache.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 class ShaderSaver;
34
35 /**
36  * This class is the owner of GL shader programs
37  */
38 class ProgramController : public ProgramCache
39 {
40 public:
41
42   /**
43    * Wrapper for a program and its hash code
44    */
45   class ProgramPair
46   {
47   public: // API
48
49     /**
50      * Constructor
51      * @param program
52      * @param shaderHash
53      */
54     ProgramPair( Program* program, size_t shaderHash )
55     : mProgram( program ), mShaderHash( shaderHash )
56     { }
57
58     /**
59      * Destructor, non-virtual as not a base
60      */
61     ~ProgramPair()
62     {
63       delete mProgram;
64     }
65
66     /**
67      * Inline getter for the program
68      * @return the program
69      */
70     inline Program* GetProgram()
71     {
72       return mProgram;
73     }
74
75     /**
76      * Inline getter for the hash
77      * @return the hash
78      */
79     inline size_t GetHash()
80     {
81       return mShaderHash;
82     }
83
84   private: // Not implemented
85     ProgramPair( const ProgramPair& );
86     ProgramPair& operator=( const ProgramPair& );
87
88   private: // Data
89     Program* mProgram;
90     size_t mShaderHash;
91   };
92
93   /**
94    * Constructor
95    * @param postProcessDispatcher to send save binary message back to update
96    */
97   ProgramController( Integration::GlAbstraction& glAbstraction );
98
99   /**
100    * Destructor, non virtual as not a base class
101    */
102   ~ProgramController();
103
104 public: // API
105
106   /**
107    * Resets the program matrices. Must be called at the beginning of every frame
108    */
109   void ResetProgramMatrices();
110
111   /**
112    * Notifies the cache that context is (re)created
113    */
114   void GlContextCreated();
115
116   /**
117    * Notifies cache that context is lost
118    */
119   void GlContextDestroyed();
120
121   /**
122    * Set the destination for compiler shader binaries so they can be saved.
123    * @note Must be called during initialisation.
124    */
125   void SetShaderSaver( ShaderSaver& shaderSaver );
126
127   /**
128    * Clear current cached program
129    */
130   void ClearCurrentProgram();
131
132 private: // From ProgramCache
133
134   /**
135    * @copydoc ProgramCache::GetGlAbstraction
136    */
137   virtual Integration::GlAbstraction& GetGlAbstraction();
138
139   /**
140    * @copydoc ProgramCache::GetProgram
141    */
142   virtual Program* GetProgram( size_t shaderHash );
143
144   /**
145    * @copydoc ProgramCache::AddProgram
146    */
147   virtual void AddProgram( size_t shaderHash, Program* program );
148
149   /**
150    * @copydoc ProgramCache::GetCurrentProgram
151    */
152   virtual Program* GetCurrentProgram();
153
154   /**
155    * @copydoc ProgramCache::SetCurrentProgram
156    */
157   virtual void SetCurrentProgram( Program* program );
158
159   /**
160    * @copydoc ProgramCache::IsBinarySupported
161    */
162   virtual bool IsBinarySupported();
163
164   /**
165    * @copydoc ProgramCache::ProgramBinaryFormat
166    */
167   virtual GLenum ProgramBinaryFormat();
168
169   /**
170    * @copydoc ProgramCache::StoreBinary
171    */
172   virtual void StoreBinary( Internal::ShaderDataPtr programData );
173
174 private: // not implemented as non-copyable
175
176   ProgramController( const ProgramController& rhs );
177   ProgramController& operator=( const ProgramController& rhs );
178
179 private: // Data
180
181   ShaderSaver* mShaderSaver;
182   Integration::GlAbstraction& mGlAbstraction;
183   Program* mCurrentProgram;
184
185   typedef OwnerContainer< ProgramPair* > ProgramContainer;
186   typedef ProgramContainer::Iterator ProgramIterator;
187   ProgramContainer mProgramCache;
188
189   GLint mProgramBinaryFormat;
190   GLint mNumberOfProgramBinaryFormats;
191
192 };
193
194 } // namespace Internal
195
196 } // namespace Dali
197
198 #endif // DALI_INTERNAL_PROGRAM_CONTROLLER_H
199