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 LOG_TAG "hwcomposer-drm"
19 #include "drm_hwcomposer.h"
20 #include "drmresources.h"
22 #include "vsyncworker.h"
30 #include <sys/param.h>
31 #include <sys/resource.h>
33 #include <xf86drmMode.h>
35 #include <cutils/log.h>
36 #include <cutils/properties.h>
37 #include <hardware/hardware.h>
38 #include <hardware/hwcomposer.h>
40 #include <sync/sync.h>
42 #define UM_PER_INCH 25400
46 typedef struct hwc_drm_display {
47 struct hwc_context_t *ctx;
50 std::vector<uint32_t> config_ids;
52 VSyncWorker vsync_worker;
55 struct hwc_context_t {
56 // map of display:hwc_drm_display_t
57 typedef std::map<int, hwc_drm_display_t> DisplayMap;
58 typedef DisplayMap::iterator DisplayMapIter;
60 hwc_context_t() : procs(NULL), importer(NULL) {
67 hwc_composer_device_1_t device;
68 hwc_procs_t const *procs;
75 static void hwc_dump(struct hwc_composer_device_1* dev, char *buff,
77 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
78 std::ostringstream out;
80 ctx->drm.compositor()->Dump(&out);
81 std::string out_str = out.str();
82 strncpy(buff, out_str.c_str(), std::min((size_t)buff_len, out_str.length()));
85 static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
86 hwc_display_contents_1_t **display_contents) {
87 // XXX: Once we have a GL compositor, just make everything HWC_OVERLAY
88 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
89 Composition *composition =
90 ctx->drm.compositor()->CreateComposition(ctx->importer);
92 ALOGE("Drm composition init failed");
96 for (int i = 0; i < (int)num_displays; ++i) {
97 if (!display_contents[i])
100 int num_layers = display_contents[i]->numHwLayers;
101 int num_planes = composition->GetRemainingLayers(i, num_layers);
103 // XXX: Should go away with atomic modeset
104 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
106 ALOGE("No crtc for display %d", i);
110 if (crtc->requires_modeset())
113 for (int j = std::max(0, num_layers - num_planes); j < num_layers; j++) {
117 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
118 if (layer->compositionType == HWC_FRAMEBUFFER)
119 layer->compositionType = HWC_OVERLAY;
127 static void hwc_set_cleanup(size_t num_displays,
128 hwc_display_contents_1_t **display_contents,
129 Composition *composition) {
130 for (int i = 0; i < (int)num_displays; ++i) {
131 if (!display_contents[i])
134 hwc_display_contents_1_t *dc = display_contents[i];
135 for (size_t j = 0; j < dc->numHwLayers; ++j) {
136 hwc_layer_1_t *layer = &dc->hwLayers[j];
137 if (layer->acquireFenceFd >= 0) {
138 close(layer->acquireFenceFd);
139 layer->acquireFenceFd = -1;
142 if (dc->outbufAcquireFenceFd >= 0) {
143 close(dc->outbufAcquireFenceFd);
144 dc->outbufAcquireFenceFd = -1;
151 static int hwc_add_layer(int display, hwc_context_t *ctx, hwc_layer_1_t *layer,
152 Composition *composition) {
154 int ret = ctx->importer->ImportBuffer(layer->handle, &bo);
156 ALOGE("Failed to import handle to bo %d", ret);
160 ret = composition->AddLayer(display, layer, &bo);
164 int destroy_ret = ctx->importer->ReleaseBuffer(&bo);
166 ALOGE("Failed to destroy buffer %d", destroy_ret);
171 static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
172 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 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
189 ALOGE("No crtc for display %d", i);
190 hwc_set_cleanup(num_displays, display_contents, composition);
194 hwc_display_contents_1_t *dc = display_contents[i];
195 unsigned num_layers = dc->numHwLayers;
196 unsigned num_planes = composition->GetRemainingLayers(i, num_layers);
197 bool use_target = false;
198 // XXX: We don't need to check for modeset required with atomic modeset
199 if (crtc->requires_modeset() || num_layers > num_planes)
202 // XXX: Won't need to worry about FB_TARGET with GL Compositor
203 for (int j = 0; use_target && j < (int)num_layers; ++j) {
204 hwc_layer_1_t *layer = &dc->hwLayers[j];
205 if (layer->compositionType != HWC_FRAMEBUFFER_TARGET)
208 ret = hwc_add_layer(i, ctx, layer, composition);
210 ALOGE("Add layer failed %d", ret);
211 hwc_set_cleanup(num_displays, display_contents, composition);
218 for (int j = 0; num_planes && j < (int)num_layers; ++j) {
219 hwc_layer_1_t *layer = &dc->hwLayers[j];
220 if (layer->compositionType != HWC_OVERLAY)
223 ret = hwc_add_layer(i, ctx, layer, composition);
225 ALOGE("Add layer failed %d", ret);
226 hwc_set_cleanup(num_displays, display_contents, composition);
233 ret = ctx->drm.compositor()->QueueComposition(composition);
235 ALOGE("Failed to queue the composition");
236 hwc_set_cleanup(num_displays, display_contents, composition);
239 hwc_set_cleanup(num_displays, display_contents, NULL);
243 static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
244 int event, int enabled) {
245 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
248 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
249 hwc_drm_display_t *hd = &ctx->displays[display];
250 return hd->vsync_worker.VSyncControl(enabled);
253 static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
255 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
257 uint64_t dpmsValue = 0;
259 case HWC_POWER_MODE_OFF:
260 dpmsValue = DRM_MODE_DPMS_OFF;
263 /* We can't support dozing right now, so go full on */
264 case HWC_POWER_MODE_DOZE:
265 case HWC_POWER_MODE_DOZE_SUSPEND:
266 case HWC_POWER_MODE_NORMAL:
267 dpmsValue = DRM_MODE_DPMS_ON;
270 return ctx->drm.SetDpmsMode(display, dpmsValue);
273 static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
276 case HWC_BACKGROUND_LAYER_SUPPORTED:
277 *value = 0; /* TODO: We should do this */
279 case HWC_VSYNC_PERIOD:
280 ALOGW("Query for deprecated vsync value, returning 60Hz");
281 *value = 1000 * 1000 * 1000 / 60;
283 case HWC_DISPLAY_TYPES_SUPPORTED:
284 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
290 static void hwc_register_procs(struct hwc_composer_device_1 *dev,
291 hwc_procs_t const *procs) {
292 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
296 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
297 iter != ctx->displays.end(); ++iter) {
298 iter->second.vsync_worker.SetProcs(procs);
302 static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
303 int display, uint32_t *configs,
304 size_t *num_configs) {
308 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
309 hwc_drm_display_t *hd = &ctx->displays[display];
310 hd->config_ids.clear();
312 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
314 ALOGE("Failed to get connector for display %d", display);
318 int ret = connector->UpdateModes();
320 ALOGE("Failed to update display modes %d", ret);
324 for (DrmConnector::ModeIter iter = connector->begin_modes();
325 iter != connector->end_modes(); ++iter) {
326 size_t idx = hd->config_ids.size();
327 if (idx == *num_configs)
329 hd->config_ids.push_back(iter->id());
330 configs[idx] = iter->id();
332 *num_configs = hd->config_ids.size();
333 return *num_configs == 0 ? -1 : 0;
336 static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
337 int display, uint32_t config,
338 const uint32_t *attributes,
340 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
341 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
343 ALOGE("Failed to get DrmConnector for display %d", display);
347 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
349 if (iter->id() == config) {
354 if (mode.id() == 0) {
355 ALOGE("Failed to find active mode for display %d", display);
359 uint32_t mm_width = c->mm_width();
360 uint32_t mm_height = c->mm_height();
361 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
362 switch (attributes[i]) {
363 case HWC_DISPLAY_VSYNC_PERIOD:
364 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
366 case HWC_DISPLAY_WIDTH:
367 values[i] = mode.h_display();
369 case HWC_DISPLAY_HEIGHT:
370 values[i] = mode.v_display();
372 case HWC_DISPLAY_DPI_X:
373 /* Dots per 1000 inches */
374 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
376 case HWC_DISPLAY_DPI_Y:
377 /* Dots per 1000 inches */
379 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
386 static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
388 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
389 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
391 ALOGE("Failed to get DrmConnector for display %d", display);
395 DrmMode mode = c->active_mode();
396 hwc_drm_display_t *hd = &ctx->displays[display];
397 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
398 if (hd->config_ids[i] == mode.id())
404 static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
406 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
407 hwc_drm_display_t *hd = &ctx->displays[display];
408 if (index >= (int)hd->config_ids.size()) {
409 ALOGE("Invalid config index %d passed in", index);
414 ctx->drm.SetDisplayActiveMode(display, hd->config_ids[index]);
416 ALOGE("Failed to set config for display %d", display);
423 static int hwc_device_close(struct hw_device_t *dev) {
424 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
430 * TODO: This function sets the active config to the first one in the list. This
431 * should be fixed such that it selects the preferred mode for the display, or
432 * some other, saner, method of choosing the config.
434 static int hwc_set_initial_config(hwc_drm_display_t *hd) {
436 size_t num_configs = 1;
437 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
439 if (ret || !num_configs)
442 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
444 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
451 static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
452 hwc_drm_display_t *hd = &ctx->displays[display];
454 hd->display = display;
456 int ret = hwc_set_initial_config(hd);
458 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
462 ret = hd->vsync_worker.Init(&ctx->drm, display);
464 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
471 static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
473 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
474 c != ctx->drm.end_connectors(); ++c) {
475 ret = hwc_initialize_display(ctx, (*c)->display());
477 ALOGE("Failed to initialize display %d", (*c)->display());
485 static int hwc_device_open(const struct hw_module_t *module, const char *name,
486 struct hw_device_t **dev) {
487 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
488 ALOGE("Invalid module name- %s", name);
492 struct hwc_context_t *ctx = new hwc_context_t();
494 ALOGE("Failed to allocate hwc context");
498 int ret = ctx->drm.Init();
500 ALOGE("Can't initialize Drm object %d", ret);
505 ctx->importer = Importer::CreateInstance(&ctx->drm);
506 if (!ctx->importer) {
507 ALOGE("Failed to create importer instance");
512 ret = hwc_enumerate_displays(ctx);
514 ALOGE("Failed to enumerate displays: %s", strerror(ret));
519 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
520 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
521 ctx->device.common.module = const_cast<hw_module_t *>(module);
522 ctx->device.common.close = hwc_device_close;
524 ctx->device.dump = hwc_dump;
525 ctx->device.prepare = hwc_prepare;
526 ctx->device.set = hwc_set;
527 ctx->device.eventControl = hwc_event_control;
528 ctx->device.setPowerMode = hwc_set_power_mode;
529 ctx->device.query = hwc_query;
530 ctx->device.registerProcs = hwc_register_procs;
531 ctx->device.getDisplayConfigs = hwc_get_display_configs;
532 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
533 ctx->device.getActiveConfig = hwc_get_active_config;
534 ctx->device.setActiveConfig = hwc_set_active_config;
535 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
537 *dev = &ctx->device.common;
543 static struct hw_module_methods_t hwc_module_methods = {
544 open : android::hwc_device_open
547 hwc_module_t HAL_MODULE_INFO_SYM = {
549 tag : HARDWARE_MODULE_TAG,
552 id : HWC_HARDWARE_MODULE_ID,
553 name : "DRM hwcomposer module",
554 author : "The Android Open Source Project",
555 methods : &hwc_module_methods,