blob: 29ecc75d5a27b92efb4b795231d3bc31c23d8b17 [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) {
175 return plane.source_layer ==
176 DrmCompositionPlane::kSourceSquash;
177 });
178}
179
Haixia Shidda2fab2015-10-22 18:12:49 -0700180DrmDisplayCompositor::FrameWorker::FrameWorker(DrmDisplayCompositor *compositor)
181 : Worker("frame-worker", HAL_PRIORITY_URGENT_DISPLAY),
182 compositor_(compositor) {
183}
184
185DrmDisplayCompositor::FrameWorker::~FrameWorker() {
186}
187
188int DrmDisplayCompositor::FrameWorker::Init() {
189 return InitWorker();
190}
191
192void DrmDisplayCompositor::FrameWorker::QueueFrame(
193 std::unique_ptr<DrmDisplayComposition> composition, int status) {
194 Lock();
195 FrameState frame;
196 frame.composition = std::move(composition);
197 frame.status = status;
198 frame_queue_.push(std::move(frame));
199 SignalLocked();
200 Unlock();
201}
202
203void DrmDisplayCompositor::FrameWorker::Routine() {
204 int ret = Lock();
205 if (ret) {
206 ALOGE("Failed to lock worker, %d", ret);
207 return;
208 }
209
210 int wait_ret = 0;
211 if (frame_queue_.empty()) {
212 wait_ret = WaitForSignalOrExitLocked();
213 }
214
215 FrameState frame;
216 if (!frame_queue_.empty()) {
217 frame = std::move(frame_queue_.front());
218 frame_queue_.pop();
219 }
220
221 ret = Unlock();
222 if (ret) {
223 ALOGE("Failed to unlock worker, %d", ret);
224 return;
225 }
226
227 if (wait_ret == -EINTR) {
228 return;
229 } else if (wait_ret) {
230 ALOGE("Failed to wait for signal, %d", wait_ret);
231 return;
232 }
233
234 compositor_->ApplyFrame(std::move(frame.composition), frame.status);
235}
236
Sean Paul98e73c82015-06-24 14:38:49 -0700237DrmDisplayCompositor::DrmDisplayCompositor()
238 : drm_(NULL),
239 display_(-1),
240 worker_(this),
Haixia Shidda2fab2015-10-22 18:12:49 -0700241 frame_worker_(this),
Sean Paul98e73c82015-06-24 14:38:49 -0700242 initialized_(false),
Sean Pauldb7a17d2015-06-24 18:46:05 -0700243 active_(false),
Sean Paul6c18b3b2015-11-25 11:04:25 -0500244 use_hw_overlays_(true),
Zach Reizner713a6782015-07-31 15:12:44 -0700245 framebuffer_index_(0),
Zach Reizner5757e822015-10-16 19:06:31 -0700246 squash_framebuffer_index_(0),
Sean Paul98e73c82015-06-24 14:38:49 -0700247 dump_frames_composited_(0),
248 dump_last_timestamp_ns_(0) {
249 struct timespec ts;
250 if (clock_gettime(CLOCK_MONOTONIC, &ts))
251 return;
252 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
253}
254
255DrmDisplayCompositor::~DrmDisplayCompositor() {
256 if (!initialized_)
257 return;
258
259 worker_.Exit();
Haixia Shidda2fab2015-10-22 18:12:49 -0700260 frame_worker_.Exit();
Sean Paul98e73c82015-06-24 14:38:49 -0700261
262 int ret = pthread_mutex_lock(&lock_);
263 if (ret)
264 ALOGE("Failed to acquire compositor lock %d", ret);
265
Sean Paul35301f42015-11-17 14:46:56 -0500266 if (mode_.blob_id)
267 drm_->DestroyPropertyBlob(mode_.blob_id);
268 if (mode_.old_blob_id)
269 drm_->DestroyPropertyBlob(mode_.old_blob_id);
270
Sean Paul98e73c82015-06-24 14:38:49 -0700271 while (!composite_queue_.empty()) {
272 composite_queue_.front().reset();
273 composite_queue_.pop();
274 }
275 active_composition_.reset();
276
277 ret = pthread_mutex_unlock(&lock_);
278 if (ret)
279 ALOGE("Failed to acquire compositor lock %d", ret);
280
281 pthread_mutex_destroy(&lock_);
282}
283
284int DrmDisplayCompositor::Init(DrmResources *drm, int display) {
285 drm_ = drm;
286 display_ = display;
287
288 int ret = pthread_mutex_init(&lock_, NULL);
289 if (ret) {
290 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
291 return ret;
292 }
293 ret = worker_.Init();
294 if (ret) {
295 pthread_mutex_destroy(&lock_);
296 ALOGE("Failed to initialize compositor worker %d\n", ret);
297 return ret;
298 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700299 ret = frame_worker_.Init();
300 if (ret) {
301 pthread_mutex_destroy(&lock_);
302 ALOGE("Failed to initialize frame worker %d\n", ret);
303 return ret;
304 }
Sean Paul98e73c82015-06-24 14:38:49 -0700305
306 initialized_ = true;
307 return 0;
308}
309
Zach Reizner92f8e632015-10-12 17:47:13 -0700310std::unique_ptr<DrmDisplayComposition> DrmDisplayCompositor::CreateComposition()
311 const {
312 return std::unique_ptr<DrmDisplayComposition>(new DrmDisplayComposition());
313}
314
Sean Paul98e73c82015-06-24 14:38:49 -0700315int DrmDisplayCompositor::QueueComposition(
316 std::unique_ptr<DrmDisplayComposition> composition) {
Sean Paulacb2a442015-06-24 18:43:01 -0700317 switch (composition->type()) {
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700318 case DRM_COMPOSITION_TYPE_FRAME:
319 if (!active_)
320 return -ENODEV;
321 break;
322 case DRM_COMPOSITION_TYPE_DPMS:
323 /*
324 * Update the state as soon as we get it so we can start/stop queuing
325 * frames asap.
326 */
327 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
328 break;
Sean Paul57355412015-09-19 09:14:34 -0400329 case DRM_COMPOSITION_TYPE_MODESET:
330 break;
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700331 case DRM_COMPOSITION_TYPE_EMPTY:
332 return 0;
333 default:
334 ALOGE("Unknown composition type %d/%d", composition->type(), display_);
335 return -ENOENT;
Sean Paulacb2a442015-06-24 18:43:01 -0700336 }
Sean Paul98e73c82015-06-24 14:38:49 -0700337
338 int ret = pthread_mutex_lock(&lock_);
339 if (ret) {
340 ALOGE("Failed to acquire compositor lock %d", ret);
341 return ret;
342 }
343
Zach Reiznerd410f042015-07-28 15:33:07 -0700344 // Block the queue if it gets too large. Otherwise, SurfaceFlinger will start
345 // to eat our buffer handles when we get about 1 second behind.
346 while (composite_queue_.size() >= DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH) {
347 pthread_mutex_unlock(&lock_);
348 sched_yield();
349 pthread_mutex_lock(&lock_);
350 }
351
Sean Paul98e73c82015-06-24 14:38:49 -0700352 composite_queue_.push(std::move(composition));
353
354 ret = pthread_mutex_unlock(&lock_);
355 if (ret) {
356 ALOGE("Failed to release compositor lock %d", ret);
357 return ret;
358 }
359
360 worker_.Signal();
361 return 0;
362}
363
Zach Reizner92f8e632015-10-12 17:47:13 -0700364std::tuple<uint32_t, uint32_t, int>
365DrmDisplayCompositor::GetActiveModeResolution() {
366 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
367 if (connector == NULL) {
368 ALOGE("Failed to determine display mode: no connector for display %d",
369 display_);
370 return std::make_tuple(0, 0, -ENODEV);
371 }
372
373 const DrmMode &mode = connector->active_mode();
374 return std::make_tuple(mode.h_display(), mode.v_display(), 0);
Zach Reizner713a6782015-07-31 15:12:44 -0700375}
376
Zach Reizner92f8e632015-10-12 17:47:13 -0700377int DrmDisplayCompositor::PrepareFramebuffer(
378 DrmFramebuffer &fb, DrmDisplayComposition *display_comp) {
379 int ret = fb.WaitReleased(-1);
380 if (ret) {
381 ALOGE("Failed to wait for framebuffer release %d", ret);
382 return ret;
383 }
384 uint32_t width, height;
385 std::tie(width, height, ret) = GetActiveModeResolution();
386 if (ret) {
387 ALOGE(
388 "Failed to allocate framebuffer because the display resolution could "
389 "not be determined %d",
390 ret);
391 return ret;
392 }
393
394 fb.set_release_fence_fd(-1);
395 if (!fb.Allocate(width, height)) {
396 ALOGE("Failed to allocate framebuffer with size %dx%d", width, height);
397 return -ENOMEM;
398 }
399
400 display_comp->layers().emplace_back();
401 DrmHwcLayer &pre_comp_layer = display_comp->layers().back();
402 pre_comp_layer.sf_handle = fb.buffer()->handle;
Haixia Shi0c7da1e2015-11-23 11:34:36 -0800403 pre_comp_layer.blending = DrmHwcBlending::kPreMult;
Zach Reizner92f8e632015-10-12 17:47:13 -0700404 pre_comp_layer.source_crop = DrmHwcRect<float>(0, 0, width, height);
405 pre_comp_layer.display_frame = DrmHwcRect<int>(0, 0, width, height);
406 ret = pre_comp_layer.buffer.ImportBuffer(fb.buffer()->handle,
407 display_comp->importer());
408 if (ret) {
409 ALOGE("Failed to import framebuffer for display %d", ret);
410 return ret;
411 }
412
413 return ret;
Zach Reizner713a6782015-07-31 15:12:44 -0700414}
415
Zach Reizner5757e822015-10-16 19:06:31 -0700416int DrmDisplayCompositor::ApplySquash(DrmDisplayComposition *display_comp) {
417 int ret = 0;
418
419 DrmFramebuffer &fb = squash_framebuffers_[squash_framebuffer_index_];
420 ret = PrepareFramebuffer(fb, display_comp);
421 if (ret) {
422 ALOGE("Failed to prepare framebuffer for squash %d", ret);
423 return ret;
424 }
425
426 std::vector<DrmCompositionRegion> &regions = display_comp->squash_regions();
427 ret = pre_compositor_->Composite(display_comp->layers().data(),
428 regions.data(), regions.size(), fb.buffer());
429 pre_compositor_->Finish();
430
431 if (ret) {
432 ALOGE("Failed to squash layers");
433 return ret;
434 }
435
436 ret = display_comp->CreateNextTimelineFence();
437 if (ret <= 0) {
438 ALOGE("Failed to create squash framebuffer release fence %d", ret);
439 return ret;
440 }
441
442 fb.set_release_fence_fd(ret);
443 display_comp->SignalSquashDone();
444
445 return 0;
446}
447
Zach Reizner713a6782015-07-31 15:12:44 -0700448int DrmDisplayCompositor::ApplyPreComposite(
449 DrmDisplayComposition *display_comp) {
450 int ret = 0;
Zach Reizner713a6782015-07-31 15:12:44 -0700451
Zach Reizner713a6782015-07-31 15:12:44 -0700452 DrmFramebuffer &fb = framebuffers_[framebuffer_index_];
Zach Reizner92f8e632015-10-12 17:47:13 -0700453 ret = PrepareFramebuffer(fb, display_comp);
Zach Reizner713a6782015-07-31 15:12:44 -0700454 if (ret) {
Zach Reizner5757e822015-10-16 19:06:31 -0700455 ALOGE("Failed to prepare framebuffer for pre-composite %d", ret);
Zach Reizner713a6782015-07-31 15:12:44 -0700456 return ret;
457 }
Zach Reizner713a6782015-07-31 15:12:44 -0700458
Zach Reizner92f8e632015-10-12 17:47:13 -0700459 std::vector<DrmCompositionRegion> &regions = display_comp->pre_comp_regions();
460 ret = pre_compositor_->Composite(display_comp->layers().data(),
461 regions.data(), regions.size(), fb.buffer());
Zach Reizner8d63e7f2015-08-20 14:52:12 -0700462 pre_compositor_->Finish();
Zach Reiznerb44fd102015-08-07 16:00:01 -0700463
Zach Reizner713a6782015-07-31 15:12:44 -0700464 if (ret) {
Zach Reizner5757e822015-10-16 19:06:31 -0700465 ALOGE("Failed to pre-composite layers");
Zach Reizner713a6782015-07-31 15:12:44 -0700466 return ret;
467 }
468
Zach Reizner92f8e632015-10-12 17:47:13 -0700469 ret = display_comp->CreateNextTimelineFence();
470 if (ret <= 0) {
Zach Reizner5757e822015-10-16 19:06:31 -0700471 ALOGE("Failed to create pre-composite framebuffer release fence %d", ret);
Zach Reizner09807052015-08-13 14:53:41 -0700472 return ret;
473 }
Zach Reizner713a6782015-07-31 15:12:44 -0700474
Zach Reizner92f8e632015-10-12 17:47:13 -0700475 fb.set_release_fence_fd(ret);
476 display_comp->SignalPreCompDone();
Zach Reizner713a6782015-07-31 15:12:44 -0700477
Zach Reizner92f8e632015-10-12 17:47:13 -0700478 return 0;
Zach Reizner713a6782015-07-31 15:12:44 -0700479}
480
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400481int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
482 drmModePropertySetPtr pset = drmModePropertySetAlloc();
483 if (!pset) {
484 ALOGE("Failed to allocate property set");
485 return -ENOMEM;
486 }
487
488 int ret;
Zach Reizner92f8e632015-10-12 17:47:13 -0700489 std::vector<DrmCompositionPlane> &comp_planes =
490 display_comp->composition_planes();
491 for (DrmCompositionPlane &comp_plane : comp_planes) {
492 DrmPlane *plane = comp_plane.plane;
493 ret =
494 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
495 0) ||
496 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(), 0);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400497 if (ret) {
498 ALOGE("Failed to add plane %d disable to pset", plane->id());
499 drmModePropertySetFree(pset);
500 return ret;
501 }
502 }
503
504 ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
505 if (ret) {
506 ALOGE("Failed to commit pset ret=%d\n", ret);
507 drmModePropertySetFree(pset);
508 return ret;
509 }
510
511 drmModePropertySetFree(pset);
512 return 0;
513}
514
Haixia Shidda2fab2015-10-22 18:12:49 -0700515int DrmDisplayCompositor::PrepareFrame(DrmDisplayComposition *display_comp) {
Sean Paul98e73c82015-06-24 14:38:49 -0700516 int ret = 0;
517
Zach Reizner92f8e632015-10-12 17:47:13 -0700518 std::vector<DrmHwcLayer> &layers = display_comp->layers();
519 std::vector<DrmCompositionPlane> &comp_planes =
520 display_comp->composition_planes();
Zach Reizner5757e822015-10-16 19:06:31 -0700521 std::vector<DrmCompositionRegion> &squash_regions =
522 display_comp->squash_regions();
Zach Reizner92f8e632015-10-12 17:47:13 -0700523 std::vector<DrmCompositionRegion> &pre_comp_regions =
524 display_comp->pre_comp_regions();
525
Zach Reizner5757e822015-10-16 19:06:31 -0700526 int squash_layer_index = -1;
527 if (squash_regions.size() > 0) {
528 squash_framebuffer_index_ = (squash_framebuffer_index_ + 1) % 2;
529 ret = ApplySquash(display_comp);
530 if (ret)
531 return ret;
Zach Reizner92f8e632015-10-12 17:47:13 -0700532
Zach Reizner5757e822015-10-16 19:06:31 -0700533 squash_layer_index = layers.size() - 1;
534 } else {
535 if (UsesSquash(comp_planes)) {
536 DrmFramebuffer &fb = squash_framebuffers_[squash_framebuffer_index_];
537 layers.emplace_back();
538 squash_layer_index = layers.size() - 1;
539 DrmHwcLayer &squash_layer = layers.back();
540 ret = squash_layer.buffer.ImportBuffer(fb.buffer()->handle,
541 display_comp->importer());
542 if (ret) {
543 ALOGE("Failed to import old squashed framebuffer %d", ret);
544 return ret;
545 }
546 squash_layer.sf_handle = fb.buffer()->handle;
Haixia Shi0c7da1e2015-11-23 11:34:36 -0800547 squash_layer.blending = DrmHwcBlending::kPreMult;
Zach Reizner5757e822015-10-16 19:06:31 -0700548 squash_layer.source_crop = DrmHwcRect<float>(
549 0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
550 squash_layer.display_frame = DrmHwcRect<int>(
551 0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
552 ret = display_comp->CreateNextTimelineFence();
553
554 if (ret <= 0) {
555 ALOGE("Failed to create squash framebuffer release fence %d", ret);
556 return ret;
557 }
558
559 fb.set_release_fence_fd(ret);
560 ret = 0;
561 }
562 }
563
564 bool do_pre_comp = pre_comp_regions.size() > 0;
565 int pre_comp_layer_index = -1;
Zach Reizner92f8e632015-10-12 17:47:13 -0700566 if (do_pre_comp) {
Zach Reizner713a6782015-07-31 15:12:44 -0700567 ret = ApplyPreComposite(display_comp);
568 if (ret)
569 return ret;
Zach Reizner92f8e632015-10-12 17:47:13 -0700570
571 pre_comp_layer_index = layers.size() - 1;
572 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
Zach Reizner713a6782015-07-31 15:12:44 -0700573 }
574
Haixia Shidda2fab2015-10-22 18:12:49 -0700575 for (DrmCompositionPlane &comp_plane : comp_planes) {
576 switch (comp_plane.source_layer) {
577 case DrmCompositionPlane::kSourceSquash:
578 comp_plane.source_layer = squash_layer_index;
579 break;
580 case DrmCompositionPlane::kSourcePreComp:
581 if (!do_pre_comp) {
582 ALOGE(
583 "Can not use pre composite framebuffer with no pre composite "
584 "regions");
585 return -EINVAL;
586 }
587 comp_plane.source_layer = pre_comp_layer_index;
588 break;
589 default:
590 break;
591 }
592 }
593
594 return ret;
595}
596
Sean Paulc07b2112015-11-17 16:38:10 -0500597int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp,
598 bool test_only) {
Haixia Shi3979f7d2015-10-29 14:33:37 -0700599 ATRACE_CALL();
600
Haixia Shidda2fab2015-10-22 18:12:49 -0700601 int ret = 0;
602
603 std::vector<DrmHwcLayer> &layers = display_comp->layers();
604 std::vector<DrmCompositionPlane> &comp_planes =
605 display_comp->composition_planes();
606 std::vector<DrmCompositionRegion> &pre_comp_regions =
607 display_comp->pre_comp_regions();
608
609 DrmFramebuffer *pre_comp_fb;
610
Sean Paul57355412015-09-19 09:14:34 -0400611 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
612 if (!connector) {
613 ALOGE("Could not locate connector for display %d", display_);
614 return -ENODEV;
615 }
616 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display_);
617 if (!crtc) {
618 ALOGE("Could not locate crtc for display %d", display_);
619 return -ENODEV;
620 }
621
Sean Paul98e73c82015-06-24 14:38:49 -0700622 drmModePropertySetPtr pset = drmModePropertySetAlloc();
623 if (!pset) {
624 ALOGE("Failed to allocate property set");
625 return -ENOMEM;
626 }
627
Sean Paul35301f42015-11-17 14:46:56 -0500628 if (mode_.needs_modeset) {
Sean Paul57355412015-09-19 09:14:34 -0400629 ret = drmModePropertySetAdd(pset, crtc->id(), crtc->mode_property().id(),
Sean Paul35301f42015-11-17 14:46:56 -0500630 mode_.blob_id) ||
Sean Paul57355412015-09-19 09:14:34 -0400631 drmModePropertySetAdd(pset, connector->id(),
632 connector->crtc_id_property().id(), crtc->id());
633 if (ret) {
Sean Paul35301f42015-11-17 14:46:56 -0500634 ALOGE("Failed to add blob %d to pset", mode_.blob_id);
Sean Paul57355412015-09-19 09:14:34 -0400635 drmModePropertySetFree(pset);
Sean Paul57355412015-09-19 09:14:34 -0400636 return ret;
637 }
638 }
639
Zach Reizner92f8e632015-10-12 17:47:13 -0700640 for (DrmCompositionPlane &comp_plane : comp_planes) {
641 DrmPlane *plane = comp_plane.plane;
642 DrmCrtc *crtc = comp_plane.crtc;
643
644 int fb_id = -1;
645 DrmHwcRect<int> display_frame;
646 DrmHwcRect<float> source_crop;
647 uint64_t rotation = 0;
Sean Pauld8aefb62015-10-15 15:17:31 -0400648 uint64_t alpha = 0xFF;
Zach Reizner92f8e632015-10-12 17:47:13 -0700649 switch (comp_plane.source_layer) {
650 case DrmCompositionPlane::kSourceNone:
651 break;
Haixia Shidda2fab2015-10-22 18:12:49 -0700652 case DrmCompositionPlane::kSourceSquash:
653 ALOGE("Actual source layer index expected for squash layer");
Zach Reizner5757e822015-10-16 19:06:31 -0700654 break;
Haixia Shidda2fab2015-10-22 18:12:49 -0700655 case DrmCompositionPlane::kSourcePreComp:
656 ALOGE("Actual source layer index expected for pre-comp layer");
Zach Reiznerd75d8d22015-10-15 14:41:41 -0700657 break;
Zach Reizner92f8e632015-10-12 17:47:13 -0700658 default: {
659 if (comp_plane.source_layer >= layers.size()) {
660 ALOGE("Source layer index %zu out of bounds %zu",
661 comp_plane.source_layer, layers.size());
662 break;
663 }
664 DrmHwcLayer &layer = layers[comp_plane.source_layer];
Sean Paulc07b2112015-11-17 16:38:10 -0500665 if (!test_only && layer.acquire_fence.get() >= 0) {
Zach Reizner92f8e632015-10-12 17:47:13 -0700666 int acquire_fence = layer.acquire_fence.get();
667 for (int i = 0; i < kAcquireWaitTries; ++i) {
668 ret = sync_wait(acquire_fence, kAcquireWaitTimeoutMs);
669 if (ret)
670 ALOGW("Acquire fence %d wait %d failed (%d). Total time %d",
671 acquire_fence, i, ret, (i + 1) * kAcquireWaitTimeoutMs);
672 }
673 if (ret) {
674 ALOGE("Failed to wait for acquire %d/%d", acquire_fence, ret);
675 break;
676 }
677 layer.acquire_fence.Close();
678 }
679 if (!layer.buffer) {
680 ALOGE("Expected a valid framebuffer for pset");
681 break;
682 }
683 fb_id = layer.buffer->fb_id;
684 display_frame = layer.display_frame;
685 source_crop = layer.source_crop;
Sean Pauld8aefb62015-10-15 15:17:31 -0400686 if (layer.blending == DrmHwcBlending::kPreMult)
687 alpha = layer.alpha;
Sean Paul04b47ea2015-11-19 21:46:11 -0500688
689 rotation = 0;
690 if (layer.transform & DrmHwcTransform::kFlipH)
691 rotation |= 1 << DRM_REFLECT_X;
692 if (layer.transform & DrmHwcTransform::kFlipV)
693 rotation |= 1 << DRM_REFLECT_Y;
694 if (layer.transform & DrmHwcTransform::kRotate90)
695 rotation |= 1 << DRM_ROTATE_90;
696 else if (layer.transform & DrmHwcTransform::kRotate180)
697 rotation |= 1 << DRM_ROTATE_180;
698 else if (layer.transform & DrmHwcTransform::kRotate270)
699 rotation |= 1 << DRM_ROTATE_270;
Sean Paul98e73c82015-06-24 14:38:49 -0700700 }
Sean Paul98e73c82015-06-24 14:38:49 -0700701 }
702
Zach Reizner92f8e632015-10-12 17:47:13 -0700703 // Disable the plane if there's no framebuffer
704 if (fb_id < 0) {
Zach Reizner952f70a2015-07-17 14:10:03 -0700705 ret = drmModePropertySetAdd(pset, plane->id(),
706 plane->crtc_property().id(), 0) ||
707 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
708 0);
Sean Paul2e46fbd2015-07-09 17:22:22 -0400709 if (ret) {
710 ALOGE("Failed to add plane %d disable to pset", plane->id());
711 break;
712 }
713 continue;
714 }
715
Sean Paul1c4c3262015-07-14 15:51:52 -0400716 // TODO: Once we have atomic test, this should fall back to GL
717 if (rotation && plane->rotation_property().id() == 0) {
718 ALOGE("Rotation is not supported on plane %d", plane->id());
719 ret = -EINVAL;
720 break;
721 }
722
Sean Pauld8aefb62015-10-15 15:17:31 -0400723 // TODO: Once we have atomic test, this should fall back to GL
724 if (alpha != 0xFF && plane->alpha_property().id() == 0) {
725 ALOGE("Alpha is not supported on plane %d", plane->id());
726 ret = -EINVAL;
727 break;
728 }
729
Sean Paul98e73c82015-06-24 14:38:49 -0700730 ret =
731 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
Zach Reizner92f8e632015-10-12 17:47:13 -0700732 crtc->id()) ||
Sean Paul98e73c82015-06-24 14:38:49 -0700733 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
Zach Reizner92f8e632015-10-12 17:47:13 -0700734 fb_id) ||
Sean Paul98e73c82015-06-24 14:38:49 -0700735 drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(),
Zach Reizner92f8e632015-10-12 17:47:13 -0700736 display_frame.left) ||
Sean Paul98e73c82015-06-24 14:38:49 -0700737 drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(),
Zach Reizner92f8e632015-10-12 17:47:13 -0700738 display_frame.top) ||
739 drmModePropertySetAdd(pset, plane->id(), plane->crtc_w_property().id(),
740 display_frame.right - display_frame.left) ||
741 drmModePropertySetAdd(pset, plane->id(), plane->crtc_h_property().id(),
742 display_frame.bottom - display_frame.top) ||
Sean Paul98e73c82015-06-24 14:38:49 -0700743 drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(),
Zach Reizner92f8e632015-10-12 17:47:13 -0700744 (int)(source_crop.left) << 16) ||
Sean Paul98e73c82015-06-24 14:38:49 -0700745 drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(),
Zach Reizner92f8e632015-10-12 17:47:13 -0700746 (int)(source_crop.top) << 16) ||
747 drmModePropertySetAdd(pset, plane->id(), plane->src_w_property().id(),
748 (int)(source_crop.right - source_crop.left)
749 << 16) ||
750 drmModePropertySetAdd(pset, plane->id(), plane->src_h_property().id(),
751 (int)(source_crop.bottom - source_crop.top)
752 << 16);
Sean Paul98e73c82015-06-24 14:38:49 -0700753 if (ret) {
754 ALOGE("Failed to add plane %d to set", plane->id());
755 break;
756 }
Sean Paul1c4c3262015-07-14 15:51:52 -0400757
758 if (plane->rotation_property().id()) {
Zach Reizner952f70a2015-07-17 14:10:03 -0700759 ret = drmModePropertySetAdd(pset, plane->id(),
760 plane->rotation_property().id(), rotation);
Sean Paul1c4c3262015-07-14 15:51:52 -0400761 if (ret) {
762 ALOGE("Failed to add rotation property %d to plane %d",
763 plane->rotation_property().id(), plane->id());
764 break;
765 }
766 }
Sean Pauld8aefb62015-10-15 15:17:31 -0400767
768 if (plane->alpha_property().id()) {
769 ret = drmModePropertySetAdd(pset, plane->id(),
770 plane->alpha_property().id(), alpha);
771 if (ret) {
772 ALOGE("Failed to add alpha property %d to plane %d",
773 plane->alpha_property().id(), plane->id());
774 break;
775 }
776 }
Sean Paul98e73c82015-06-24 14:38:49 -0700777 }
778
Zach Reizner92f8e632015-10-12 17:47:13 -0700779out:
Sean Paul98e73c82015-06-24 14:38:49 -0700780 if (!ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500781 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
782 if (test_only)
783 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
784
785 ret = drmModePropertySetCommit(drm_->fd(), flags, drm_, pset);
Sean Paul57355412015-09-19 09:14:34 -0400786 if (ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500787 if (test_only)
788 ALOGI("Commit test pset failed ret=%d\n", ret);
789 else
790 ALOGE("Failed to commit pset ret=%d\n", ret);
Sean Paul57355412015-09-19 09:14:34 -0400791 drmModePropertySetFree(pset);
Sean Paul57355412015-09-19 09:14:34 -0400792 return ret;
793 }
Sean Paul98e73c82015-06-24 14:38:49 -0700794 }
795 if (pset)
796 drmModePropertySetFree(pset);
797
Sean Paulc07b2112015-11-17 16:38:10 -0500798 if (!test_only && mode_.needs_modeset) {
Sean Paul35301f42015-11-17 14:46:56 -0500799 ret = drm_->DestroyPropertyBlob(mode_.old_blob_id);
Sean Paul57355412015-09-19 09:14:34 -0400800 if (ret) {
Sean Paul35301f42015-11-17 14:46:56 -0500801 ALOGE("Failed to destroy old mode property blob %lld/%d",
802 mode_.old_blob_id, ret);
Sean Paul57355412015-09-19 09:14:34 -0400803 return ret;
804 }
805
806 /* TODO: Add dpms to the pset when the kernel supports it */
807 ret = ApplyDpms(display_comp);
808 if (ret) {
809 ALOGE("Failed to apply DPMS after modeset %d\n", ret);
810 return ret;
811 }
812
Sean Paul35301f42015-11-17 14:46:56 -0500813 connector->set_active_mode(mode_.mode);
814 mode_.old_blob_id = mode_.blob_id;
815 mode_.blob_id = 0;
816 mode_.needs_modeset = false;
Sean Paul57355412015-09-19 09:14:34 -0400817 }
818
Sean Paul98e73c82015-06-24 14:38:49 -0700819 return ret;
820}
821
Sean Pauldb7a17d2015-06-24 18:46:05 -0700822int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
823 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
824 if (!conn) {
825 ALOGE("Failed to get DrmConnector for display %d", display_);
826 return -ENODEV;
827 }
828
829 const DrmProperty &prop = conn->dpms_property();
830 int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(),
831 display_comp->dpms_mode());
832 if (ret) {
833 ALOGE("Failed to set DPMS property for connector %d", conn->id());
834 return ret;
835 }
836 return 0;
837}
838
Sean Paul35301f42015-11-17 14:46:56 -0500839std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob(
840 const DrmMode &mode) {
841 struct drm_mode_modeinfo drm_mode;
842 memset(&drm_mode, 0, sizeof(drm_mode));
843 mode.ToDrmModeModeInfo(&drm_mode);
844
845 uint32_t id = 0;
846 int ret = drm_->CreatePropertyBlob(&drm_mode,
847 sizeof(struct drm_mode_modeinfo), &id);
848 if (ret) {
849 ALOGE("Failed to create mode property blob %d", ret);
850 return std::make_tuple(ret, 0);
851 }
852 ALOGE("Create blob_id %ld\n", id);
853 return std::make_tuple(ret, id);
854}
855
Haixia Shidda2fab2015-10-22 18:12:49 -0700856void DrmDisplayCompositor::ApplyFrame(
857 std::unique_ptr<DrmDisplayComposition> composition, int status) {
858 int ret = status;
859
860 if (!ret)
Sean Paulc07b2112015-11-17 16:38:10 -0500861 ret = CommitFrame(composition.get(), false);
Haixia Shidda2fab2015-10-22 18:12:49 -0700862
863 if (ret) {
864 ALOGE("Composite failed for display %d", display_);
865
866 // Disable the hw used by the last active composition. This allows us to
867 // signal the release fences from that composition to avoid hanging.
868 if (DisablePlanes(active_composition_.get()))
869 return;
870 }
871 ++dump_frames_composited_;
872
873 if (active_composition_)
874 active_composition_->SignalCompositionDone();
875
876 ret = pthread_mutex_lock(&lock_);
877 if (ret)
878 ALOGE("Failed to acquire lock for active_composition swap");
879
880 active_composition_.swap(composition);
881
882 if (!ret)
883 ret = pthread_mutex_unlock(&lock_);
884 if (ret)
885 ALOGE("Failed to release lock for active_composition swap");
886}
887
Sean Paul98e73c82015-06-24 14:38:49 -0700888int DrmDisplayCompositor::Composite() {
889 ATRACE_CALL();
Zach Reizner09807052015-08-13 14:53:41 -0700890
891 if (!pre_compositor_) {
892 pre_compositor_.reset(new GLWorkerCompositor());
893 int ret = pre_compositor_->Init();
894 if (ret) {
895 ALOGE("Failed to initialize OpenGL compositor %d", ret);
896 return ret;
897 }
898 }
899
Sean Paul98e73c82015-06-24 14:38:49 -0700900 int ret = pthread_mutex_lock(&lock_);
901 if (ret) {
902 ALOGE("Failed to acquire compositor lock %d", ret);
903 return ret;
904 }
905 if (composite_queue_.empty()) {
906 ret = pthread_mutex_unlock(&lock_);
907 if (ret)
908 ALOGE("Failed to release compositor lock %d", ret);
909 return ret;
910 }
911
912 std::unique_ptr<DrmDisplayComposition> composition(
913 std::move(composite_queue_.front()));
Zach Reizner4a253652015-09-10 18:30:54 -0700914
Sean Paul98e73c82015-06-24 14:38:49 -0700915 composite_queue_.pop();
916
917 ret = pthread_mutex_unlock(&lock_);
918 if (ret) {
919 ALOGE("Failed to release compositor lock %d", ret);
920 return ret;
921 }
922
Sean Paulacb2a442015-06-24 18:43:01 -0700923 switch (composition->type()) {
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700924 case DRM_COMPOSITION_TYPE_FRAME:
Sean Paule3141c62015-11-30 14:35:45 -0500925 ret = PrepareFrame(composition.get());
926 if (ret) {
927 ALOGE("Failed to prepare frame for display %d", display_);
928 return ret;
Sean Paul647beb22015-11-19 13:55:48 -0500929 }
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800930 if (composition->geometry_changed()) {
931 // Send the composition to the kernel to ensure we can commit it. This
932 // is just a test, it won't actually commit the frame. If rejected,
933 // squash the frame into one layer and use the squashed composition
934 ret = CommitFrame(composition.get(), true);
Sean Paul6c18b3b2015-11-25 11:04:25 -0500935 if (ret)
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800936 ALOGI("Commit test failed, squashing frame for display %d", display_);
Sean Paul6c18b3b2015-11-25 11:04:25 -0500937 use_hw_overlays_ = !ret;
938 }
939
940 // If use_hw_overlays_ is false, we can't use hardware to composite the
941 // frame. So squash all layers into a single composition and queue that
942 // instead.
943 if (!use_hw_overlays_) {
944 std::unique_ptr<DrmDisplayComposition> squashed = CreateComposition();
945 ret = SquashFrame(composition.get(), squashed.get());
946 if (!ret) {
947 composition = std::move(squashed);
948 } else {
949 ALOGE("Failed to squash frame for display %d", display_);
950 return ret;
Sean Paul647beb22015-11-19 13:55:48 -0500951 }
952 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700953 frame_worker_.QueueFrame(std::move(composition), ret);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700954 break;
955 case DRM_COMPOSITION_TYPE_DPMS:
956 ret = ApplyDpms(composition.get());
957 if (ret)
958 ALOGE("Failed to apply dpms for display %d", display_);
Sean Paulacb2a442015-06-24 18:43:01 -0700959 return ret;
Sean Paul57355412015-09-19 09:14:34 -0400960 case DRM_COMPOSITION_TYPE_MODESET:
Sean Paul35301f42015-11-17 14:46:56 -0500961 mode_.mode = composition->display_mode();
962 if (mode_.blob_id)
963 drm_->DestroyPropertyBlob(mode_.blob_id);
964 std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
965 if (ret) {
966 ALOGE("Failed to create mode blob for display %d", display_);
967 return ret;
968 }
969 mode_.needs_modeset = true;
Sean Paul57355412015-09-19 09:14:34 -0400970 return 0;
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700971 default:
972 ALOGE("Unknown composition type %d", composition->type());
973 return -EINVAL;
Sean Paul98e73c82015-06-24 14:38:49 -0700974 }
Sean Paul98e73c82015-06-24 14:38:49 -0700975
Sean Paul98e73c82015-06-24 14:38:49 -0700976 return ret;
977}
978
979bool DrmDisplayCompositor::HaveQueuedComposites() const {
980 int ret = pthread_mutex_lock(&lock_);
981 if (ret) {
982 ALOGE("Failed to acquire compositor lock %d", ret);
983 return false;
984 }
985
986 bool empty_ret = !composite_queue_.empty();
987
988 ret = pthread_mutex_unlock(&lock_);
989 if (ret) {
990 ALOGE("Failed to release compositor lock %d", ret);
991 return false;
992 }
993
994 return empty_ret;
995}
996
Zach Reiznerbff33ac2015-11-16 11:08:46 -0800997int DrmDisplayCompositor::SquashAll() {
998 AutoLock lock(&lock_, "compositor");
999 int ret = lock.Lock();
1000 if (ret)
1001 return ret;
1002
1003 if (!active_composition_)
1004 return 0;
1005
Sean Pauld51c7612015-11-18 14:12:51 -05001006 std::unique_ptr<DrmDisplayComposition> comp = CreateComposition();
1007 ret = SquashFrame(active_composition_.get(), comp.get());
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001008
Sean Pauld51c7612015-11-18 14:12:51 -05001009 // ApplyFrame needs the lock
1010 lock.Unlock();
1011
1012 if (!ret)
1013 ApplyFrame(std::move(comp), 0);
1014
1015 return ret;
1016}
1017
1018// Returns:
1019// - 0 if src is successfully squashed into dst
1020// - -EALREADY if the src is already squashed
1021// - Appropriate error if the squash fails
1022int DrmDisplayCompositor::SquashFrame(DrmDisplayComposition *src,
1023 DrmDisplayComposition *dst) {
1024 if (src->type() != DRM_COMPOSITION_TYPE_FRAME)
1025 return -ENOTSUP;
1026
1027 std::vector<DrmCompositionPlane> &src_planes = src->composition_planes();
1028 std::vector<DrmHwcLayer> &src_layers = src->layers();
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001029
1030 // Make sure there is more than one layer to squash.
Sean Pauld51c7612015-11-18 14:12:51 -05001031 size_t src_planes_with_layer = std::count_if(
1032 src_planes.begin(), src_planes.end(), [](DrmCompositionPlane &p) {
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001033 return p.source_layer <= DrmCompositionPlane::kSourceLayerMax;
1034 });
Sean Pauld51c7612015-11-18 14:12:51 -05001035 if (src_planes_with_layer <= 1)
1036 return -EALREADY;
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001037
1038 int pre_comp_layer_index;
1039
Sean Pauld51c7612015-11-18 14:12:51 -05001040 int ret = dst->Init(drm_, src->crtc(), src->importer(), src->frame_no());
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001041 if (ret) {
1042 ALOGE("Failed to init squash all composition %d", ret);
1043 return ret;
1044 }
1045
1046 std::vector<DrmPlane *> primary_planes;
1047 std::vector<DrmPlane *> fake_overlay_planes;
Sean Pauld51c7612015-11-18 14:12:51 -05001048 std::vector<DrmHwcLayer> dst_layers;
1049 for (DrmCompositionPlane &comp_plane : src_planes) {
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001050 // Composition planes without DRM planes should never happen
1051 if (comp_plane.plane == NULL) {
1052 ALOGE("Skipping squash all because of NULL plane");
Sean Pauld51c7612015-11-18 14:12:51 -05001053 ret = -EINVAL;
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001054 goto move_layers_back;
1055 }
1056
Zach Reizner2b4b1ee2015-11-18 13:54:31 -08001057 if (comp_plane.source_layer == DrmCompositionPlane::kSourceNone)
1058 continue;
1059
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001060 // Out of range layers should never happen. If they do, somebody probably
1061 // forgot to replace the symbolic names (kSourceSquash, kSourcePreComp) with
1062 // real ones.
Sean Pauld51c7612015-11-18 14:12:51 -05001063 if (comp_plane.source_layer >= src_layers.size()) {
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001064 ALOGE("Skipping squash all because of out of range source layer %zu",
1065 comp_plane.source_layer);
Sean Pauld51c7612015-11-18 14:12:51 -05001066 ret = -EINVAL;
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001067 goto move_layers_back;
1068 }
1069
Sean Pauld51c7612015-11-18 14:12:51 -05001070 DrmHwcLayer &layer = src_layers[comp_plane.source_layer];
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001071
1072 // Squashing protected layers is impossible.
Sean Pauld51c7612015-11-18 14:12:51 -05001073 if (layer.protected_usage()) {
1074 ret = -ENOTSUP;
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001075 goto move_layers_back;
Sean Pauld51c7612015-11-18 14:12:51 -05001076 }
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001077
1078 // The OutputFds point to freed memory after hwc_set returns. They are
1079 // returned to the default to prevent DrmDisplayComposition::Plan from
1080 // filling the OutputFds.
1081 layer.release_fence = OutputFd();
Sean Pauld51c7612015-11-18 14:12:51 -05001082 dst_layers.emplace_back(std::move(layer));
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001083
1084 if (comp_plane.plane->type() == DRM_PLANE_TYPE_PRIMARY &&
1085 primary_planes.size() == 0)
1086 primary_planes.push_back(comp_plane.plane);
1087 else
Sean Pauld51c7612015-11-18 14:12:51 -05001088 dst->AddPlaneDisable(comp_plane.plane);
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001089 }
1090
Sean Pauld51c7612015-11-18 14:12:51 -05001091 ret = dst->SetLayers(dst_layers.data(), dst_layers.size(), false);
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001092 if (ret) {
1093 ALOGE("Failed to set layers for squash all composition %d", ret);
1094 goto move_layers_back;
1095 }
1096
1097 ret =
Sean Pauld51c7612015-11-18 14:12:51 -05001098 dst->Plan(NULL /* SquashState */, &primary_planes, &fake_overlay_planes);
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001099 if (ret) {
1100 ALOGE("Failed to plan for squash all composition %d", ret);
1101 goto move_layers_back;
1102 }
1103
Sean Pauld51c7612015-11-18 14:12:51 -05001104 ret = ApplyPreComposite(dst);
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001105 if (ret) {
1106 ALOGE("Failed to pre-composite for squash all composition %d", ret);
1107 goto move_layers_back;
1108 }
1109
Sean Pauld51c7612015-11-18 14:12:51 -05001110 pre_comp_layer_index = dst->layers().size() - 1;
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001111 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
1112
Sean Pauld51c7612015-11-18 14:12:51 -05001113 for (DrmCompositionPlane &plane : dst->composition_planes())
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001114 if (plane.source_layer == DrmCompositionPlane::kSourcePreComp)
1115 plane.source_layer = pre_comp_layer_index;
1116
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001117 return 0;
1118
1119// TODO(zachr): think of a better way to transfer ownership back to the active
1120// composition.
1121move_layers_back:
1122 for (size_t plane_index = 0;
Sean Pauld51c7612015-11-18 14:12:51 -05001123 plane_index < src_planes.size() && plane_index < dst_layers.size();
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001124 plane_index++) {
Sean Pauld51c7612015-11-18 14:12:51 -05001125 size_t source_layer_index = src_planes[plane_index].source_layer;
1126 src_layers[source_layer_index] = std::move(dst_layers[plane_index]);
Zach Reiznerbff33ac2015-11-16 11:08:46 -08001127 }
1128
1129 return ret;
1130}
1131
Sean Paul98e73c82015-06-24 14:38:49 -07001132void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
Zach Reiznerfd6dc332015-10-13 21:12:48 -07001133 int ret = pthread_mutex_lock(&lock_);
1134 if (ret)
1135 return;
1136
1137 uint64_t num_frames = dump_frames_composited_;
1138 dump_frames_composited_ = 0;
1139
1140 struct timespec ts;
1141 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
1142 if (ret) {
1143 pthread_mutex_unlock(&lock_);
1144 return;
1145 }
1146
1147 uint64_t cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
1148 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
1149 float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f;
1150
1151 *out << "--DrmDisplayCompositor[" << display_
1152 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
1153 << " fps=" << fps << "\n";
1154
1155 dump_last_timestamp_ns_ = cur_ts;
1156
1157 if (active_composition_)
1158 active_composition_->Dump(out);
1159
Zach Reizner5757e822015-10-16 19:06:31 -07001160 squash_state_.Dump(out);
1161
Zach Reiznerfd6dc332015-10-13 21:12:48 -07001162 pthread_mutex_unlock(&lock_);
Sean Paul98e73c82015-06-24 14:38:49 -07001163}
1164}