[dali_1.0.17] Merge branch 'tizen'
[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 namespace SceneGraph
38 {
39 class UpdateManager;
40 }
41
42 /**
43  * An abstract base class for a shader effect proxy.
44  * The corresponding scene-graph object is a collection of shader programs,
45  * which can apply the same effect to different geometry types.
46  */
47 class ShaderEffect : public ProxyObject
48 {
49 public:
50   typedef Dali::ShaderEffect::UniformCoordinateType UniformCoordinateType;
51
52   enum GeometryState
53   {
54     DOESNT_MODIFY_GEOMETRY,
55     MODIFIES_GEOMETRY
56   };
57
58   /**
59    * Create a new ShaderEffect with no programs
60    * @param hints GeometryHints to define the geometry of the rendered object
61    * @return A smart-pointer to a newly allocated shader effect.
62    */
63   static ShaderEffectPtr New( Dali::ShaderEffect::GeometryHints hints = Dali::ShaderEffect::HINT_NONE );
64
65   /**
66    * @copydoc Dali::ShaderEffect::SetEffectImage
67    */
68   void SetEffectImage( Dali::Image image );
69
70   /**
71    * @copydoc Dali::ShaderEffect::SetUniform( const std::string& name, Property::Value value, UniformCoordinateType uniformCoordinateType )
72    */
73   void SetUniform( const std::string& name,
74                    Property::Value value,
75                    UniformCoordinateType uniformCoordinateType );
76
77   /**
78    * @copydoc Dali::ShaderEffect::AttachExtension()
79    */
80   void AttachExtension( Dali::ShaderEffect::Extension *object );
81
82   /**
83    * @copydoc Dali::ShaderEffect::GetExtension()
84    */
85   Dali::ShaderEffect::Extension& GetExtension();
86
87   /**
88    * @copydoc Dali::ShaderEffect::GetExtension() const
89    */
90   const Dali::ShaderEffect::Extension& GetExtension() const;
91
92   /**
93    * Add a GeometryType specific default program to this ShaderEffect
94    * @param[in] geometryType    The GeometryType rendered by the shader program
95    * @param[in] vertexSource    The source code for the vertex shader
96    * @param[in] fragmentSource  The source code for the fragment shader
97    */
98   void SetPrograms( GeometryType geometryType, const std::string& vertexSource, const std::string& fragmentSource );
99
100   /**
101    * Add a GeometryType specific default program to this ShaderEffect.
102    * This overload allows the optional prefixing for both the vertex and fragment shader.
103    * A useful prefix may be shader \#defines for conditional compilation.
104    * @param[in] geometryType    The GeometryType rendered by the shader program
105    * @param[in] vertexPrefix    The prefix source code for the vertex shader
106    * @param[in] fragmentPrefix  The prefix source code for the fragment shader
107    * @param[in] vertexSource    The source code for the vertex shader
108    * @param[in] fragmentSource  The source code for the fragment shader
109    */
110   void SetPrograms( GeometryType geometryType,
111                     const std::string& vertexPrefix, const std::string& fragmentPrefix,
112                     const std::string& vertexSource, const std::string& fragmentSource );
113
114   /**
115    * Send shader program to scene-graph object.
116    * @param[in] geometryType     The GeometryType rendered by the shader program
117    * @param[in] subType          The subtype, one of ShaderSubTypes.
118    * @param[in] vertexSource     The source code for the vertex shader
119    * @param[in] fragmentSource   The source code for the fragment shader
120    * @param[in] modifiesGeometry True if the shader modifies geometry
121    */
122   void SendProgramMessage( GeometryType geometryType, ShaderSubTypes subType,
123                            const std::string& vertexSource, const std::string& fragmentSource,
124                            bool modifiesGeometry );
125
126   /**
127    * Notify ShaderEffect that it's being used by an Actor.
128    */
129   void Connect();
130
131   /**
132    * Notify ShaderEffect that an Actor is no longer using it.
133    */
134   void Disconnect();
135
136 public: // Default property extensions from ProxyObject
137
138   /**
139    * @copydoc Dali::Internal::ProxyObject::IsSceneObjectRemovable()
140    */
141   virtual bool IsSceneObjectRemovable() const;
142
143   /**
144    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyCount()
145    */
146   virtual unsigned int GetDefaultPropertyCount() const;
147
148   /**
149    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndices()
150    */
151   virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const;
152
153   /**
154    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyName()
155    */
156   virtual const std::string& GetDefaultPropertyName( Property::Index index ) const;
157
158   /**
159    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndex()
160    */
161   virtual Property::Index GetDefaultPropertyIndex( const std::string& name ) const;
162
163   /**
164    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyWritable()
165    */
166   virtual bool IsDefaultPropertyWritable( Property::Index index ) const;
167
168   /**
169    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAnimatable()
170    */
171   virtual bool IsDefaultPropertyAnimatable( Property::Index index ) const;
172
173   /**
174    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAConstraintInput()
175    */
176   virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const;
177
178   /**
179    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyType()
180    */
181   virtual Property::Type GetDefaultPropertyType( Property::Index index ) const;
182
183   /**
184    * @copydoc Dali::Internal::ProxyObject::SetDefaultProperty()
185    */
186   virtual void SetDefaultProperty( Property::Index index, const Property::Value& propertyValue );
187
188   /**
189    * @copydoc Dali::Internal::ProxyObject::GetDefaultProperty()
190    */
191   virtual Property::Value GetDefaultProperty( Property::Index index ) const;
192
193   /**
194    * @copydoc Dali::Internal::ProxyObject::InstallSceneObjectProperty()
195    */
196   virtual void InstallSceneObjectProperty( SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index );
197
198   /**
199    * @copydoc Dali::Internal::ProxyObject::GetSceneObject()
200    */
201   virtual const SceneGraph::PropertyOwner* GetSceneObject() const;
202
203   /**
204    * @copydoc Dali::Internal::ProxyObject::GetSceneObjectAnimatableProperty()
205    */
206   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
207
208   /**
209    * @copydoc Dali::Internal::ProxyObject::GetSceneObjectInputProperty()
210    */
211   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
212
213 protected:
214
215   /**
216    * Protected constructor.
217    */
218   ShaderEffect( SceneGraph::UpdateManager& updateManager, Dali::ShaderEffect::GeometryHints hints );
219
220   /**
221    * A reference counted object may only be deleted by calling Unreference()
222    */
223   virtual ~ShaderEffect();
224
225 private:
226
227   // No copying allowed, thus these are undefined
228   ShaderEffect( const ShaderEffect& );
229   ShaderEffect& operator=( const ShaderEffect& rhs );
230
231 private: // Data
232
233   SceneGraph::UpdateManager& mUpdateManager;///< reference to the update manager
234   SceneGraph::Shader* mSceneObject;         ///< pointer to the scene shader, should not be changed on this thread
235   Dali::Image mImage;                       ///< Client-side handle for the effect image
236   CustomUniformMetaLookup mCustomMetadata;  ///< Used for accessing metadata for custom Shader properties
237   IntrusivePtr<Dali::ShaderEffect::Extension> mExtension;
238   std::vector<ResourceTicketPtr>  mTickets; ///< Collection of shader program tickets
239   unsigned int  mConnectionCount;           ///< number of on-stage ImageActors using this shader effect
240   Dali::ShaderEffect::GeometryHints  mGeometryHints; ///< shader geometry hints for building the geometry
241
242   // Default properties
243   typedef std::map<std::string, Property::Index> DefaultPropertyLookup;
244   static DefaultPropertyLookup* mDefaultPropertyLookup;
245 };
246
247 } // namespace Internal
248
249 // Helpers for public-api forwarding methods
250
251 inline Internal::ShaderEffect& GetImplementation(Dali::ShaderEffect& effect)
252 {
253   DALI_ASSERT_ALWAYS(effect && "ShaderEffect handle is empty");
254
255   BaseObject& handle = effect.GetBaseObject();
256
257   return static_cast<Internal::ShaderEffect&>(handle);
258 }
259
260 inline const Internal::ShaderEffect& GetImplementation(const Dali::ShaderEffect& effect)
261 {
262   DALI_ASSERT_ALWAYS(effect && "ShaderEffect handle is empty");
263
264   const BaseObject& handle = effect.GetBaseObject();
265
266   return static_cast<const Internal::ShaderEffect&>(handle);
267 }
268
269 } // namespace Dali
270
271 #endif // __DALI_INTERNAL_SHADER_EFFECT_H__