1 // Copyright 2021 The Android Open Source Project 2 // 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 expresso or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #pragma once 15 16 #include "aemu/base/threads/FunctorThread.h" 17 #include "aemu/base/synchronization/MessageChannel.h" 18 19 #include <inttypes.h> 20 #include <functional> 21 22 namespace gfxstream { 23 24 // A thread that regularly processes tasks that need to happen at some vsync 25 // interval (initialized from FrameBuffer). 26 // 27 // Some examples of such tasks include 28 // 29 // 1. Signaling guest-side hwc present syncfd's at a regular interval so SF can 30 // properly schedule presents 31 // 2. 32 class VsyncThread { 33 public: 34 using Count = uint64_t; 35 using VsyncTask = std::function<void(Count)>; 36 VsyncThread(uint64_t vsyncPeriod); 37 ~VsyncThread(); 38 39 // Runs specificed |task| at the next vsync time as specified by the vsync 40 // period. Passes the number of vsyncs so far (Count). 41 void schedule(VsyncTask task); 42 getCount()43 uint64_t getCount() const { 44 return mCount; 45 } 46 getPeriod()47 uint64_t getPeriod() const { 48 return mPeriodNs; 49 } 50 51 // Sets the period dynamically. This is implemented as another scheduled task, so as to not 52 // interfere with assumptions of previously scheduled tasks. 53 void setPeriod(uint64_t newPeriod); 54 55 private: 56 57 enum class CommandType { 58 Default = 0, 59 Exit = 1, 60 ChangePeriod = 2, 61 }; 62 63 struct VsyncThreadCommand { 64 CommandType type; 65 VsyncTask task; 66 uint64_t newPeriod; 67 }; 68 69 void exit(); 70 void threadFunc(); 71 72 uint64_t mPeriodNs = 0; 73 uint64_t mCount = 0; 74 bool mExiting = false; 75 android::base::MessageChannel<VsyncThreadCommand, 128> mChannel; 76 android::base::FunctorThread mThread; 77 }; 78 79 } // namespace gfxstream