blob: 5ad2de21e25eae545fc1088b35757873cc827500 [file] [log] [blame]
Sean Paul98e73c82015-06-24 14:38:49 -07001/*
2 * Copyright (C) 2015 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#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18#define LOG_TAG "hwc-drm-display-compositor"
19
Roman Stratiienko13cc3662020-08-29 21:35:39 +030020#include "DrmDisplayCompositor.h"
Zach Reiznerbff33ac2015-11-16 11:08:46 -080021
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030022#include <drm/drm_mode.h>
Zach Reiznerbff33ac2015-11-16 11:08:46 -080023#include <pthread.h>
24#include <sched.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030025#include <sync/sync.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030026#include <utils/Trace.h>
27
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020028#include <array>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020029#include <cstdlib>
30#include <ctime>
Zach Reiznerbff33ac2015-11-16 11:08:46 -080031#include <sstream>
32#include <vector>
33
Roman Stratiienko13cc3662020-08-29 21:35:39 +030034#include "drm/DrmCrtc.h"
35#include "drm/DrmDevice.h"
36#include "drm/DrmPlane.h"
Roman Stratiienko3e8ce572021-09-29 12:46:28 +030037#include "drm/DrmUnique.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030038#include "utils/autolock.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020039#include "utils/log.h"
Sean Paul98e73c82015-06-24 14:38:49 -070040
Sean Paul98e73c82015-06-24 14:38:49 -070041namespace android {
42
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020043std::ostream &operator<<(std::ostream &str, FlatteningState state) {
44 std::array<const char *, 6> flattenting_state_str = {
45 "None", "Not needed", "SF Requested", "Squashed by GPU",
46 "Serial", "Concurrent",
47 };
48
49 return str << flattenting_state_str[static_cast<int>(state)];
50}
51
Sean Paul98e73c82015-06-24 14:38:49 -070052DrmDisplayCompositor::DrmDisplayCompositor()
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020053 : resource_manager_(nullptr),
Sean Paul98e73c82015-06-24 14:38:49 -070054 display_(-1),
Sean Paul98e73c82015-06-24 14:38:49 -070055 initialized_(false),
Sean Pauldb7a17d2015-06-24 18:46:05 -070056 active_(false),
Sean Paul6c18b3b2015-11-25 11:04:25 -050057 use_hw_overlays_(true),
Sean Paul98e73c82015-06-24 14:38:49 -070058 dump_frames_composited_(0),
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010059 dump_last_timestamp_ns_(0),
60 flatten_countdown_(FLATTEN_COUNTDOWN_INIT),
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020061 flattening_state_(FlatteningState::kNone),
62 frames_flattened_(0) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020063 struct timespec ts {};
Sean Paul98e73c82015-06-24 14:38:49 -070064 if (clock_gettime(CLOCK_MONOTONIC, &ts))
65 return;
66 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
67}
68
69DrmDisplayCompositor::~DrmDisplayCompositor() {
70 if (!initialized_)
71 return;
72
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010073 vsync_worker_.Exit();
Sean Paul98e73c82015-06-24 14:38:49 -070074 int ret = pthread_mutex_lock(&lock_);
75 if (ret)
76 ALOGE("Failed to acquire compositor lock %d", ret);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010077 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
Sean Paul35301f42015-11-17 14:46:56 -050078 if (mode_.blob_id)
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010079 drm->DestroyPropertyBlob(mode_.blob_id);
Sean Paul35301f42015-11-17 14:46:56 -050080 if (mode_.old_blob_id)
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010081 drm->DestroyPropertyBlob(mode_.old_blob_id);
Sean Paul35301f42015-11-17 14:46:56 -050082
Sean Paul98e73c82015-06-24 14:38:49 -070083 active_composition_.reset();
84
85 ret = pthread_mutex_unlock(&lock_);
86 if (ret)
87 ALOGE("Failed to acquire compositor lock %d", ret);
88
89 pthread_mutex_destroy(&lock_);
90}
91
Roman Stratiienko863a3c22021-09-29 13:00:29 +030092auto DrmDisplayCompositor::Init(ResourceManager *resource_manager, int display,
93 std::function<void()> client_refresh_callback)
94 -> int {
95 client_refresh_callback_ = std::move(client_refresh_callback);
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +010096 resource_manager_ = resource_manager;
Sean Paul98e73c82015-06-24 14:38:49 -070097 display_ = display;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010098 DrmDevice *drm = resource_manager_->GetDrmDevice(display);
99 if (!drm) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100100 ALOGE("Could not find drmdevice for display");
101 return -EINVAL;
102 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200103 int ret = pthread_mutex_init(&lock_, nullptr);
Sean Paul98e73c82015-06-24 14:38:49 -0700104 if (ret) {
105 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
106 return ret;
107 }
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100108 planner_ = Planner::CreateInstance(drm);
109
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300110 vsync_worker_.Init(drm, display_, [this](int64_t timestamp) {
111 AutoLock lock(&lock_, "DrmDisplayCompositor::Init()");
112 if (lock.Lock())
113 return;
114 flatten_countdown_--;
115 if (!CountdownExpired())
116 return;
117 lock.Unlock();
118 int ret = FlattenActiveComposition();
119 ALOGV("scene flattening triggered for display %d at timestamp %" PRIu64
120 " result = %d \n",
121 display_, timestamp, ret);
122 });
Sean Paul98e73c82015-06-24 14:38:49 -0700123
124 initialized_ = true;
125 return 0;
126}
127
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100128std::unique_ptr<DrmDisplayComposition>
129DrmDisplayCompositor::CreateInitializedComposition() const {
130 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
131 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
132 if (!crtc) {
133 ALOGE("Failed to find crtc for display = %d", display_);
134 return std::unique_ptr<DrmDisplayComposition>();
135 }
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200136
137 return std::make_unique<DrmDisplayComposition>(crtc, planner_.get());
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100138}
139
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200140FlatteningState DrmDisplayCompositor::GetFlatteningState() const {
141 return flattening_state_;
142}
143
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200144uint32_t DrmDisplayCompositor::GetFlattenedFramesCount() const {
145 return frames_flattened_;
146}
147
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200148bool DrmDisplayCompositor::ShouldFlattenOnClient() const {
149 return flattening_state_ == FlatteningState::kClientRequested ||
150 flattening_state_ == FlatteningState::kClientDone;
151}
152
Zach Reizner92f8e632015-10-12 17:47:13 -0700153std::tuple<uint32_t, uint32_t, int>
154DrmDisplayCompositor::GetActiveModeResolution() {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100155 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
156 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200157 if (connector == nullptr) {
Zach Reizner92f8e632015-10-12 17:47:13 -0700158 ALOGE("Failed to determine display mode: no connector for display %d",
159 display_);
160 return std::make_tuple(0, 0, -ENODEV);
161 }
162
163 const DrmMode &mode = connector->active_mode();
164 return std::make_tuple(mode.h_display(), mode.v_display(), 0);
Zach Reizner713a6782015-07-31 15:12:44 -0700165}
166
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400167int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300168 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400169 if (!pset) {
170 ALOGE("Failed to allocate property set");
171 return -ENOMEM;
172 }
173
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200174 int ret = 0;
Sean Paulf72cccd2018-08-27 13:59:08 -0400175 std::vector<DrmCompositionPlane> &comp_planes = display_comp
176 ->composition_planes();
Zach Reizner92f8e632015-10-12 17:47:13 -0700177 for (DrmCompositionPlane &comp_plane : comp_planes) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300178 if (comp_plane.plane()->AtomicDisablePlane(*pset) != 0) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300179 return -EINVAL;
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400180 }
181 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100182 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300183 ret = drmModeAtomicCommit(drm->fd(), pset.get(), 0, drm);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400184 if (ret) {
185 ALOGE("Failed to commit pset ret=%d\n", ret);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400186 return ret;
187 }
188
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400189 return 0;
190}
191
Sean Paulc07b2112015-11-17 16:38:10 -0500192int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp,
Roman Stratiienko74774712021-02-05 16:32:47 +0200193 bool test_only) {
Haixia Shi3979f7d2015-10-29 14:33:37 -0700194 ATRACE_CALL();
195
Haixia Shidda2fab2015-10-22 18:12:49 -0700196 int ret = 0;
197
198 std::vector<DrmHwcLayer> &layers = display_comp->layers();
Sean Paulf72cccd2018-08-27 13:59:08 -0400199 std::vector<DrmCompositionPlane> &comp_planes = display_comp
200 ->composition_planes();
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100201 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
202 uint64_t out_fences[drm->crtcs().size()];
Haixia Shidda2fab2015-10-22 18:12:49 -0700203
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100204 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400205 if (!connector) {
206 ALOGE("Could not locate connector for display %d", display_);
207 return -ENODEV;
208 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100209 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400210 if (!crtc) {
211 ALOGE("Could not locate crtc for display %d", display_);
212 return -ENODEV;
213 }
214
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300215 auto pset = MakeDrmModeAtomicReqUnique();
Sean Paul98e73c82015-06-24 14:38:49 -0700216 if (!pset) {
217 ALOGE("Failed to allocate property set");
218 return -ENOMEM;
219 }
220
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300221 if (crtc->out_fence_ptr_property() &&
222 !crtc->out_fence_ptr_property()
223 .AtomicSet(*pset, (uint64_t)&out_fences[crtc->pipe()])) {
224 return -EINVAL;
Robert Fossa1ade4e2017-09-27 19:28:15 +0200225 }
226
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300227 if (mode_.needs_modeset &&
228 (!crtc->active_property().AtomicSet(*pset, 1) ||
229 !crtc->mode_property().AtomicSet(*pset, mode_.blob_id) ||
230 !connector->crtc_id_property().AtomicSet(*pset, crtc->id()))) {
231 return -EINVAL;
Sean Paul57355412015-09-19 09:14:34 -0400232 }
233
Zach Reizner92f8e632015-10-12 17:47:13 -0700234 for (DrmCompositionPlane &comp_plane : comp_planes) {
Sean Paulca699be2016-05-11 16:29:45 -0400235 DrmPlane *plane = comp_plane.plane();
Sean Paulca699be2016-05-11 16:29:45 -0400236 std::vector<size_t> &source_layers = comp_plane.source_layers();
Zach Reizner92f8e632015-10-12 17:47:13 -0700237
Sean Paulbbe39db2016-05-11 16:57:26 -0400238 if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) {
Sean Paulca699be2016-05-11 16:29:45 -0400239 if (source_layers.size() > 1) {
240 ALOGE("Can't handle more than one source layer sz=%zu type=%d",
241 source_layers.size(), comp_plane.type());
242 continue;
243 }
244
245 if (source_layers.empty() || source_layers.front() >= layers.size()) {
246 ALOGE("Source layer index %zu out of bounds %zu type=%d",
247 source_layers.front(), layers.size(), comp_plane.type());
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300248 return -EINVAL;
Sean Paul98e73c82015-06-24 14:38:49 -0700249 }
Sean Paulca699be2016-05-11 16:29:45 -0400250 DrmHwcLayer &layer = layers[source_layers.front()];
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300251
252 if (plane->AtomicSetState(*pset, layer, source_layers.front(),
253 crtc->id()) != 0) {
254 return -EINVAL;
Sean Paul39b37842016-05-11 13:50:28 -0400255 }
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300256 } else {
257 if (plane->AtomicDisablePlane(*pset) != 0) {
258 return -EINVAL;
Matvii Zorin8338c342020-09-08 16:12:51 +0300259 }
260 }
Sean Paul98e73c82015-06-24 14:38:49 -0700261 }
262
263 if (!ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500264 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
265 if (test_only)
266 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
267
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300268 ret = drmModeAtomicCommit(drm->fd(), pset.get(), flags, drm);
Sean Paul57355412015-09-19 09:14:34 -0400269 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700270 if (!test_only)
Sean Paulc07b2112015-11-17 16:38:10 -0500271 ALOGE("Failed to commit pset ret=%d\n", ret);
Sean Paul57355412015-09-19 09:14:34 -0400272 return ret;
273 }
Sean Paul98e73c82015-06-24 14:38:49 -0700274 }
Sean Paul98e73c82015-06-24 14:38:49 -0700275
Sean Paulc07b2112015-11-17 16:38:10 -0500276 if (!test_only && mode_.needs_modeset) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100277 ret = drm->DestroyPropertyBlob(mode_.old_blob_id);
Sean Paul57355412015-09-19 09:14:34 -0400278 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400279 ALOGE("Failed to destroy old mode property blob %" PRIu32 "/%d",
Sean Paul35301f42015-11-17 14:46:56 -0500280 mode_.old_blob_id, ret);
Sean Paul57355412015-09-19 09:14:34 -0400281 return ret;
282 }
283
284 /* TODO: Add dpms to the pset when the kernel supports it */
285 ret = ApplyDpms(display_comp);
286 if (ret) {
287 ALOGE("Failed to apply DPMS after modeset %d\n", ret);
288 return ret;
289 }
290
Sean Paul35301f42015-11-17 14:46:56 -0500291 connector->set_active_mode(mode_.mode);
292 mode_.old_blob_id = mode_.blob_id;
293 mode_.blob_id = 0;
294 mode_.needs_modeset = false;
Sean Paul57355412015-09-19 09:14:34 -0400295 }
296
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300297 if (crtc->out_fence_ptr_property()) {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200298 display_comp->out_fence_ = UniqueFd((int)out_fences[crtc->pipe()]);
Robert Fossa1ade4e2017-09-27 19:28:15 +0200299 }
300
Sean Paul98e73c82015-06-24 14:38:49 -0700301 return ret;
302}
303
Sean Pauldb7a17d2015-06-24 18:46:05 -0700304int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100305 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
306 DrmConnector *conn = drm->GetConnectorForDisplay(display_);
Sean Pauldb7a17d2015-06-24 18:46:05 -0700307 if (!conn) {
308 ALOGE("Failed to get DrmConnector for display %d", display_);
309 return -ENODEV;
310 }
311
312 const DrmProperty &prop = conn->dpms_property();
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100313 int ret = drmModeConnectorSetProperty(drm->fd(), conn->id(), prop.id(),
Sean Pauldb7a17d2015-06-24 18:46:05 -0700314 display_comp->dpms_mode());
315 if (ret) {
316 ALOGE("Failed to set DPMS property for connector %d", conn->id());
317 return ret;
318 }
319 return 0;
320}
321
Sean Paul35301f42015-11-17 14:46:56 -0500322std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob(
323 const DrmMode &mode) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200324 struct drm_mode_modeinfo drm_mode {};
Sean Paul35301f42015-11-17 14:46:56 -0500325 mode.ToDrmModeModeInfo(&drm_mode);
326
327 uint32_t id = 0;
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100328 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
329 int ret = drm->CreatePropertyBlob(&drm_mode, sizeof(struct drm_mode_modeinfo),
330 &id);
Sean Paul35301f42015-11-17 14:46:56 -0500331 if (ret) {
332 ALOGE("Failed to create mode property blob %d", ret);
333 return std::make_tuple(ret, 0);
334 }
Sean Paulf741c672016-05-11 13:49:38 -0400335 ALOGE("Create blob_id %" PRIu32 "\n", id);
Sean Paul35301f42015-11-17 14:46:56 -0500336 return std::make_tuple(ret, id);
337}
338
Sean Paul137a6a82016-06-22 22:48:22 -0400339void DrmDisplayCompositor::ClearDisplay() {
Sean Paul137a6a82016-06-22 22:48:22 -0400340 if (!active_composition_)
341 return;
342
343 if (DisablePlanes(active_composition_.get()))
344 return;
345
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200346 active_composition_.reset(nullptr);
Sean Paul137a6a82016-06-22 22:48:22 -0400347}
348
Haixia Shidda2fab2015-10-22 18:12:49 -0700349void DrmDisplayCompositor::ApplyFrame(
Roman Stratiienko74774712021-02-05 16:32:47 +0200350 std::unique_ptr<DrmDisplayComposition> composition, int status) {
Alexandru Gheorghedb440a92018-03-29 14:38:21 +0100351 AutoLock lock(&lock_, __func__);
352 if (lock.Lock())
353 return;
Haixia Shidda2fab2015-10-22 18:12:49 -0700354 int ret = status;
355
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100356 if (!ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500357 ret = CommitFrame(composition.get(), false);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100358 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700359
360 if (ret) {
361 ALOGE("Composite failed for display %d", display_);
Haixia Shidda2fab2015-10-22 18:12:49 -0700362 // Disable the hw used by the last active composition. This allows us to
363 // signal the release fences from that composition to avoid hanging.
Sean Paul137a6a82016-06-22 22:48:22 -0400364 ClearDisplay();
365 return;
Haixia Shidda2fab2015-10-22 18:12:49 -0700366 }
367 ++dump_frames_composited_;
368
Haixia Shidda2fab2015-10-22 18:12:49 -0700369 active_composition_.swap(composition);
370
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100371 flatten_countdown_ = FLATTEN_COUNTDOWN_INIT;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200372 if (flattening_state_ != FlatteningState::kClientRequested) {
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200373 SetFlattening(FlatteningState::kNone);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200374 } else {
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200375 SetFlattening(FlatteningState::kClientDone);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200376 }
Roman Stratiienko74774712021-02-05 16:32:47 +0200377 vsync_worker_.VSyncControl(true);
Haixia Shidda2fab2015-10-22 18:12:49 -0700378}
379
Sean Pauled45a8e2017-02-28 13:17:34 -0500380int DrmDisplayCompositor::ApplyComposition(
381 std::unique_ptr<DrmDisplayComposition> composition) {
382 int ret = 0;
Sean Paulacb2a442015-06-24 18:43:01 -0700383 switch (composition->type()) {
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700384 case DRM_COMPOSITION_TYPE_FRAME:
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800385 if (composition->geometry_changed()) {
386 // Send the composition to the kernel to ensure we can commit it. This
Rob Herringaf0d9752018-05-04 16:34:19 -0500387 // is just a test, it won't actually commit the frame.
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800388 ret = CommitFrame(composition.get(), true);
Rob Herringaf0d9752018-05-04 16:34:19 -0500389 if (ret) {
390 ALOGE("Commit test failed for display %d, FIXME", display_);
Sean Paul6c18b3b2015-11-25 11:04:25 -0500391 return ret;
Sean Paul647beb22015-11-19 13:55:48 -0500392 }
393 }
Rob Herringaf0d9752018-05-04 16:34:19 -0500394
Sean Pauled45a8e2017-02-28 13:17:34 -0500395 ApplyFrame(std::move(composition), ret);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700396 break;
397 case DRM_COMPOSITION_TYPE_DPMS:
Sean Pauled45a8e2017-02-28 13:17:34 -0500398 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700399 ret = ApplyDpms(composition.get());
400 if (ret)
401 ALOGE("Failed to apply dpms for display %d", display_);
Sean Paulacb2a442015-06-24 18:43:01 -0700402 return ret;
Sean Paul57355412015-09-19 09:14:34 -0400403 case DRM_COMPOSITION_TYPE_MODESET:
Sean Paul35301f42015-11-17 14:46:56 -0500404 mode_.mode = composition->display_mode();
405 if (mode_.blob_id)
Sean Paulf72cccd2018-08-27 13:59:08 -0400406 resource_manager_->GetDrmDevice(display_)->DestroyPropertyBlob(
407 mode_.blob_id);
Sean Paul35301f42015-11-17 14:46:56 -0500408 std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
409 if (ret) {
410 ALOGE("Failed to create mode blob for display %d", display_);
411 return ret;
412 }
413 mode_.needs_modeset = true;
Sean Paul57355412015-09-19 09:14:34 -0400414 return 0;
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700415 default:
416 ALOGE("Unknown composition type %d", composition->type());
417 return -EINVAL;
Sean Paul98e73c82015-06-24 14:38:49 -0700418 }
Sean Paul98e73c82015-06-24 14:38:49 -0700419
Sean Paul98e73c82015-06-24 14:38:49 -0700420 return ret;
421}
422
Rob Herring4f6c62e2018-05-17 14:33:02 -0500423int DrmDisplayCompositor::TestComposition(DrmDisplayComposition *composition) {
424 return CommitFrame(composition, true);
425}
426
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200427void DrmDisplayCompositor::SetFlattening(FlatteningState new_state) {
428 if (flattening_state_ != new_state) {
429 switch (flattening_state_) {
430 case FlatteningState::kClientDone:
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200431 ++frames_flattened_;
432 break;
433 case FlatteningState::kClientRequested:
434 case FlatteningState::kNone:
435 case FlatteningState::kNotNeeded:
436 break;
437 }
438 }
439 flattening_state_ = new_state;
440}
441
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200442bool DrmDisplayCompositor::IsFlatteningNeeded() const {
443 return CountdownExpired() && active_composition_->layers().size() >= 2;
444}
445
446int DrmDisplayCompositor::FlattenOnClient() {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300447 if (client_refresh_callback_) {
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200448 {
449 AutoLock lock(&lock_, __func__);
450 if (!IsFlatteningNeeded()) {
451 if (flattening_state_ != FlatteningState::kClientDone) {
452 ALOGV("Flattening is not needed");
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200453 SetFlattening(FlatteningState::kNotNeeded);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200454 }
455 return -EALREADY;
456 }
457 }
458
459 ALOGV(
460 "No writeback connector available, "
461 "falling back to client composition");
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200462 SetFlattening(FlatteningState::kClientRequested);
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300463 client_refresh_callback_();
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200464 return 0;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200465 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200466
467 ALOGV("No writeback connector available");
468 return -EINVAL;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200469}
470
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100471int DrmDisplayCompositor::FlattenActiveComposition() {
Roman Stratiienko74774712021-02-05 16:32:47 +0200472 return FlattenOnClient();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100473}
474
475bool DrmDisplayCompositor::CountdownExpired() const {
476 return flatten_countdown_ <= 0;
477}
478
Sean Paul98e73c82015-06-24 14:38:49 -0700479void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700480 int ret = pthread_mutex_lock(&lock_);
481 if (ret)
482 return;
483
484 uint64_t num_frames = dump_frames_composited_;
485 dump_frames_composited_ = 0;
486
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200487 struct timespec ts {};
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700488 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
489 if (ret) {
490 pthread_mutex_unlock(&lock_);
491 return;
492 }
493
494 uint64_t cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
495 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300496 float fps = num_ms ? static_cast<float>(num_frames) * 1000.0F /
497 static_cast<float>(num_ms)
498 : 0.0F;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700499
500 *out << "--DrmDisplayCompositor[" << display_
501 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
502 << " fps=" << fps << "\n";
503
504 dump_last_timestamp_ns_ = cur_ts;
505
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700506 pthread_mutex_unlock(&lock_);
Sean Paul98e73c82015-06-24 14:38:49 -0700507}
Sean Paulf72cccd2018-08-27 13:59:08 -0400508} // namespace android