1 /* 2 * Copyright (C) 2024 The Android Open Source Project 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 #pragma once 18 19 #include <queue> 20 21 #include "interface/Event.h" 22 23 namespace android::hardware::graphics::composer { 24 25 struct EventQueue { 26 public: 27 EventQueue() = default; 28 postEventEventQueue29 void postEvent(VrrControllerEventType type, TimedEvent& timedEvent) { 30 VrrControllerEvent event; 31 event.mEventType = type; 32 setTimedEventWithAbsoluteTime(timedEvent); 33 event.mWhenNs = timedEvent.mWhenNs; 34 event.mFunctor = std::move(timedEvent.mFunctor); 35 mPriorityQueue.emplace(event); 36 } 37 postEventEventQueue38 void postEvent(VrrControllerEventType type, int64_t when) { 39 VrrControllerEvent event; 40 event.mEventType = type; 41 event.mWhenNs = when; 42 mPriorityQueue.emplace(event); 43 } 44 dropEventEventQueue45 void dropEvent() { mPriorityQueue = std::priority_queue<VrrControllerEvent>(); } 46 dropEventEventQueue47 void dropEvent(VrrControllerEventType event_type) { 48 std::priority_queue<VrrControllerEvent> q; 49 while (!mPriorityQueue.empty()) { 50 const auto& it = mPriorityQueue.top(); 51 if (it.mEventType != event_type) { 52 q.push(it); 53 } 54 mPriorityQueue.pop(); 55 } 56 mPriorityQueue = std::move(q); 57 } 58 getNumberOfEventsEventQueue59 size_t getNumberOfEvents(VrrControllerEventType eventType) { 60 size_t res = 0; 61 std::priority_queue<VrrControllerEvent> q; 62 while (!mPriorityQueue.empty()) { 63 const auto& it = mPriorityQueue.top(); 64 if (it.mEventType == eventType) { 65 ++res; 66 } 67 q.push(it); 68 mPriorityQueue.pop(); 69 } 70 mPriorityQueue = std::move(q); 71 return res; 72 } 73 74 std::priority_queue<VrrControllerEvent> mPriorityQueue; 75 }; 76 77 } // namespace android::hardware::graphics::composer 78