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