Resolve incorrect position for Ellipsis when mixed LTR & RTL languages and set layout...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-visual.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "svg-visual.h"
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
23 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
24 #include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
25 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
26 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
27 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
28
29 // EXTERNAL INCLUDES
30 #include <dali/devel-api/common/stage.h>
31 #include <dali/integration-api/debug.h>
32
33 namespace Dali
34 {
35 namespace Toolkit
36 {
37 namespace Internal
38 {
39 namespace
40 {
41 const int CUSTOM_PROPERTY_COUNT(6); // atlas + corner/border
42
43 // property name
44 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
45
46 } // namespace
47
48 SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
49 {
50   SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl));
51   svgVisual->SetProperties(properties);
52   svgVisual->Initialize();
53   return svgVisual;
54 }
55
56 SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
57 {
58   SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl));
59   svgVisual->Initialize();
60   return svgVisual;
61 }
62
63 SvgVisual::SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
64 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::SVG),
65   mImageVisualShaderFactory(shaderFactory),
66   mAtlasRect(FULL_TEXTURE_RECT),
67   mImageUrl(imageUrl),
68   mVectorRenderer(VectorImageRenderer::New()),
69   mDefaultWidth(0),
70   mDefaultHeight(0),
71   mPlacementActor(),
72   mRasterizedSize(Vector2::ZERO),
73   mLoadFailed(false),
74   mAttemptAtlasing(false)
75 {
76   // the rasterized image is with pre-multiplied alpha format
77   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
78 }
79
80 SvgVisual::~SvgVisual()
81 {
82 }
83
84 void SvgVisual::OnInitialize()
85 {
86   Shader   shader   = GenerateShader();
87   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
88   mImpl->mRenderer  = VisualRenderer::New(geometry, shader);
89   mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
90
91   Vector2 dpi     = Stage::GetCurrent().GetDpi();
92   float   meanDpi = (dpi.height + dpi.width) * 0.5f;
93
94   SvgTaskPtr newTask = new SvgLoadingTask(this, mVectorRenderer, mImageUrl, meanDpi);
95
96   if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
97   {
98     newTask->Process();
99   }
100   else
101   {
102     mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
103   }
104 }
105
106 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
107 {
108   // url already passed in from constructor
109   for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
110   {
111     KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
112     if(keyValue.first.type == Property::Key::INDEX)
113     {
114       DoSetProperty(keyValue.first.indexKey, keyValue.second);
115     }
116     else if(keyValue.first == IMAGE_ATLASING)
117     {
118       DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
119     }
120     else if(keyValue.first == SYNCHRONOUS_LOADING)
121     {
122       DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
123     }
124   }
125 }
126
127 void SvgVisual::DoSetProperty(Property::Index index, const Property::Value& value)
128 {
129   switch(index)
130   {
131     case Toolkit::ImageVisual::Property::ATLASING:
132     {
133       value.Get(mAttemptAtlasing);
134       break;
135     }
136     case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
137     {
138       bool sync = false;
139       if(value.Get(sync))
140       {
141         if(sync)
142         {
143           mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
144         }
145         else
146         {
147           mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
148         }
149       }
150       else
151       {
152         DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
153       }
154       break;
155     }
156   }
157 }
158
159 void SvgVisual::DoSetOnScene(Actor& actor)
160 {
161   TextureSet textureSet = TextureSet::New();
162   mImpl->mRenderer.SetTextures(textureSet);
163
164   // Register transform properties
165   mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
166
167   // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
168
169   // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
170   mPlacementActor = actor;
171
172   if(mLoadFailed)
173   {
174     Vector2 imageSize = Vector2::ZERO;
175     imageSize         = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
176     mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
177     actor.AddRenderer(mImpl->mRenderer);
178
179     ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
180   }
181   else
182   {
183     if(mImpl->mEventObserver)
184     {
185       // SVG visual needs it's size set before it can be rasterized hence request relayout once on stage
186       mImpl->mEventObserver->RelayoutRequest(*this);
187     }
188   }
189 }
190
191 void SvgVisual::DoSetOffScene(Actor& actor)
192 {
193   mFactoryCache.GetSVGRasterizationThread()->RemoveTask(this);
194
195   actor.RemoveRenderer(mImpl->mRenderer);
196   mPlacementActor.Reset();
197
198   // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
199   mRasterizedSize = Vector2::ZERO;
200 }
201
202 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
203 {
204   if(mLoadFailed && mImpl->mRenderer)
205   {
206     // Load failed, use broken image size
207     auto textureSet = mImpl->mRenderer.GetTextures();
208     if(textureSet && textureSet.GetTextureCount())
209     {
210       auto texture = textureSet.GetTexture(0);
211       if(texture)
212       {
213         naturalSize.x = texture.GetWidth();
214         naturalSize.y = texture.GetHeight();
215         return;
216       }
217     }
218   }
219   else
220   {
221     naturalSize.x = mDefaultWidth;
222     naturalSize.y = mDefaultHeight;
223   }
224 }
225
226 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
227 {
228   map.Clear();
229   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG);
230   if(mImageUrl.IsValid())
231   {
232     map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
233     map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
234   }
235   map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
236 }
237
238 void SvgVisual::DoCreateInstancePropertyMap(Property::Map& map) const
239 {
240   // Do nothing
241 }
242
243 void SvgVisual::EnablePreMultipliedAlpha(bool preMultiplied)
244 {
245   // Make always enable pre multiplied alpha whether preMultiplied value is false.
246   if(!preMultiplied)
247   {
248     DALI_LOG_WARNING("Note : SvgVisual cannot disable PreMultipliedAlpha\n");
249   }
250 }
251
252 void SvgVisual::AddRasterizationTask(const Vector2& size)
253 {
254   if(mImpl->mRenderer)
255   {
256     unsigned int width  = static_cast<unsigned int>(size.width);
257     unsigned int height = static_cast<unsigned int>(size.height);
258
259     SvgTaskPtr newTask = new SvgRasterizingTask(this, mVectorRenderer, width, height);
260
261     if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
262     {
263       newTask->Process();
264       ApplyRasterizedImage(newTask->GetPixelData(), newTask->HasSucceeded());
265     }
266     else
267     {
268       mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
269     }
270   }
271 }
272
273 void SvgVisual::ApplyRasterizedImage(PixelData rasterizedPixelData, bool success)
274 {
275   if(success)
276   {
277     if(mDefaultWidth == 0 || mDefaultHeight == 0)
278     {
279       mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
280     }
281
282     // Rasterization success
283     if(rasterizedPixelData && IsOnScene())
284     {
285       mRasterizedSize.x = static_cast<float>(rasterizedPixelData.GetWidth());
286       mRasterizedSize.y = static_cast<float>(rasterizedPixelData.GetHeight());
287
288       TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
289       if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
290       {
291         mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
292       }
293
294       TextureSet textureSet;
295
296       if(mAttemptAtlasing && !mImpl->mCustomShader)
297       {
298         Vector4 atlasRect;
299         textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
300         if(textureSet) // atlasing
301         {
302           if(textureSet != currentTextureSet)
303           {
304             mImpl->mRenderer.SetTextures(textureSet);
305           }
306           mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
307           mAtlasRect = atlasRect;
308           mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
309         }
310       }
311
312       if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
313       {
314         Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
315         texture.Upload(rasterizedPixelData);
316         mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
317
318         if(mAtlasRect == FULL_TEXTURE_RECT)
319         {
320           textureSet = currentTextureSet;
321         }
322         else
323         {
324           textureSet = TextureSet::New();
325           mImpl->mRenderer.SetTextures(textureSet);
326
327           mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
328           mAtlasRect = FULL_TEXTURE_RECT;
329         }
330
331         if(textureSet)
332         {
333           textureSet.SetTexture(0, texture);
334         }
335       }
336
337       // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
338       Actor actor = mPlacementActor.GetHandle();
339       if(actor)
340       {
341         actor.AddRenderer(mImpl->mRenderer);
342         // reset the weak handle so that the renderer only get added to actor once
343         mPlacementActor.Reset();
344       }
345
346       // Svg loaded and ready to display
347       ResourceReady(Toolkit::Visual::ResourceStatus::READY);
348     }
349   }
350   else if(!success && !mLoadFailed)
351   {
352     mLoadFailed = true;
353
354     Actor actor = mPlacementActor.GetHandle();
355     if(actor)
356     {
357       Vector2 imageSize = Vector2::ZERO;
358       imageSize         = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
359       mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
360       actor.AddRenderer(mImpl->mRenderer);
361     }
362
363     ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
364   }
365 }
366
367 void SvgVisual::OnSetTransform()
368 {
369   Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
370
371   if(IsOnScene() && !mLoadFailed)
372   {
373     if(visualSize != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0)
374     {
375       mRasterizedSize = visualSize;
376       AddRasterizationTask(visualSize);
377     }
378   }
379
380   if(mImpl->mRenderer)
381   {
382     mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
383   }
384 }
385
386 void SvgVisual::UpdateShader()
387 {
388   if(mImpl->mRenderer)
389   {
390     Shader shader = GenerateShader();
391     mImpl->mRenderer.SetShader(shader);
392   }
393 }
394
395 Shader SvgVisual::GenerateShader() const
396 {
397   Shader shader;
398   if(!mImpl->mCustomShader)
399   {
400     shader = mImageVisualShaderFactory.GetShader(
401       mFactoryCache,
402       ImageVisualShaderFeature::FeatureBuilder()
403         .EnableTextureAtlas(mAttemptAtlasing)
404         .EnableRoundedCorner(IsRoundedCornerRequired())
405         .EnableBorderline(IsBorderlineRequired()));
406   }
407   else
408   {
409     shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
410                          mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
411                          mImpl->mCustomShader->mHints);
412
413     shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
414   }
415   return shader;
416 }
417
418 } // namespace Internal
419
420 } // namespace Toolkit
421
422 } // namespace Dali