Merge "Remove Atlas usage from ImageVisual" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image / image-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_IMAGE_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 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
23 #include <dali-toolkit/devel-api/image-loader/atlas-upload-observer.h>
24
25 // EXTERNAL INCLUDES
26 #include <dali/public-api/images/image.h>
27 #include <dali/public-api/images/image-operations.h>
28 #include <dali/public-api/images/resource-image.h>
29 #include <dali/devel-api/object/weak-handle.h>
30
31 namespace Dali
32 {
33
34 class NativeImage;
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 class ImageVisual;
43 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
44
45 /**
46  * The visual which renders an image to the control's quad
47  *
48  * The following properties are optional
49  *
50  * | %Property Name     | Type             |
51  * |--------------------|------------------|
52  * | url                | STRING           |
53  * | fittingMode        | STRING           |
54  * | samplingMode       | STRING           |
55  * | desiredWidth       | INT              |
56  * | desiredHeight      | INT              |
57  * | synchronousLoading | BOOLEAN          |
58  *
59  * where imageFittingMode should be one of the following fitting modes:
60  *   "SHRINK_TO_FIT"
61  *   "SCALE_TO_FIT"
62  *   "FIT_WIDTH"
63  *   "FIT_HEIGHT"
64  *   "DEFAULT"
65  *
66  * where imageSamplingMode should be one of the following sampling modes:
67  *   "BOX"
68  *   "NEAREST"
69  *   "LINEAR"
70  *   "BOX_THEN_NEAREST"
71  *   "BOX_THEN_LINEAR"
72  *   "NO_FILTER"
73  *   "DONT_CARE"
74  *   "DEFAULT"
75  *
76  */
77 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver
78 {
79 public:
80
81   /**
82    * @brief Constructor.
83    *
84    * @param[in] factoryCache The VisualFactoryCache object
85    */
86   ImageVisual( VisualFactoryCache& factoryCache );
87
88   /**
89    * @brief Constructor with a URL.
90    *
91    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
92    *
93    * @param[in] factoryCache The VisualFactoryCache object
94    * @param[in] imageUrl The URL of the image resource to use
95    * @param[in] size The width and height to fit the loaded image to.
96    * @param[in] fittingMode The FittingMode of the resource to load
97    * @param[in] samplingMode The SamplingMode of the resource to load
98    */
99   ImageVisual( VisualFactoryCache& factoryCache,
100                const std::string& imageUrl,
101                ImageDimensions size=ImageDimensions(),
102                FittingMode::Type fittingMode = FittingMode::DEFAULT,
103                Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
104
105   /**
106    * @brief Constructor with an Image type.
107    *
108    * @param[in] factoryCache The VisualFactoryCache object
109    * @param[in] image The image to use
110    */
111   ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
112
113   /**
114    * @brief A reference counted object may only be deleted by calling Unreference().
115    */
116   ~ImageVisual();
117
118 public:  // from Visual
119
120   /**
121    * @copydoc Visual::Base::GetNaturalSize
122    */
123   virtual void GetNaturalSize( Vector2& naturalSize ) const;
124
125   /**
126    * @copydoc Visual::Base::CreatePropertyMap
127    */
128   virtual void DoCreatePropertyMap( Property::Map& map ) const;
129
130   /**
131    * @copydoc Visual::Base::DoSetProperty
132    */
133   virtual void DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
134
135   /**
136    * @copydoc Visual::Base::DoGetProperty
137    */
138   virtual Dali::Property::Value DoGetProperty( Dali::Property::Index index );
139
140 protected:
141   /**
142    * @copydoc Visual::Base::DoInitialize
143    */
144   virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
145
146   /**
147    * @copydoc Visual::Base::DoSetOnStage
148    */
149   virtual void DoSetOnStage( Actor& actor );
150
151   /**
152    * @copydoc Visual::Base::DoSetOffStage
153    */
154   virtual void DoSetOffStage( Actor& actor );
155
156 public:
157
158   /**
159    * Get the standard image rendering shader.
160    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
161    * @param[in] atlasing Whether texture atlasing is applied.
162    * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
163    */
164   static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
165
166   /**
167    * @copydoc AtlasUploadObserver::UploadCompleted
168    *
169    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
170    * This callback is the place to add the renderer as it would be called once the loading is finished.
171    */
172   virtual void UploadCompleted();
173
174 private:
175
176   /**
177    * @brief Applies the image to the texture set used for this renderer
178    *
179    * @param[in] image The Image to apply to the texture set used for this renderer
180    */
181   void ApplyImageToSampler( const Image& image );
182
183   /**
184    * @brief Initializes the Dali::Renderer from an image url string
185    *
186    * @param[in] imageUrl The image url string to intialize this ImageVisual from
187    */
188   void InitializeRenderer( const std::string& imageUrl );
189
190   /**
191    * @brief Initializes the Dali::Renderer from an image handle
192    *
193    * @param[in] image The image handle to intialize this ImageVisual from
194    */
195   void InitializeRenderer( const Image& image );
196
197   /**
198    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
199    */
200   void CreateRenderer();
201
202   /**
203    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
204    */
205   void CreateNativeImageRenderer();
206
207   /**
208    * @brief Query whether resources requires to be loaded synchronously.
209    * @return Returns true if synchronous resource loading is required, false otherwise.
210    */
211   bool IsSynchronousResourceLoading() const;
212
213   /**
214    * @brief Load the resource synchronously
215    */
216   void LoadResourceSynchronously();
217
218   /**
219    * Creates the texture set and adds the texture to it
220    * @param[out] textureRect The texture area of the texture in the atlas.
221    * @param[in] url The URL of the image resource to use.
222    * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
223    * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
224    */
225   void CreateTextureSet( Vector4& textureRect, const std::string& url, bool synchronousLoading, bool attemptAtlasing );
226
227   /**
228    * Callback function of image resource loading succeed
229    * @param[in] image The Image content that we attempted to load from mImageUrl
230    */
231   void OnImageLoaded( ResourceImage image );
232
233   /**
234    * Set the value to the uTextureRect uniform
235    * @param[in] textureRect The texture rectangular area.
236    */
237   void SetTextureRectUniform( const Vector4& textureRect  );
238
239   /**
240    * Clean the renderer from cache, and remove the image from atlas if it is not used anymore
241    */
242   void CleanCache(const std::string& url);
243
244   /**
245    * Set shader code for nativeimage if it exists
246    */
247   void SetNativeFragmentShaderCode( Dali::NativeImage& nativeImage );
248
249 private:
250
251   Image mImage;
252   PixelData mPixels;
253   TextureSet mTextureSet;
254   Vector4 mPixelArea;
255   WeakHandle<Actor> mPlacementActor;
256   std::string mImageUrl;
257   std::string mNativeFragmentShaderCode;
258
259   Dali::ImageDimensions mDesiredSize;
260   Dali::FittingMode::Type mFittingMode:3;
261   Dali::SamplingMode::Type mSamplingMode:4;
262   Dali::WrapMode::Type mWrapModeU:3;
263   Dali::WrapMode::Type mWrapModeV:3;
264   bool mNativeImageFlag:1;
265 };
266
267 } // namespace Internal
268
269 } // namespace Toolkit
270
271 } // namespace Dali
272
273 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */