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 "vsyncworker.h"
34 #include <sys/param.h>
35 #include <sys/resource.h>
37 #include <xf86drmMode.h>
39 #include <cutils/log.h>
40 #include <cutils/properties.h>
41 #include <hardware/hardware.h>
42 #include <hardware/hwcomposer.h>
44 #include <sync/sync.h>
45 #include <utils/Trace.h>
47 #define UM_PER_INCH 25400
48 #define HWC_FB_BUFFERS 3
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;
130 typedef DisplayMap::iterator DisplayMapIter;
132 hwc_context_t() : procs(NULL), importer(NULL), use_framebuffer_target(false) {
139 hwc_composer_device_1_t device;
140 hwc_procs_t const *procs;
145 const gralloc_module_t *gralloc;
146 DummySwSyncTimeline dummy_timeline;
147 bool use_framebuffer_target;
150 static native_handle_t *dup_buffer_handle(buffer_handle_t handle) {
151 native_handle_t *new_handle =
152 native_handle_create(handle->numFds, handle->numInts);
153 if (new_handle == NULL)
156 const int *old_data = handle->data;
157 int *new_data = new_handle->data;
158 for (int i = 0; i < handle->numFds; i++) {
159 *new_data = dup(*old_data);
163 memcpy(new_data, old_data, sizeof(int) * handle->numInts);
168 static void free_buffer_handle(native_handle_t *handle) {
169 int ret = native_handle_close(handle);
171 ALOGE("Failed to close native handle %d", ret);
172 ret = native_handle_delete(handle);
174 ALOGE("Failed to delete native handle %d", ret);
177 OutputFd &OutputFd::operator=(OutputFd &&rhs) {
179 std::swap(fd_, rhs.fd_);
182 ALOGE("Failed to fill OutputFd %p before assignment", fd_);
191 hwc_drm_bo *DrmHwcBuffer::operator->() {
192 if (importer_ == NULL) {
193 ALOGE("Access of none existent BO");
200 void DrmHwcBuffer::Clear() {
201 if (importer_ != NULL) {
202 importer_->ReleaseBuffer(&bo_);
207 int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
210 int ret = importer->ImportBuffer(handle, &tmp_bo);
214 if (importer_ != NULL) {
215 importer_->ReleaseBuffer(&bo_);
218 importer_ = importer;
225 int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle,
226 const gralloc_module_t *gralloc) {
227 native_handle_t *handle_copy = dup_buffer_handle(handle);
228 if (handle_copy == NULL) {
229 ALOGE("Failed to duplicate handle");
233 int ret = gralloc->registerBuffer(gralloc, handle_copy);
235 ALOGE("Failed to register buffer handle %d", ret);
236 free_buffer_handle(handle_copy);
243 handle_ = handle_copy;
248 DrmHwcNativeHandle::~DrmHwcNativeHandle() {
252 void DrmHwcNativeHandle::Clear() {
253 if (gralloc_ != NULL && handle_ != NULL) {
254 gralloc_->unregisterBuffer(gralloc_, handle_);
255 free_buffer_handle(handle_);
261 int DrmHwcLayer::InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer,
262 const gralloc_module_t *gralloc) {
263 sf_handle = sf_layer->handle;
264 int ret = buffer.ImportBuffer(sf_layer->handle, importer);
268 ret = handle.CopyBufferHandle(sf_layer->handle, gralloc);
272 alpha = sf_layer->planeAlpha;
274 switch (sf_layer->transform) {
276 transform = DrmHwcTransform::kIdentity;
278 case HWC_TRANSFORM_FLIP_H:
279 transform = DrmHwcTransform::kFlipH;
281 case HWC_TRANSFORM_FLIP_V:
282 transform = DrmHwcTransform::kFlipV;
284 case HWC_TRANSFORM_ROT_90:
285 transform = DrmHwcTransform::kRotate90;
287 case HWC_TRANSFORM_ROT_180:
288 transform = DrmHwcTransform::kRotate180;
290 case HWC_TRANSFORM_ROT_270:
291 transform = DrmHwcTransform::kRotate270;
294 ALOGE("Invalid transform in hwc_layer_1_t %d", sf_layer->transform);
298 switch (sf_layer->blending) {
299 case HWC_BLENDING_NONE:
300 blending = DrmHwcBlending::kNone;
302 case HWC_BLENDING_PREMULT:
303 blending = DrmHwcBlending::kPreMult;
305 case HWC_BLENDING_COVERAGE:
306 blending = DrmHwcBlending::kCoverage;
309 ALOGE("Invalid blending in hwc_layer_1_t %d", sf_layer->blending);
313 source_crop = DrmHwcRect<float>(
314 sf_layer->sourceCropf.left, sf_layer->sourceCropf.top,
315 sf_layer->sourceCropf.right, sf_layer->sourceCropf.bottom);
316 display_frame = DrmHwcRect<int>(
317 sf_layer->displayFrame.left, sf_layer->displayFrame.top,
318 sf_layer->displayFrame.right, sf_layer->displayFrame.bottom);
323 static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
325 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
326 std::ostringstream out;
328 ctx->drm.compositor()->Dump(&out);
329 std::string out_str = out.str();
330 strncpy(buff, out_str.c_str(), std::min((size_t)buff_len, out_str.length()));
333 static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
334 hwc_display_contents_1_t **display_contents) {
335 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
337 char use_framebuffer_target[PROPERTY_VALUE_MAX];
338 property_get("hwc.drm.use_framebuffer_target", use_framebuffer_target, "0");
339 bool new_use_framebuffer_target = atoi(use_framebuffer_target);
340 if (ctx->use_framebuffer_target != new_use_framebuffer_target)
341 ALOGW("Starting to %s HWC_FRAMEBUFFER_TARGET",
342 new_use_framebuffer_target ? "use" : "not use");
343 ctx->use_framebuffer_target = new_use_framebuffer_target;
345 for (int i = 0; i < (int)num_displays; ++i) {
346 if (!display_contents[i])
349 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
351 ALOGE("No crtc for display %d", i);
355 int num_layers = display_contents[i]->numHwLayers;
356 for (int j = 0; j < num_layers; j++) {
357 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
359 if (!ctx->use_framebuffer_target) {
360 if (layer->compositionType == HWC_FRAMEBUFFER)
361 layer->compositionType = HWC_OVERLAY;
363 switch (layer->compositionType) {
367 case HWC_CURSOR_OVERLAY:
368 layer->compositionType = HWC_FRAMEBUFFER;
378 static void hwc_add_layer_to_retire_fence(
379 hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) {
380 if (layer->releaseFenceFd < 0)
383 if (display_contents->retireFenceFd >= 0) {
384 int old_retire_fence = display_contents->retireFenceFd;
385 display_contents->retireFenceFd =
386 sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd);
387 close(old_retire_fence);
389 display_contents->retireFenceFd = dup(layer->releaseFenceFd);
393 static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
394 hwc_display_contents_1_t **sf_display_contents) {
396 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
399 std::vector<CheckedOutputFd> checked_output_fences;
400 std::vector<DrmHwcDisplayContents> displays_contents;
401 std::vector<DrmCompositionDisplayLayersMap> layers_map;
402 std::vector<std::vector<size_t>> layers_indices;
403 displays_contents.reserve(num_displays);
404 // layers_map.reserve(num_displays);
405 layers_indices.reserve(num_displays);
407 // Phase one does nothing that would cause errors. Only take ownership of FDs.
408 for (size_t i = 0; i < num_displays; ++i) {
409 hwc_display_contents_1_t *dc = sf_display_contents[i];
410 displays_contents.emplace_back();
411 DrmHwcDisplayContents &display_contents = displays_contents.back();
412 layers_indices.emplace_back();
413 std::vector<size_t> &indices_to_composite = layers_indices.back();
415 if (!sf_display_contents[i])
418 std::ostringstream display_index_formatter;
419 display_index_formatter << "retire fence for display " << i;
420 std::string display_fence_description(display_index_formatter.str());
421 checked_output_fences.emplace_back(&dc->retireFenceFd,
422 display_fence_description.c_str(),
423 ctx->dummy_timeline);
424 display_contents.retire_fence = OutputFd(&dc->retireFenceFd);
426 size_t num_dc_layers = dc->numHwLayers;
427 int framebuffer_target_index = -1;
428 for (size_t j = 0; j < num_dc_layers; ++j) {
429 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
431 display_contents.layers.emplace_back();
432 DrmHwcLayer &layer = display_contents.layers.back();
434 if (sf_layer->flags & HWC_SKIP_LAYER)
437 if (!ctx->use_framebuffer_target) {
438 if (sf_layer->compositionType == HWC_OVERLAY)
439 indices_to_composite.push_back(j);
440 if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET)
441 framebuffer_target_index = j;
443 if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET)
444 indices_to_composite.push_back(j);
447 layer.acquire_fence.Set(sf_layer->acquireFenceFd);
448 sf_layer->acquireFenceFd = -1;
450 std::ostringstream layer_fence_formatter;
451 layer_fence_formatter << "release fence for layer " << j << " of display "
453 std::string layer_fence_description(layer_fence_formatter.str());
454 checked_output_fences.emplace_back(&sf_layer->releaseFenceFd,
455 layer_fence_description.c_str(),
456 ctx->dummy_timeline);
457 layer.release_fence = OutputFd(&sf_layer->releaseFenceFd);
460 if (ctx->use_framebuffer_target) {
461 if (indices_to_composite.size() != 1) {
462 ALOGE("Expected 1 (got %d) layer with HWC_FRAMEBUFFER_TARGET",
463 indices_to_composite.size());
467 if (indices_to_composite.empty() && framebuffer_target_index >= 0) {
468 hwc_layer_1_t *sf_layer = &dc->hwLayers[framebuffer_target_index];
469 if (!sf_layer->handle || (sf_layer->flags & HWC_SKIP_LAYER)) {
471 "Expected valid layer with HWC_FRAMEBUFFER_TARGET when all "
472 "HWC_OVERLAY layers are skipped.");
475 indices_to_composite.push_back(framebuffer_target_index);
483 for (size_t i = 0; i < num_displays; ++i) {
484 hwc_display_contents_1_t *dc = sf_display_contents[i];
485 DrmHwcDisplayContents &display_contents = displays_contents[i];
486 if (!sf_display_contents[i])
489 layers_map.emplace_back();
490 DrmCompositionDisplayLayersMap &map = layers_map.back();
491 std::vector<size_t> &indices_to_composite = layers_indices[i];
492 for (size_t j : indices_to_composite) {
493 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
495 DrmHwcLayer &layer = display_contents.layers[j];
497 layer.InitFromHwcLayer(sf_layer, ctx->importer, ctx->gralloc);
498 map.layers.emplace_back(std::move(layer));
502 std::unique_ptr<DrmComposition> composition(
503 ctx->drm.compositor()->CreateComposition(ctx->importer));
505 ALOGE("Drm composition init failed");
509 ret = composition->SetLayers(layers_map.size(), layers_map.data());
514 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
519 composition.reset(NULL);
524 static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
525 int event, int enabled) {
526 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
529 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
530 hwc_drm_display_t *hd = &ctx->displays[display];
531 return hd->vsync_worker.VSyncControl(enabled);
534 static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
536 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
538 uint64_t dpmsValue = 0;
540 case HWC_POWER_MODE_OFF:
541 dpmsValue = DRM_MODE_DPMS_OFF;
544 /* We can't support dozing right now, so go full on */
545 case HWC_POWER_MODE_DOZE:
546 case HWC_POWER_MODE_DOZE_SUSPEND:
547 case HWC_POWER_MODE_NORMAL:
548 dpmsValue = DRM_MODE_DPMS_ON;
551 return ctx->drm.SetDpmsMode(display, dpmsValue);
554 static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
557 case HWC_BACKGROUND_LAYER_SUPPORTED:
558 *value = 0; /* TODO: We should do this */
560 case HWC_VSYNC_PERIOD:
561 ALOGW("Query for deprecated vsync value, returning 60Hz");
562 *value = 1000 * 1000 * 1000 / 60;
564 case HWC_DISPLAY_TYPES_SUPPORTED:
565 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
571 static void hwc_register_procs(struct hwc_composer_device_1 *dev,
572 hwc_procs_t const *procs) {
573 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
577 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
578 iter != ctx->displays.end(); ++iter) {
579 iter->second.vsync_worker.SetProcs(procs);
583 static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
584 int display, uint32_t *configs,
585 size_t *num_configs) {
589 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
590 hwc_drm_display_t *hd = &ctx->displays[display];
591 hd->config_ids.clear();
593 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
595 ALOGE("Failed to get connector for display %d", display);
599 int ret = connector->UpdateModes();
601 ALOGE("Failed to update display modes %d", ret);
605 for (DrmConnector::ModeIter iter = connector->begin_modes();
606 iter != connector->end_modes(); ++iter) {
607 size_t idx = hd->config_ids.size();
608 if (idx == *num_configs)
610 hd->config_ids.push_back(iter->id());
611 configs[idx] = iter->id();
613 *num_configs = hd->config_ids.size();
614 return *num_configs == 0 ? -1 : 0;
617 static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
618 int display, uint32_t config,
619 const uint32_t *attributes,
621 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
622 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
624 ALOGE("Failed to get DrmConnector for display %d", display);
628 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
630 if (iter->id() == config) {
635 if (mode.id() == 0) {
636 ALOGE("Failed to find active mode for display %d", display);
640 uint32_t mm_width = c->mm_width();
641 uint32_t mm_height = c->mm_height();
642 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
643 switch (attributes[i]) {
644 case HWC_DISPLAY_VSYNC_PERIOD:
645 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
647 case HWC_DISPLAY_WIDTH:
648 values[i] = mode.h_display();
650 case HWC_DISPLAY_HEIGHT:
651 values[i] = mode.v_display();
653 case HWC_DISPLAY_DPI_X:
654 /* Dots per 1000 inches */
655 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
657 case HWC_DISPLAY_DPI_Y:
658 /* Dots per 1000 inches */
660 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
667 static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
669 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
670 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
672 ALOGE("Failed to get DrmConnector for display %d", display);
676 DrmMode mode = c->active_mode();
677 hwc_drm_display_t *hd = &ctx->displays[display];
678 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
679 if (hd->config_ids[i] == mode.id())
685 static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
687 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
688 hwc_drm_display_t *hd = &ctx->displays[display];
689 if (index >= (int)hd->config_ids.size()) {
690 ALOGE("Invalid config index %d passed in", index);
694 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
696 ALOGE("Failed to get connector for display %d", display);
700 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
702 if (iter->id() == hd->config_ids[index]) {
707 if (mode.id() != hd->config_ids[index]) {
708 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
711 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
713 ALOGE("Failed to set active config %d", ret);
719 static int hwc_device_close(struct hw_device_t *dev) {
720 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
726 * TODO: This function sets the active config to the first one in the list. This
727 * should be fixed such that it selects the preferred mode for the display, or
728 * some other, saner, method of choosing the config.
730 static int hwc_set_initial_config(hwc_drm_display_t *hd) {
732 size_t num_configs = 1;
733 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
735 if (ret || !num_configs)
738 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
740 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
747 static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
748 hwc_drm_display_t *hd = &ctx->displays[display];
750 hd->display = display;
752 int ret = hwc_set_initial_config(hd);
754 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
758 ret = hd->vsync_worker.Init(&ctx->drm, display);
760 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
767 static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
769 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
770 c != ctx->drm.end_connectors(); ++c) {
771 ret = hwc_initialize_display(ctx, (*c)->display());
773 ALOGE("Failed to initialize display %d", (*c)->display());
781 static int hwc_device_open(const struct hw_module_t *module, const char *name,
782 struct hw_device_t **dev) {
783 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
784 ALOGE("Invalid module name- %s", name);
788 struct hwc_context_t *ctx = new hwc_context_t();
790 ALOGE("Failed to allocate hwc context");
794 int ret = ctx->drm.Init();
796 ALOGE("Can't initialize Drm object %d", ret);
801 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
802 (const hw_module_t **)&ctx->gralloc);
804 ALOGE("Failed to open gralloc module %d", ret);
809 ret = ctx->dummy_timeline.Init();
811 ALOGE("Failed to create dummy sw sync timeline %d", ret);
815 ctx->importer = Importer::CreateInstance(&ctx->drm);
816 if (!ctx->importer) {
817 ALOGE("Failed to create importer instance");
822 ret = hwc_enumerate_displays(ctx);
824 ALOGE("Failed to enumerate displays: %s", strerror(ret));
829 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
830 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
831 ctx->device.common.module = const_cast<hw_module_t *>(module);
832 ctx->device.common.close = hwc_device_close;
834 ctx->device.dump = hwc_dump;
835 ctx->device.prepare = hwc_prepare;
836 ctx->device.set = hwc_set;
837 ctx->device.eventControl = hwc_event_control;
838 ctx->device.setPowerMode = hwc_set_power_mode;
839 ctx->device.query = hwc_query;
840 ctx->device.registerProcs = hwc_register_procs;
841 ctx->device.getDisplayConfigs = hwc_get_display_configs;
842 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
843 ctx->device.getActiveConfig = hwc_get_active_config;
844 ctx->device.setActiveConfig = hwc_set_active_config;
845 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
847 *dev = &ctx->device.common;
853 static struct hw_module_methods_t hwc_module_methods = {
854 open : android::hwc_device_open
857 hwc_module_t HAL_MODULE_INFO_SYM = {
859 tag : HARDWARE_MODULE_TAG,
862 id : HWC_HARDWARE_MODULE_ID,
863 name : "DRM hwcomposer module",
864 author : "The Android Open Source Project",
865 methods : &hwc_module_methods,