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