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