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"
31 #include <sys/param.h>
32 #include <sys/resource.h>
34 #include <xf86drmMode.h>
36 #include <cutils/log.h>
37 #include <cutils/properties.h>
38 #include <hardware/hardware.h>
39 #include <hardware/hwcomposer.h>
40 #include <utils/Trace.h>
42 #define UM_PER_INCH 25400
43 #define HWC_FB_BUFFERS 3
47 typedef struct hwc_drm_display {
48 struct hwc_context_t *ctx;
51 std::vector<uint32_t> config_ids;
53 VSyncWorker vsync_worker;
56 struct hwc_context_t {
57 // map of display:hwc_drm_display_t
58 typedef std::map<int, hwc_drm_display_t> DisplayMap;
59 typedef DisplayMap::iterator DisplayMapIter;
61 hwc_context_t() : procs(NULL), importer(NULL) {
68 hwc_composer_device_1_t device;
69 hwc_procs_t const *procs;
76 static void hwc_dump(struct hwc_composer_device_1* dev, char *buff,
78 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
79 std::ostringstream out;
81 ctx->drm.compositor()->Dump(&out);
82 std::string out_str = out.str();
83 strncpy(buff, out_str.c_str(), std::min((size_t)buff_len, out_str.length()));
86 static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
87 hwc_display_contents_1_t **display_contents) {
88 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
89 for (int i = 0; i < (int)num_displays; ++i) {
90 if (!display_contents[i])
93 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
95 ALOGE("No crtc for display %d", i);
99 int num_layers = display_contents[i]->numHwLayers;
100 for (int j = 0; j < num_layers; j++) {
101 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
103 if (layer->compositionType == HWC_FRAMEBUFFER)
104 layer->compositionType = HWC_OVERLAY;
111 static void hwc_set_cleanup(size_t num_displays,
112 hwc_display_contents_1_t **display_contents,
113 Composition *composition) {
114 for (int i = 0; i < (int)num_displays; ++i) {
115 if (!display_contents[i])
118 hwc_display_contents_1_t *dc = display_contents[i];
119 for (size_t j = 0; j < dc->numHwLayers; ++j) {
120 hwc_layer_1_t *layer = &dc->hwLayers[j];
121 if (layer->acquireFenceFd >= 0) {
122 close(layer->acquireFenceFd);
123 layer->acquireFenceFd = -1;
126 if (dc->outbufAcquireFenceFd >= 0) {
127 close(dc->outbufAcquireFenceFd);
128 dc->outbufAcquireFenceFd = -1;
135 static int hwc_add_layer(int display, hwc_context_t *ctx, hwc_layer_1_t *layer,
136 Composition *composition) {
138 int ret = ctx->importer->ImportBuffer(layer->handle, &bo);
140 ALOGE("Failed to import handle to bo %d", ret);
144 ret = composition->AddLayer(display, layer, &bo);
148 int destroy_ret = ctx->importer->ReleaseBuffer(&bo);
150 ALOGE("Failed to destroy buffer %d", destroy_ret);
155 static void hwc_add_layer_to_retire_fence(hwc_layer_1_t *layer,
156 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 = sync_merge("dc_retire", old_retire_fence,
163 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;
174 Composition *composition =
175 ctx->drm.compositor()->CreateComposition(ctx->importer);
177 ALOGE("Drm composition init failed");
178 hwc_set_cleanup(num_displays, display_contents, NULL);
183 for (int i = 0; i < (int)num_displays; ++i) {
184 if (!display_contents[i])
187 hwc_display_contents_1_t *dc = display_contents[i];
189 unsigned num_layers = 0;
190 unsigned num_dc_layers = dc->numHwLayers;
191 for (j = 0; j < (int)num_dc_layers; ++j) {
192 hwc_layer_1_t *layer = &dc->hwLayers[j];
193 if (layer->flags & HWC_SKIP_LAYER)
195 if (layer->compositionType == HWC_OVERLAY)
199 unsigned num_planes = composition->GetRemainingLayers(i, num_layers);
201 if (num_layers > num_planes) {
202 ALOGE("Can not composite %u with only %u planes", num_layers, num_planes);
205 for (j = 0; num_planes && j < (int)num_dc_layers; ++j) {
206 hwc_layer_1_t *layer = &dc->hwLayers[j];
207 if (layer->flags & HWC_SKIP_LAYER)
209 if (layer->compositionType != HWC_OVERLAY)
212 ret = hwc_add_layer(i, ctx, layer, composition);
214 ALOGE("Add layer failed %d", ret);
215 hwc_set_cleanup(num_displays, display_contents, composition);
218 hwc_add_layer_to_retire_fence(layer, dc);
224 ret = ctx->drm.compositor()->QueueComposition(composition);
227 ALOGE("Failed to queue the composition");
228 hwc_set_cleanup(num_displays, display_contents, NULL);
231 hwc_set_cleanup(num_displays, display_contents, NULL);
235 static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
236 int event, int enabled) {
237 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
240 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
241 hwc_drm_display_t *hd = &ctx->displays[display];
242 return hd->vsync_worker.VSyncControl(enabled);
245 static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
247 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
249 uint64_t dpmsValue = 0;
251 case HWC_POWER_MODE_OFF:
252 dpmsValue = DRM_MODE_DPMS_OFF;
255 /* We can't support dozing right now, so go full on */
256 case HWC_POWER_MODE_DOZE:
257 case HWC_POWER_MODE_DOZE_SUSPEND:
258 case HWC_POWER_MODE_NORMAL:
259 dpmsValue = DRM_MODE_DPMS_ON;
262 return ctx->drm.SetDpmsMode(display, dpmsValue);
265 static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
268 case HWC_BACKGROUND_LAYER_SUPPORTED:
269 *value = 0; /* TODO: We should do this */
271 case HWC_VSYNC_PERIOD:
272 ALOGW("Query for deprecated vsync value, returning 60Hz");
273 *value = 1000 * 1000 * 1000 / 60;
275 case HWC_DISPLAY_TYPES_SUPPORTED:
276 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
282 static void hwc_register_procs(struct hwc_composer_device_1 *dev,
283 hwc_procs_t const *procs) {
284 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
288 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
289 iter != ctx->displays.end(); ++iter) {
290 iter->second.vsync_worker.SetProcs(procs);
294 static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
295 int display, uint32_t *configs,
296 size_t *num_configs) {
300 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
301 hwc_drm_display_t *hd = &ctx->displays[display];
302 hd->config_ids.clear();
304 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
306 ALOGE("Failed to get connector for display %d", display);
310 int ret = connector->UpdateModes();
312 ALOGE("Failed to update display modes %d", ret);
316 for (DrmConnector::ModeIter iter = connector->begin_modes();
317 iter != connector->end_modes(); ++iter) {
318 size_t idx = hd->config_ids.size();
319 if (idx == *num_configs)
321 hd->config_ids.push_back(iter->id());
322 configs[idx] = iter->id();
324 *num_configs = hd->config_ids.size();
325 return *num_configs == 0 ? -1 : 0;
328 static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
329 int display, uint32_t config,
330 const uint32_t *attributes,
332 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
333 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
335 ALOGE("Failed to get DrmConnector for display %d", display);
339 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
341 if (iter->id() == config) {
346 if (mode.id() == 0) {
347 ALOGE("Failed to find active mode for display %d", display);
351 uint32_t mm_width = c->mm_width();
352 uint32_t mm_height = c->mm_height();
353 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
354 switch (attributes[i]) {
355 case HWC_DISPLAY_VSYNC_PERIOD:
356 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
358 case HWC_DISPLAY_WIDTH:
359 values[i] = mode.h_display();
361 case HWC_DISPLAY_HEIGHT:
362 values[i] = mode.v_display();
364 case HWC_DISPLAY_DPI_X:
365 /* Dots per 1000 inches */
366 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
368 case HWC_DISPLAY_DPI_Y:
369 /* Dots per 1000 inches */
371 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
378 static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
380 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
381 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
383 ALOGE("Failed to get DrmConnector for display %d", display);
387 DrmMode mode = c->active_mode();
388 hwc_drm_display_t *hd = &ctx->displays[display];
389 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
390 if (hd->config_ids[i] == mode.id())
396 static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
398 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
399 hwc_drm_display_t *hd = &ctx->displays[display];
400 if (index >= (int)hd->config_ids.size()) {
401 ALOGE("Invalid config index %d passed in", index);
405 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
407 ALOGE("Failed to get connector for display %d", display);
411 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
413 if (iter->id() == hd->config_ids[index]) {
418 if (mode.id() != hd->config_ids[index]) {
419 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
422 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
424 ALOGE("Failed to set active config %d", ret);
430 static int hwc_device_close(struct hw_device_t *dev) {
431 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
437 * TODO: This function sets the active config to the first one in the list. This
438 * should be fixed such that it selects the preferred mode for the display, or
439 * some other, saner, method of choosing the config.
441 static int hwc_set_initial_config(hwc_drm_display_t *hd) {
443 size_t num_configs = 1;
444 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
446 if (ret || !num_configs)
449 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
451 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
458 static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
459 hwc_drm_display_t *hd = &ctx->displays[display];
461 hd->display = display;
463 int ret = hwc_set_initial_config(hd);
465 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
469 ret = hd->vsync_worker.Init(&ctx->drm, display);
471 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
478 static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
480 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
481 c != ctx->drm.end_connectors(); ++c) {
482 ret = hwc_initialize_display(ctx, (*c)->display());
484 ALOGE("Failed to initialize display %d", (*c)->display());
492 static int hwc_device_open(const struct hw_module_t *module, const char *name,
493 struct hw_device_t **dev) {
494 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
495 ALOGE("Invalid module name- %s", name);
499 struct hwc_context_t *ctx = new hwc_context_t();
501 ALOGE("Failed to allocate hwc context");
505 int ret = ctx->drm.Init();
507 ALOGE("Can't initialize Drm object %d", ret);
512 ctx->importer = Importer::CreateInstance(&ctx->drm);
513 if (!ctx->importer) {
514 ALOGE("Failed to create importer instance");
519 ret = hwc_enumerate_displays(ctx);
521 ALOGE("Failed to enumerate displays: %s", strerror(ret));
526 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
527 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
528 ctx->device.common.module = const_cast<hw_module_t *>(module);
529 ctx->device.common.close = hwc_device_close;
531 ctx->device.dump = hwc_dump;
532 ctx->device.prepare = hwc_prepare;
533 ctx->device.set = hwc_set;
534 ctx->device.eventControl = hwc_event_control;
535 ctx->device.setPowerMode = hwc_set_power_mode;
536 ctx->device.query = hwc_query;
537 ctx->device.registerProcs = hwc_register_procs;
538 ctx->device.getDisplayConfigs = hwc_get_display_configs;
539 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
540 ctx->device.getActiveConfig = hwc_get_active_config;
541 ctx->device.setActiveConfig = hwc_set_active_config;
542 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
544 *dev = &ctx->device.common;
550 static struct hw_module_methods_t hwc_module_methods = {
551 open : android::hwc_device_open
554 hwc_module_t HAL_MODULE_INFO_SYM = {
556 tag : HARDWARE_MODULE_TAG,
559 id : HWC_HARDWARE_MODULE_ID,
560 name : "DRM hwcomposer module",
561 author : "The Android Open Source Project",
562 methods : &hwc_module_methods,