2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 #define LOG_TAG "hwc-drm-display-compositor"
20 #include "drmdisplaycompositor.h"
23 #include "drmresources.h"
33 #include <drm/drm_mode.h>
34 #include <cutils/log.h>
35 #include <sync/sync.h>
36 #include <utils/Trace.h>
40 DrmDisplayCompositor::DrmDisplayCompositor()
47 framebuffer_index_(0),
48 dump_frames_composited_(0),
49 dump_last_timestamp_ns_(0) {
51 if (clock_gettime(CLOCK_MONOTONIC, &ts))
53 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
56 DrmDisplayCompositor::~DrmDisplayCompositor() {
62 int ret = pthread_mutex_lock(&lock_);
64 ALOGE("Failed to acquire compositor lock %d", ret);
66 while (!composite_queue_.empty()) {
67 composite_queue_.front().reset();
68 composite_queue_.pop();
70 active_composition_.reset();
72 ret = pthread_mutex_unlock(&lock_);
74 ALOGE("Failed to acquire compositor lock %d", ret);
76 pthread_mutex_destroy(&lock_);
79 int DrmDisplayCompositor::Init(DrmResources *drm, int display) {
83 int ret = pthread_mutex_init(&lock_, NULL);
85 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
90 pthread_mutex_destroy(&lock_);
91 ALOGE("Failed to initialize compositor worker %d\n", ret);
99 int DrmDisplayCompositor::QueueComposition(
100 std::unique_ptr<DrmDisplayComposition> composition) {
101 switch (composition->type()) {
102 case DRM_COMPOSITION_TYPE_FRAME:
106 case DRM_COMPOSITION_TYPE_DPMS:
108 * Update the state as soon as we get it so we can start/stop queuing
111 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
113 case DRM_COMPOSITION_TYPE_EMPTY:
116 ALOGE("Unknown composition type %d/%d", composition->type(), display_);
120 int ret = pthread_mutex_lock(&lock_);
122 ALOGE("Failed to acquire compositor lock %d", ret);
126 composite_queue_.push(std::move(composition));
128 ret = pthread_mutex_unlock(&lock_);
130 ALOGE("Failed to release compositor lock %d", ret);
138 static bool drm_composition_layer_has_plane(
139 const DrmCompositionLayer_t &comp_layer) {
140 if (comp_layer.plane != NULL)
141 if (comp_layer.plane->type() == DRM_PLANE_TYPE_OVERLAY ||
142 comp_layer.plane->type() == DRM_PLANE_TYPE_PRIMARY)
147 static bool drm_composition_layer_has_no_plane(
148 const DrmCompositionLayer_t &comp_layer) {
149 return comp_layer.plane == NULL;
152 int DrmDisplayCompositor::ApplyPreComposite(
153 DrmDisplayComposition *display_comp) {
155 DrmCompositionLayerVector_t *layers = display_comp->GetCompositionLayers();
157 auto last_layer = find_if(layers->rbegin(), layers->rend(),
158 drm_composition_layer_has_plane);
159 if (last_layer == layers->rend()) {
160 ALOGE("Frame has no overlays");
164 DrmCompositionLayer_t &comp_layer = *last_layer;
165 DrmPlane *stolen_plane = NULL;
166 std::swap(stolen_plane, comp_layer.plane);
168 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
169 if (connector == NULL) {
170 ALOGE("Failed to determine display mode: no connector for display %d",
175 const DrmMode &mode = connector->active_mode();
176 DrmFramebuffer &fb = framebuffers_[framebuffer_index_];
177 ret = fb.WaitReleased(-1);
179 ALOGE("Failed to wait for framebuffer release %d", ret);
182 fb.set_release_fence_fd(-1);
183 if (!fb.Allocate(mode.h_display(), mode.v_display())) {
184 ALOGE("Failed to allocate framebuffer with size %dx%d", mode.h_display(),
189 std::vector<hwc_layer_1_t> pre_comp_layers;
190 for (const auto &comp_layer : *layers)
191 if (comp_layer.plane == NULL)
192 pre_comp_layers.push_back(comp_layer.layer);
194 if (!pre_compositor_) {
195 pre_compositor_.reset(new GLWorkerCompositor());
196 ret = pre_compositor_->Init();
198 ALOGE("Failed to initialize OpenGL compositor %d", ret);
202 ret = pre_compositor_->CompositeAndFinish(
203 pre_comp_layers.data(), pre_comp_layers.size(), fb.buffer());
205 ALOGE("Failed to composite layers");
209 layers->erase(std::remove_if(layers->begin(), layers->end(),
210 drm_composition_layer_has_no_plane),
213 hwc_layer_1_t pre_comp_output_layer;
214 memset(&pre_comp_output_layer, 0, sizeof(pre_comp_output_layer));
215 pre_comp_output_layer.compositionType = HWC_OVERLAY;
216 pre_comp_output_layer.handle = fb.buffer()->handle;
217 pre_comp_output_layer.acquireFenceFd = -1;
218 pre_comp_output_layer.releaseFenceFd = -1;
219 pre_comp_output_layer.planeAlpha = 0xff;
220 pre_comp_output_layer.visibleRegionScreen.numRects = 1;
221 pre_comp_output_layer.visibleRegionScreen.rects =
222 &pre_comp_output_layer.displayFrame;
223 pre_comp_output_layer.sourceCropf.top =
224 pre_comp_output_layer.displayFrame.top = 0;
225 pre_comp_output_layer.sourceCropf.left =
226 pre_comp_output_layer.displayFrame.left = 0;
227 pre_comp_output_layer.sourceCropf.right =
228 pre_comp_output_layer.displayFrame.right = fb.buffer()->getWidth();
229 pre_comp_output_layer.sourceCropf.bottom =
230 pre_comp_output_layer.displayFrame.bottom = fb.buffer()->getHeight();
232 ret = display_comp->AddLayer(&pre_comp_output_layer,
233 drm_->GetCrtcForDisplay(display_), stolen_plane);
235 ALOGE("Failed to add composited layer %d", ret);
239 fb.set_release_fence_fd(pre_comp_output_layer.releaseFenceFd);
240 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
245 int DrmDisplayCompositor::ApplyFrame(DrmDisplayComposition *display_comp) {
248 DrmCompositionLayerVector_t *layers = display_comp->GetCompositionLayers();
249 bool use_pre_comp = std::any_of(layers->begin(), layers->end(),
250 drm_composition_layer_has_no_plane);
253 ret = ApplyPreComposite(display_comp);
258 drmModePropertySetPtr pset = drmModePropertySetAlloc();
260 ALOGE("Failed to allocate property set");
264 for (DrmCompositionLayerVector_t::iterator iter = layers->begin();
265 iter != layers->end(); ++iter) {
266 hwc_layer_1_t *layer = &iter->layer;
268 if (layer->acquireFenceFd >= 0) {
269 ret = sync_wait(layer->acquireFenceFd, -1);
271 ALOGE("Failed to wait for acquire %d/%d", layer->acquireFenceFd, ret);
272 drmModePropertySetFree(pset);
275 close(layer->acquireFenceFd);
276 layer->acquireFenceFd = -1;
279 DrmPlane *plane = iter->plane;
280 DrmCrtc *crtc = iter->crtc;
282 // Disable the plane if there's no crtc
284 ret = drmModePropertySetAdd(pset, plane->id(),
285 plane->crtc_property().id(), 0) ||
286 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
289 ALOGE("Failed to add plane %d disable to pset", plane->id());
296 switch (layer->transform) {
297 case HWC_TRANSFORM_FLIP_H:
298 rotation = 1 << DRM_REFLECT_X;
300 case HWC_TRANSFORM_FLIP_V:
301 rotation = 1 << DRM_REFLECT_Y;
303 case HWC_TRANSFORM_ROT_90:
304 rotation = 1 << DRM_ROTATE_90;
306 case HWC_TRANSFORM_ROT_180:
307 rotation = 1 << DRM_ROTATE_180;
309 case HWC_TRANSFORM_ROT_270:
310 rotation = 1 << DRM_ROTATE_270;
316 ALOGE("Invalid transform value 0x%x given", layer->transform);
323 // TODO: Once we have atomic test, this should fall back to GL
324 if (rotation && plane->rotation_property().id() == 0) {
325 ALOGE("Rotation is not supported on plane %d", plane->id());
331 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
333 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
335 drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(),
336 layer->displayFrame.left) ||
337 drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(),
338 layer->displayFrame.top) ||
339 drmModePropertySetAdd(
340 pset, plane->id(), plane->crtc_w_property().id(),
341 layer->displayFrame.right - layer->displayFrame.left) ||
342 drmModePropertySetAdd(
343 pset, plane->id(), plane->crtc_h_property().id(),
344 layer->displayFrame.bottom - layer->displayFrame.top) ||
345 drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(),
346 (int)(layer->sourceCropf.left) << 16) ||
347 drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(),
348 (int)(layer->sourceCropf.top) << 16) ||
349 drmModePropertySetAdd(
350 pset, plane->id(), plane->src_w_property().id(),
351 (int)(layer->sourceCropf.right - layer->sourceCropf.left) << 16) ||
352 drmModePropertySetAdd(
353 pset, plane->id(), plane->src_h_property().id(),
354 (int)(layer->sourceCropf.bottom - layer->sourceCropf.top) << 16);
356 ALOGE("Failed to add plane %d to set", plane->id());
360 if (plane->rotation_property().id()) {
361 ret = drmModePropertySetAdd(pset, plane->id(),
362 plane->rotation_property().id(), rotation);
364 ALOGE("Failed to add rotation property %d to plane %d",
365 plane->rotation_property().id(), plane->id());
372 ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
374 ALOGE("Failed to commit pset ret=%d\n", ret);
377 drmModePropertySetFree(pset);
382 int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
383 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
385 ALOGE("Failed to get DrmConnector for display %d", display_);
389 const DrmProperty &prop = conn->dpms_property();
390 int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(),
391 display_comp->dpms_mode());
393 ALOGE("Failed to set DPMS property for connector %d", conn->id());
399 int DrmDisplayCompositor::Composite() {
401 int ret = pthread_mutex_lock(&lock_);
403 ALOGE("Failed to acquire compositor lock %d", ret);
406 if (composite_queue_.empty()) {
407 ret = pthread_mutex_unlock(&lock_);
409 ALOGE("Failed to release compositor lock %d", ret);
413 std::unique_ptr<DrmDisplayComposition> composition(
414 std::move(composite_queue_.front()));
415 composite_queue_.pop();
417 ret = pthread_mutex_unlock(&lock_);
419 ALOGE("Failed to release compositor lock %d", ret);
423 switch (composition->type()) {
424 case DRM_COMPOSITION_TYPE_FRAME:
425 ret = ApplyFrame(composition.get());
427 ALOGE("Composite failed for display %d", display_);
430 ++dump_frames_composited_;
432 case DRM_COMPOSITION_TYPE_DPMS:
433 ret = ApplyDpms(composition.get());
435 ALOGE("Failed to apply dpms for display %d", display_);
438 ALOGE("Unknown composition type %d", composition->type());
442 if (active_composition_)
443 active_composition_->FinishComposition();
445 ret = pthread_mutex_lock(&lock_);
447 ALOGE("Failed to acquire lock for active_composition swap");
449 active_composition_.swap(composition);
452 ret = pthread_mutex_unlock(&lock_);
454 ALOGE("Failed to release lock for active_composition swap");
459 bool DrmDisplayCompositor::HaveQueuedComposites() const {
460 int ret = pthread_mutex_lock(&lock_);
462 ALOGE("Failed to acquire compositor lock %d", ret);
466 bool empty_ret = !composite_queue_.empty();
468 ret = pthread_mutex_unlock(&lock_);
470 ALOGE("Failed to release compositor lock %d", ret);
477 void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
480 int ret = pthread_mutex_lock(&lock_);
484 uint64_t num_frames = dump_frames_composited_;
485 dump_frames_composited_ = 0;
488 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
490 DrmCompositionLayerVector_t layers;
491 if (active_composition_)
492 layers = *active_composition_->GetCompositionLayers();
496 ret |= pthread_mutex_unlock(&lock_);
500 cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
501 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
502 float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f;
504 *out << "--DrmDisplayCompositor[" << display_
505 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
506 << " fps=" << fps << "\n";
508 dump_last_timestamp_ns_ = cur_ts;
510 *out << "---- DrmDisplayCompositor Layers: num=" << layers.size() << "\n";
511 for (DrmCompositionLayerVector_t::iterator iter = layers.begin();
512 iter != layers.end(); ++iter) {
513 hwc_layer_1_t *layer = &iter->layer;
514 DrmPlane *plane = iter->plane;
516 *out << "------ DrmDisplayCompositor Layer: plane=" << plane->id() << " ";
518 DrmCrtc *crtc = iter->crtc;
520 *out << "disabled\n";
524 *out << "crtc=" << crtc->id()
525 << " crtc[x/y/w/h]=" << layer->displayFrame.left << "/"
526 << layer->displayFrame.top << "/"
527 << layer->displayFrame.right - layer->displayFrame.left << "/"
528 << layer->displayFrame.bottom - layer->displayFrame.top << " "
529 << " src[x/y/w/h]=" << layer->sourceCropf.left << "/"
530 << layer->sourceCropf.top << "/"
531 << layer->sourceCropf.right - layer->sourceCropf.left << "/"
532 << layer->sourceCropf.bottom - layer->sourceCropf.top
533 << " transform=" << layer->transform << "\n";