132a6e48fSopenharmony_ci/*
232a6e48fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
332a6e48fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
432a6e48fSopenharmony_ci * you may not use this file except in compliance with the License.
532a6e48fSopenharmony_ci * You may obtain a copy of the License at
632a6e48fSopenharmony_ci *
732a6e48fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
832a6e48fSopenharmony_ci *
932a6e48fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1032a6e48fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1132a6e48fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1232a6e48fSopenharmony_ci * See the License for the specific language governing permissions and
1332a6e48fSopenharmony_ci * limitations under the License.
1432a6e48fSopenharmony_ci */
1532a6e48fSopenharmony_ci
1632a6e48fSopenharmony_ci#ifndef UTILS_INCLUDE_SYNC_FENCE_TRACKER_H
1732a6e48fSopenharmony_ci#define UTILS_INCLUDE_SYNC_FENCE_TRACKER_H
1832a6e48fSopenharmony_ci
1932a6e48fSopenharmony_ci#include <atomic>
2032a6e48fSopenharmony_ci#include <event_handler.h>
2132a6e48fSopenharmony_ci#include <queue>
2232a6e48fSopenharmony_ci#include "sync_fence.h"
2332a6e48fSopenharmony_ci
2432a6e48fSopenharmony_cinamespace OHOS {
2532a6e48fSopenharmony_ciclass SyncFenceTracker {
2632a6e48fSopenharmony_cipublic:
2732a6e48fSopenharmony_ci    explicit SyncFenceTracker(const std::string threadName);
2832a6e48fSopenharmony_ci
2932a6e48fSopenharmony_ci    SyncFenceTracker() = delete;
3032a6e48fSopenharmony_ci    ~SyncFenceTracker() = default;
3132a6e48fSopenharmony_ci
3232a6e48fSopenharmony_ci    void TrackFence(const sptr<SyncFence>& fence, bool traceTag = true);
3332a6e48fSopenharmony_ci    void SetBlurSize(int32_t blurSize);
3432a6e48fSopenharmony_ci    void SetContainerNodeNum(int containerNodeNum);
3532a6e48fSopenharmony_ci
3632a6e48fSopenharmony_ciprivate:
3732a6e48fSopenharmony_ci    const uint32_t SYNC_TIME_OUT = 3000;
3832a6e48fSopenharmony_ci    const int32_t GPU_SUBHEALTH_EVENT_LIMIT = 200;
3932a6e48fSopenharmony_ci    const int32_t GPU_SUBHEALTH_EVENT_THRESHOLD = 12;
4032a6e48fSopenharmony_ci    const uint32_t FRAME_QUEUE_SIZE_LIMIT = 4;
4132a6e48fSopenharmony_ci    const int32_t FRAME_PERIOD = 1000;
4232a6e48fSopenharmony_ci    const std::string threadName_;
4332a6e48fSopenharmony_ci    bool isGpuFence_ = false;
4432a6e48fSopenharmony_ci    bool isGpuEnable_ = false;
4532a6e48fSopenharmony_ci    std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ = nullptr;
4632a6e48fSopenharmony_ci    std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ = nullptr;
4732a6e48fSopenharmony_ci    std::atomic<uint32_t> fencesQueued_;
4832a6e48fSopenharmony_ci    std::atomic<uint32_t> fencesSignaled_;
4932a6e48fSopenharmony_ci    int32_t gpuSubhealthEventNum_ = 0;
5032a6e48fSopenharmony_ci    int32_t gpuSubhealthEventDay_ = 0;
5132a6e48fSopenharmony_ci    std::queue<int32_t> *frameStartTimes_ = new std::queue<int32_t>;
5232a6e48fSopenharmony_ci    int processedNodeNum_ = 0;
5332a6e48fSopenharmony_ci    void Loop(const sptr<SyncFence>& fence, bool traceTag);
5432a6e48fSopenharmony_ci    int32_t WaitFence(const sptr<SyncFence>& fence);
5532a6e48fSopenharmony_ci    bool CheckGpuSubhealthEventLimit();
5632a6e48fSopenharmony_ci    void ReportEventGpuSubhealth(int32_t duration);
5732a6e48fSopenharmony_ci    inline void UpdateFrameQueue(int32_t startTime);
5832a6e48fSopenharmony_ci    int32_t GetFrameRate();
5932a6e48fSopenharmony_ci};
6032a6e48fSopenharmony_ci}
6132a6e48fSopenharmony_ci#endif // UTILS_INCLUDE_SYNC_FENCE_TRACKER_H