Conversion to Apache 2.0 license
[platform/core/uifw/dali-core.git] / dali / internal / event / effects / shader-effect-impl.h
1 #ifndef __DALI_INTERNAL_SHADER_EFFECT_H__
2 #define __DALI_INTERNAL_SHADER_EFFECT_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/public-api/object/ref-object.h>
23 #include <dali/public-api/shader-effects/shader-effect.h>
24 #include <dali/internal/event/effects/shader-declarations.h>
25 #include <dali/internal/event/common/proxy-object.h>
26 #include <dali/internal/render/shaders/uniform-meta.h>
27 #include <dali/internal/event/resources/resource-ticket.h>
28 #include <dali/internal/render/shaders/shader.h>
29
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 class ShaderFactory;
38
39 namespace SceneGraph
40 {
41 class UpdateManager;
42 }
43
44 /**
45  * An abstract base class for a shader effect proxy.
46  * The corresponding scene-graph object is a collection of shader programs,
47  * which can apply the same effect to different geometry types.
48  */
49 class ShaderEffect : public ProxyObject
50 {
51 public:
52
53   typedef Dali::ShaderEffect::UniformCoordinateType UniformCoordinateType;
54
55   /**
56    * Create a new ShaderEffect with no programs
57    * @param hints GeometryHints to define the geometry of the rendered object
58    * @return A smart-pointer to a newly allocated shader effect.
59    */
60   static ShaderEffectPtr New(Dali::ShaderEffect::GeometryHints hints = Dali::ShaderEffect::HINT_NONE);
61
62   /**
63    * Create a new ShaderEffect
64    * @param vertexShader code for the effect. If empty, the default version will be used
65    * @param fragmentShader code for the effect. If empty, the default version will be used
66    * @param type GeometryType
67    * @param hints GeometryHints to define the geometry of the rendered object
68    * @return A smart-pointer to a newly allocated shader effect.
69    */
70   static ShaderEffectPtr New( const std::string& vertexShader,
71                               const std::string& fragmentShader,
72                               GeometryType type,
73                               Dali::ShaderEffect::GeometryHints hints );
74   /**
75    * Create a new ShaderEffect
76    * @param vertexShaderPrefix code for the effect. It will be inserted before the default uniforms.
77    * @param vertexShader code for the effect. If empty, the default version will be used
78    * @param fragmentShaderPrefix code for the effect. It will be inserted before the default uniforms.
79    * @param fragmentShader code for the effect. If empty, the default version will be used
80    * @param type GeometryType
81    * @param hints GeometryHints to define the geometry of the rendered object
82    * @return A smart-pointer to a newly allocated shader effect.
83    */
84   static ShaderEffectPtr NewWithPrefix( const std::string& vertexShaderPrefix,
85                                         const std::string& vertexShader,
86                                         const std::string& fragmentShaderPrefix,
87                                         const std::string& fragmentShader,
88                                         GeometryType type,
89                                         Dali::ShaderEffect::GeometryHints hints );
90
91   /**
92    * Create a new ShaderEffect.
93    * If you pass in an empty string in the following arguments, the default version will be used instead.
94    * @param imageVertexShader code for the effect.
95    * @param imageFragmentShader code for the effect.
96    * @param textVertexShader code for the effect.
97    * @param textFragmentShader code for the effect.
98    * @param texturedMeshVertexShader code for the effect.
99    * @param texturedMeshFragmentShader code for the effect.
100    * @param meshVertexShader code for the effect.
101    * @param meshFragmentShader code for the effect.
102    * @param hints GeometryHints to define the geometry of the rendered object
103    * @return A handle to a shader effect
104    */
105   static ShaderEffectPtr New( const std::string& imageVertexShader,
106                               const std::string& imageFragmentShader,
107                               const std::string& textVertexShader,
108                               const std::string& textFragmentShader,
109                               const std::string& texturedMeshVertexShader,
110                               const std::string& texturedMeshFragmentShader,
111                               const std::string& meshVertexShader,
112                               const std::string& meshFragmentShader,
113                               Dali::ShaderEffect::GeometryHints hints );
114
115   /**
116    * Creates object with data from the property value map
117    * @param [in] map The property value map with fields such as 'vertex-filename' '..'
118    * @return a pointer to a newly created object.
119    */
120   static ShaderEffectPtr New( const Property::Value& map );
121
122   /**
123    * @copydoc Dali::ShaderEffect::SetEffectImage
124    */
125   void SetEffectImage( Dali::Image image );
126
127   /**
128    * @copydoc Dali::ShaderEffect::SetUniform( const std::string& name, Property::Value value, UniformCoordinateType uniformCoordinateType )
129    */
130   void SetUniform( const std::string& name,
131                    Property::Value value,
132                    UniformCoordinateType uniformCoordinateType );
133
134   /**
135    * @copydoc Dali::ShaderEffect::AttachExtension()
136    */
137   void AttachExtension( Dali::ShaderEffect::Extension *object );
138
139   /**
140    * @copydoc Dali::ShaderEffect::GetExtension()
141    */
142   Dali::ShaderEffect::Extension& GetExtension();
143
144   /**
145    * @copydoc Dali::ShaderEffect::GetExtension() const
146    */
147   const Dali::ShaderEffect::Extension& GetExtension() const;
148
149   /**
150    * Add a GeometryType specific default program to this ShaderEffect
151    * @param[in] geometryType    The GeometryType rendered by the shader program
152    * @param[in] subType         The subtype, one of ShaderSubTypes.
153    * @param[in] vertexSource    The source code for the vertex shader
154    * @param[in] fragmentSource  The source code for the fragment shader
155    */
156   void SetProgram( GeometryType geometryType, ShaderSubTypes subType,
157                    const std::string& vertexSource, const std::string& fragmentSource );
158
159   /**
160    * Add a GeometryType specific default program to this ShaderEffect.
161    * This overload allows the optional prefixing for both the vertex and fragment shader.
162    * A useful prefix may be shader \#defines for conditional compilation.
163    * @param[in] geometryType    The GeometryType rendered by the shader program
164    * @param[in] subType         The subtype, one of ShaderSubTypes.
165    * @param[in] vertexPrefix    The prefix source code for the vertex shader
166    * @param[in] fragmentPrefix  The prefix source code for the fragment shader
167    * @param[in] vertexSource    The source code for the vertex shader
168    * @param[in] fragmentSource  The source code for the fragment shader
169    */
170   void SetProgram( GeometryType geometryType, ShaderSubTypes subType,
171                    const std::string& vertexPrefix, const std::string& fragmentPrefix,
172                    const std::string& vertexSource, const std::string& fragmentSource );
173
174   /**
175    * Notify ShaderEffect that it's being used by an Actor.
176    */
177   void Connect();
178
179   /**
180    * Notify ShaderEffect that an Actor is no longer using it.
181    */
182   void Disconnect();
183
184 public: // Default property extensions from ProxyObject
185
186   /**
187    * @copydoc Dali::Internal::ProxyObject::IsSceneObjectRemovable()
188    */
189   virtual bool IsSceneObjectRemovable() const;
190
191   /**
192    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyCount()
193    */
194   virtual unsigned int GetDefaultPropertyCount() const;
195
196   /**
197    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndices()
198    */
199   virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const;
200
201   /**
202    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyName()
203    */
204   virtual const std::string& GetDefaultPropertyName( Property::Index index ) const;
205
206   /**
207    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndex()
208    */
209   virtual Property::Index GetDefaultPropertyIndex( const std::string& name ) const;
210
211   /**
212    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyWritable()
213    */
214   virtual bool IsDefaultPropertyWritable( Property::Index index ) const;
215
216   /**
217    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAnimatable()
218    */
219   virtual bool IsDefaultPropertyAnimatable( Property::Index index ) const;
220
221   /**
222    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAConstraintInput()
223    */
224   virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const;
225
226   /**
227    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyType()
228    */
229   virtual Property::Type GetDefaultPropertyType( Property::Index index ) const;
230
231   /**
232    * @copydoc Dali::Internal::ProxyObject::SetDefaultProperty()
233    */
234   virtual void SetDefaultProperty( Property::Index index, const Property::Value& propertyValue );
235
236   /**
237    * @copydoc Dali::Internal::ProxyObject::GetDefaultProperty()
238    */
239   virtual Property::Value GetDefaultProperty( Property::Index index ) const;
240
241   /**
242    * @copydoc Dali::Internal::ProxyObject::InstallSceneObjectProperty()
243    */
244   virtual void InstallSceneObjectProperty( SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index );
245
246   /**
247    * @copydoc Dali::Internal::ProxyObject::GetSceneObject()
248    */
249   virtual const SceneGraph::PropertyOwner* GetSceneObject() const;
250
251   /**
252    * @copydoc Dali::Internal::ProxyObject::GetSceneObjectAnimatableProperty()
253    */
254   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
255
256   /**
257    * @copydoc Dali::Internal::ProxyObject::GetSceneObjectInputProperty()
258    */
259   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
260
261 protected:
262
263   /**
264    * Protected constructor.
265    */
266   ShaderEffect( SceneGraph::UpdateManager& updateManager, ShaderFactory& shaderFactory, SceneGraph::Shader& sceneObject );
267
268   /**
269    * A reference counted object may only be deleted by calling Unreference()
270    */
271   virtual ~ShaderEffect();
272
273 private:
274
275   // No copying allowed, thus these are undefined
276   ShaderEffect( const ShaderEffect& );
277   ShaderEffect& operator=( const ShaderEffect& rhs );
278
279   void OnImageLoaded( Dali::Image image ); ///< just a helper for image loaded callback
280
281 private: // Data
282
283   SceneGraph::UpdateManager& mUpdateManager;            ///< reference to the update manager
284   ShaderFactory& mShaderFactory;                        ///< reference to the shader factory
285
286   SceneGraph::Shader* mSceneObject;                     ///< pointer to the scene shader, should not be changed on this thread
287
288   Dali::Image mImage;                                   ///< Client-side handle for the effect image
289
290   CustomUniformMetaLookup mCustomMetadata;            ///< Used for accessing metadata for custom Shader properties
291
292   IntrusivePtr<Dali::ShaderEffect::Extension> mExtension;
293
294   std::vector<ResourceTicketPtr>  mTickets;             ///< Collection of shader program tickets
295
296   unsigned int  mConnectionCount;                       ///< number of on-stage ImageActors using this shader effect
297
298   // Default properties
299   typedef std::map<std::string, Property::Index> DefaultPropertyLookup;
300   static DefaultPropertyLookup* mDefaultPropertyLookup;
301 };
302
303 } // namespace Internal
304
305 // Helpers for public-api forwarding methods
306
307 inline Internal::ShaderEffect& GetImplementation(Dali::ShaderEffect& effect)
308 {
309   DALI_ASSERT_ALWAYS(effect && "ShaderEffect handle is empty");
310
311   BaseObject& handle = effect.GetBaseObject();
312
313   return static_cast<Internal::ShaderEffect&>(handle);
314 }
315
316 inline const Internal::ShaderEffect& GetImplementation(const Dali::ShaderEffect& effect)
317 {
318   DALI_ASSERT_ALWAYS(effect && "ShaderEffect handle is empty");
319
320   const BaseObject& handle = effect.GetBaseObject();
321
322   return static_cast<const Internal::ShaderEffect&>(handle);
323 }
324
325 } // namespace Dali
326
327 #endif // __DALI_INTERNAL_SHADER_EFFECT_H__