Skip to content

Commit

Permalink
[NUI] Create ImageVisual synchronously if AlphaMaskURL use generated …
Browse files Browse the repository at this point in the history
…ImageUrl

Since ImageUrl's reference should be hold on image visual, we should generate
image visual synchronously, not on Processor time.

+

Minor code clean up applied

Signed-off-by: Eunki, Hong <[email protected]>
  • Loading branch information
Eunki, Hong committed Dec 18, 2024
1 parent 2217d8a commit 3bc7a1d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 16 deletions.
108 changes: 93 additions & 15 deletions src/Tizen.NUI/src/public/BaseComponents/ImageView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private static string ConvertResourceUrl(ref string value)
// Collection of image-sensitive properties, and need to update C# side cache value.
private static readonly List<int> cachedNUIImageViewPropertyKeyList = new List<int> {
ImageVisualProperty.URL,
ImageVisualProperty.AlphaMaskURL,
ImageVisualProperty.DesiredWidth,
ImageVisualProperty.DesiredHeight,
ImageVisualProperty.FastTrackUploading,
Expand All @@ -155,8 +156,9 @@ private static string ConvertResourceUrl(ref string value)
private bool imagePropertyUpdateProcessAttachedFlag = false;
private Rectangle _border;

// Development Guide : Please make ensure that these 4 values are matched with current image.
// Development Guide : Please make ensure that these 5 values are matched with current image.
private string _resourceUrl = "";
private string _alphaMaskUrl = "";
private int _desired_width = -1;
private int _desired_height = -1;
private bool _fastTrackUploading = false;
Expand Down Expand Up @@ -858,6 +860,7 @@ private MaskingModeType InternalMaskingMode
/// - Seamless visual change didn't supported.<br />
/// - Alpha masking didn't supported. If you try, It will load as normal case.<br />
/// - Synchronous loading didn't supported. If you try, It will load as normal case.<br />
/// - Synchronous sizing didn't supported. If you try, It will load as normal case.<br />
/// - Reload action didn't supported. If you try, It will load as normal case.<br />
/// - Atlas loading didn't supported. If you try, It will load as normal case.<br />
/// - Custom shader didn't supported. If you try, It will load as normal case.
Expand Down Expand Up @@ -910,7 +913,7 @@ private bool InternalFastTrackUploading
UpdateImage(ImageVisualProperty.FastTrackUploading, setValue);
setValue?.Dispose();

if (_fastTrackUploading && !string.IsNullOrEmpty(_resourceUrl))
if (SynchronousVisualCreationRequired())
{
// Special case. If user set FastTrackUploading mean, user want to upload image As-Soon-As-Possible.
// Create ImageVisual synchronously.
Expand Down Expand Up @@ -983,6 +986,36 @@ public void SetImage(string url)
{
UpdateImage(ImageVisualProperty.URL, urlValue, false);
}
if (_desired_width != -1)
{
_desired_width = -1;
using (PropertyValue desiredWidthValue = new PropertyValue(_desired_width))
{
UpdateImage(ImageVisualProperty.DesiredWidth, desiredWidthValue, false);
}
}
if (_desired_height != -1)
{
_desired_height = -1;
using (PropertyValue desiredHeightValue = new PropertyValue(_desired_height))
{
UpdateImage(ImageVisualProperty.DesiredHeight, desiredHeightValue, false);
}
}
if (_fastTrackUploading)
{
_fastTrackUploading = false;
using (PropertyValue fastTrackUploadingValue = new PropertyValue(_fastTrackUploading))
{
UpdateImage(ImageVisualProperty.FastTrackUploading, fastTrackUploadingValue, false);
}
}
if (!string.IsNullOrEmpty(_alphaMaskUrl))
{
_alphaMaskUrl = "";
using PropertyValue emptyValue = new PropertyValue();
UpdateImage(ImageVisualProperty.AlphaMaskURL, emptyValue, false);
}
imagePropertyUpdatedFlag = false;
}

Expand Down Expand Up @@ -1090,17 +1123,13 @@ private string InternalAlphaMaskURL
{
get
{
string ret = "";

PropertyValue maskUrl = GetCachedImageVisualProperty(ImageVisualProperty.AlphaMaskURL);
maskUrl?.Get(out ret);
maskUrl?.Dispose();

return ret;
return _alphaMaskUrl;
}
set
{
PropertyValue setValue = new PropertyValue(value ?? "");
_alphaMaskUrl = value ?? "";

PropertyValue setValue = new PropertyValue(_alphaMaskUrl);
UpdateImage(ImageVisualProperty.AlphaMaskURL, setValue);
// When we never set CropToMask property before, we should set default value as true.
using (PropertyValue cropToMask = GetCachedImageVisualProperty(ImageVisualProperty.CropToMask))
Expand All @@ -1112,6 +1141,11 @@ private string InternalAlphaMaskURL
}
}
setValue?.Dispose();

if (SynchronousVisualCreationRequired())
{
UpdateImage();
}
}
}

Expand Down Expand Up @@ -1911,7 +1945,21 @@ internal void SetImage(string url, Uint16Pair size)
}
using (PropertyValue desiredHeightValue = new PropertyValue(_desired_height))
{
UpdateImage(ImageVisualProperty.DesiredWidth, desiredHeightValue, false);
UpdateImage(ImageVisualProperty.DesiredHeight, desiredHeightValue, false);
}
if (_fastTrackUploading)
{
_fastTrackUploading = false;
using (PropertyValue fastTrackUploadingValue = new PropertyValue(_fastTrackUploading))
{
UpdateImage(ImageVisualProperty.FastTrackUploading, fastTrackUploadingValue, false);
}
}
if (!string.IsNullOrEmpty(_alphaMaskUrl))
{
_alphaMaskUrl = "";
using PropertyValue emptyValue = new PropertyValue();
UpdateImage(ImageVisualProperty.AlphaMaskURL, emptyValue, false);
}
imagePropertyUpdatedFlag = false;
}
Expand Down Expand Up @@ -2046,8 +2094,8 @@ private void SetResourceUrl(string value)
{
UpdateImage(ImageVisualProperty.URL, setValue);
}
// Special case. If we set GeneratedUrl, or FastTrackUploading, Create ImageVisual synchronously.
if (value.StartsWith("dali://") || value.StartsWith("enbuf://") || _fastTrackUploading)

if (SynchronousVisualCreationRequired())
{
UpdateImage();
}
Expand Down Expand Up @@ -2087,7 +2135,10 @@ private void RemoveImage()

// Update resourceUrl as empty value
_resourceUrl = "";
cachedImagePropertyMap[ImageVisualProperty.URL] = emptyValue;
if (cachedImagePropertyMap != null)
{
cachedImagePropertyMap[ImageVisualProperty.URL] = emptyValue;
}
}
}

Expand All @@ -2099,23 +2150,29 @@ internal void SetImageByPropertyMap(PropertyMap map)
cachedImagePropertyMap = null;
MergeCachedImageVisualProperty(map);

// Update _resourceUrl, _desired_width, _desired_height, _fastTrackUploading here.
// Update _resourceUrl, _alphaMaskUrl, _desired_width, _desired_height, _fastTrackUploading here.
// Those values are C# side cached value.
_desired_width = _desired_height = -1;
_fastTrackUploading = false;

if (map != null)
{
_resourceUrl = "";
_alphaMaskUrl = "";
foreach (int key in cachedNUIImageViewPropertyKeyList)
{
using PropertyValue propertyValue = map.Find(key);
if (propertyValue != null)
{
// Note : Since ImageVisualProperty is not a constant value, we cannot use switch-case here.
if (key == ImageVisualProperty.URL)
{
propertyValue.Get(out _resourceUrl);
}
else if (key == ImageVisualProperty.AlphaMaskURL)
{
propertyValue.Get(out _alphaMaskUrl);
}
else if (key == ImageVisualProperty.DesiredWidth)
{
propertyValue.Get(out _desired_width);
Expand Down Expand Up @@ -2406,6 +2463,27 @@ private void OnResourceLoaded(IntPtr view)
}
}

private bool SynchronousVisualCreationRequired()
{
// Special case. If we set GeneratedUrl, or FastTrackUploading, Create ImageVisual synchronously.
if (!string.IsNullOrEmpty(_resourceUrl))
{
if (_fastTrackUploading)
{
return true;
}
if (_resourceUrl.StartsWith("dali://") || _resourceUrl.StartsWith("enbuf://"))
{
return true;
}
if (!string.IsNullOrEmpty(_alphaMaskUrl) && (_alphaMaskUrl.StartsWith("dali://") || _alphaMaskUrl.StartsWith("enbuf://")))
{
return true;
}
}
return false;
}

/// <summary>
/// Event arguments of resource ready.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class EncodedImageTest : IExample
(DEMO_IMAGE_DIR + "../a.json", EncodedImageBuffer.ImageTypes.AnimatedVectorImage),
};

string TestMaskImage = DEMO_IMAGE_DIR + "Dali/DaliDemo/shape-circle.png";

public void Activate()
{
win = NUIApplication.GetDefaultWindow();
Expand Down Expand Up @@ -66,7 +68,18 @@ private async void SetImage(int index)

imageUrl = buffer?.GenerateUrl();
imageView.ResourceUrl = imageUrl?.ToString();
imageView.Play();

if (TestImages[index].Item2 == EncodedImageBuffer.ImageTypes.RegularImage)
{
var encodedMaskTask = CreateEncodedImageBufferAsync(TestMaskImage, EncodedImageBuffer.ImageTypes.RegularImage);
using var maskBuffer = await encodedMaskTask;
using var maskImageUrl = maskBuffer?.GenerateUrl();
imageView.AlphaMaskURL = maskImageUrl?.ToString();
}
else if (TestImages[index].Item2 == EncodedImageBuffer.ImageTypes.AnimatedVectorImage)
{
imageView.Play();
}

imageUrl?.Dispose();
buffer?.Dispose();
Expand Down

0 comments on commit 3bc7a1d

Please sign in to comment.