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