[dali_1.9.31] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_SVG_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_SVG_VISUAL_H
3
4 /*
5  * Copyright (c) 2019 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/common/intrusive-ptr.h>
23 #include <dali/public-api/object/weak-handle.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
27 #include <dali-toolkit/internal/visuals/visual-url.h>
28
29 #ifdef NO_THORVG
30 struct NSVGimage;
31 #endif /* NO_THORVG */
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 class ImageVisualShaderFactory;
43 class SvgVisual;
44 typedef IntrusivePtr< SvgVisual > SvgVisualPtr;
45
46 /**
47  * The visual which renders a svg image
48  *
49  * The following property is essential
50  *
51  * | %Property Name           | Type             |
52  * |--------------------------|------------------|
53  * | url                      | STRING           |
54  *
55  */
56 class SvgVisual: public Visual::Base
57 {
58 public:
59
60   /**
61    * @brief Create the SVG Visual using the image URL.
62    *
63    * The visual will parse the SVG image once it is set.
64    * And rasterize it into BufferImage synchronously when the associated actor is put on stage, and destroy the BufferImage when it is off stage
65    *
66    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
67    * @param[in] shaderFactory The ImageVisualShaderFactory object
68    * @param[in] imageUrl The URL to svg resource to use
69    * @param[in] properties A Property::Map containing settings for this visual
70    * @return A smart-pointer to the newly allocated visual.
71    */
72   static SvgVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties );
73
74   /**
75    * @brief Create the SVG Visual using the image URL.
76    *
77    * The visual will parse the SVG image once it is set.
78    * And rasterize it into BufferImage synchronously when the associated actor is put on stage, and destroy the BufferImage when it is off stage
79    *
80    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
81    * @param[in] shaderFactory The ImageVisualShaderFactory object
82    * @param[in] imageUrl The URL to svg resource to use
83    * @return A smart-pointer to the newly allocated visual.
84    */
85   static SvgVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl );
86
87 public:  // from Visual
88
89   /**
90    * @copydoc Visual::Base::GetNaturalSize
91    */
92   void GetNaturalSize( Vector2& naturalSize ) override;
93
94   /**
95    * @copydoc Visual::Base::CreatePropertyMap
96    */
97   void DoCreatePropertyMap( Property::Map& map ) const override;
98
99   /**
100    * @copydoc Visual::Base::CreateInstancePropertyMap
101    */
102   void DoCreateInstancePropertyMap( Property::Map& map ) const override;
103
104 protected:
105
106   /**
107    * @brief Constructor.
108    *
109    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
110    * @param[in] shaderFactory The ImageVisualShaderFactory object
111    * @param[in] imageUrl The URL to svg resource to use
112    */
113   SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl );
114
115   /**
116    * @brief A reference counted object may only be deleted by calling Unreference().
117    */
118   virtual ~SvgVisual();
119
120   /**
121    * @copydoc Visual::Base::DoSetProperties
122    */
123   void DoSetProperties( const Property::Map& propertyMap ) override;
124
125   /**
126    * @copydoc Visual::Base::DoSetOnScene
127    */
128   void DoSetOnScene( Actor& actor ) override;
129
130   /**
131    * @copydoc Visual::Base::DoSetOffScene
132    */
133   void DoSetOffScene( Actor& actor ) override;
134
135   /**
136    * @copydoc Visual::Base::OnSetTransform
137    */
138   void OnSetTransform() override;
139
140   /**
141    * @copydoc Visual::Base::IsResourceReady
142    */
143   bool IsResourceReady() const override;
144
145 public:
146
147 #ifdef NO_THORVG
148   /**
149    * @bried Apply the rasterized image to the visual.
150    *
151    * @param[in] parsedSvg The data of parsed image.
152    * @param[in] rasterizedPixelData The pixel buffer with the rasterized pixels
153    */
154   void ApplyRasterizedImage( NSVGimage* parsedSvg, PixelData rasterizedPixelData );
155 #else /* NO_THORVG */
156   /**
157    * @bried Apply the rasterized image to the visual.
158    *
159    * @param[in] vectorImage The data of vector image.
160    * @param[in] rasterizedPixelData The pixel buffer with the rasterized pixels
161    * @param[in] bool Whether the resource is loaded
162    */
163   void ApplyRasterizedImage( VectorImageRenderer vectorImage, PixelData rasterizedPixelData, bool isLoaded );
164 #endif /* NO_THORVG */
165
166 private:
167 #ifdef NO_THORVG
168   /**
169     * @brief Parses the SVG Image from the set URL.
170     *
171     * @param[in] imageUrl The URL of the image to parse the SVG from.
172     */
173    void ParseFromUrl( const VisualUrl& imageUrl );
174 #else /* NO_THORVG */
175   /**
176     * @brief Load the SVG Image from the set URL.
177     */
178    void Load();
179 #endif /* NO_THORVG */
180
181   /**
182    * @bried Rasterize the svg with the given size, and add it to the visual.
183    *
184    * @param[in] size The target size of the SVG rasterization.
185    */
186   void AddRasterizationTask( const Vector2& size );
187
188   /**
189    * Helper method to set individual values by index key.
190    * @param[in] index The index key of the value
191    * @param[in] value The value
192    */
193   void DoSetProperty( Property::Index index, const Property::Value& value );
194
195   // Undefined
196   SvgVisual( const SvgVisual& svgRenderer );
197
198   // Undefined
199   SvgVisual& operator=( const SvgVisual& svgRenderer );
200
201 private:
202   ImageVisualShaderFactory& mImageVisualShaderFactory;
203   Vector4                   mAtlasRect;
204   VisualUrl                 mImageUrl;
205 #ifdef NO_THORVG
206   NSVGimage*                mParsedImage;
207 #else /* NO_THORVG */
208   VectorImageRenderer       mVectorRenderer;
209   uint32_t                  mDefaultWidth;
210   uint32_t                  mDefaultHeight;
211   bool                      mLoaded;
212   bool                      mLocalResource;
213 #endif /* NO_THORVG */
214   WeakHandle<Actor>         mPlacementActor;
215   Vector2                   mVisualSize;
216   bool                      mAttemptAtlasing;  ///< If true will attempt atlasing, otherwise create unique texture
217 };
218
219 } // namespace Internal
220
221 } // namespace Toolkit
222
223 } // namespace Dali
224
225 #endif /* DALI_TOOLKIT_INTERNAL_SVG_VISUAL_H */