Changes required after WeakHandle moved to Public API in Core
[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) 2017 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 <memory>
23
24 #include <dali/public-api/common/intrusive-ptr.h>
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 #include <dali/public-api/object/weak-handle.h>
29
30 // INTERNAL INCLUDES
31 #include <dali-toolkit/devel-api/image-loader/atlas-upload-observer.h>
32 #include <dali-toolkit/internal/visuals/texture-upload-observer.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-url.h>
35
36 namespace Dali
37 {
38
39 class NativeImage;
40
41 namespace Toolkit
42 {
43
44 namespace Internal
45 {
46
47 class ImageVisual;
48 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
49
50 /**
51  * The visual which renders an image to a quad geometry
52  *
53  * The following properties are optional
54  *
55  * | %Property Name     | Type              |
56  * |--------------------|-------------------|
57  * | url                | STRING            |
58  * | alphaMaskUrl       | STRING            |
59  * | fittingMode        | INTEGER OR STRING |
60  * | samplingMode       | INTEGER OR STRING |
61  * | desiredWidth       | INTEGER           |
62  * | desiredHeight      | INTEGER           |
63  * | synchronousLoading | BOOLEAN           |
64  * | pixelArea          | VECTOR4           |
65  * | wrapModeU          | INTEGER OR STRING |
66  * | wrapModeV          | INTEGER OR STRING |
67  *
68  * where pixelArea is a rectangular area.
69  * In its Vector4 value, the first two elements indicate the top-left position of the area,
70  * and the last two elements are the area width and height respectively.
71  * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
72  *
73  * where wrapModeU and wrapModeV separately decide how the texture should be sampled when the u and v coordinate exceeds the range of 0.0 to 1.0.
74  * Its value should be one of the following wrap mode:
75  *   "DEFAULT"
76  *   "CLAMP_TO_EDGE"
77  *   "REPEAT"
78  *   "MIRRORED_REPEAT"
79  *
80  * where imageFittingMode should be one of the following fitting modes:
81  *   "SHRINK_TO_FIT"
82  *   "SCALE_TO_FIT"
83  *   "FIT_WIDTH"
84  *   "FIT_HEIGHT"
85  *   "DEFAULT"
86  *
87  * where imageSamplingMode should be one of the following sampling modes:
88  *   "BOX"
89  *   "NEAREST"
90  *   "LINEAR"
91  *   "BOX_THEN_NEAREST"
92  *   "BOX_THEN_LINEAR"
93  *   "NO_FILTER"
94  *   "DONT_CARE"
95  *   "DEFAULT"
96  *
97  *
98  * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
99  * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
100  */
101 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver, public TextureUploadObserver
102 {
103 public:
104
105   /**
106    * @brief Create a new image visual with a URL.
107    *
108    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
109    *
110    * @param[in] factoryCache The VisualFactoryCache object
111    * @param[in] imageUrl The URL of the image resource to use
112    * @param[in] properties A Property::Map containing settings for this visual
113    * @param[in] size The width and height to fit the loaded image to.
114    * @param[in] fittingMode The FittingMode of the resource to load
115    * @param[in] samplingMode The SamplingMode of the resource to load
116    * @return A smart-pointer to the newly allocated visual.
117    */
118   static ImageVisualPtr New( VisualFactoryCache& factoryCache,
119                              const VisualUrl& imageUrl,
120                              const Property::Map& properties,
121                              ImageDimensions size = ImageDimensions(),
122                              FittingMode::Type fittingMode = FittingMode::DEFAULT,
123                              Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
124
125   /**
126    * @brief Create a new image visual with a URL.
127    *
128    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
129    *
130    * @param[in] factoryCache The VisualFactoryCache object
131    * @param[in] imageUrl The URL of the image resource to use
132    * @param[in] size The width and height to fit the loaded image to.
133    * @param[in] fittingMode The FittingMode of the resource to load
134    * @param[in] samplingMode The SamplingMode of the resource to load
135    * @return A smart-pointer to the newly allocated visual.
136    */
137   static ImageVisualPtr New( VisualFactoryCache& factoryCache,
138                              const VisualUrl& imageUrl,
139                              ImageDimensions size = ImageDimensions(),
140                              FittingMode::Type fittingMode = FittingMode::DEFAULT,
141                              Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
142
143   /**
144    * @brief Create a new image visual with an Image type.
145    *
146    * @param[in] factoryCache The VisualFactoryCache object
147    * @param[in] image The image to use
148    */
149   static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
150
151 public:  // from Visual
152
153   /**
154    * @copydoc Visual::Base::GetNaturalSize
155    */
156   virtual void GetNaturalSize( Vector2& naturalSize );
157
158   /**
159    * @copydoc Visual::Base::CreatePropertyMap
160    */
161   virtual void DoCreatePropertyMap( Property::Map& map ) const;
162
163   /**
164    * @copydoc Visual::Base::CreateInstancePropertyMap
165    */
166   virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
167
168 protected:
169
170   /**
171    * @brief Constructor with a URL.
172    *
173    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
174    *
175    * @param[in] factoryCache The VisualFactoryCache object
176    * @param[in] imageUrl The URL of the image resource to use
177    * @param[in] size The width and height to fit the loaded image to.
178    * @param[in] fittingMode The FittingMode of the resource to load
179    * @param[in] samplingMode The SamplingMode of the resource to load
180    */
181   ImageVisual( VisualFactoryCache& factoryCache,
182                const VisualUrl& imageUrl,
183                ImageDimensions size,
184                FittingMode::Type fittingMode,
185                Dali::SamplingMode::Type samplingMode );
186
187   /**
188    * @brief Constructor with an Image type.
189    *
190    * @param[in] factoryCache The VisualFactoryCache object
191    * @param[in] image The image to use
192    */
193   ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
194
195   /**
196    * @brief A reference counted object may only be deleted by calling Unreference().
197    */
198   virtual ~ImageVisual();
199
200   /**
201    * @copydoc Visual::Base::DoSetProperties
202    */
203   virtual void DoSetProperties( const Property::Map& propertyMap );
204
205   /**
206    * @copydoc Visual::Base::DoSetOnStage
207    */
208   virtual void DoSetOnStage( Actor& actor );
209
210   /**
211    * @copydoc Visual::Base::DoSetOffStage
212    */
213   virtual void DoSetOffStage( Actor& actor );
214
215   /**
216    * @copydoc Visual::Base::OnSetTransform
217    */
218   virtual void OnSetTransform();
219
220 public:
221
222   /**
223    * Get the standard image rendering shader.
224    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
225    * @param[in] atlasing Whether texture atlasing is applied.
226    * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
227    */
228   static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
229
230   /**
231    * @copydoc AtlasUploadObserver::UploadCompleted
232    *
233    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
234    * This callback is the place to add the renderer as it would be called once the loading is finished.
235    */
236   virtual void UploadCompleted();
237
238   /**
239    * @copydoc TextureUploadObserver::UploadCompleted
240    *
241    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
242    * This callback is the place to add the renderer as it would be called once the loading is finished.
243    */
244   virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle );
245
246 private:
247
248   /**
249    * Allocate the mask data when a masking property is defined in the property map
250    */
251   void AllocateMaskData();
252
253   /**
254    * @brief Applies the image to the texture set used for this renderer
255    *
256    * @param[in] image The Image to apply to the texture set used for this renderer
257    */
258   void ApplyImageToSampler( const Image& image );
259
260   /**
261    * @brief Initializes the Dali::Renderer from the image url
262    */
263   void InitializeRenderer();
264
265   /**
266    * @brief Initializes the Dali::Renderer from an image handle
267    *
268    * @param[in] image The image handle to intialize this ImageVisual from
269    */
270   void InitializeRenderer( const Image& image );
271
272   /**
273    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
274    * @param[in] textures to use
275    */
276   void CreateRenderer( TextureSet& textures );
277
278   /**
279    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
280    * @param NativeImageRenderer
281    */
282   void CreateNativeImageRenderer( NativeImage& nativeImage );
283
284   /**
285    * @brief Query whether resources requires to be loaded synchronously.
286    * @return Returns true if synchronous resource loading is required, false otherwise.
287    */
288   bool IsSynchronousResourceLoading() const;
289
290   /**
291    * Creates the texture set and adds the texture to it
292    * @param[out] textureRect The texture area of the texture in the atlas.
293    * @param[in] url The URL of the image resource to use.
294    * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
295    * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
296    * @return the texture set to use
297    */
298   TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
299
300   /**
301    * Set the value to the uTextureRect uniform
302    * @param[in] textureRect The texture rectangular area.
303    */
304   void SetTextureRectUniform( const Vector4& textureRect  );
305
306   /**
307    * Remove the texture if it is not used anymore.
308    */
309   void RemoveTexture(const std::string& url);
310
311   /**
312    * Helper method to set individual values by index key.
313    * @param[in] index The index key of the value
314    * @param[in] value The value
315    */
316   void DoSetProperty( Property::Index index, const Property::Value& value );
317
318 private:
319
320   Image mImage;
321   Vector4 mPixelArea;
322   WeakHandle<Actor> mPlacementActor;
323   VisualUrl mImageUrl;
324   TextureManager::MaskingDataPointer mMaskingData;
325
326   Dali::ImageDimensions mDesiredSize;
327   TextureManager::TextureId mTextureId;
328
329   Dali::FittingMode::Type mFittingMode:3;
330   Dali::SamplingMode::Type mSamplingMode:4;
331   Dali::WrapMode::Type mWrapModeU:3;
332   Dali::WrapMode::Type mWrapModeV:3;
333   bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
334   bool mLoadingStatus;  ///< True if the texture is being loaded asynchronously, or false when it has loaded.
335 };
336
337
338
339 } // namespace Internal
340
341 } // namespace Toolkit
342
343 } // namespace Dali
344
345 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */