Skip to content

Commit

Permalink
[NUI] Support EncodedImageBuffer ImageType property + Upgrade Encoded…
Browse files Browse the repository at this point in the history
…ImageBuffer demo

Let we allow to give the type of buffer s.t. what buffer is mean.
We can load svg and lottie image as encoded image buffer now.

+

Let we make EncodedImageBuffer can be created at worker thread.
And make the demo to load buffer asynchronously.

This patch have dependency with below dali patch :

https://review.tizen.org/gerrit/c/platform/core/uifw/dali-csharp-binder/+/301314

Signed-off-by: Eunki, Hong <[email protected]>
  • Loading branch information
Eunki, Hong authored and hinohie committed Nov 14, 2023
1 parent 70cbc01 commit 08f1eb2
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ internal static partial class EncodedImageBuffer
[DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_EncodedImageBuffer_New")]
public static extern IntPtr New(global::System.Runtime.InteropServices.HandleRef jarg1);

[DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_EncodedImageBuffer_New__SWIG_1")]
public static extern IntPtr New(global::System.Runtime.InteropServices.HandleRef jarg1, int imageType);

[DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_new_EncodedImageBuffer__SWIG_0")]
public static extern IntPtr NewEncodedImageBuffer();

Expand All @@ -36,6 +39,12 @@ internal static partial class EncodedImageBuffer
[DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_delete_EncodedImageBuffer")]
public static extern void DeleteEncodedImageBuffer(global::System.Runtime.InteropServices.HandleRef jarg1);

[DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_EncodedImageBuffer_SetImageType")]
public static extern void SetImageType(global::System.Runtime.InteropServices.HandleRef jarg1, int imageType);

[DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_EncodedImageBuffer_GetImageType")]
public static extern int GetImageType(global::System.Runtime.InteropServices.HandleRef jarg1);

[DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_EncodedImageBuffer_GetRawBuffer")]
public static extern IntPtr GetRawBuffer(IntPtr handle);

Expand Down
76 changes: 73 additions & 3 deletions src/Tizen.NUI/src/public/Images/EncodedImageBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ public class EncodedImageBuffer : BaseHandle
{
private VectorUnsignedChar mCachedBuffer = null; // cached encoded raw buffer

/// <summary>
/// The list of type of encoded image buffer.
/// It will be used when we want to specify the buffer data type.
/// </summary>
/// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1717:Only FlagsAttribute enums should have plural names")]
public enum ImageTypes
{
/// <summary>
/// Regular images.
/// </summary>
/// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
RegularImage = 0,

/// <summary>
/// Vector rasterize images.
/// </summary>
/// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
VectorImage,

/// <summary>
/// Animated vector rasterize images.
/// </summary>
/// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
AnimatedVectorImage,
}

/// <summary>
/// Constructor.
/// </summary>
Expand All @@ -45,19 +76,58 @@ public class EncodedImageBuffer : BaseHandle
/// <exception cref="InvalidOperationException"> Thrown when stream don't have any data. </exception>
/// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public EncodedImageBuffer(System.IO.Stream stream) : this(GetRawBuffrFromStreamHelper(stream))
public EncodedImageBuffer(System.IO.Stream stream) : this(GetRawBuffrFromStreamHelper(stream), ImageTypes.RegularImage)
{
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}

/// <summary>
/// Constructor with image type.
/// </summary>
/// <param name="stream">The Stream of the image file.</param>
/// <param name="imageType">The type of the image stream.</param>
/// <exception cref="ArgumentNullException"> Thrown when stream is null. </exception>
/// <exception cref="InvalidOperationException"> Thrown when stream don't have any data. </exception>
/// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public EncodedImageBuffer(System.IO.Stream stream, ImageTypes imageType) : this(GetRawBuffrFromStreamHelper(stream), imageType)
{
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}

internal EncodedImageBuffer(VectorUnsignedChar buffer) : this(Interop.EncodedImageBuffer.New(VectorUnsignedChar.getCPtr(buffer)), true)
internal EncodedImageBuffer(VectorUnsignedChar buffer, ImageTypes imageType) : this(Interop.EncodedImageBuffer.New(VectorUnsignedChar.getCPtr(buffer), (int)imageType), true)
{
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
mCachedBuffer = buffer;
}

internal EncodedImageBuffer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
internal EncodedImageBuffer(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, false)
{
// Note : EncodedImageBuffer don't need to be register in Registry default. So we can create this class from worker thread.
}

internal EncodedImageBuffer(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
{
}

/// <summary>
/// The type of image for this EncodedImageBuffer.
/// </summary>
/// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public ImageTypes ImageType
{
set
{
Interop.EncodedImageBuffer.SetImageType(SwigCPtr, (int)value);
NDalicPINVOKE.ThrowExceptionIfExists();
}
get
{
ImageTypes ret = (ImageTypes)Interop.EncodedImageBuffer.GetImageType(SwigCPtr);
NDalicPINVOKE.ThrowExceptionIfExists();
return ret;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,91 @@

using Tizen.NUI.BaseComponents;
using Tizen.NUI.BaseComponents;
using Tizen.NUI.Components;

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Tizen.NUI.Samples
{
public class EncodedImageTest : IExample
{
Window win;
ImageView imageView;
int index = 0;
Timer timer;

static private string DEMO_IMAGE_DIR = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "images/";

static public string DEMO_IMAGE_DIR = CommonResource.GetDaliResourcePath() + "DaliDemo/";
static private readonly List<(string, EncodedImageBuffer.ImageTypes)> TestImages = new()
{
(DEMO_IMAGE_DIR + "Dali/DaliDemo/Logo-for-demo.png", EncodedImageBuffer.ImageTypes.RegularImage),
(DEMO_IMAGE_DIR + "Dali/DaliDemo/Kid1.svg", EncodedImageBuffer.ImageTypes.VectorImage),
(DEMO_IMAGE_DIR + "../a.json", EncodedImageBuffer.ImageTypes.AnimatedVectorImage),
};

public void Activate()
{
win = NUIApplication.GetDefaultWindow();


SetImage(index);
}

private bool OnTick(object o, Timer.TickEventArgs e)
{
index = (index + 1) % TestImages.Count;
SetImage(index);
return false;
}

private void OnResourceReady(object o, global::System.EventArgs e)
{
timer = new Timer(2000);
timer.Tick += OnTick;
timer.Start();
}

private async void SetImage(int index)
{
var encodedTask = CreateEncodedImageBufferAsync(TestImages[index].Item1, TestImages[index].Item2);

imageView?.Unparent();
imageView?.Dispose();

EncodedImageBuffer buffer;
ImageUrl imageUrl;

buffer = CreateEncodedImageBuffer(DEMO_IMAGE_DIR + "Logo-for-demo.png");

imageUrl = buffer?.GenerateUrl();

imageView = new ImageView()
{
WidthResizePolicy = ResizePolicyType.FillToParent,
HeightResizePolicy = ResizePolicyType.FillToParent,

ResourceUrl = imageUrl?.ToString(),
};
imageView.ResourceReady += OnResourceReady;

buffer = await encodedTask;

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

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

win.GetDefaultLayer().Add(imageView);
}

private EncodedImageBuffer CreateEncodedImageBuffer(string filename)
private async Task<EncodedImageBuffer> CreateEncodedImageBufferAsync(string filename, EncodedImageBuffer.ImageTypes imageType)
{
EncodedImageBuffer buffer = null;
global::System.IO.Stream stream = new global::System.IO.FileStream(filename, global::System.IO.FileMode.Open);
buffer = new EncodedImageBuffer(stream);
buffer = new EncodedImageBuffer(stream, imageType);
return buffer;
}

public void Deactivate()
{
timer?.Stop();
timer?.Dispose();
imageView?.Unparent();
imageView?.Dispose();
}
Expand Down

0 comments on commit 08f1eb2

Please sign in to comment.