1*0a9764feSAndroid Build Coastguard Worker /* 2*0a9764feSAndroid Build Coastguard Worker * Copyright (C) 2023 The Android Open Source Project 3*0a9764feSAndroid Build Coastguard Worker * 4*0a9764feSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*0a9764feSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*0a9764feSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*0a9764feSAndroid Build Coastguard Worker * 8*0a9764feSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*0a9764feSAndroid Build Coastguard Worker * 10*0a9764feSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*0a9764feSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*0a9764feSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*0a9764feSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*0a9764feSAndroid Build Coastguard Worker * limitations under the License. 15*0a9764feSAndroid Build Coastguard Worker */ 16*0a9764feSAndroid Build Coastguard Worker 17*0a9764feSAndroid Build Coastguard Worker #pragma once 18*0a9764feSAndroid Build Coastguard Worker 19*0a9764feSAndroid Build Coastguard Worker #include <chrono> 20*0a9764feSAndroid Build Coastguard Worker #include <condition_variable> 21*0a9764feSAndroid Build Coastguard Worker #include <functional> 22*0a9764feSAndroid Build Coastguard Worker #include <thread> 23*0a9764feSAndroid Build Coastguard Worker 24*0a9764feSAndroid Build Coastguard Worker namespace android { 25*0a9764feSAndroid Build Coastguard Worker 26*0a9764feSAndroid Build Coastguard Worker // NOLINTNEXTLINE(misc-unused-using-decls): False positive 27*0a9764feSAndroid Build Coastguard Worker using std::chrono_literals::operator""s; 28*0a9764feSAndroid Build Coastguard Worker 29*0a9764feSAndroid Build Coastguard Worker struct FlatConCallbacks { 30*0a9764feSAndroid Build Coastguard Worker std::function<void()> trigger; 31*0a9764feSAndroid Build Coastguard Worker }; 32*0a9764feSAndroid Build Coastguard Worker 33*0a9764feSAndroid Build Coastguard Worker class FlatteningController { 34*0a9764feSAndroid Build Coastguard Worker public: 35*0a9764feSAndroid Build Coastguard Worker static auto CreateInstance(FlatConCallbacks &cbks) 36*0a9764feSAndroid Build Coastguard Worker -> std::shared_ptr<FlatteningController>; 37*0a9764feSAndroid Build Coastguard Worker Disable()38*0a9764feSAndroid Build Coastguard Worker void Disable() { 39*0a9764feSAndroid Build Coastguard Worker auto lock = std::lock_guard<std::mutex>(mutex_); 40*0a9764feSAndroid Build Coastguard Worker flatten_next_frame_ = false; 41*0a9764feSAndroid Build Coastguard Worker disabled_ = true; 42*0a9764feSAndroid Build Coastguard Worker } 43*0a9764feSAndroid Build Coastguard Worker 44*0a9764feSAndroid Build Coastguard Worker /* Compositor should call this every frame */ 45*0a9764feSAndroid Build Coastguard Worker bool NewFrame(); 46*0a9764feSAndroid Build Coastguard Worker ShouldFlatten()47*0a9764feSAndroid Build Coastguard Worker auto ShouldFlatten() const { 48*0a9764feSAndroid Build Coastguard Worker return flatten_next_frame_; 49*0a9764feSAndroid Build Coastguard Worker } 50*0a9764feSAndroid Build Coastguard Worker StopThread()51*0a9764feSAndroid Build Coastguard Worker void StopThread() { 52*0a9764feSAndroid Build Coastguard Worker auto lock = std::lock_guard<std::mutex>(mutex_); 53*0a9764feSAndroid Build Coastguard Worker cbks_ = {}; 54*0a9764feSAndroid Build Coastguard Worker cv_.notify_all(); 55*0a9764feSAndroid Build Coastguard Worker } 56*0a9764feSAndroid Build Coastguard Worker 57*0a9764feSAndroid Build Coastguard Worker static constexpr auto kTimeout = 1s; 58*0a9764feSAndroid Build Coastguard Worker 59*0a9764feSAndroid Build Coastguard Worker private: 60*0a9764feSAndroid Build Coastguard Worker FlatteningController() = default; 61*0a9764feSAndroid Build Coastguard Worker static void ThreadFn(const std::shared_ptr<FlatteningController> &fc); 62*0a9764feSAndroid Build Coastguard Worker 63*0a9764feSAndroid Build Coastguard Worker bool flatten_next_frame_{}; 64*0a9764feSAndroid Build Coastguard Worker bool disabled_{}; decltype(std::chrono::system_clock::now ())65*0a9764feSAndroid Build Coastguard Worker decltype(std::chrono::system_clock::now()) sleep_until_{}; 66*0a9764feSAndroid Build Coastguard Worker std::mutex mutex_; 67*0a9764feSAndroid Build Coastguard Worker std::condition_variable cv_; 68*0a9764feSAndroid Build Coastguard Worker FlatConCallbacks cbks_; 69*0a9764feSAndroid Build Coastguard Worker }; 70*0a9764feSAndroid Build Coastguard Worker 71*0a9764feSAndroid Build Coastguard Worker } // namespace android 72