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