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