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 /*
18*0a9764feSAndroid Build Coastguard Worker * Usually, display controllers do not use intermediate buffer for composition
19*0a9764feSAndroid Build Coastguard Worker * results. Instead, they scan-out directly from the input buffers, composing
20*0a9764feSAndroid Build Coastguard Worker * the planes on the fly every VSYNC.
21*0a9764feSAndroid Build Coastguard Worker *
22*0a9764feSAndroid Build Coastguard Worker * Flattening is a technique that reduces memory bandwidth and power consumption
23*0a9764feSAndroid Build Coastguard Worker * by converting non-updating multi-plane composition into a single-plane.
24*0a9764feSAndroid Build Coastguard Worker * Additionally, flattening also makes more shared planes available for use by
25*0a9764feSAndroid Build Coastguard Worker * other CRTCs.
26*0a9764feSAndroid Build Coastguard Worker *
27*0a9764feSAndroid Build Coastguard Worker * If the client is not updating layers for 1 second, FlatCon triggers a
28*0a9764feSAndroid Build Coastguard Worker * callback to refresh the screen. The compositor should mark all layers to be
29*0a9764feSAndroid Build Coastguard Worker * composed by the client into a single framebuffer using GPU.
30*0a9764feSAndroid Build Coastguard Worker */
31*0a9764feSAndroid Build Coastguard Worker
32*0a9764feSAndroid Build Coastguard Worker #define LOG_TAG "drmhwc"
33*0a9764feSAndroid Build Coastguard Worker
34*0a9764feSAndroid Build Coastguard Worker #include "FlatteningController.h"
35*0a9764feSAndroid Build Coastguard Worker
36*0a9764feSAndroid Build Coastguard Worker #include "utils/log.h"
37*0a9764feSAndroid Build Coastguard Worker
38*0a9764feSAndroid Build Coastguard Worker namespace android {
39*0a9764feSAndroid Build Coastguard Worker
CreateInstance(FlatConCallbacks & cbks)40*0a9764feSAndroid Build Coastguard Worker auto FlatteningController::CreateInstance(FlatConCallbacks &cbks)
41*0a9764feSAndroid Build Coastguard Worker -> std::shared_ptr<FlatteningController> {
42*0a9764feSAndroid Build Coastguard Worker auto fc = std::shared_ptr<FlatteningController>(new FlatteningController());
43*0a9764feSAndroid Build Coastguard Worker
44*0a9764feSAndroid Build Coastguard Worker /* Disable the controller by default as it can cause refresh event to be
45*0a9764feSAndroid Build Coastguard Worker * issued at creation time, even when it is not required. This can fail VTS
46*0a9764feSAndroid Build Coastguard Worker * tests at teardown that check for this behaviour. See:
47*0a9764feSAndroid Build Coastguard Worker * https://cs.android.com/android/platform/superproject/main/+/cedca652b903e4f4e584e457b5a7038e0825fb94:hardware/interfaces/graphics/composer/aidl/vts/VtsComposerClient.cpp;drc=a2a6deaf5036e081f48379b6573db4465538b5ac;l=604
48*0a9764feSAndroid Build Coastguard Worker */
49*0a9764feSAndroid Build Coastguard Worker fc->Disable();
50*0a9764feSAndroid Build Coastguard Worker fc->cbks_ = cbks;
51*0a9764feSAndroid Build Coastguard Worker
52*0a9764feSAndroid Build Coastguard Worker std::thread(&FlatteningController::ThreadFn, fc).detach();
53*0a9764feSAndroid Build Coastguard Worker
54*0a9764feSAndroid Build Coastguard Worker return fc;
55*0a9764feSAndroid Build Coastguard Worker }
56*0a9764feSAndroid Build Coastguard Worker
57*0a9764feSAndroid Build Coastguard Worker /* Compositor should call this every frame */
NewFrame()58*0a9764feSAndroid Build Coastguard Worker bool FlatteningController::NewFrame() {
59*0a9764feSAndroid Build Coastguard Worker bool wake_it = false;
60*0a9764feSAndroid Build Coastguard Worker auto lock = std::lock_guard<std::mutex>(mutex_);
61*0a9764feSAndroid Build Coastguard Worker
62*0a9764feSAndroid Build Coastguard Worker if (flatten_next_frame_) {
63*0a9764feSAndroid Build Coastguard Worker flatten_next_frame_ = false;
64*0a9764feSAndroid Build Coastguard Worker return true;
65*0a9764feSAndroid Build Coastguard Worker }
66*0a9764feSAndroid Build Coastguard Worker
67*0a9764feSAndroid Build Coastguard Worker sleep_until_ = std::chrono::system_clock::now() + kTimeout;
68*0a9764feSAndroid Build Coastguard Worker if (disabled_) {
69*0a9764feSAndroid Build Coastguard Worker wake_it = true;
70*0a9764feSAndroid Build Coastguard Worker disabled_ = false;
71*0a9764feSAndroid Build Coastguard Worker }
72*0a9764feSAndroid Build Coastguard Worker
73*0a9764feSAndroid Build Coastguard Worker if (wake_it)
74*0a9764feSAndroid Build Coastguard Worker cv_.notify_all();
75*0a9764feSAndroid Build Coastguard Worker
76*0a9764feSAndroid Build Coastguard Worker return false;
77*0a9764feSAndroid Build Coastguard Worker }
78*0a9764feSAndroid Build Coastguard Worker
ThreadFn(const std::shared_ptr<FlatteningController> & fc)79*0a9764feSAndroid Build Coastguard Worker void FlatteningController::ThreadFn(
80*0a9764feSAndroid Build Coastguard Worker const std::shared_ptr<FlatteningController> &fc) {
81*0a9764feSAndroid Build Coastguard Worker for (;;) {
82*0a9764feSAndroid Build Coastguard Worker std::unique_lock<std::mutex> lock(fc->mutex_);
83*0a9764feSAndroid Build Coastguard Worker if (fc.use_count() == 1 || !fc->cbks_.trigger)
84*0a9764feSAndroid Build Coastguard Worker break;
85*0a9764feSAndroid Build Coastguard Worker
86*0a9764feSAndroid Build Coastguard Worker if (fc->sleep_until_ <= std::chrono::system_clock::now() &&
87*0a9764feSAndroid Build Coastguard Worker !fc->disabled_) {
88*0a9764feSAndroid Build Coastguard Worker fc->disabled_ = true;
89*0a9764feSAndroid Build Coastguard Worker fc->flatten_next_frame_ = true;
90*0a9764feSAndroid Build Coastguard Worker ALOGV("Timeout. Sending an event to compositor");
91*0a9764feSAndroid Build Coastguard Worker fc->cbks_.trigger();
92*0a9764feSAndroid Build Coastguard Worker }
93*0a9764feSAndroid Build Coastguard Worker
94*0a9764feSAndroid Build Coastguard Worker if (fc->disabled_) {
95*0a9764feSAndroid Build Coastguard Worker ALOGV("Wait");
96*0a9764feSAndroid Build Coastguard Worker fc->cv_.wait(lock);
97*0a9764feSAndroid Build Coastguard Worker } else {
98*0a9764feSAndroid Build Coastguard Worker ALOGV("Wait_until");
99*0a9764feSAndroid Build Coastguard Worker fc->cv_.wait_until(lock, fc->sleep_until_);
100*0a9764feSAndroid Build Coastguard Worker }
101*0a9764feSAndroid Build Coastguard Worker }
102*0a9764feSAndroid Build Coastguard Worker }
103*0a9764feSAndroid Build Coastguard Worker
104*0a9764feSAndroid Build Coastguard Worker } // namespace android
105