Change the precision of size and uSize from mediumP to highP
[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 struct NSVGimage;
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 class ImageVisualShaderFactory;
41 class SvgVisual;
42 typedef IntrusivePtr< SvgVisual > SvgVisualPtr;
43
44 /**
45  * The visual which renders a svg image
46  *
47  * The following property is essential
48  *
49  * | %Property Name           | Type             |
50  * |--------------------------|------------------|
51  * | url                      | STRING           |
52  *
53  */
54 class SvgVisual: public Visual::Base
55 {
56 public:
57
58   /**
59    * @brief Create the SVG Visual using the image URL.
60    *
61    * The visual will parse the SVG image once it is set.
62    * And rasterize it into BufferImage synchronously when the associated actor is put on stage, and destroy the BufferImage when it is off stage
63    *
64    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
65    * @param[in] shaderFactory The ImageVisualShaderFactory object
66    * @param[in] imageUrl The URL to svg resource to use
67    * @param[in] properties A Property::Map containing settings for this visual
68    * @return A smart-pointer to the newly allocated visual.
69    */
70   static SvgVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties );
71
72   /**
73    * @brief Create the SVG Visual using the image URL.
74    *
75    * The visual will parse the SVG image once it is set.
76    * And rasterize it into BufferImage synchronously when the associated actor is put on stage, and destroy the BufferImage when it is off stage
77    *
78    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
79    * @param[in] shaderFactory The ImageVisualShaderFactory object
80    * @param[in] imageUrl The URL to svg resource to use
81    * @return A smart-pointer to the newly allocated visual.
82    */
83   static SvgVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl );
84
85 public:  // from Visual
86
87   /**
88    * @copydoc Visual::Base::GetNaturalSize
89    */
90   void GetNaturalSize( Vector2& naturalSize ) override;
91
92   /**
93    * @copydoc Visual::Base::CreatePropertyMap
94    */
95   void DoCreatePropertyMap( Property::Map& map ) const override;
96
97   /**
98    * @copydoc Visual::Base::CreateInstancePropertyMap
99    */
100   void DoCreateInstancePropertyMap( Property::Map& map ) const override;
101
102 protected:
103
104   /**
105    * @brief Constructor.
106    *
107    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
108    * @param[in] shaderFactory The ImageVisualShaderFactory object
109    * @param[in] imageUrl The URL to svg resource to use
110    */
111   SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl );
112
113   /**
114    * @brief A reference counted object may only be deleted by calling Unreference().
115    */
116   virtual ~SvgVisual();
117
118   /**
119    * @copydoc Visual::Base::DoSetProperties
120    */
121   void DoSetProperties( const Property::Map& propertyMap ) override;
122
123   /**
124    * @copydoc Visual::Base::DoSetOnScene
125    */
126   void DoSetOnScene( Actor& actor ) override;
127
128   /**
129    * @copydoc Visual::Base::DoSetOffScene
130    */
131   void DoSetOffScene( Actor& actor ) override;
132
133   /**
134    * @copydoc Visual::Base::OnSetTransform
135    */
136   void OnSetTransform() override;
137
138   /**
139    * @copydoc Visual::Base::IsResourceReady
140    */
141   bool IsResourceReady() const override;
142
143 public:
144
145   /**
146    * @bried Apply the rasterized image to the visual.
147    *
148    * @param[in] parsedSvg The data of parsed image.
149    * @param[in] rasterizedPixelData The pixel buffer with the rasterized pixels
150    */
151   void ApplyRasterizedImage( NSVGimage* parsedSvg, PixelData rasterizedPixelData );
152
153 private:
154   /**
155     * @brief Parses the SVG Image from the set URL.
156     *
157     * @param[in] imageUrl The URL of the image to parse the SVG from.
158     */
159    void ParseFromUrl( const VisualUrl& imageUrl );
160
161   /**
162    * @bried Rasterize the svg with the given size, and add it to the visual.
163    *
164    * @param[in] size The target size of the SVG rasterization.
165    */
166   void AddRasterizationTask( const Vector2& size );
167
168   /**
169    * Helper method to set individual values by index key.
170    * @param[in] index The index key of the value
171    * @param[in] value The value
172    */
173   void DoSetProperty( Property::Index index, const Property::Value& value );
174
175   // Undefined
176   SvgVisual( const SvgVisual& svgRenderer );
177
178   // Undefined
179   SvgVisual& operator=( const SvgVisual& svgRenderer );
180
181 private:
182   ImageVisualShaderFactory& mImageVisualShaderFactory;
183   Vector4                   mAtlasRect;
184   VisualUrl                 mImageUrl;
185   NSVGimage*                mParsedImage;
186   WeakHandle<Actor>         mPlacementActor;
187   Vector2                   mVisualSize;
188   bool                      mAttemptAtlasing;  ///< If true will attempt atlasing, otherwise create unique texture
189 };
190
191 } // namespace Internal
192
193 } // namespace Toolkit
194
195 } // namespace Dali
196
197 #endif /* DALI_TOOLKIT_INTERNAL_SVG_VISUAL_H */