blob: c9cf40c4c10ce91198c718862448c9f32d336614 [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
20#include "drmdisplaycompositor.h"
Zach Reiznerbff33ac2015-11-16 11:08:46 -080021
22#include <pthread.h>
23#include <sched.h>
24#include <stdlib.h>
25#include <time.h>
26#include <sstream>
27#include <vector>
28
29#include <cutils/log.h>
30#include <drm/drm_mode.h>
31#include <sync/sync.h>
32#include <utils/Trace.h>
33
34#include "autolock.h"
Sean Paul98e73c82015-06-24 14:38:49 -070035#include "drmcrtc.h"
36#include "drmplane.h"
37#include "drmresources.h"
Zach Reizner713a6782015-07-31 15:12:44 -070038#include "glworker.h"
Sean Paul98e73c82015-06-24 14:38:49 -070039
Haixia Shidda2fab2015-10-22 18:12:49 -070040#define DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH 2
Zach Reiznerd410f042015-07-28 15:33:07 -070041
Sean Paul98e73c82015-06-24 14:38:49 -070042namespace android {
43
Zach Reizner92f8e632015-10-12 17:47:13 -070044void SquashState::Init(DrmHwcLayer *layers, size_t num_layers) {
45 generation_number_++;
46 valid_history_ = 0;
47 regions_.clear();
48 last_handles_.clear();
49
50 std::vector<DrmHwcRect<int>> in_rects;
51 for (size_t i = 0; i < num_layers; i++) {
52 DrmHwcLayer *layer = &layers[i];
53 in_rects.emplace_back(layer->display_frame);
54 last_handles_.push_back(layer->sf_handle);
55 }
56
Haixia Shiaa2f4a52015-11-02 10:54:29 -080057 std::vector<separate_rects::RectSet<uint64_t, int>> out_regions;
58 separate_rects::separate_rects_64(in_rects, &out_regions);
Zach Reizner92f8e632015-10-12 17:47:13 -070059
Haixia Shiaa2f4a52015-11-02 10:54:29 -080060 for (const separate_rects::RectSet<uint64_t, int> &out_region : out_regions) {
Zach Reizner92f8e632015-10-12 17:47:13 -070061 regions_.emplace_back();
62 Region &region = regions_.back();
63 region.rect = out_region.rect;
64 region.layer_refs = out_region.id_set.getBits();
65 }
66}
67
Zach Reizner5757e822015-10-16 19:06:31 -070068void SquashState::GenerateHistory(DrmHwcLayer *layers, size_t num_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -070069 std::vector<bool> &changed_regions) const {
Zach Reizner5757e822015-10-16 19:06:31 -070070 changed_regions.resize(regions_.size());
71 if (num_layers != last_handles_.size()) {
72 ALOGE("SquashState::GenerateHistory expected %zu layers but got %zu layers",
73 last_handles_.size(), num_layers);
74 return;
75 }
Zach Reizner92f8e632015-10-12 17:47:13 -070076 std::bitset<kMaxLayers> changed_layers;
77 for (size_t i = 0; i < last_handles_.size(); i++) {
78 DrmHwcLayer *layer = &layers[i];
Zach Reiznerdb81fce2015-10-27 16:18:06 -070079 // Protected layers can't be squashed so we treat them as constantly
80 // changing.
81 if (layer->protected_usage() || last_handles_[i] != layer->sf_handle)
Zach Reizner92f8e632015-10-12 17:47:13 -070082 changed_layers.set(i);
Zach Reizner92f8e632015-10-12 17:47:13 -070083 }
84
Zach Reizner92f8e632015-10-12 17:47:13 -070085 for (size_t i = 0; i < regions_.size(); i++) {
86 changed_regions[i] = (regions_[i].layer_refs & changed_layers).any();
87 }
88}
89
90void SquashState::StableRegionsWithMarginalHistory(
91 const std::vector<bool> &changed_regions,
92 std::vector<bool> &stable_regions) const {
93 stable_regions.resize(regions_.size());
94 for (size_t i = 0; i < regions_.size(); i++) {
95 stable_regions[i] = !changed_regions[i] && is_stable(i);
96 }
97}
98
Zach Reizner5757e822015-10-16 19:06:31 -070099void SquashState::RecordHistory(DrmHwcLayer *layers, size_t num_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -0700100 const std::vector<bool> &changed_regions) {
Zach Reizner5757e822015-10-16 19:06:31 -0700101 if (num_layers != last_handles_.size()) {
102 ALOGE("SquashState::RecordHistory expected %zu layers but got %zu layers",
103 last_handles_.size(), num_layers);
104 return;
105 }
106 if (changed_regions.size() != regions_.size()) {
107 ALOGE("SquashState::RecordHistory expected %zu regions but got %zu regions",
108 regions_.size(), changed_regions.size());
109 return;
110 }
111
Zach Reizner92f8e632015-10-12 17:47:13 -0700112 for (size_t i = 0; i < last_handles_.size(); i++) {
113 DrmHwcLayer *layer = &layers[i];
114 last_handles_[i] = layer->sf_handle;
115 }
116
117 for (size_t i = 0; i < regions_.size(); i++) {
118 regions_[i].change_history <<= 1;
119 regions_[i].change_history.set(/* LSB */ 0, changed_regions[i]);
120 }
121
122 valid_history_++;
123}
124
Zach Reizner5757e822015-10-16 19:06:31 -0700125bool SquashState::RecordAndCompareSquashed(
126 const std::vector<bool> &squashed_regions) {
127 if (squashed_regions.size() != regions_.size()) {
128 ALOGE(
129 "SquashState::RecordAndCompareSquashed expected %zu regions but got "
130 "%zu regions",
131 regions_.size(), squashed_regions.size());
132 return false;
Zach Reizner92f8e632015-10-12 17:47:13 -0700133 }
Zach Reizner5757e822015-10-16 19:06:31 -0700134 bool changed = false;
135 for (size_t i = 0; i < regions_.size(); i++) {
136 if (regions_[i].squashed != squashed_regions[i]) {
137 regions_[i].squashed = squashed_regions[i];
138 changed = true;
139 }
140 }
141 return changed;
Zach Reizner92f8e632015-10-12 17:47:13 -0700142}
143
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700144void SquashState::Dump(std::ostringstream *out) const {
145 *out << "----SquashState generation=" << generation_number_
146 << " history=" << valid_history_ << "\n"
147 << " Regions: count=" << regions_.size() << "\n";
148 for (size_t i = 0; i < regions_.size(); i++) {
149 const Region &region = regions_[i];
150 *out << " [" << i << "]"
151 << " history=" << region.change_history << " rect";
152 region.rect.Dump(out);
153 *out << " layers=(";
154 bool first = true;
155 for (size_t layer_index = 0; layer_index < kMaxLayers; layer_index++) {
156 if ((region.layer_refs &
157 std::bitset<kMaxLayers>((size_t)1 << layer_index))
158 .any()) {
159 if (!first)
160 *out << " ";
161 first = false;
162 *out << layer_index;
163 }
164 }
165 *out << ")";
166 if (region.squashed)
167 *out << " squashed";
168 *out << "\n";
169 }
170}
171
Zach Reizner5757e822015-10-16 19:06:31 -0700172static bool UsesSquash(const std::vector<DrmCompositionPlane> &comp_planes) {
173 return std::any_of(comp_planes.begin(), comp_planes.end(),
174 [](const DrmCompositionPlane &plane) {
Sean Paul39b37842016-05-11 13:50:28 -0400175 return plane.type == DrmCompositionPlaneType::kSquash;
176 });
Zach Reizner5757e822015-10-16 19:06:31 -0700177}
178
Haixia Shidda2fab2015-10-22 18:12:49 -0700179DrmDisplayCompositor::FrameWorker::FrameWorker(DrmDisplayCompositor *compositor)
180 : Worker("frame-worker", HAL_PRIORITY_URGENT_DISPLAY),
181 compositor_(compositor) {
182}
183
184DrmDisplayCompositor::FrameWorker::~FrameWorker() {
185}
186
187int DrmDisplayCompositor::FrameWorker::Init() {
188 return InitWorker();
189}
190
191void DrmDisplayCompositor::FrameWorker::QueueFrame(
192 std::unique_ptr<DrmDisplayComposition> composition, int status) {
193 Lock();
194 FrameState frame;
195 frame.composition = std::move(composition);
196 frame.status = status;
197 frame_queue_.push(std::move(frame));
198 SignalLocked();
199 Unlock();
200}
201
202void DrmDisplayCompositor::FrameWorker::Routine() {
203 int ret = Lock();
204 if (ret) {
205 ALOGE("Failed to lock worker, %d", ret);
206 return;
207 }
208
209 int wait_ret = 0;
210 if (frame_queue_.empty()) {
211 wait_ret = WaitForSignalOrExitLocked();
212 }
213
214 FrameState frame;
215 if (!frame_queue_.empty()) {
216 frame = std::move(frame_queue_.front());
217 frame_queue_.pop();
218 }
219
220 ret = Unlock();
221 if (ret) {
222 ALOGE("Failed to unlock worker, %d", ret);
223 return;
224 }
225
226 if (wait_ret == -EINTR) {
227 return;
228 } else if (wait_ret) {
229 ALOGE("Failed to wait for signal, %d", wait_ret);
230 return;
231 }
232
233 compositor_->ApplyFrame(std::move(frame.composition), frame.status);
234}
235
Sean Paul98e73c82015-06-24 14:38:49 -0700236DrmDisplayCompositor::DrmDisplayCompositor()
237 : drm_(NULL),
238 display_(-1),
239 worker_(this),
Haixia Shidda2fab2015-10-22 18:12:49 -0700240 frame_worker_(this),
Sean Paul98e73c82015-06-24 14:38:49 -0700241 initialized_(false),
Sean Pauldb7a17d2015-06-24 18:46:05 -0700242 active_(false),
Sean Paul6c18b3b2015-11-25 11:04:25 -0500243 use_hw_overlays_(true),
Zach Reizner713a6782015-07-31 15:12:44 -0700244 framebuffer_index_(0),
Zach Reizner5757e822015-10-16 19:06:31 -0700245 squash_framebuffer_index_(0),
Sean Paul98e73c82015-06-24 14:38:49 -0700246 dump_frames_composited_(0),
247 dump_last_timestamp_ns_(0) {
248 struct timespec ts;
249 if (clock_gettime(CLOCK_MONOTONIC, &ts))
250 return;
251 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
252}
253
254DrmDisplayCompositor::~DrmDisplayCompositor() {
255 if (!initialized_)
256 return;
257
258 worker_.Exit();
Haixia Shidda2fab2015-10-22 18:12:49 -0700259 frame_worker_.Exit();
Sean Paul98e73c82015-06-24 14:38:49 -0700260
261 int ret = pthread_mutex_lock(&lock_);
262 if (ret)
263 ALOGE("Failed to acquire compositor lock %d", ret);
264
Sean Paul35301f42015-11-17 14:46:56 -0500265 if (mode_.blob_id)
266 drm_->DestroyPropertyBlob(mode_.blob_id);
267 if (mode_.old_blob_id)
268 drm_->DestroyPropertyBlob(mode_.old_blob_id);
269
Sean Paul98e73c82015-06-24 14:38:49 -0700270 while (!composite_queue_.empty()) {
271 composite_queue_.front().reset();
272 composite_queue_.pop();
273 }
274 active_composition_.reset();
275
276 ret = pthread_mutex_unlock(&lock_);
277 if (ret)
278 ALOGE("Failed to acquire compositor lock %d", ret);
279
280 pthread_mutex_destroy(&lock_);
281}
282
283int DrmDisplayCompositor::Init(DrmResources *drm, int display) {
284 drm_ = drm;
285 display_ = display;
286
287 int ret = pthread_mutex_init(&lock_, NULL);
288 if (ret) {
289 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
290 return ret;
291 }
292 ret = worker_.Init();
293 if (ret) {
294 pthread_mutex_destroy(&lock_);
295 ALOGE("Failed to initialize compositor worker %d\n", ret);
296 return ret;
297 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700298 ret = frame_worker_.Init();
299 if (ret) {
300 pthread_mutex_destroy(&lock_);
301 ALOGE("Failed to initialize frame worker %d\n", ret);
302 return ret;
303 }
Sean Paul98e73c82015-06-24 14:38:49 -0700304
305 initialized_ = true;
306 return 0;
307}
308
Zach Reizner92f8e632015-10-12 17:47:13 -0700309std::unique_ptr<DrmDisplayComposition> DrmDisplayCompositor::CreateComposition()
310 const {
311 return std::unique_ptr<DrmDisplayComposition>(new DrmDisplayComposition());
312}
313
Sean Paul98e73c82015-06-24 14:38:49 -0700314int DrmDisplayCompositor::QueueComposition(
315 std::unique_ptr<DrmDisplayComposition> composition) {
Sean Paulacb2a442015-06-24 18:43:01 -0700316 switch (composition->type()) {
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700317 case DRM_COMPOSITION_TYPE_FRAME:
318 if (!active_)
319 return -ENODEV;
320 break;
321 case DRM_COMPOSITION_TYPE_DPMS:
322 /*
323 * Update the state as soon as we get it so we can start/stop queuing
324 * frames asap.
325 */
326 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
327 break;
Sean Paul57355412015-09-19 09:14:34 -0400328 case DRM_COMPOSITION_TYPE_MODESET:
329 break;
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700330 case DRM_COMPOSITION_TYPE_EMPTY:
331 return 0;
332 default:
333 ALOGE("Unknown composition type %d/%d", composition->type(), display_);
334 return -ENOENT;
Sean Paulacb2a442015-06-24 18:43:01 -0700335 }
Sean Paul98e73c82015-06-24 14:38:49 -0700336
337 int ret = pthread_mutex_lock(&lock_);
338 if (ret) {
339 ALOGE("Failed to acquire compositor lock %d", ret);
340 return ret;
341 }
342
Zach Reiznerd410f042015-07-28 15:33:07 -0700343 // Block the queue if it gets too large. Otherwise, SurfaceFlinger will start
344 // to eat our buffer handles when we get about 1 second behind.
345 while (composite_queue_.size() >= DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH) {
346 pthread_mutex_unlock(&lock_);
347 sched_yield();
348 pthread_mutex_lock(&lock_);
349 }
350
Sean Paul98e73c82015-06-24 14:38:49 -0700351 composite_queue_.push(std::move(composition));
352
353 ret = pthread_mutex_unlock(&lock_);
354 if (ret) {
355 ALOGE("Failed to release compositor lock %d", ret);
356 return ret;
357 }
358
359 worker_.Signal();
360 return 0;
361}
362
Zach Reizner92f8e632015-10-12 17:47:13 -0700363std::tuple<uint32_t, uint32_t, int>
364DrmDisplayCompositor::GetActiveModeResolution() {
365 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
366 if (connector == NULL) {
367 ALOGE("Failed to determine display mode: no connector for display %d",
368 display_);
369 return std::make_tuple(0, 0, -ENODEV);
370 }
371
372 const DrmMode &mode = connector->active_mode();
373 return std::make_tuple(mode.h_display(), mode.v_display(), 0);
Zach Reizner713a6782015-07-31 15:12:44 -0700374}
375
Zach Reizner92f8e632015-10-12 17:47:13 -0700376int DrmDisplayCompositor::PrepareFramebuffer(
377 DrmFramebuffer &fb, DrmDisplayComposition *display_comp) {
378 int ret = fb.WaitReleased(-1);
379 if (ret) {
380 ALOGE("Failed to wait for framebuffer release %d", ret);
381 return ret;
382 }
383 uint32_t width, height;
384 std::tie(width, height, ret) = GetActiveModeResolution();
385 if (ret) {
386 ALOGE(
387 "Failed to allocate framebuffer because the display resolution could "
388 "not be determined %d",
389 ret);
390 return ret;
391 }
392
393 fb.set_release_fence_fd(-1);
394 if (!fb.Allocate(width, height)) {
395 ALOGE("Failed to allocate framebuffer with size %dx%d", width, height);
396 return -ENOMEM;
397 }
398
399 display_comp->layers().emplace_back();
400 DrmHwcLayer &pre_comp_layer = display_comp->layers().back();
401 pre_comp_layer.sf_handle = fb.buffer()->handle;
Haixia Shi0c7da1e2015-11-23 11:34:36 -0800402 pre_comp_layer.blending = DrmHwcBlending::kPreMult;
Zach Reizner92f8e632015-10-12 17:47:13 -0700403 pre_comp_layer.source_crop = DrmHwcRect<float>(0, 0, width, height);
404 pre_comp_layer.display_frame = DrmHwcRect<int>(0, 0, width, height);
405 ret = pre_comp_layer.buffer.ImportBuffer(fb.buffer()->handle,
406 display_comp->importer());
407 if (ret) {
408 ALOGE("Failed to import framebuffer for display %d", ret);
409 return ret;
410 }
411
412 return ret;
Zach Reizner713a6782015-07-31 15:12:44 -0700413}
414
Zach Reizner5757e822015-10-16 19:06:31 -0700415int DrmDisplayCompositor::ApplySquash(DrmDisplayComposition *display_comp) {
416 int ret = 0;
417
418 DrmFramebuffer &fb = squash_framebuffers_[squash_framebuffer_index_];
419 ret = PrepareFramebuffer(fb, display_comp);
420 if (ret) {
421 ALOGE("Failed to prepare framebuffer for squash %d", ret);
422 return ret;
423 }
424
425 std::vector<DrmCompositionRegion> &regions = display_comp->squash_regions();
426 ret = pre_compositor_->Composite(display_comp->layers().data(),
427 regions.data(), regions.size(), fb.buffer());
428 pre_compositor_->Finish();
429
430 if (ret) {
431 ALOGE("Failed to squash layers");
432 return ret;
433 }
434
435 ret = display_comp->CreateNextTimelineFence();
436 if (ret <= 0) {
437 ALOGE("Failed to create squash framebuffer release fence %d", ret);
438 return ret;
439 }
440
441 fb.set_release_fence_fd(ret);
442 display_comp->SignalSquashDone();
443
444 return 0;
445}
446
Zach Reizner713a6782015-07-31 15:12:44 -0700447int DrmDisplayCompositor::ApplyPreComposite(
448 DrmDisplayComposition *display_comp) {
449 int ret = 0;
Zach Reizner713a6782015-07-31 15:12:44 -0700450
Zach Reizner713a6782015-07-31 15:12:44 -0700451 DrmFramebuffer &fb = framebuffers_[framebuffer_index_];
Zach Reizner92f8e632015-10-12 17:47:13 -0700452 ret = PrepareFramebuffer(fb, display_comp);
Zach Reizner713a6782015-07-31 15:12:44 -0700453 if (ret) {
Zach Reizner5757e822015-10-16 19:06:31 -0700454 ALOGE("Failed to prepare framebuffer for pre-composite %d", ret);
Zach Reizner713a6782015-07-31 15:12:44 -0700455 return ret;
456 }
Zach Reizner713a6782015-07-31 15:12:44 -0700457
Zach Reizner92f8e632015-10-12 17:47:13 -0700458 std::vector<DrmCompositionRegion> &regions = display_comp->pre_comp_regions();
459 ret = pre_compositor_->Composite(display_comp->layers().data(),
460 regions.data(), regions.size(), fb.buffer());
Zach Reizner8d63e7f2015-08-20 14:52:12 -0700461 pre_compositor_->Finish();
Zach Reiznerb44fd102015-08-07 16:00:01 -0700462
Zach Reizner713a6782015-07-31 15:12:44 -0700463 if (ret) {
Zach Reizner5757e822015-10-16 19:06:31 -0700464 ALOGE("Failed to pre-composite layers");
Zach Reizner713a6782015-07-31 15:12:44 -0700465 return ret;
466 }
467
Zach Reizner92f8e632015-10-12 17:47:13 -0700468 ret = display_comp->CreateNextTimelineFence();
469 if (ret <= 0) {
Zach Reizner5757e822015-10-16 19:06:31 -0700470 ALOGE("Failed to create pre-composite framebuffer release fence %d", ret);
Zach Reizner09807052015-08-13 14:53:41 -0700471 return ret;
472 }
Zach Reizner713a6782015-07-31 15:12:44 -0700473
Zach Reizner92f8e632015-10-12 17:47:13 -0700474 fb.set_release_fence_fd(ret);
475 display_comp->SignalPreCompDone();
Zach Reizner713a6782015-07-31 15:12:44 -0700476
Zach Reizner92f8e632015-10-12 17:47:13 -0700477 return 0;
Zach Reizner713a6782015-07-31 15:12:44 -0700478}
479
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400480int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600481 drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400482 if (!pset) {
483 ALOGE("Failed to allocate property set");
484 return -ENOMEM;
485 }
486
487 int ret;
Zach Reizner92f8e632015-10-12 17:47:13 -0700488 std::vector<DrmCompositionPlane> &comp_planes =
489 display_comp->composition_planes();
490 for (DrmCompositionPlane &comp_plane : comp_planes) {
491 DrmPlane *plane = comp_plane.plane;
Rob Herringbc9305e2016-02-04 14:01:24 -0600492 ret = drmModeAtomicAddProperty(pset, plane->id(),
493 plane->crtc_property().id(), 0) < 0 ||
494 drmModeAtomicAddProperty(pset, plane->id(), plane->fb_property().id(),
495 0) < 0;
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400496 if (ret) {
497 ALOGE("Failed to add plane %d disable to pset", plane->id());
Rob Herringbc9305e2016-02-04 14:01:24 -0600498 drmModeAtomicFree(pset);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400499 return ret;
500 }
501 }
502
Rob Herringbc9305e2016-02-04 14:01:24 -0600503 ret = drmModeAtomicCommit(drm_->fd(), pset, 0, drm_);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400504 if (ret) {
505 ALOGE("Failed to commit pset ret=%d\n", ret);
Rob Herringbc9305e2016-02-04 14:01:24 -0600506 drmModeAtomicFree(pset);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400507 return ret;
508 }
509
Rob Herringbc9305e2016-02-04 14:01:24 -0600510 drmModeAtomicFree(pset);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400511 return 0;
512}
513
Haixia Shidda2fab2015-10-22 18:12:49 -0700514int DrmDisplayCompositor::PrepareFrame(DrmDisplayComposition *display_comp) {
Sean Paul98e73c82015-06-24 14:38:49 -0700515 int ret = 0;
516
Zach Reizner92f8e632015-10-12 17:47:13 -0700517 std::vector<DrmHwcLayer> &layers = display_comp->layers();
518 std::vector<DrmCompositionPlane> &comp_planes =
519 display_comp->composition_planes();
Zach Reizner5757e822015-10-16 19:06:31 -0700520 std::vector<DrmCompositionRegion> &squash_regions =
521 display_comp->squash_regions();
Zach Reizner92f8e632015-10-12 17:47:13 -0700522 std::vector<DrmCompositionRegion> &pre_comp_regions =
523 display_comp->pre_comp_regions();
524
Zach Reizner5757e822015-10-16 19:06:31 -0700525 int squash_layer_index = -1;
526 if (squash_regions.size() > 0) {
527 squash_framebuffer_index_ = (squash_framebuffer_index_ + 1) % 2;
528 ret = ApplySquash(display_comp);
529 if (ret)
530 return ret;
Zach Reizner92f8e632015-10-12 17:47:13 -0700531
Zach Reizner5757e822015-10-16 19:06:31 -0700532 squash_layer_index = layers.size() - 1;
533 } else {
534 if (UsesSquash(comp_planes)) {
535 DrmFramebuffer &fb = squash_framebuffers_[squash_framebuffer_index_];
536 layers.emplace_back();
537 squash_layer_index = layers.size() - 1;
538 DrmHwcLayer &squash_layer = layers.back();
539 ret = squash_layer.buffer.ImportBuffer(fb.buffer()->handle,
540 display_comp->importer());
541 if (ret) {
542 ALOGE("Failed to import old squashed framebuffer %d", ret);
543 return ret;
544 }
545 squash_layer.sf_handle = fb.buffer()->handle;
Haixia Shi0c7da1e2015-11-23 11:34:36 -0800546 squash_layer.blending = DrmHwcBlending::kPreMult;
Zach Reizner5757e822015-10-16 19:06:31 -0700547 squash_layer.source_crop = DrmHwcRect<float>(
548 0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
549 squash_layer.display_frame = DrmHwcRect<int>(
550 0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
551 ret = display_comp->CreateNextTimelineFence();
552
553 if (ret <= 0) {
554 ALOGE("Failed to create squash framebuffer release fence %d", ret);
555 return ret;
556 }
557
558 fb.set_release_fence_fd(ret);
559 ret = 0;
560 }
561 }
562
563 bool do_pre_comp = pre_comp_regions.size() > 0;
564 int pre_comp_layer_index = -1;
Zach Reizner92f8e632015-10-12 17:47:13 -0700565 if (do_pre_comp) {
Zach Reizner713a6782015-07-31 15:12:44 -0700566 ret = ApplyPreComposite(display_comp);
567 if (ret)
568 return ret;
Zach Reizner92f8e632015-10-12 17:47:13 -0700569
570 pre_comp_layer_index = layers.size() - 1;
571 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
Zach Reizner713a6782015-07-31 15:12:44 -0700572 }
573
Haixia Shidda2fab2015-10-22 18:12:49 -0700574 for (DrmCompositionPlane &comp_plane : comp_planes) {
Sean Paul39b37842016-05-11 13:50:28 -0400575 switch (comp_plane.type) {
576 case DrmCompositionPlaneType::kSquash:
Haixia Shidda2fab2015-10-22 18:12:49 -0700577 comp_plane.source_layer = squash_layer_index;
578 break;
Sean Paul39b37842016-05-11 13:50:28 -0400579 case DrmCompositionPlaneType::kPrecomp:
Haixia Shidda2fab2015-10-22 18:12:49 -0700580 if (!do_pre_comp) {
581 ALOGE(
582 "Can not use pre composite framebuffer with no pre composite "
583 "regions");
584 return -EINVAL;
585 }
586 comp_plane.source_layer = pre_comp_layer_index;
587 break;
588 default:
589 break;
590 }
591 }
592
593 return ret;
594}
595
Sean Paulc07b2112015-11-17 16:38:10 -0500596int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp,
597 bool test_only) {
Haixia Shi3979f7d2015-10-29 14:33:37 -0700598 ATRACE_CALL();
599
Haixia Shidda2fab2015-10-22 18:12:49 -0700600 int ret = 0;
601
602 std::vector<DrmHwcLayer> &layers = display_comp->layers();
603 std::vector<DrmCompositionPlane> &comp_planes =
604 display_comp->composition_planes();
605 std::vector<DrmCompositionRegion> &pre_comp_regions =
606 display_comp->pre_comp_regions();
607
Sean Paul57355412015-09-19 09:14:34 -0400608 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
609 if (!connector) {
610 ALOGE("Could not locate connector for display %d", display_);
611 return -ENODEV;
612 }
613 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display_);
614 if (!crtc) {
615 ALOGE("Could not locate crtc for display %d", display_);
616 return -ENODEV;
617 }
618
Rob Herringbc9305e2016-02-04 14:01:24 -0600619 drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
Sean Paul98e73c82015-06-24 14:38:49 -0700620 if (!pset) {
621 ALOGE("Failed to allocate property set");
622 return -ENOMEM;
623 }
624
Sean Paul35301f42015-11-17 14:46:56 -0500625 if (mode_.needs_modeset) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600626 ret = drmModeAtomicAddProperty(pset, crtc->id(), crtc->mode_property().id(),
627 mode_.blob_id) < 0 ||
628 drmModeAtomicAddProperty(pset, connector->id(),
629 connector->crtc_id_property().id(),
630 crtc->id()) < 0;
Sean Paul57355412015-09-19 09:14:34 -0400631 if (ret) {
Sean Paul35301f42015-11-17 14:46:56 -0500632 ALOGE("Failed to add blob %d to pset", mode_.blob_id);
Rob Herringbc9305e2016-02-04 14:01:24 -0600633 drmModeAtomicFree(pset);
Sean Paul57355412015-09-19 09:14:34 -0400634 return ret;
635 }
636 }
637
Zach Reizner92f8e632015-10-12 17:47:13 -0700638 for (DrmCompositionPlane &comp_plane : comp_planes) {
639 DrmPlane *plane = comp_plane.plane;
640 DrmCrtc *crtc = comp_plane.crtc;
641
642 int fb_id = -1;
643 DrmHwcRect<int> display_frame;
644 DrmHwcRect<float> source_crop;
645 uint64_t rotation = 0;
Sean Pauld8aefb62015-10-15 15:17:31 -0400646 uint64_t alpha = 0xFF;
Sean Paul04b47ea2015-11-19 21:46:11 -0500647
Sean Paul39b37842016-05-11 13:50:28 -0400648 if (comp_plane.type != DrmCompositionPlaneType::kDisable) {
649 if (comp_plane.source_layer < 0 ||
650 static_cast<size_t>(comp_plane.source_layer) >= layers.size()) {
651 ALOGE("Source layer index %d out of bounds %zu type=%d",
652 comp_plane.source_layer, layers.size(), comp_plane.type);
653 break;
Sean Paul98e73c82015-06-24 14:38:49 -0700654 }
Sean Paul39b37842016-05-11 13:50:28 -0400655 DrmHwcLayer &layer = layers[comp_plane.source_layer];
656 if (!test_only && layer.acquire_fence.get() >= 0) {
657 int acquire_fence = layer.acquire_fence.get();
658 int total_fence_timeout = 0;
659 for (int i = 0; i < kAcquireWaitTries; ++i) {
660 int fence_timeout = kAcquireWaitTimeoutMs * (1 << i);
661 total_fence_timeout += fence_timeout;
662 ret = sync_wait(acquire_fence, fence_timeout);
663 if (ret)
664 ALOGW("Acquire fence %d wait %d failed (%d). Total time %d",
665 acquire_fence, i, ret, total_fence_timeout);
666 }
667 if (ret) {
668 ALOGE("Failed to wait for acquire %d/%d", acquire_fence, ret);
669 break;
670 }
671 layer.acquire_fence.Close();
672 }
673 if (!layer.buffer) {
674 ALOGE("Expected a valid framebuffer for pset");
675 break;
676 }
677 fb_id = layer.buffer->fb_id;
678 display_frame = layer.display_frame;
679 source_crop = layer.source_crop;
680 if (layer.blending == DrmHwcBlending::kPreMult)
681 alpha = layer.alpha;
Sean Paul98e73c82015-06-24 14:38:49 -0700682
Sean Paul39b37842016-05-11 13:50:28 -0400683 rotation = 0;
684 if (layer.transform & DrmHwcTransform::kFlipH)
685 rotation |= 1 << DRM_REFLECT_X;
686 if (layer.transform & DrmHwcTransform::kFlipV)
687 rotation |= 1 << DRM_REFLECT_Y;
688 if (layer.transform & DrmHwcTransform::kRotate90)
689 rotation |= 1 << DRM_ROTATE_90;
690 else if (layer.transform & DrmHwcTransform::kRotate180)
691 rotation |= 1 << DRM_ROTATE_180;
692 else if (layer.transform & DrmHwcTransform::kRotate270)
693 rotation |= 1 << DRM_ROTATE_270;
694 }
Zach Reizner92f8e632015-10-12 17:47:13 -0700695 // Disable the plane if there's no framebuffer
696 if (fb_id < 0) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600697 ret = drmModeAtomicAddProperty(pset, plane->id(),
698 plane->crtc_property().id(), 0) < 0 ||
699 drmModeAtomicAddProperty(pset, plane->id(),
700 plane->fb_property().id(), 0) < 0;
Sean Paul2e46fbd2015-07-09 17:22:22 -0400701 if (ret) {
702 ALOGE("Failed to add plane %d disable to pset", plane->id());
703 break;
704 }
705 continue;
706 }
707
Sean Paul1c4c3262015-07-14 15:51:52 -0400708 // TODO: Once we have atomic test, this should fall back to GL
709 if (rotation && plane->rotation_property().id() == 0) {
710 ALOGE("Rotation is not supported on plane %d", plane->id());
711 ret = -EINVAL;
712 break;
713 }
714
Sean Pauld8aefb62015-10-15 15:17:31 -0400715 // TODO: Once we have atomic test, this should fall back to GL
716 if (alpha != 0xFF && plane->alpha_property().id() == 0) {
717 ALOGE("Alpha is not supported on plane %d", plane->id());
718 ret = -EINVAL;
719 break;
720 }
721
Rob Herringbc9305e2016-02-04 14:01:24 -0600722 ret = drmModeAtomicAddProperty(pset, plane->id(),
723 plane->crtc_property().id(), crtc->id()) < 0;
724 ret |= drmModeAtomicAddProperty(pset, plane->id(),
725 plane->fb_property().id(), fb_id) < 0;
726 ret |= drmModeAtomicAddProperty(pset, plane->id(),
727 plane->crtc_x_property().id(),
728 display_frame.left) < 0;
729 ret |= drmModeAtomicAddProperty(pset, plane->id(),
730 plane->crtc_y_property().id(),
731 display_frame.top) < 0;
732 ret |= drmModeAtomicAddProperty(
733 pset, plane->id(), plane->crtc_w_property().id(),
734 display_frame.right - display_frame.left) < 0;
735 ret |= drmModeAtomicAddProperty(
736 pset, plane->id(), plane->crtc_h_property().id(),
737 display_frame.bottom - display_frame.top) < 0;
738 ret |= drmModeAtomicAddProperty(pset, plane->id(),
739 plane->src_x_property().id(),
740 (int)(source_crop.left) << 16) < 0;
741 ret |= drmModeAtomicAddProperty(pset, plane->id(),
742 plane->src_y_property().id(),
743 (int)(source_crop.top) << 16) < 0;
744 ret |= drmModeAtomicAddProperty(
745 pset, plane->id(), plane->src_w_property().id(),
746 (int)(source_crop.right - source_crop.left) << 16) < 0;
747 ret |= drmModeAtomicAddProperty(
748 pset, plane->id(), plane->src_h_property().id(),
749 (int)(source_crop.bottom - source_crop.top) << 16) < 0;
Sean Paul98e73c82015-06-24 14:38:49 -0700750 if (ret) {
751 ALOGE("Failed to add plane %d to set", plane->id());
752 break;
753 }
Sean Paul1c4c3262015-07-14 15:51:52 -0400754
755 if (plane->rotation_property().id()) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600756 ret = drmModeAtomicAddProperty(pset, plane->id(),
757 plane->rotation_property().id(),
758 rotation) < 0;
Sean Paul1c4c3262015-07-14 15:51:52 -0400759 if (ret) {
760 ALOGE("Failed to add rotation property %d to plane %d",
761 plane->rotation_property().id(), plane->id());
762 break;
763 }
764 }
Sean Pauld8aefb62015-10-15 15:17:31 -0400765
766 if (plane->alpha_property().id()) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600767 ret = drmModeAtomicAddProperty(pset, plane->id(),
768 plane->alpha_property().id(),
769 alpha) < 0;
Sean Pauld8aefb62015-10-15 15:17:31 -0400770 if (ret) {
771 ALOGE("Failed to add alpha property %d to plane %d",
772 plane->alpha_property().id(), plane->id());
773 break;
774 }
775 }
Sean Paul98e73c82015-06-24 14:38:49 -0700776 }
777
Zach Reizner92f8e632015-10-12 17:47:13 -0700778out:
Sean Paul98e73c82015-06-24 14:38:49 -0700779 if (!ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500780 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
781 if (test_only)
782 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
783
Rob Herringbc9305e2016-02-04 14:01:24 -0600784 ret = drmModeAtomicCommit(drm_->fd(), pset, flags, drm_);
Sean Paul57355412015-09-19 09:14:34 -0400785 if (ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500786 if (test_only)
787 ALOGI("Commit test pset failed ret=%d\n", ret);
788 else
789 ALOGE("Failed to commit pset ret=%d\n", ret);
Rob Herringbc9305e2016-02-04 14:01:24 -0600790 drmModeAtomicFree(pset);
Sean Paul57355412015-09-19 09:14:34 -0400791 return ret;
792 }
Sean Paul98e73c82015-06-24 14:38:49 -0700793 }
794 if (pset)
Rob Herringbc9305e2016-02-04 14:01:24 -0600795 drmModeAtomicFree(pset);
Sean Paul98e73c82015-06-24 14:38:49 -0700796
Sean Paulc07b2112015-11-17 16:38:10 -0500797 if (!test_only && mode_.needs_modeset) {
Sean Paul35301f42015-11-17 14:46:56 -0500798 ret = drm_->DestroyPropertyBlob(mode_.old_blob_id);
Sean Paul57355412015-09-19 09:14:34 -0400799 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400800 ALOGE("Failed to destroy old mode property blob %" PRIu32 "/%d",
Sean Paul35301f42015-11-17 14:46:56 -0500801 mode_.old_blob_id, ret);
Sean Paul57355412015-09-19 09:14:34 -0400802 return ret;
803 }
804
805 /* TODO: Add dpms to the pset when the kernel supports it */
806 ret = ApplyDpms(display_comp);
807 if (ret) {
808 ALOGE("Failed to apply DPMS after modeset %d\n", ret);
809 return ret;
810 }
811
Sean Paul35301f42015-11-17 14:46:56 -0500812 connector->set_active_mode(mode_.mode);
813 mode_.old_blob_id = mode_.blob_id;
814 mode_.blob_id = 0;
815 mode_.needs_modeset = false;
Sean Paul57355412015-09-19 09:14:34 -0400816 }
817
Sean Paul98e73c82015-06-24 14:38:49 -0700818 return ret;
819}
820
Sean Pauldb7a17d2015-06-24 18:46:05 -0700821int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
822 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
823 if (!conn) {
824 ALOGE("Failed to get DrmConnector for display %d", display_);
825 return -ENODEV;
826 }
827
828 const DrmProperty &prop = conn->dpms_property();
829 int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(),
830 display_comp->dpms_mode());
831 if (ret) {
832 ALOGE("Failed to set DPMS property for connector %d", conn->id());
833 return ret;
834 }
835 return 0;
836}
837
Sean Paul35301f42015-11-17 14:46:56 -0500838std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob(
839 const DrmMode &mode) {
840 struct drm_mode_modeinfo drm_mode;
841 memset(&drm_mode, 0, sizeof(drm_mode));
842 mode.ToDrmModeModeInfo(&drm_mode);
843
844 uint32_t id = 0;
845 int ret = drm_->CreatePropertyBlob(&drm_mode,
846 sizeof(struct drm_mode_modeinfo), &id);
847 if (ret) {
848 ALOGE("Failed to create mode property blob %d", ret);
849 return std::make_tuple(ret, 0);
850 }
Sean Paulf741c672016-05-11 13:49:38 -0400851 ALOGE("Create blob_id %" PRIu32 "\n", id);
Sean Paul35301f42015-11-17 14:46:56 -0500852 return std::make_tuple(ret, id);
853}
854
Haixia Shidda2fab2015-10-22 18:12:49 -0700855void DrmDisplayCompositor::ApplyFrame(
856 std::unique_ptr<DrmDisplayComposition> composition, int status) {
857 int ret = status;
858
859 if (!ret)
Sean Paulc07b2112015-11-17 16:38:10 -0500860 ret = CommitFrame(composition.get(), false);
Haixia Shidda2fab2015-10-22 18:12:49 -0700861
862 if (ret) {
863 ALOGE("Composite failed for display %d", display_);
864
865 // Disable the hw used by the last active composition. This allows us to
866 // signal the release fences from that composition to avoid hanging.
867 if (DisablePlanes(active_composition_.get()))
868 return;
869 }
870 ++dump_frames_composited_;
871
872 if (active_composition_)
873 active_composition_->SignalCompositionDone();
874
875 ret = pthread_mutex_lock(&lock_);
876 if (ret)
877 ALOGE("Failed to acquire lock for active_composition swap");
878
879 active_composition_.swap(composition);
880
881 if (!ret)
882 ret = pthread_mutex_unlock(&lock_);
883 if (ret)
884 ALOGE("Failed to release lock for active_composition swap");
885}
886
Sean Paul98e73c82015-06-24 14:38:49 -0700887int DrmDisplayCompositor::Composite() {
888 ATRACE_CALL();
Zach Reizner09807052015-08-13 14:53:41 -0700889
890 if (!pre_compositor_) {
891 pre_compositor_.reset(new GLWorkerCompositor());
892 int ret = pre_compositor_->Init();
893 if (ret) {
894 ALOGE("Failed to initialize OpenGL compositor %d", ret);
895 return ret;
896 }
897 }
898
Sean Paul98e73c82015-06-24 14:38:49 -0700899 int ret = pthread_mutex_lock(&lock_);
900 if (ret) {
901 ALOGE("Failed to acquire compositor lock %d", ret);
902 return ret;
903 }
904 if (composite_queue_.empty()) {
905 ret = pthread_mutex_unlock(&lock_);
906 if (ret)
907 ALOGE("Failed to release compositor lock %d", ret);
908 return ret;
909 }
910
911 std::unique_ptr<DrmDisplayComposition> composition(
912 std::move(composite_queue_.front()));
Zach Reizner4a253652015-09-10 18:30:54 -0700913
Sean Paul98e73c82015-06-24 14:38:49 -0700914 composite_queue_.pop();
915
916 ret = pthread_mutex_unlock(&lock_);
917 if (ret) {
918 ALOGE("Failed to release compositor lock %d", ret);
919 return ret;
920 }
921
Sean Paulacb2a442015-06-24 18:43:01 -0700922 switch (composition->type()) {
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700923 case DRM_COMPOSITION_TYPE_FRAME:
Sean Paule3141c62015-11-30 14:35:45 -0500924 ret = PrepareFrame(composition.get());
925 if (ret) {
926 ALOGE("Failed to prepare frame for display %d", display_);
927 return ret;
Sean Paul647beb22015-11-19 13:55:48 -0500928 }
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800929 if (composition->geometry_changed()) {
930 // Send the composition to the kernel to ensure we can commit it. This
931 // is just a test, it won't actually commit the frame. If rejected,
932 // squash the frame into one layer and use the squashed composition
933 ret = CommitFrame(composition.get(), true);
Sean Paul6c18b3b2015-11-25 11:04:25 -0500934 if (ret)
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800935 ALOGI("Commit test failed, squashing frame for display %d", display_);
Sean Paul6c18b3b2015-11-25 11:04:25 -0500936 use_hw_overlays_ = !ret;
937 }
938
939 // If use_hw_overlays_ is false, we can't use hardware to composite the
940 // frame. So squash all layers into a single composition and queue that
941 // instead.
942 if (!use_hw_overlays_) {
943 std::unique_ptr<DrmDisplayComposition> squashed = CreateComposition();
944 ret = SquashFrame(composition.get(), squashed.get());
945 if (!ret) {
946 composition = std::move(squashed);
947 } else {
948 ALOGE("Failed to squash frame for display %d", display_);
949 return ret;
Sean Paul647beb22015-11-19 13:55:48 -0500950 }
951 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700952 frame_worker_.QueueFrame(std::move(composition), ret);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700953 break;
954 case DRM_COMPOSITION_TYPE_DPMS:
955 ret = ApplyDpms(composition.get());
956 if (ret)
957 ALOGE("Failed to apply dpms for display %d", display_);
Sean Paulacb2a442015-06-24 18:43:01 -0700958 return ret;
Sean Paul57355412015-09-19 09:14:34 -0400959 case DRM_COMPOSITION_TYPE_MODESET:
Sean Paul35301f42015-11-17 14:46:56 -0500960 mode_.mode = composition->display_mode();
961 if (mode_.blob_id)
962 drm_->DestroyPropertyBlob(mode_.blob_id);
963 std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
964 if (ret) {
965 ALOGE("Failed to create mode blob for display %d", display_);
966 return ret;
967 }
968 mode_.needs_modeset = true;
Sean Paul57355412015-09-19 09:14:34 -0400969 return 0;
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700970 default:
971 ALOGE("Unknown composition type %d", composition->type());
972 return -EINVAL;
Sean Paul98e73c82015-06-24 14:38:49 -0700973 }
Sean Paul98e73c82015-06-24 14:38:49 -0700974
Sean Paul98e73c82015-06-24 14:38:49 -0700975 return ret;
976}
977
978bool DrmDisplayCompositor::HaveQueuedComposites() const {
979 int ret = pthread_mutex_lock(&lock_);
980 if (ret) {
981 ALOGE("Failed to acquire compositor lock %d", ret);
982 return false;
983 }
984
985 bool empty_ret = !composite_queue_.empty();
986
987 ret = pthread_mutex_unlock(&lock_);
988 if (ret) {
989 ALOGE("Failed to release compositor lock %d", ret);
990 return false;
991 }
992
993 return empty_ret;
994}
995
Zach Reiznerbff33ac2015-11-16 11:08:46 -0800996int DrmDisplayCompositor::SquashAll() {
997 AutoLock lock(&lock_, "compositor");
998 int ret = lock.Lock();
999 if (ret)
1000 return ret;
1001
1002 if (!active_composition_)
1003 return 0;
1004
Sean Pauld51c7612015-11-18 14:12:51 -05001005 std::unique_ptr<DrmDisplayComposition> comp = CreateComposition();
1006 ret = SquashFrame(active_composition_.get(), comp.get());
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001007
Sean Pauld51c7612015-11-18 14:12:51 -05001008 // ApplyFrame needs the lock
1009 lock.Unlock();
1010
1011 if (!ret)
1012 ApplyFrame(std::move(comp), 0);
1013
1014 return ret;
1015}
1016
1017// Returns:
1018// - 0 if src is successfully squashed into dst
1019// - -EALREADY if the src is already squashed
1020// - Appropriate error if the squash fails
1021int DrmDisplayCompositor::SquashFrame(DrmDisplayComposition *src,
1022 DrmDisplayComposition *dst) {
1023 if (src->type() != DRM_COMPOSITION_TYPE_FRAME)
1024 return -ENOTSUP;
1025
1026 std::vector<DrmCompositionPlane> &src_planes = src->composition_planes();
1027 std::vector<DrmHwcLayer> &src_layers = src->layers();
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001028
1029 // Make sure there is more than one layer to squash.
Sean Pauld51c7612015-11-18 14:12:51 -05001030 size_t src_planes_with_layer = std::count_if(
1031 src_planes.begin(), src_planes.end(), [](DrmCompositionPlane &p) {
Sean Paul39b37842016-05-11 13:50:28 -04001032 return p.type == DrmCompositionPlaneType::kLayer;
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001033 });
Sean Pauld51c7612015-11-18 14:12:51 -05001034 if (src_planes_with_layer <= 1)
1035 return -EALREADY;
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001036
1037 int pre_comp_layer_index;
1038
Sean Pauld51c7612015-11-18 14:12:51 -05001039 int ret = dst->Init(drm_, src->crtc(), src->importer(), src->frame_no());
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001040 if (ret) {
1041 ALOGE("Failed to init squash all composition %d", ret);
1042 return ret;
1043 }
1044
1045 std::vector<DrmPlane *> primary_planes;
1046 std::vector<DrmPlane *> fake_overlay_planes;
Sean Pauld51c7612015-11-18 14:12:51 -05001047 std::vector<DrmHwcLayer> dst_layers;
1048 for (DrmCompositionPlane &comp_plane : src_planes) {
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001049 // Composition planes without DRM planes should never happen
1050 if (comp_plane.plane == NULL) {
1051 ALOGE("Skipping squash all because of NULL plane");
Sean Pauld51c7612015-11-18 14:12:51 -05001052 ret = -EINVAL;
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001053 goto move_layers_back;
1054 }
1055
Sean Paul39b37842016-05-11 13:50:28 -04001056 if (comp_plane.type == DrmCompositionPlaneType::kDisable ||
1057 comp_plane.source_layer < 0)
Zach Reizner2b4b1ee2015-11-18 13:54:31 -08001058 continue;
1059
Sean Pauld51c7612015-11-18 14:12:51 -05001060 DrmHwcLayer &layer = src_layers[comp_plane.source_layer];
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001061
1062 // Squashing protected layers is impossible.
Sean Pauld51c7612015-11-18 14:12:51 -05001063 if (layer.protected_usage()) {
1064 ret = -ENOTSUP;
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001065 goto move_layers_back;
Sean Pauld51c7612015-11-18 14:12:51 -05001066 }
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001067
1068 // The OutputFds point to freed memory after hwc_set returns. They are
1069 // returned to the default to prevent DrmDisplayComposition::Plan from
1070 // filling the OutputFds.
1071 layer.release_fence = OutputFd();
Sean Pauld51c7612015-11-18 14:12:51 -05001072 dst_layers.emplace_back(std::move(layer));
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001073
1074 if (comp_plane.plane->type() == DRM_PLANE_TYPE_PRIMARY &&
1075 primary_planes.size() == 0)
1076 primary_planes.push_back(comp_plane.plane);
1077 else
Sean Pauld51c7612015-11-18 14:12:51 -05001078 dst->AddPlaneDisable(comp_plane.plane);
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001079 }
1080
Sean Pauld51c7612015-11-18 14:12:51 -05001081 ret = dst->SetLayers(dst_layers.data(), dst_layers.size(), false);
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001082 if (ret) {
1083 ALOGE("Failed to set layers for squash all composition %d", ret);
1084 goto move_layers_back;
1085 }
1086
1087 ret =
Sean Pauld51c7612015-11-18 14:12:51 -05001088 dst->Plan(NULL /* SquashState */, &primary_planes, &fake_overlay_planes);
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001089 if (ret) {
1090 ALOGE("Failed to plan for squash all composition %d", ret);
1091 goto move_layers_back;
1092 }
1093
Sean Pauld51c7612015-11-18 14:12:51 -05001094 ret = ApplyPreComposite(dst);
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001095 if (ret) {
1096 ALOGE("Failed to pre-composite for squash all composition %d", ret);
1097 goto move_layers_back;
1098 }
1099
Sean Pauld51c7612015-11-18 14:12:51 -05001100 pre_comp_layer_index = dst->layers().size() - 1;
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001101 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
1102
Sean Pauld51c7612015-11-18 14:12:51 -05001103 for (DrmCompositionPlane &plane : dst->composition_planes())
Sean Paul39b37842016-05-11 13:50:28 -04001104 if (plane.type == DrmCompositionPlaneType::kPrecomp)
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001105 plane.source_layer = pre_comp_layer_index;
1106
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001107 return 0;
1108
1109// TODO(zachr): think of a better way to transfer ownership back to the active
1110// composition.
1111move_layers_back:
1112 for (size_t plane_index = 0;
Sean Pauld51c7612015-11-18 14:12:51 -05001113 plane_index < src_planes.size() && plane_index < dst_layers.size();
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001114 plane_index++) {
Sean Pauld51c7612015-11-18 14:12:51 -05001115 size_t source_layer_index = src_planes[plane_index].source_layer;
1116 src_layers[source_layer_index] = std::move(dst_layers[plane_index]);
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001117 }
1118
1119 return ret;
1120}
1121
Sean Paul98e73c82015-06-24 14:38:49 -07001122void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
Zach Reiznerfd6dc332015-10-13 21:12:48 -07001123 int ret = pthread_mutex_lock(&lock_);
1124 if (ret)
1125 return;
1126
1127 uint64_t num_frames = dump_frames_composited_;
1128 dump_frames_composited_ = 0;
1129
1130 struct timespec ts;
1131 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
1132 if (ret) {
1133 pthread_mutex_unlock(&lock_);
1134 return;
1135 }
1136
1137 uint64_t cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
1138 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
1139 float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f;
1140
1141 *out << "--DrmDisplayCompositor[" << display_
1142 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
1143 << " fps=" << fps << "\n";
1144
1145 dump_last_timestamp_ns_ = cur_ts;
1146
1147 if (active_composition_)
1148 active_composition_->Dump(out);
1149
Zach Reizner5757e822015-10-16 19:06:31 -07001150 squash_state_.Dump(out);
1151
Zach Reiznerfd6dc332015-10-13 21:12:48 -07001152 pthread_mutex_unlock(&lock_);
Sean Paul98e73c82015-06-24 14:38:49 -07001153}
1154}