2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Runtime.InteropServices;
20 using System.ComponentModel;
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.
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.
34 internal sealed class ProcessorController : Disposable
36 private static ProcessorController instance = null;
37 private bool initialied = false;
39 private ProcessorController() : this(Interop.ProcessorController.New(), true)
43 internal ProcessorController(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
47 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
48 internal delegate void ProcessorEventHandler();
50 private ProcessorEventHandler processorCallback = null;
52 private uint onceEventIndex;
53 // Double buffered once event processing
54 private EventHandler[] internalProcessorOnceEvent;
56 public event EventHandler ProcessorOnceEvent
60 internalProcessorOnceEvent[onceEventIndex] += value;
64 internalProcessorOnceEvent[onceEventIndex] -= value;
67 public event EventHandler ProcessorEvent;
68 public event EventHandler LayoutProcessorEvent;
70 public bool Initialized => initialied;
72 public static ProcessorController Instance
78 instance = new ProcessorController();
84 public void Initialize()
86 if (initialied == false)
90 Interop.ProcessorController.Initialize(SwigCPtr);
93 internalProcessorOnceEvent = new EventHandler[2];
94 internalProcessorOnceEvent[0] = null;
95 internalProcessorOnceEvent[1] = null;
97 processorCallback = new ProcessorEventHandler(Process);
98 Interop.ProcessorController.SetCallback(SwigCPtr, processorCallback);
99 NDalicPINVOKE.ThrowExceptionIfExists();
103 public void Process()
105 // Let us add once event into 1 index during 0 event invoke
107 internalProcessorOnceEvent[0]?.Invoke(this, null);
108 internalProcessorOnceEvent[0] = null;
110 ProcessorEvent?.Invoke(this, null);
111 LayoutProcessorEvent?.Invoke(this, null);
113 // Let us add once event into 0 index during 1 event invoke
114 // Note : To avoid ImageView's properties mismatched problem,
115 // We need to invoke events now which attached during internalProcessorOnceEvent[0] and LayoutProcessor.
117 internalProcessorOnceEvent[1]?.Invoke(this, null);
118 internalProcessorOnceEvent[1] = null;
122 /// Dispose ProcessorController.
124 [EditorBrowsable(EditorBrowsableState.Never)]
125 protected override void Dispose(DisposeTypes type)
127 Interop.ProcessorController.RemoveCallback(SwigCPtr, processorCallback);
128 internalProcessorOnceEvent[0] = null;
129 internalProcessorOnceEvent[1] = null;
130 ProcessorEvent = null;
131 LayoutProcessorEvent = null;
132 GC.SuppressFinalize(this);
138 /// Awake ProcessorController.
139 /// It will call ProcessController.processorCallback and ProcessController.processorPostCallback hardly.
142 /// When event thread is not in idle state, This function will be ignored.
144 [EditorBrowsable(EditorBrowsableState.Never)]
147 Interop.ProcessorController.Awake(SwigCPtr);
148 NDalicPINVOKE.ThrowExceptionIfExists();
150 } // class ProcessorController
151 } // namespace Tizen.NUI