[NUI] Change for AddFrameRenderedCallback to maintain the reference of user callback...
authordongsug-song <35130733+dongsug-song@users.noreply.github.com>
Tue, 21 Jul 2020 03:38:17 +0000 (12:38 +0900)
committerGitHub <noreply@github.com>
Tue, 21 Jul 2020 03:38:17 +0000 (12:38 +0900)
src/Tizen.NUI/src/public/Window.cs
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/AddFrameRenderedCallbackTest.cs
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/CaptureTest.cs

index 6f01fe3..4d17177 100755 (executable)
@@ -1311,6 +1311,60 @@ namespace Tizen.NUI
             Interop.Window.delete_Window(swigCPtr);
         }
 
+        private static Dictionary<int, internalHookCallbackType> frameCallbackList = new Dictionary<int, internalHookCallbackType>();
+
+        private static readonly object locker = new object();
+
+        private static int key = 0;
+
+        private static FrameCallbackType internalHookFrameCallback = OnInternalHookFrameCallback;
+
+        private struct internalHookCallbackType
+        {
+            public FrameCallbackType userCallback;
+            public int frameId;
+        }
+
+        private static void OnInternalHookFrameCallback(int id)
+        {
+            lock (locker)
+            {
+                if (frameCallbackList.ContainsKey(id))
+                {
+                    if (frameCallbackList[id].userCallback != null)
+                    {
+                        frameCallbackList[id].userCallback.Invoke(frameCallbackList[id].frameId);
+                        frameCallbackList.Remove(id);
+                    }
+                    else
+                    {
+                        NUILog.Error($"found userCallback is NULL");
+                        frameCallbackList.Remove(id);
+                    }
+                }
+            }
+        }
+
+        private int AddInterHookCallback(FrameCallbackType callback, int frameId)
+        {
+            if (null == callback)
+            {
+                throw new ArgumentNullException(nameof(callback), "FrameCallbackType should not be null");
+            }
+            var assignedKey = 0;
+            lock (locker)
+            {
+                key++;
+                assignedKey = key;
+                frameCallbackList.Add(assignedKey, new internalHookCallbackType()
+                {
+                    userCallback = callback,
+                    frameId = frameId,
+                });
+            }
+            return assignedKey;
+        }
+
         /// <summary>
         /// Type of callback which is called when the frame rendering is done by graphics driver or when the frame is displayed on display.
         /// </summary>
@@ -1335,14 +1389,8 @@ namespace Tizen.NUI
         [EditorBrowsable(EditorBrowsableState.Never)]
         public void AddFrameRenderedCallback(FrameCallbackType callback, int frameId)
         {
-            IntPtr ip = Marshal.GetFunctionPointerForDelegate<Delegate>(callback);
-
-            if (IntPtr.Zero == ip)
-            {
-                throw new ArgumentNullException(nameof(callback), "FrameCallbackType should not be null");
-            }
-
-            Interop.WindowInternal.AddFrameRenderedCallback(swigCPtr, new HandleRef(this, ip), frameId);
+            var assignedKey = AddInterHookCallback(callback, frameId);
+            Interop.WindowInternal.AddFrameRenderedCallback(swigCPtr, new HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(internalHookFrameCallback)), assignedKey);
 
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
@@ -1364,14 +1412,8 @@ namespace Tizen.NUI
         [EditorBrowsable(EditorBrowsableState.Never)]
         public void AddFramePresentedCallback(FrameCallbackType callback, int frameId)
         {
-            IntPtr ip = Marshal.GetFunctionPointerForDelegate<Delegate>(callback);
-
-            if (IntPtr.Zero == ip)
-            {
-                throw new ArgumentNullException(nameof(callback), "FrameCallbackType should not be null");
-            }
-
-            Interop.WindowInternal.AddFramePresentedCallback(swigCPtr, new HandleRef(this, ip), frameId);
+            var assignedKey = AddInterHookCallback(callback, frameId);
+            Interop.WindowInternal.AddFramePresentedCallback(swigCPtr, new HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(internalHookFrameCallback)), assignedKey);
 
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
index cc21b81..016c94c 100755 (executable)
@@ -22,13 +22,18 @@ namespace Tizen.NUI.Samples
 
         Window win;
         Window.FrameCallbackType cb;
+        TextLabel tl;
+        View view;
         void test()
         {
+            tlog.Fatal(tag, $"test start");
             win = NUIApplication.GetDefaultWindow();
             win.TouchEvent += WinTouchEvent;
-            View view = new View();
+            view = new View();
             view.Size = new Size(100, 100);
             view.BackgroundColor = Color.Blue;
+            view.Focusable = true;
+            view.KeyEvent += View_KeyEvent;
             win.Add(view);
 
             cb = testCallback;
@@ -38,6 +43,14 @@ namespace Tizen.NUI.Samples
             Timer timer = new Timer(5000);
             timer.Tick += testOnTick;
             timer.Start();
+
+            tl = new TextLabel("frameId");
+            tl.Size = new Size(500, 200);
+            tl.Position = new Position(10, 200);
+            tl.BackgroundColor = Color.White;
+            win.Add(tl);
+
+            FocusManager.Instance.SetCurrentFocusView(view);
         }
 
         private void WinTouchEvent(object sender, Window.TouchEventArgs e)
@@ -60,7 +73,49 @@ namespace Tizen.NUI.Samples
 
         void testCallback(int id)
         {
-            Console.WriteLine($"testCallback() id={id}");
+            tlog.Fatal(tag, $"testCallback() id={id}");
+            tl.Text = $"frameId={id}";
+        }
+
+        private bool View_KeyEvent(object source, View.KeyEventArgs e)
+        {
+            if (e.Key.State == Key.StateType.Down)
+            {
+                if (e.Key.KeyPressedName == "1")
+                {
+                    cb = testCallback;
+                    win.AddFrameRenderedCallback(cb, cnt++);
+                    tlog.Fatal(tag, $"1) testOnTick() AddFrameRenderedCallback() send id={cnt}");
+                }
+                else if (e.Key.KeyPressedName == "2")
+                {
+                    cb = testCallback;
+                    win.AddFramePresentedCallback(cb, cnt++);
+                    tlog.Fatal(tag, $"2) testOnTick() AddFramePresentedCallback() send id={cnt}");
+                }
+                else if (e.Key.KeyPressedName == "3")
+                {
+                    cb = testCallback;
+                    win.AddFramePresentedCallback(cb, cnt++);
+                    cb = null;
+                    tlog.Fatal(tag, $"3) testOnTick() AddFramePresentedCallback() send id={cnt}");
+                }
+                else if (e.Key.KeyPressedName == "4")
+                {
+                    win.AddFrameRenderedCallback((int id) =>
+                    {
+                        tlog.Fatal(tag, $"testCallback() id={id}");
+                        tl.Text = $"frameId={id}";
+                    }, cnt++);
+                    tlog.Fatal(tag, $"4) testOnTick() AddFrameRenderedCallback() send id={cnt}");
+                }
+                else if (e.Key.KeyPressedName == "Return")
+                {
+                    Random rand = new Random();
+                    view.BackgroundColor = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble(), 1);
+                }
+            }
+            return true;
         }
 
 
index c4e2612..ccf0f33 100755 (executable)
@@ -51,7 +51,7 @@ namespace Tizen.NUI.Samples
             //checkCaptureNew();
         }
 
-        private void onCaptureFinished(object sender, CaptureFinishStateEventArgs e)
+        private void onCaptureFinished(object sender, CaptureFinishedEventArgs e)
         {
             log.Debug(tag, $"onCaptureFinished() statue={e.Success} \n");