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