13003472f1ffacb68f08ba217e1afe97ef97e65e
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_VISUAL_H
3
4 /*
5  * Copyright (c) 2016 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 // EXTERNAL INCLUDES
22 #include <dali/public-api/images/image-operations.h>
23 #include <dali/public-api/object/base-object.h>
24 #include <dali/public-api/rendering/shader.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
28 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
29 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 namespace Visual
41 {
42
43 class ResourceObserver;
44
45 /**
46  * Base class for all Control rendering logic. A control may have multiple visuals.
47  *
48  * Note: The visual responds to the the Actor::COLOR by blending it with the 'Multiply' operator.
49  *
50  * The following properties are optional
51  *
52  * | %Property Name          | Type             |
53  * |-------------------------|------------------|
54  * | customShader            | MAP              |
55  *
56  * where custom-shader is a map with the following properties:
57  * | %Property Name          | Type             |
58  * |-------------------------|------------------|
59  * | vertexShader            | STRING           |
60  * | fragmentShader          | STRING           |
61  * | subdivideGridX          | INT              |
62  * | subdivideGridY          | INT              |
63  * | shaderHints             | INT              |
64  */
65 class Base : public BaseObject
66 {
67 public:
68
69   /**
70    *  Initialisation of the visual, this API should only called by the VisualFactory:
71    *  request the geometry and shader from the cache, if not available, create and save to the cache for sharing;
72    *  record the property values.
73    *
74    * @param[in] actor The Actor the visual is applied to if, empty if the visual has not been applied to any Actor
75    * @param[in] propertyMap The properties for the requested Visual object.
76    */
77   void Initialize( Actor& actor, const Property::Map& propertyMap );
78
79   /**
80    * @copydoc Toolkit::Visual::Base::SetSize
81    */
82   virtual void SetSize( const Vector2& size );
83
84   /**
85    * @copydoc Toolkit::Visual::Base::GetSize
86    */
87   const Vector2& GetSize() const;
88
89   /**
90    * @copydoc Toolkit::Visual::Base::SetName
91    */
92   void SetName( const std::string& name );
93
94   /**
95    * @copydoc Toolkit::Visual::Base::GetName
96    */
97   const std::string& GetName() const;
98
99   /**
100    * @copydoc Toolkit::Visual::Base::GetNaturalSize
101    */
102   virtual void GetNaturalSize( Vector2& naturalSize ) const;
103
104   /**
105    * ToDo: Add this function to Toolkit::Visual when it is fully implemented.
106    *
107    * Set the clip rectangular of this visual.
108    * The contents of the visual will not be visible outside this rectangular.
109    *
110    * @param [in] clipRect The clipping rectangular.
111    */
112   virtual void SetClipRect( const Rect<int>& clipRect );
113
114   /**
115    *ToDo: Add this function to Toolkit::Visual when it is fully implemented.
116    *
117    * Reposition this visual with a 2D offset.
118    *
119    * @param[in] offset The offset to reposition the visual.
120    */
121   virtual void SetOffset( const Vector2& offset );
122
123   /**
124    * @copydoc Toolkit::Visual::Base::SetDepthIndex
125    */
126   void SetDepthIndex( float index );
127
128   /**
129    * @copydoc Toolkit::Visual::Base::GetDepthIndex
130    */
131   float GetDepthIndex() const;
132
133   /**
134    * @copydoc Toolkit::Visual::Base::SetOnStage
135    * @pre Impl->mGeometry must be created before this method is called
136    */
137   void SetOnStage( Actor& actor );
138
139   /**
140    * @copydoc Toolkit::Visual::Base::SetOffStage
141    */
142   void SetOffStage( Actor& actor );
143
144   /**
145    * @copydoc Toolkit::Visual::Base::CreatePropertyMap
146    */
147   void CreatePropertyMap( Property::Map& map ) const;
148
149   /**
150    * @brief Set whether the Pre-multiplied Alpha Blending is required
151    *
152    * @param[in] preMultipled whether alpha is pre-multiplied.
153    */
154   void EnablePreMultipliedAlpha(  bool preMultipled );
155
156   /**
157    * @brief Query whether alpha is pre-multiplied.
158    *
159    * @return True is alpha is pre-multiplied, false otherwise.
160    */
161   bool IsPreMultipliedAlphaEnabled() const;
162
163   /**
164    * @brief Sets properties of custom shader
165    * @param[in] propertyMap Property map containing the custom shader data
166    */
167   void SetCustomShader( const Property::Map& propertyMap );
168
169   /**
170    * @brief Add an observer to watch for when the Visuals resources are loaded.
171    * Currently only supports a single observer
172    *
173    */
174   void AddResourceObserver( Visual::ResourceObserver& observer );
175
176   /**
177    * @brief Remove an observer
178    */
179   void RemoveResourceObserver( Visual::ResourceObserver& observer );
180
181   /**
182    * @brief Called when the visuals resources are loaded / ready
183    */
184   void ResourceReady();
185
186   /**
187    * @brief Called when the visuals resources are loaded / ready
188    * @return true if ready, false otherwise
189    */
190   bool IsResourceReady() const;
191
192 protected:
193
194   /**
195    * @brief Constructor.
196    *
197    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
198    */
199   Base( VisualFactoryCache& factoryCache );
200
201   /**
202    * @brief A reference counted object may only be deleted by calling Unreference().
203    */
204   virtual ~Base();
205
206 protected:
207   /**
208    * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
209    *
210    * @param[out] map The visual property map.
211    */
212   virtual void DoCreatePropertyMap( Property::Map& map ) const = 0;
213
214   /**
215    * @brief Called by Initialize() allowing sub classes to respond to the Initialize event
216    *
217    * @param[in] actor The Actor the visual is applied to if, empty if the visual has not been applied to any Actor
218    * @param[in] propertyMap The properties for the requested Visual object.
219    */
220   virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap ) {};
221
222 protected:
223
224   /**
225    * @brief Called by SetOnStage() allowing sub classes to respond to the SetOnStage event
226    *
227    * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
228    * @param[in] actor The actor applying this visual.
229    */
230   virtual void DoSetOnStage( Actor& actor ) = 0;
231
232   /**
233    * @brief Called by SetOffStage() allowing sub classes to respond to the SetOffStage event
234    *
235    * @param[in] actor The actor applying this visual.
236    */
237   virtual void DoSetOffStage( Actor& actor );
238
239 protected:
240   /**
241    * @brief Gets the on stage state for this Visual
242    *
243    * @return Returns true if this Visual is on stage, false if it is off the stage
244    */
245   bool GetIsOnStage() const;
246
247   /**
248    * @brief Gets whether the Dali::Renderer is from a shared cache (and therefore any modifications will affect other users of that renderer)
249    *
250    * @return Returns true if the renderer is from shared cache, false otherwise
251    */
252   bool GetIsFromCache() const;
253
254 private:
255
256   // Undefined
257   Base( const Visual::Base& visual );
258
259   // Undefined
260   Base& operator=( const Visual::Base& visual );
261
262 protected:
263   struct Impl;
264   Impl* mImpl;
265   VisualFactoryCache& mFactoryCache;
266 };
267
268 } // namspace Visual
269
270 } // namespace Internal
271
272 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase )
273 {
274   DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
275
276   const BaseObject& handle = visualBase.GetBaseObject();
277
278   return static_cast<const Internal::Visual::Base&>(handle);
279 }
280
281 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
282 {
283   DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
284
285   BaseObject& handle = visualBase.GetBaseObject();
286
287   return static_cast<Internal::Visual::Base&>(handle);
288 }
289
290 } // namespace Toolkit
291
292 } // namespace Dali
293
294 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H