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"
33 #include <sys/param.h>
34 #include <sys/resource.h>
36 #include <xf86drmMode.h>
38 #include <cutils/log.h>
39 #include <cutils/properties.h>
40 #include <hardware/hardware.h>
41 #include <hardware/hwcomposer.h>
42 #include <utils/Trace.h>
44 #define UM_PER_INCH 25400
45 #define HWC_FB_BUFFERS 3
49 typedef struct hwc_drm_display {
50 struct hwc_context_t *ctx;
53 std::vector<uint32_t> config_ids;
55 VSyncWorker vsync_worker;
58 struct hwc_context_t {
59 // map of display:hwc_drm_display_t
60 typedef std::map<int, hwc_drm_display_t> DisplayMap;
61 typedef DisplayMap::iterator DisplayMapIter;
63 hwc_context_t() : procs(NULL), importer(NULL), use_framebuffer_target(false) {
70 hwc_composer_device_1_t device;
71 hwc_procs_t const *procs;
76 bool use_framebuffer_target;
79 static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
81 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
82 std::ostringstream out;
84 ctx->drm.compositor()->Dump(&out);
85 std::string out_str = out.str();
86 strncpy(buff, out_str.c_str(), std::min((size_t)buff_len, out_str.length()));
89 static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
90 hwc_display_contents_1_t **display_contents) {
91 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
93 char use_framebuffer_target[PROPERTY_VALUE_MAX];
94 property_get("hwc.drm.use_framebuffer_target", use_framebuffer_target, "0");
95 bool new_use_framebuffer_target = atoi(use_framebuffer_target);
96 if (ctx->use_framebuffer_target != new_use_framebuffer_target)
97 ALOGW("Starting to %s HWC_FRAMEBUFFER_TARGET",
98 new_use_framebuffer_target ? "use" : "not use");
99 ctx->use_framebuffer_target = new_use_framebuffer_target;
101 for (int i = 0; i < (int)num_displays; ++i) {
102 if (!display_contents[i])
105 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
107 ALOGE("No crtc for display %d", i);
111 int num_layers = display_contents[i]->numHwLayers;
112 for (int j = 0; j < num_layers; j++) {
113 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
115 if (!ctx->use_framebuffer_target) {
116 if (layer->compositionType == HWC_FRAMEBUFFER)
117 layer->compositionType = HWC_OVERLAY;
119 switch (layer->compositionType) {
123 case HWC_CURSOR_OVERLAY:
124 layer->compositionType = HWC_FRAMEBUFFER;
134 static void hwc_set_cleanup(size_t num_displays,
135 hwc_display_contents_1_t **display_contents) {
136 for (int i = 0; i < (int)num_displays; ++i) {
137 if (!display_contents[i])
140 hwc_display_contents_1_t *dc = display_contents[i];
141 for (size_t j = 0; j < dc->numHwLayers; ++j) {
142 hwc_layer_1_t *layer = &dc->hwLayers[j];
143 if (layer->acquireFenceFd >= 0) {
144 close(layer->acquireFenceFd);
145 layer->acquireFenceFd = -1;
148 if (dc->outbufAcquireFenceFd >= 0) {
149 close(dc->outbufAcquireFenceFd);
150 dc->outbufAcquireFenceFd = -1;
155 static void hwc_add_layer_to_retire_fence(
156 hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) {
157 if (layer->releaseFenceFd < 0)
160 if (display_contents->retireFenceFd >= 0) {
161 int old_retire_fence = display_contents->retireFenceFd;
162 display_contents->retireFenceFd =
163 sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd);
164 close(old_retire_fence);
166 display_contents->retireFenceFd = dup(layer->releaseFenceFd);
170 static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
171 hwc_display_contents_1_t **display_contents) {
173 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
175 std::unique_ptr<DrmComposition> composition(
176 ctx->drm.compositor()->CreateComposition(ctx->importer));
178 ALOGE("Drm composition init failed");
179 hwc_set_cleanup(num_displays, display_contents);
183 std::vector<DrmCompositionDisplayLayersMap> layers_map;
184 std::vector<std::vector<size_t>> layers_indices;
185 layers_map.reserve(num_displays);
186 layers_indices.reserve(num_displays);
188 for (int i = 0; i < (int)num_displays; ++i) {
189 if (!display_contents[i])
191 hwc_display_contents_1_t *dc = display_contents[i];
193 layers_map.emplace_back();
194 DrmCompositionDisplayLayersMap &map = layers_map[i];
196 map.layers = dc->hwLayers;
198 std::vector<size_t> indices_to_composite;
199 unsigned num_dc_layers = dc->numHwLayers;
200 int framebuffer_target_index = -1;
201 for (int j = 0; j < (int)num_dc_layers; ++j) {
202 hwc_layer_1_t *layer = &dc->hwLayers[j];
203 if (layer->flags & HWC_SKIP_LAYER)
205 if (!ctx->use_framebuffer_target) {
206 if (layer->compositionType == HWC_OVERLAY)
207 indices_to_composite.push_back(j);
208 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET)
209 framebuffer_target_index = j;
211 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET)
212 indices_to_composite.push_back(j);
215 if (ctx->use_framebuffer_target) {
216 if (indices_to_composite.size() != 1) {
217 ALOGE("Expected 1 (got %d) layer with HWC_FRAMEBUFFER_TARGET",
218 indices_to_composite.size());
219 hwc_set_cleanup(num_displays, display_contents);
223 if (indices_to_composite.empty() && framebuffer_target_index >= 0) {
224 // Fall back to use HWC_FRAMEBUFFER_TARGET if all HWC_OVERLAY layers
226 hwc_layer_1_t *layer = &dc->hwLayers[framebuffer_target_index];
227 if (!layer->handle || (layer->flags & HWC_SKIP_LAYER)) {
228 ALOGE("Expected valid layer with HWC_FRAMEBUFFER_TARGET when all "
229 "HWC_OVERLAY layers are skipped.");
230 hwc_set_cleanup(num_displays, display_contents);
233 indices_to_composite.push_back(framebuffer_target_index);
237 map.num_layers = indices_to_composite.size();
238 layers_indices.emplace_back(std::move(indices_to_composite));
239 map.layer_indices = layers_indices.back().data();
242 ret = composition->SetLayers(layers_map.size(), layers_map.data());
244 hwc_set_cleanup(num_displays, display_contents);
248 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
250 hwc_set_cleanup(num_displays, display_contents);
254 composition.reset(NULL);
256 for (int i = 0; i < (int)num_displays; ++i) {
257 if (!display_contents[i])
259 hwc_display_contents_1_t *dc = display_contents[i];
260 unsigned num_dc_layers = dc->numHwLayers;
261 for (int j = 0; j < (int)num_dc_layers; ++j) {
262 hwc_layer_1_t *layer = &dc->hwLayers[j];
263 if (layer->flags & HWC_SKIP_LAYER)
265 if (layer->compositionType == HWC_OVERLAY)
266 hwc_add_layer_to_retire_fence(layer, dc);
271 ALOGE("Failed to queue the composition");
273 hwc_set_cleanup(num_displays, display_contents);
277 static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
278 int event, int enabled) {
279 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
282 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
283 hwc_drm_display_t *hd = &ctx->displays[display];
284 return hd->vsync_worker.VSyncControl(enabled);
287 static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
289 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
291 uint64_t dpmsValue = 0;
293 case HWC_POWER_MODE_OFF:
294 dpmsValue = DRM_MODE_DPMS_OFF;
297 /* We can't support dozing right now, so go full on */
298 case HWC_POWER_MODE_DOZE:
299 case HWC_POWER_MODE_DOZE_SUSPEND:
300 case HWC_POWER_MODE_NORMAL:
301 dpmsValue = DRM_MODE_DPMS_ON;
304 return ctx->drm.SetDpmsMode(display, dpmsValue);
307 static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
310 case HWC_BACKGROUND_LAYER_SUPPORTED:
311 *value = 0; /* TODO: We should do this */
313 case HWC_VSYNC_PERIOD:
314 ALOGW("Query for deprecated vsync value, returning 60Hz");
315 *value = 1000 * 1000 * 1000 / 60;
317 case HWC_DISPLAY_TYPES_SUPPORTED:
318 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
324 static void hwc_register_procs(struct hwc_composer_device_1 *dev,
325 hwc_procs_t const *procs) {
326 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
330 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
331 iter != ctx->displays.end(); ++iter) {
332 iter->second.vsync_worker.SetProcs(procs);
336 static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
337 int display, uint32_t *configs,
338 size_t *num_configs) {
342 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
343 hwc_drm_display_t *hd = &ctx->displays[display];
344 hd->config_ids.clear();
346 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
348 ALOGE("Failed to get connector for display %d", display);
352 int ret = connector->UpdateModes();
354 ALOGE("Failed to update display modes %d", ret);
358 for (DrmConnector::ModeIter iter = connector->begin_modes();
359 iter != connector->end_modes(); ++iter) {
360 size_t idx = hd->config_ids.size();
361 if (idx == *num_configs)
363 hd->config_ids.push_back(iter->id());
364 configs[idx] = iter->id();
366 *num_configs = hd->config_ids.size();
367 return *num_configs == 0 ? -1 : 0;
370 static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
371 int display, uint32_t config,
372 const uint32_t *attributes,
374 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
375 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
377 ALOGE("Failed to get DrmConnector for display %d", display);
381 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
383 if (iter->id() == config) {
388 if (mode.id() == 0) {
389 ALOGE("Failed to find active mode for display %d", display);
393 uint32_t mm_width = c->mm_width();
394 uint32_t mm_height = c->mm_height();
395 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
396 switch (attributes[i]) {
397 case HWC_DISPLAY_VSYNC_PERIOD:
398 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
400 case HWC_DISPLAY_WIDTH:
401 values[i] = mode.h_display();
403 case HWC_DISPLAY_HEIGHT:
404 values[i] = mode.v_display();
406 case HWC_DISPLAY_DPI_X:
407 /* Dots per 1000 inches */
408 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
410 case HWC_DISPLAY_DPI_Y:
411 /* Dots per 1000 inches */
413 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
420 static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
422 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
423 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
425 ALOGE("Failed to get DrmConnector for display %d", display);
429 DrmMode mode = c->active_mode();
430 hwc_drm_display_t *hd = &ctx->displays[display];
431 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
432 if (hd->config_ids[i] == mode.id())
438 static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
440 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
441 hwc_drm_display_t *hd = &ctx->displays[display];
442 if (index >= (int)hd->config_ids.size()) {
443 ALOGE("Invalid config index %d passed in", index);
447 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
449 ALOGE("Failed to get connector for display %d", display);
453 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
455 if (iter->id() == hd->config_ids[index]) {
460 if (mode.id() != hd->config_ids[index]) {
461 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
464 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
466 ALOGE("Failed to set active config %d", ret);
472 static int hwc_device_close(struct hw_device_t *dev) {
473 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
479 * TODO: This function sets the active config to the first one in the list. This
480 * should be fixed such that it selects the preferred mode for the display, or
481 * some other, saner, method of choosing the config.
483 static int hwc_set_initial_config(hwc_drm_display_t *hd) {
485 size_t num_configs = 1;
486 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
488 if (ret || !num_configs)
491 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
493 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
500 static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
501 hwc_drm_display_t *hd = &ctx->displays[display];
503 hd->display = display;
505 int ret = hwc_set_initial_config(hd);
507 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
511 ret = hd->vsync_worker.Init(&ctx->drm, display);
513 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
520 static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
522 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
523 c != ctx->drm.end_connectors(); ++c) {
524 ret = hwc_initialize_display(ctx, (*c)->display());
526 ALOGE("Failed to initialize display %d", (*c)->display());
534 static int hwc_device_open(const struct hw_module_t *module, const char *name,
535 struct hw_device_t **dev) {
536 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
537 ALOGE("Invalid module name- %s", name);
541 struct hwc_context_t *ctx = new hwc_context_t();
543 ALOGE("Failed to allocate hwc context");
547 int ret = ctx->drm.Init();
549 ALOGE("Can't initialize Drm object %d", ret);
554 ctx->importer = Importer::CreateInstance(&ctx->drm);
555 if (!ctx->importer) {
556 ALOGE("Failed to create importer instance");
561 ret = hwc_enumerate_displays(ctx);
563 ALOGE("Failed to enumerate displays: %s", strerror(ret));
568 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
569 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
570 ctx->device.common.module = const_cast<hw_module_t *>(module);
571 ctx->device.common.close = hwc_device_close;
573 ctx->device.dump = hwc_dump;
574 ctx->device.prepare = hwc_prepare;
575 ctx->device.set = hwc_set;
576 ctx->device.eventControl = hwc_event_control;
577 ctx->device.setPowerMode = hwc_set_power_mode;
578 ctx->device.query = hwc_query;
579 ctx->device.registerProcs = hwc_register_procs;
580 ctx->device.getDisplayConfigs = hwc_get_display_configs;
581 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
582 ctx->device.getActiveConfig = hwc_get_active_config;
583 ctx->device.setActiveConfig = hwc_set_active_config;
584 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
586 *dev = &ctx->device.common;
592 static struct hw_module_methods_t hwc_module_methods = {
593 open : android::hwc_device_open
596 hwc_module_t HAL_MODULE_INFO_SYM = {
598 tag : HARDWARE_MODULE_TAG,
601 id : HWC_HARDWARE_MODULE_ID,
602 name : "DRM hwcomposer module",
603 author : "The Android Open Source Project",
604 methods : &hwc_module_methods,