[NUI] Update ImageView properties lazy
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Common / ProcessorController.cs
1 /*
2  * Copyright (c) 2021 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 using System.Runtime.InteropServices;
19 using System;
20 using System.ComponentModel;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// Singleton class.
26     /// ProcessorController handles ProcessorCallbacks those are requested to be called for each end of event process.
27     /// If there are works those required to be called after all of the event process for the efficiency, add them ProcessorController.
28     /// The calling order is not guaranteed but ProcessorCallbackes of LayoutController those are added by using AddLayoutControllerProcessor,
29     /// are called after ordinaly ProcessCallbacks added by AddProcessor.
30     ///
31     /// The added callbacks will be called every time of event tick.
32     /// If the callback is not be wanted to be called anymore, remove it.
33     /// </summary>
34     internal sealed class ProcessorController : Disposable
35     {
36         private static ProcessorController instance = null;
37
38         private ProcessorController() : this(Interop.ProcessorController.New(), true)
39         {
40         }
41
42         internal ProcessorController(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
43         {
44             processorCallback = new ProcessorEventHandler(Process);
45             Interop.ProcessorController.SetCallback(SwigCPtr, processorCallback);
46         }
47
48         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
49         internal delegate void ProcessorEventHandler();
50
51         private ProcessorEventHandler processorCallback = null;
52         private bool awaken = false;
53
54         public event EventHandler ProcessorOnceEvent;
55         public event EventHandler ProcessorEvent;
56         public event EventHandler LayoutProcessorEvent;
57
58         public static ProcessorController Instance
59         {
60             get
61             {
62                 if (instance == null)
63                 {
64                     instance = new ProcessorController();
65                 }
66                 return instance;
67             }
68         }
69
70         public void Process()
71         {
72             awaken = false;
73             ProcessorOnceEvent?.Invoke(this, null);
74             ProcessorOnceEvent = null;
75             ProcessorEvent?.Invoke(this, null);
76             LayoutProcessorEvent?.Invoke(this, null);
77         }
78
79         /// <summary>
80         /// Dispose ProcessorController.
81         /// </summary>
82         [EditorBrowsable(EditorBrowsableState.Never)]
83         protected override void Dispose(DisposeTypes type)
84         {
85             Interop.ProcessorController.RemoveCallback(SwigCPtr, processorCallback);
86             ProcessorOnceEvent = null;
87             ProcessorEvent = null;
88             LayoutProcessorEvent = null;
89             GC.SuppressFinalize(this);
90
91             base.Dispose(type);
92         }
93
94         /// <summary>
95         /// Awake ProcessorController. It will call ProcessController.processorCallback hardly
96         /// </summary>
97         [EditorBrowsable(EditorBrowsableState.Never)]
98         public void Awake()
99         {
100             if(!awaken)
101             {
102                 Interop.ProcessorController.Awake(SwigCPtr);
103                 awaken = true;
104             }
105         }
106     } // class ProcessorController
107 } // namespace Tizen.NUI