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