• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hwc-flatcon"
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   fc->cbks_ = cbks;
45 
46   std::thread(&FlatteningController::ThreadFn, fc).detach();
47 
48   return fc;
49 }
50 
51 /* Compositor should call this every frame */
NewFrame()52 bool FlatteningController::NewFrame() {
53   bool wake_it = false;
54   auto lock = std::lock_guard<std::mutex>(mutex_);
55 
56   if (flatten_next_frame_) {
57     flatten_next_frame_ = false;
58     return true;
59   }
60 
61   sleep_until_ = std::chrono::system_clock::now() + kTimeout;
62   if (disabled_) {
63     wake_it = true;
64     disabled_ = false;
65   }
66 
67   if (wake_it)
68     cv_.notify_all();
69 
70   return false;
71 }
72 
ThreadFn(const std::shared_ptr<FlatteningController> & fc)73 void FlatteningController::ThreadFn(
74     const std::shared_ptr<FlatteningController> &fc) {
75   for (;;) {
76     std::unique_lock<std::mutex> lock(fc->mutex_);
77     if (fc.use_count() == 1 || !fc->cbks_.trigger)
78       break;
79 
80     if (fc->sleep_until_ <= std::chrono::system_clock::now() &&
81         !fc->disabled_) {
82       fc->disabled_ = true;
83       fc->flatten_next_frame_ = true;
84       ALOGV("Timeout. Sending an event to compositor");
85       fc->cbks_.trigger();
86     }
87 
88     if (fc->disabled_) {
89       ALOGV("Wait");
90       fc->cv_.wait(lock);
91     } else {
92       ALOGV("Wait_until");
93       fc->cv_.wait_until(lock, fc->sleep_until_);
94     }
95   }
96 }
97 
98 }  // namespace android
99