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 "hwcomposer-drm"
20 #include "drm_hwcomposer.h"
21 #include "drmresources.h"
23 #include "virtualcompositorworker.h"
24 #include "vsyncworker.h"
35 #include <sys/param.h>
36 #include <sys/resource.h>
38 #include <xf86drmMode.h>
40 #include <cutils/log.h>
41 #include <cutils/properties.h>
42 #include <hardware/hardware.h>
43 #include <hardware/hwcomposer.h>
45 #include <sync/sync.h>
46 #include <utils/Trace.h>
48 #define UM_PER_INCH 25400
52 class DummySwSyncTimeline {
55 int ret = timeline_fd_.Set(sw_sync_timeline_create());
61 UniqueFd CreateDummyFence() {
62 int ret = sw_sync_fence_create(timeline_fd_.get(), "dummy fence",
65 ALOGE("Failed to create dummy fence %d", ret);
71 ret = sw_sync_timeline_inc(timeline_fd_.get(), 1);
73 ALOGE("Failed to increment dummy sync timeline %d", ret);
82 UniqueFd timeline_fd_;
86 struct CheckedOutputFd {
87 CheckedOutputFd(int *fd, const char *description,
88 DummySwSyncTimeline &timeline)
89 : fd_(fd), description_(description), timeline_(timeline) {
91 CheckedOutputFd(CheckedOutputFd &&rhs)
92 : description_(rhs.description_), timeline_(rhs.timeline_) {
93 std::swap(fd_, rhs.fd_);
96 CheckedOutputFd &operator=(const CheckedOutputFd &rhs) = delete;
105 *fd_ = timeline_.CreateDummyFence().Release();
108 ALOGE("Failed to fill %s (%p == %d) before destruction",
109 description_.c_str(), fd_, *fd_);
114 std::string description_;
115 DummySwSyncTimeline &timeline_;
118 typedef struct hwc_drm_display {
119 struct hwc_context_t *ctx;
122 std::vector<uint32_t> config_ids;
124 VSyncWorker vsync_worker;
127 struct hwc_context_t {
128 // map of display:hwc_drm_display_t
129 typedef std::map<int, hwc_drm_display_t> DisplayMap;
132 virtual_compositor_worker.Exit();
135 hwc_composer_device_1_t device;
136 hwc_procs_t const *procs = NULL;
140 std::unique_ptr<Importer> importer;
141 const gralloc_module_t *gralloc;
142 DummySwSyncTimeline dummy_timeline;
143 VirtualCompositorWorker virtual_compositor_worker;
146 static native_handle_t *dup_buffer_handle(buffer_handle_t handle) {
147 native_handle_t *new_handle =
148 native_handle_create(handle->numFds, handle->numInts);
149 if (new_handle == NULL)
152 const int *old_data = handle->data;
153 int *new_data = new_handle->data;
154 for (int i = 0; i < handle->numFds; i++) {
155 *new_data = dup(*old_data);
159 memcpy(new_data, old_data, sizeof(int) * handle->numInts);
164 static void free_buffer_handle(native_handle_t *handle) {
165 int ret = native_handle_close(handle);
167 ALOGE("Failed to close native handle %d", ret);
168 ret = native_handle_delete(handle);
170 ALOGE("Failed to delete native handle %d", ret);
173 const hwc_drm_bo *DrmHwcBuffer::operator->() const {
174 if (importer_ == NULL) {
175 ALOGE("Access of non-existent BO");
182 void DrmHwcBuffer::Clear() {
183 if (importer_ != NULL) {
184 importer_->ReleaseBuffer(&bo_);
189 int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
192 int ret = importer->ImportBuffer(handle, &tmp_bo);
196 if (importer_ != NULL) {
197 importer_->ReleaseBuffer(&bo_);
200 importer_ = importer;
207 int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle,
208 const gralloc_module_t *gralloc) {
209 native_handle_t *handle_copy = dup_buffer_handle(handle);
210 if (handle_copy == NULL) {
211 ALOGE("Failed to duplicate handle");
215 int ret = gralloc->registerBuffer(gralloc, handle_copy);
217 ALOGE("Failed to register buffer handle %d", ret);
218 free_buffer_handle(handle_copy);
225 handle_ = handle_copy;
230 DrmHwcNativeHandle::~DrmHwcNativeHandle() {
234 void DrmHwcNativeHandle::Clear() {
235 if (gralloc_ != NULL && handle_ != NULL) {
236 gralloc_->unregisterBuffer(gralloc_, handle_);
237 free_buffer_handle(handle_);
243 int DrmHwcLayer::InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer,
244 const gralloc_module_t *gralloc) {
245 sf_handle = sf_layer->handle;
246 alpha = sf_layer->planeAlpha;
248 source_crop = DrmHwcRect<float>(
249 sf_layer->sourceCropf.left, sf_layer->sourceCropf.top,
250 sf_layer->sourceCropf.right, sf_layer->sourceCropf.bottom);
251 display_frame = DrmHwcRect<int>(
252 sf_layer->displayFrame.left, sf_layer->displayFrame.top,
253 sf_layer->displayFrame.right, sf_layer->displayFrame.bottom);
255 switch (sf_layer->transform) {
257 transform = DrmHwcTransform::kIdentity;
259 case HWC_TRANSFORM_FLIP_H:
260 transform = DrmHwcTransform::kFlipH;
262 case HWC_TRANSFORM_FLIP_V:
263 transform = DrmHwcTransform::kFlipV;
265 case HWC_TRANSFORM_ROT_90:
266 transform = DrmHwcTransform::kRotate90;
268 case HWC_TRANSFORM_ROT_180:
269 transform = DrmHwcTransform::kRotate180;
271 case HWC_TRANSFORM_ROT_270:
272 transform = DrmHwcTransform::kRotate270;
275 ALOGE("Invalid transform in hwc_layer_1_t %d", sf_layer->transform);
279 switch (sf_layer->blending) {
280 case HWC_BLENDING_NONE:
281 blending = DrmHwcBlending::kNone;
283 case HWC_BLENDING_PREMULT:
284 blending = DrmHwcBlending::kPreMult;
286 case HWC_BLENDING_COVERAGE:
287 blending = DrmHwcBlending::kCoverage;
290 ALOGE("Invalid blending in hwc_layer_1_t %d", sf_layer->blending);
294 int ret = buffer.ImportBuffer(sf_layer->handle, importer);
298 ret = handle.CopyBufferHandle(sf_layer->handle, gralloc);
302 ret = gralloc->perform(gralloc, GRALLOC_MODULE_PERFORM_GET_USAGE,
303 handle.get(), &gralloc_buffer_usage);
305 // TODO(zachr): Once GRALLOC_MODULE_PERFORM_GET_USAGE is implemented, remove
306 // "ret = 0" and enable the error logging code.
309 ALOGE("Failed to get usage for buffer %p (%d)", handle.get(), ret);
317 static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
319 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
320 std::ostringstream out;
322 ctx->drm.compositor()->Dump(&out);
323 std::string out_str = out.str();
324 strncpy(buff, out_str.c_str(),
325 std::min((size_t)buff_len, out_str.length() + 1));
326 buff[buff_len - 1] = '\0';
329 static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
330 hwc_display_contents_1_t **display_contents) {
331 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
333 for (int i = 0; i < (int)num_displays; ++i) {
334 if (!display_contents[i])
337 bool use_framebuffer_target = false;
338 if (i == HWC_DISPLAY_VIRTUAL) {
339 use_framebuffer_target = true;
341 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
343 ALOGE("No crtc for display %d", i);
348 int num_layers = display_contents[i]->numHwLayers;
349 for (int j = 0; j < num_layers; j++) {
350 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
352 if (!use_framebuffer_target) {
353 if (layer->compositionType == HWC_FRAMEBUFFER)
354 layer->compositionType = HWC_OVERLAY;
356 switch (layer->compositionType) {
360 case HWC_CURSOR_OVERLAY:
361 layer->compositionType = HWC_FRAMEBUFFER;
371 static void hwc_add_layer_to_retire_fence(
372 hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) {
373 if (layer->releaseFenceFd < 0)
376 if (display_contents->retireFenceFd >= 0) {
377 int old_retire_fence = display_contents->retireFenceFd;
378 display_contents->retireFenceFd =
379 sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd);
380 close(old_retire_fence);
382 display_contents->retireFenceFd = dup(layer->releaseFenceFd);
386 static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
387 hwc_display_contents_1_t **sf_display_contents) {
389 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
392 std::vector<CheckedOutputFd> checked_output_fences;
393 std::vector<DrmHwcDisplayContents> displays_contents;
394 std::vector<DrmCompositionDisplayLayersMap> layers_map;
395 std::vector<std::vector<size_t>> layers_indices;
396 displays_contents.reserve(num_displays);
397 // layers_map.reserve(num_displays);
398 layers_indices.reserve(num_displays);
400 // Phase one does nothing that would cause errors. Only take ownership of FDs.
401 for (size_t i = 0; i < num_displays; ++i) {
402 hwc_display_contents_1_t *dc = sf_display_contents[i];
403 displays_contents.emplace_back();
404 DrmHwcDisplayContents &display_contents = displays_contents.back();
405 layers_indices.emplace_back();
406 std::vector<size_t> &indices_to_composite = layers_indices.back();
408 if (!sf_display_contents[i])
411 if (i == HWC_DISPLAY_VIRTUAL) {
412 ctx->virtual_compositor_worker.QueueComposite(dc);
416 std::ostringstream display_index_formatter;
417 display_index_formatter << "retire fence for display " << i;
418 std::string display_fence_description(display_index_formatter.str());
419 checked_output_fences.emplace_back(&dc->retireFenceFd,
420 display_fence_description.c_str(),
421 ctx->dummy_timeline);
422 display_contents.retire_fence = OutputFd(&dc->retireFenceFd);
424 size_t num_dc_layers = dc->numHwLayers;
425 int framebuffer_target_index = -1;
426 for (size_t j = 0; j < num_dc_layers; ++j) {
427 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
429 display_contents.layers.emplace_back();
430 DrmHwcLayer &layer = display_contents.layers.back();
432 if (sf_layer->flags & HWC_SKIP_LAYER)
435 if (sf_layer->compositionType == HWC_OVERLAY)
436 indices_to_composite.push_back(j);
437 if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET)
438 framebuffer_target_index = j;
440 layer.acquire_fence.Set(sf_layer->acquireFenceFd);
441 sf_layer->acquireFenceFd = -1;
443 std::ostringstream layer_fence_formatter;
444 layer_fence_formatter << "release fence for layer " << j << " of display "
446 std::string layer_fence_description(layer_fence_formatter.str());
447 checked_output_fences.emplace_back(&sf_layer->releaseFenceFd,
448 layer_fence_description.c_str(),
449 ctx->dummy_timeline);
450 layer.release_fence = OutputFd(&sf_layer->releaseFenceFd);
453 if (indices_to_composite.empty() && framebuffer_target_index >= 0) {
454 hwc_layer_1_t *sf_layer = &dc->hwLayers[framebuffer_target_index];
455 if (!sf_layer->handle || (sf_layer->flags & HWC_SKIP_LAYER)) {
457 "Expected valid layer with HWC_FRAMEBUFFER_TARGET when all "
458 "HWC_OVERLAY layers are skipped.");
461 indices_to_composite.push_back(framebuffer_target_index);
468 for (size_t i = 0; i < num_displays; ++i) {
469 hwc_display_contents_1_t *dc = sf_display_contents[i];
470 DrmHwcDisplayContents &display_contents = displays_contents[i];
471 if (!sf_display_contents[i] || i == HWC_DISPLAY_VIRTUAL)
474 layers_map.emplace_back();
475 DrmCompositionDisplayLayersMap &map = layers_map.back();
477 map.geometry_changed =
478 (dc->flags & HWC_GEOMETRY_CHANGED) == HWC_GEOMETRY_CHANGED;
479 std::vector<size_t> &indices_to_composite = layers_indices[i];
480 for (size_t j : indices_to_composite) {
481 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
483 DrmHwcLayer &layer = display_contents.layers[j];
485 ret = layer.InitFromHwcLayer(sf_layer, ctx->importer.get(), ctx->gralloc);
487 ALOGE("Failed to init composition from layer %d", ret);
490 map.layers.emplace_back(std::move(layer));
494 std::unique_ptr<DrmComposition> composition(
495 ctx->drm.compositor()->CreateComposition(ctx->importer.get()));
497 ALOGE("Drm composition init failed");
501 ret = composition->SetLayers(layers_map.size(), layers_map.data());
506 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
511 for (size_t i = 0; i < num_displays; ++i) {
512 hwc_display_contents_1_t *dc = sf_display_contents[i];
516 size_t num_dc_layers = dc->numHwLayers;
517 for (size_t j = 0; j < num_dc_layers; ++j) {
518 hwc_layer_1_t *layer = &dc->hwLayers[j];
519 if (layer->flags & HWC_SKIP_LAYER)
521 hwc_add_layer_to_retire_fence(layer, dc);
525 composition.reset(NULL);
530 static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
531 int event, int enabled) {
532 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
535 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
536 hwc_drm_display_t *hd = &ctx->displays[display];
537 return hd->vsync_worker.VSyncControl(enabled);
540 static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
542 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
544 uint64_t dpmsValue = 0;
546 case HWC_POWER_MODE_OFF:
547 dpmsValue = DRM_MODE_DPMS_OFF;
550 /* We can't support dozing right now, so go full on */
551 case HWC_POWER_MODE_DOZE:
552 case HWC_POWER_MODE_DOZE_SUSPEND:
553 case HWC_POWER_MODE_NORMAL:
554 dpmsValue = DRM_MODE_DPMS_ON;
557 return ctx->drm.SetDpmsMode(display, dpmsValue);
560 static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
563 case HWC_BACKGROUND_LAYER_SUPPORTED:
564 *value = 0; /* TODO: We should do this */
566 case HWC_VSYNC_PERIOD:
567 ALOGW("Query for deprecated vsync value, returning 60Hz");
568 *value = 1000 * 1000 * 1000 / 60;
570 case HWC_DISPLAY_TYPES_SUPPORTED:
571 *value = HWC_DISPLAY_PRIMARY_BIT | HWC_DISPLAY_EXTERNAL_BIT |
572 HWC_DISPLAY_VIRTUAL_BIT;
578 static void hwc_register_procs(struct hwc_composer_device_1 *dev,
579 hwc_procs_t const *procs) {
580 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
584 for (std::pair<const int, hwc_drm_display> &display_entry : ctx->displays)
585 display_entry.second.vsync_worker.SetProcs(procs);
588 static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
589 int display, uint32_t *configs,
590 size_t *num_configs) {
594 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
595 hwc_drm_display_t *hd = &ctx->displays[display];
596 hd->config_ids.clear();
598 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
600 ALOGE("Failed to get connector for display %d", display);
604 int ret = connector->UpdateModes();
606 ALOGE("Failed to update display modes %d", ret);
610 for (const DrmMode &mode : connector->modes()) {
611 size_t idx = hd->config_ids.size();
612 if (idx == *num_configs)
614 hd->config_ids.push_back(mode.id());
615 configs[idx] = mode.id();
617 *num_configs = hd->config_ids.size();
618 return *num_configs == 0 ? -1 : 0;
621 static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
622 int display, uint32_t config,
623 const uint32_t *attributes,
625 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
626 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
628 ALOGE("Failed to get DrmConnector for display %d", display);
632 for (const DrmMode &conn_mode : c->modes()) {
633 if (conn_mode.id() == config) {
638 if (mode.id() == 0) {
639 ALOGE("Failed to find active mode for display %d", display);
643 uint32_t mm_width = c->mm_width();
644 uint32_t mm_height = c->mm_height();
645 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
646 switch (attributes[i]) {
647 case HWC_DISPLAY_VSYNC_PERIOD:
648 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
650 case HWC_DISPLAY_WIDTH:
651 values[i] = mode.h_display();
653 case HWC_DISPLAY_HEIGHT:
654 values[i] = mode.v_display();
656 case HWC_DISPLAY_DPI_X:
657 /* Dots per 1000 inches */
658 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
660 case HWC_DISPLAY_DPI_Y:
661 /* Dots per 1000 inches */
663 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
670 static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
672 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
673 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
675 ALOGE("Failed to get DrmConnector for display %d", display);
679 DrmMode mode = c->active_mode();
680 hwc_drm_display_t *hd = &ctx->displays[display];
681 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
682 if (hd->config_ids[i] == mode.id())
688 static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
690 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
691 hwc_drm_display_t *hd = &ctx->displays[display];
692 if (index >= (int)hd->config_ids.size()) {
693 ALOGE("Invalid config index %d passed in", index);
697 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
699 ALOGE("Failed to get connector for display %d", display);
703 for (const DrmMode &conn_mode : c->modes()) {
704 if (conn_mode.id() == hd->config_ids[index]) {
709 if (mode.id() != hd->config_ids[index]) {
710 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
713 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
715 ALOGE("Failed to set active config %d", ret);
721 static int hwc_device_close(struct hw_device_t *dev) {
722 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
728 * TODO: This function sets the active config to the first one in the list. This
729 * should be fixed such that it selects the preferred mode for the display, or
730 * some other, saner, method of choosing the config.
732 static int hwc_set_initial_config(hwc_drm_display_t *hd) {
734 size_t num_configs = 1;
735 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
737 if (ret || !num_configs)
740 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
742 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
749 static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
750 hwc_drm_display_t *hd = &ctx->displays[display];
752 hd->display = display;
754 int ret = hwc_set_initial_config(hd);
756 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
760 ret = hd->vsync_worker.Init(&ctx->drm, display);
762 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
769 static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
771 for (auto &conn : ctx->drm.connectors()) {
772 ret = hwc_initialize_display(ctx, conn->display());
774 ALOGE("Failed to initialize display %d", conn->display());
779 ret = ctx->virtual_compositor_worker.Init();
781 ALOGE("Failed to initialize virtual compositor worker");
787 static int hwc_device_open(const struct hw_module_t *module, const char *name,
788 struct hw_device_t **dev) {
789 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
790 ALOGE("Invalid module name- %s", name);
794 std::unique_ptr<hwc_context_t> ctx(new hwc_context_t());
796 ALOGE("Failed to allocate hwc context");
800 int ret = ctx->drm.Init();
802 ALOGE("Can't initialize Drm object %d", ret);
806 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
807 (const hw_module_t **)&ctx->gralloc);
809 ALOGE("Failed to open gralloc module %d", ret);
813 ret = ctx->dummy_timeline.Init();
815 ALOGE("Failed to create dummy sw sync timeline %d", ret);
819 ctx->importer.reset(Importer::CreateInstance(&ctx->drm));
820 if (!ctx->importer) {
821 ALOGE("Failed to create importer instance");
825 ret = hwc_enumerate_displays(ctx.get());
827 ALOGE("Failed to enumerate displays: %s", strerror(ret));
831 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
832 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
833 ctx->device.common.module = const_cast<hw_module_t *>(module);
834 ctx->device.common.close = hwc_device_close;
836 ctx->device.dump = hwc_dump;
837 ctx->device.prepare = hwc_prepare;
838 ctx->device.set = hwc_set;
839 ctx->device.eventControl = hwc_event_control;
840 ctx->device.setPowerMode = hwc_set_power_mode;
841 ctx->device.query = hwc_query;
842 ctx->device.registerProcs = hwc_register_procs;
843 ctx->device.getDisplayConfigs = hwc_get_display_configs;
844 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
845 ctx->device.getActiveConfig = hwc_get_active_config;
846 ctx->device.setActiveConfig = hwc_set_active_config;
847 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
849 *dev = &ctx->device.common;
856 static struct hw_module_methods_t hwc_module_methods = {
857 open : android::hwc_device_open
860 hwc_module_t HAL_MODULE_INFO_SYM = {
862 tag : HARDWARE_MODULE_TAG,
865 id : HWC_HARDWARE_MODULE_ID,
866 name : "DRM hwcomposer module",
867 author : "The Android Open Source Project",
868 methods : &hwc_module_methods,