1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef GRAPHIC_LITE_TIMER_H
17#define GRAPHIC_LITE_TIMER_H
18
19#include <cstdint>
20#ifdef _WIN32
21#elif defined(__LITEOS_M__)
22#include "cmsis_os2.h"
23#else
24#include <ctime>
25#include <time.h>
26#endif
27
28namespace OHOS {
29class GraphicTimer {
30public:
31    using GraphicTimerCb = void (*)(void*);
32    GraphicTimer(int32_t periodMs, GraphicTimerCb cb, void* arg, bool isPeriodic = false);
33    ~GraphicTimer();
34
35    bool Start();
36    bool SetPeriod(int32_t periodMs);
37    int32_t GetPeriod()
38    {
39        return periodMs_;
40    }
41
42#ifdef _WIN32
43    void* GetNativeTimer()
44#elif defined(__LITEOS_M__)
45    osTimerId_t GetNativeTimer()
46#else
47    timer_t GetNativeTimer()
48#endif
49    {
50        return timer_;
51    }
52
53    void Stop();
54    bool IsPeriodic()
55    {
56        return isPeriodic_;
57    }
58
59    void Callback()
60    {
61        if (cb_ != nullptr) {
62            cb_(arg_);
63        }
64    }
65
66    static constexpr int32_t MAX_PERIOD_MS = 36E5;
67
68private:
69    int32_t periodMs_ = -1;
70    GraphicTimerCb cb_ = nullptr;
71    void* arg_ = nullptr;
72    bool isPeriodic_ = false;
73
74#ifdef _WIN32
75    void* timer_ = nullptr;
76#elif defined(__LITEOS_M__)
77    osTimerId_t timer_;
78#else
79    timer_t timer_;
80#endif
81
82    GraphicTimer() = delete;
83    GraphicTimer(const GraphicTimer&) = delete;
84    GraphicTimer& operator=(const GraphicTimer&) = delete;
85    GraphicTimer(GraphicTimer&&) = delete;
86    GraphicTimer& operator=(GraphicTimer&&) = delete;
87};
88};     // namespace OHOS
89#endif // GRAPHIC_LITE_TIMER_H
90