Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "hwcomposer-drm" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <errno.h> |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 21 | #include <list> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 22 | #include <sys/param.h> |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 23 | #include <sys/resource.h> |
| 24 | #include <pthread.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 25 | |
| 26 | #include <cutils/log.h> |
| 27 | |
| 28 | #include <xf86drm.h> |
| 29 | #include <xf86drmMode.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 30 | |
| 31 | #include <hardware/hardware.h> |
| 32 | #include <hardware/hwcomposer.h> |
| 33 | |
Lauri Peltonen | 64717b2 | 2015-02-04 16:55:31 +0200 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 35 | #include <sync/sync.h> |
Sean Paul | f1dc191 | 2015-01-24 01:34:31 -0500 | [diff] [blame] | 36 | #include <sw_sync.h> |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 37 | |
Sean Paul | cd36a9e | 2015-01-22 18:01:18 -0500 | [diff] [blame] | 38 | #include "drm_hwcomposer.h" |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 39 | |
| 40 | #define ARRAY_SIZE(arr) (int)(sizeof(arr) / sizeof((arr)[0])) |
| 41 | |
| 42 | #define HWCOMPOSER_DRM_DEVICE "/dev/dri/card0" |
| 43 | #define MAX_NUM_DISPLAYS 3 |
| 44 | #define UM_PER_INCH 25400 |
| 45 | |
| 46 | static const uint32_t panel_types[] = { |
| 47 | DRM_MODE_CONNECTOR_LVDS, |
| 48 | DRM_MODE_CONNECTOR_eDP, |
| 49 | DRM_MODE_CONNECTOR_DSI, |
| 50 | }; |
| 51 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 52 | struct hwc_worker { |
| 53 | pthread_t thread; |
| 54 | pthread_mutex_t lock; |
| 55 | pthread_cond_t cond; |
| 56 | bool exit; |
| 57 | }; |
| 58 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 59 | struct hwc_drm_display { |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 60 | struct hwc_context_t *ctx; |
| 61 | int display; |
| 62 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 63 | uint32_t connector_id; |
| 64 | |
| 65 | drmModeModeInfoPtr configs; |
| 66 | uint32_t num_configs; |
| 67 | |
Sean Paul | fa406a1 | 2015-02-04 10:05:44 -0800 | [diff] [blame] | 68 | drmModeModeInfo active_mode; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 69 | uint32_t active_crtc; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 70 | int active_pipe; |
Sean Paul | efb20cb | 2015-02-04 09:29:15 -0800 | [diff] [blame] | 71 | bool initial_modeset_required; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 72 | |
| 73 | struct hwc_worker set_worker; |
| 74 | |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 75 | std::list<struct hwc_drm_bo> buf_queue; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 76 | struct hwc_drm_bo front; |
Sean Paul | f1dc191 | 2015-01-24 01:34:31 -0500 | [diff] [blame] | 77 | |
| 78 | int timeline_fd; |
| 79 | unsigned timeline_next; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 80 | |
| 81 | struct hwc_worker vsync_worker; |
| 82 | bool enable_vsync_events; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | struct hwc_context_t { |
| 86 | hwc_composer_device_1_t device; |
| 87 | |
| 88 | int fd; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 89 | |
| 90 | hwc_procs_t const *procs; |
Sean Paul | cd36a9e | 2015-01-22 18:01:18 -0500 | [diff] [blame] | 91 | struct hwc_import_context *import_ctx; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 92 | |
| 93 | struct hwc_drm_display displays[MAX_NUM_DISPLAYS]; |
| 94 | int num_displays; |
| 95 | }; |
| 96 | |
| 97 | static int hwc_get_drm_display(struct hwc_context_t *ctx, int display, |
| 98 | struct hwc_drm_display **hd) |
| 99 | { |
| 100 | if (display >= MAX_NUM_DISPLAYS) { |
| 101 | ALOGE("Requested display is out-of-bounds %d %d", display, |
| 102 | MAX_NUM_DISPLAYS); |
| 103 | return -EINVAL; |
| 104 | } |
| 105 | *hd = &ctx->displays[display]; |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int hwc_prepare_layer(hwc_layer_1_t *layer) |
| 110 | { |
| 111 | /* TODO: We can't handle background right now, defer to sufaceFlinger */ |
| 112 | if (layer->compositionType == HWC_BACKGROUND) { |
| 113 | layer->compositionType = HWC_FRAMEBUFFER; |
| 114 | ALOGV("Can't handle background layers yet"); |
| 115 | |
| 116 | /* TODO: Support sideband compositions */ |
| 117 | } else if (layer->compositionType == HWC_SIDEBAND) { |
| 118 | layer->compositionType = HWC_FRAMEBUFFER; |
| 119 | ALOGV("Can't handle sideband content yet"); |
| 120 | } |
| 121 | |
| 122 | layer->hints = 0; |
| 123 | |
| 124 | /* TODO: Handle cursor by setting compositionType=HWC_CURSOR_OVERLAY */ |
| 125 | if (layer->flags & HWC_IS_CURSOR_LAYER) { |
| 126 | ALOGV("Can't handle async cursors yet"); |
| 127 | } |
| 128 | |
| 129 | /* TODO: Handle transformations */ |
| 130 | if (layer->transform) { |
| 131 | ALOGV("Can't handle transformations yet"); |
| 132 | } |
| 133 | |
| 134 | /* TODO: Handle blending & plane alpha*/ |
| 135 | if (layer->blending == HWC_BLENDING_PREMULT || |
| 136 | layer->blending == HWC_BLENDING_COVERAGE) { |
| 137 | ALOGV("Can't handle blending yet"); |
| 138 | } |
| 139 | |
| 140 | /* TODO: Handle cropping & scaling */ |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int hwc_prepare(hwc_composer_device_1_t */* dev */, size_t num_displays, |
| 146 | hwc_display_contents_1_t** display_contents) |
| 147 | { |
| 148 | int ret = 0, i, j; |
| 149 | |
| 150 | /* TODO: Check flags for HWC_GEOMETRY_CHANGED */ |
| 151 | |
| 152 | for (i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; i++) { |
Sean Paul | dffca95 | 2015-02-04 10:19:55 -0800 | [diff] [blame] | 153 | |
| 154 | if (!display_contents[i]) |
| 155 | continue; |
| 156 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 157 | for (j = 0; j < (int)display_contents[i]->numHwLayers; j++) { |
| 158 | ret = hwc_prepare_layer( |
| 159 | &display_contents[i]->hwLayers[j]); |
| 160 | if (ret) { |
| 161 | ALOGE("Failed to prepare layer %d:%d", j, i); |
| 162 | return ret; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 170 | static bool hwc_mode_is_equal(drmModeModeInfoPtr a, drmModeModeInfoPtr b) |
| 171 | { |
| 172 | return a->clock == b->clock && |
| 173 | a->hdisplay == b->hdisplay && |
| 174 | a->hsync_start == b->hsync_start && |
| 175 | a->hsync_end == b->hsync_end && |
| 176 | a->htotal == b->htotal && |
| 177 | a->hskew == b->hskew && |
| 178 | a->vdisplay == b->vdisplay && |
| 179 | a->vsync_start == b->vsync_start && |
| 180 | a->vsync_end == b->vsync_end && |
| 181 | a->vtotal == b->vtotal && |
| 182 | a->vscan == b->vscan && |
| 183 | a->vrefresh == b->vrefresh && |
| 184 | a->flags == b->flags && |
| 185 | a->type == b->type && |
| 186 | !strcmp(a->name, b->name); |
| 187 | } |
| 188 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 189 | static int hwc_modeset_required(struct hwc_drm_display *hd, |
| 190 | bool *modeset_required) |
| 191 | { |
| 192 | drmModeCrtcPtr crtc; |
| 193 | drmModeModeInfoPtr m; |
| 194 | |
Sean Paul | efb20cb | 2015-02-04 09:29:15 -0800 | [diff] [blame] | 195 | if (hd->initial_modeset_required) { |
| 196 | *modeset_required = true; |
| 197 | hd->initial_modeset_required = false; |
| 198 | return 0; |
| 199 | } |
| 200 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 201 | crtc = drmModeGetCrtc(hd->ctx->fd, hd->active_crtc); |
| 202 | if (!crtc) { |
| 203 | ALOGE("Failed to get crtc for display %d", hd->display); |
| 204 | return -ENODEV; |
| 205 | } |
| 206 | |
Sean Paul | fa406a1 | 2015-02-04 10:05:44 -0800 | [diff] [blame] | 207 | m = &hd->active_mode; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 208 | |
| 209 | /* Do a modeset if we haven't done one, or the mode has changed */ |
| 210 | if (!crtc->mode_valid || !hwc_mode_is_equal(m, &crtc->mode)) |
| 211 | *modeset_required = true; |
| 212 | else |
| 213 | *modeset_required = false; |
| 214 | |
| 215 | drmModeFreeCrtc(crtc); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static void hwc_flip_handler(int /* fd */, unsigned int /* sequence */, |
| 221 | unsigned int /* tv_sec */, unsigned int /* tv_usec */, |
| 222 | void */* user_data */) |
| 223 | { |
| 224 | } |
| 225 | |
Sean Paul | 9b1bb84 | 2015-01-23 01:11:58 -0500 | [diff] [blame] | 226 | static int hwc_flip(struct hwc_drm_display *hd, struct hwc_drm_bo *buf) |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 227 | { |
| 228 | fd_set fds; |
| 229 | drmEventContext event_context; |
| 230 | int ret; |
| 231 | bool modeset_required; |
| 232 | |
| 233 | ret = hwc_modeset_required(hd, &modeset_required); |
| 234 | if (ret) { |
| 235 | ALOGE("Failed to determine if modeset is required %d", ret); |
| 236 | return ret; |
| 237 | } |
| 238 | if (modeset_required) { |
Sean Paul | 9b1bb84 | 2015-01-23 01:11:58 -0500 | [diff] [blame] | 239 | ret = drmModeSetCrtc(hd->ctx->fd, hd->active_crtc, buf->fb_id, |
| 240 | 0, 0, &hd->connector_id, 1, |
Sean Paul | fa406a1 | 2015-02-04 10:05:44 -0800 | [diff] [blame] | 241 | &hd->active_mode); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 242 | if (ret) { |
| 243 | ALOGE("Modeset failed for crtc %d", |
| 244 | hd->active_crtc); |
| 245 | return ret; |
| 246 | } |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | FD_ZERO(&fds); |
| 251 | FD_SET(hd->ctx->fd, &fds); |
| 252 | |
| 253 | event_context.version = DRM_EVENT_CONTEXT_VERSION; |
| 254 | event_context.page_flip_handler = hwc_flip_handler; |
| 255 | |
Sean Paul | 9b1bb84 | 2015-01-23 01:11:58 -0500 | [diff] [blame] | 256 | ret = drmModePageFlip(hd->ctx->fd, hd->active_crtc, buf->fb_id, |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 257 | DRM_MODE_PAGE_FLIP_EVENT, hd); |
| 258 | if (ret) { |
| 259 | ALOGE("Failed to flip buffer for crtc %d", |
| 260 | hd->active_crtc); |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | do { |
| 265 | ret = select(hd->ctx->fd + 1, &fds, NULL, NULL, NULL); |
| 266 | } while (ret == -1 && errno == EINTR); |
| 267 | |
| 268 | if (ret != 1) { |
| 269 | ALOGE("Failed waiting for flip to complete\n"); |
| 270 | return -EINVAL; |
| 271 | } |
| 272 | drmHandleEvent(hd->ctx->fd, &event_context); |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 277 | static int hwc_wait_and_set(struct hwc_drm_display *hd, |
| 278 | struct hwc_drm_bo *buf) |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 279 | { |
Sean Paul | aea15c2 | 2015-02-09 02:24:11 -0500 | [diff] [blame] | 280 | struct drm_gem_close args; |
| 281 | int ret, i; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 282 | |
Lauri Peltonen | 132e010 | 2015-02-12 13:54:33 +0200 | [diff] [blame] | 283 | if (buf->acquire_fence_fd >= 0) { |
| 284 | ret = sync_wait(buf->acquire_fence_fd, -1); |
| 285 | close(buf->acquire_fence_fd); |
| 286 | buf->acquire_fence_fd = -1; |
| 287 | if (ret) { |
| 288 | ALOGE("Failed to wait for acquire %d", ret); |
| 289 | return ret; |
| 290 | } |
| 291 | } |
| 292 | |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 293 | ret = hwc_flip(hd, buf); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 294 | if (ret) { |
| 295 | ALOGE("Failed to perform flip\n"); |
| 296 | return ret; |
| 297 | } |
| 298 | |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 299 | if (hwc_import_bo_release(hd->ctx->fd, hd->ctx->import_ctx, &hd->front)) { |
| 300 | memset(&args, 0, sizeof(args)); |
| 301 | for (i = 0; i < ARRAY_SIZE(hd->front.gem_handles); i++) { |
| 302 | if (!hd->front.gem_handles[i]) |
| 303 | continue; |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 304 | |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 305 | /* check for duplicate handle in buf_queue */ |
| 306 | bool found; |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 307 | |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 308 | ret = pthread_mutex_lock(&hd->set_worker.lock); |
| 309 | if (ret) { |
| 310 | ALOGE("Failed to lock set lock in wait_and_set() %d", ret); |
| 311 | continue; |
| 312 | } |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 313 | |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 314 | found = false; |
| 315 | for (std::list<struct hwc_drm_bo>::iterator bi = hd->buf_queue.begin(); |
| 316 | bi != hd->buf_queue.end(); |
| 317 | ++bi) |
| 318 | for (int j = 0; j < ARRAY_SIZE(bi->gem_handles); j++) |
| 319 | if (hd->front.gem_handles[i] == bi->gem_handles[j] ) |
| 320 | found = true; |
| 321 | |
| 322 | for (int j = 0; j < ARRAY_SIZE(buf->gem_handles); j++) |
| 323 | if (hd->front.gem_handles[i] == buf->gem_handles[j]) |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 324 | found = true; |
| 325 | |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 326 | if (!found) { |
| 327 | args.handle = hd->front.gem_handles[i]; |
| 328 | drmIoctl(hd->ctx->fd, DRM_IOCTL_GEM_CLOSE, &args); |
| 329 | } |
| 330 | if (pthread_mutex_unlock(&hd->set_worker.lock)) |
| 331 | ALOGE("Failed to unlock set lock in wait_and_set() %d", ret); |
Allen Martin | 91fa3f8 | 2015-02-22 04:48:14 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 335 | hd->front = *buf; |
| 336 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | static void *hwc_set_worker(void *arg) |
| 341 | { |
| 342 | struct hwc_drm_display *hd = (struct hwc_drm_display *)arg; |
| 343 | int ret; |
| 344 | |
| 345 | setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY); |
| 346 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 347 | do { |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 348 | struct hwc_drm_bo buf; |
| 349 | |
| 350 | ret = pthread_mutex_lock(&hd->set_worker.lock); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 351 | if (ret) { |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 352 | ALOGE("Failed to lock set lock %d", ret); |
| 353 | return NULL; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 356 | if (hd->set_worker.exit) |
| 357 | goto out; |
| 358 | |
| 359 | if (hd->buf_queue.empty()) { |
| 360 | ret = pthread_cond_wait(&hd->set_worker.cond, |
| 361 | &hd->set_worker.lock); |
| 362 | if (ret) { |
| 363 | ALOGE("Failed to wait on condition %d", ret); |
| 364 | goto out; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | buf = hd->buf_queue.front(); |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 369 | hd->buf_queue.pop_front(); |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 370 | |
| 371 | ret = pthread_mutex_unlock(&hd->set_worker.lock); |
| 372 | if (ret) { |
| 373 | ALOGE("Failed to unlock set lock %d", ret); |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | ret = hwc_wait_and_set(hd, &buf); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 378 | if (ret) |
| 379 | ALOGE("Failed to wait and set %d", ret); |
Sean Paul | f1dc191 | 2015-01-24 01:34:31 -0500 | [diff] [blame] | 380 | |
| 381 | ret = sw_sync_timeline_inc(hd->timeline_fd, 1); |
| 382 | if (ret) |
| 383 | ALOGE("Failed to increment sync timeline %d", ret); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 384 | } while (true); |
| 385 | |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 386 | out: |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 387 | ret = pthread_mutex_unlock(&hd->set_worker.lock); |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 388 | if (ret) |
| 389 | ALOGE("Failed to unlock set lock while exiting %d", ret); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 390 | |
| 391 | return NULL; |
| 392 | } |
| 393 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 394 | static int hwc_set_display(hwc_context_t *ctx, int display, |
| 395 | hwc_display_contents_1_t* display_contents) |
| 396 | { |
| 397 | struct hwc_drm_display *hd = NULL; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 398 | hwc_layer_1_t *layer = NULL; |
Sean Paul | 9b1bb84 | 2015-01-23 01:11:58 -0500 | [diff] [blame] | 399 | struct hwc_drm_bo buf; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 400 | int ret, i; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 401 | uint32_t fb_id; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 402 | |
Sean Paul | 9b1bb84 | 2015-01-23 01:11:58 -0500 | [diff] [blame] | 403 | memset(&buf, 0, sizeof(buf)); |
| 404 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 405 | ret = hwc_get_drm_display(ctx, display, &hd); |
| 406 | if (ret) |
Lauri Peltonen | 132e010 | 2015-02-12 13:54:33 +0200 | [diff] [blame] | 407 | goto out; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 408 | |
| 409 | if (!hd->active_crtc) { |
| 410 | ALOGE("There is no active crtc for display %d", display); |
Lauri Peltonen | 132e010 | 2015-02-12 13:54:33 +0200 | [diff] [blame] | 411 | ret = -ENOENT; |
| 412 | goto out; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* |
| 416 | * TODO: We can only support one hw layer atm, so choose either the |
| 417 | * first one or the framebuffer target. |
| 418 | */ |
| 419 | if (!display_contents->numHwLayers) { |
| 420 | return 0; |
| 421 | } else if (display_contents->numHwLayers == 1) { |
| 422 | layer = &display_contents->hwLayers[0]; |
| 423 | } else { |
| 424 | for (i = 0; i < (int)display_contents->numHwLayers; i++) { |
| 425 | layer = &display_contents->hwLayers[i]; |
| 426 | if (layer->compositionType == HWC_FRAMEBUFFER_TARGET) |
| 427 | break; |
| 428 | } |
| 429 | if (i == (int)display_contents->numHwLayers) { |
| 430 | ALOGE("Could not find a suitable layer for display %d", |
| 431 | display); |
| 432 | } |
| 433 | } |
| 434 | |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 435 | |
| 436 | ret = pthread_mutex_lock(&hd->set_worker.lock); |
| 437 | if (ret) { |
| 438 | ALOGE("Failed to lock set lock in set() %d", ret); |
| 439 | goto out; |
| 440 | } |
| 441 | |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 442 | ret = hwc_import_bo_create(ctx->fd, ctx->import_ctx, layer->handle, |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 443 | &buf); |
| 444 | if (ret) { |
| 445 | ALOGE("Failed to import handle to drm bo %d", ret); |
Lauri Peltonen | 132e010 | 2015-02-12 13:54:33 +0200 | [diff] [blame] | 446 | goto out; |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 447 | } |
| 448 | buf.acquire_fence_fd = layer->acquireFenceFd; |
Lauri Peltonen | 132e010 | 2015-02-12 13:54:33 +0200 | [diff] [blame] | 449 | layer->acquireFenceFd = -1; |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 450 | |
Sean Paul | f1dc191 | 2015-01-24 01:34:31 -0500 | [diff] [blame] | 451 | /* |
| 452 | * TODO: Retire and release can use the same sync point here b/c hwc is |
| 453 | * restricted to one layer. Once that is no longer true, this will need |
| 454 | * to change |
| 455 | */ |
| 456 | hd->timeline_next++; |
| 457 | display_contents->retireFenceFd = sw_sync_fence_create(hd->timeline_fd, |
| 458 | "drm_hwc_retire", hd->timeline_next); |
| 459 | layer->releaseFenceFd = sw_sync_fence_create(hd->timeline_fd, |
| 460 | "drm_hwc_release", hd->timeline_next); |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 461 | hd->buf_queue.push_back(buf); |
Sean Paul | 9b1bb84 | 2015-01-23 01:11:58 -0500 | [diff] [blame] | 462 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 463 | ret = pthread_cond_signal(&hd->set_worker.cond); |
Lauri Peltonen | 132e010 | 2015-02-12 13:54:33 +0200 | [diff] [blame] | 464 | if (ret) |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 465 | ALOGE("Failed to signal set worker %d", ret); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 466 | |
Lauri Peltonen | 132e010 | 2015-02-12 13:54:33 +0200 | [diff] [blame] | 467 | if (pthread_mutex_unlock(&hd->set_worker.lock)) |
| 468 | ALOGE("Failed to unlock set lock in set()"); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 469 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 470 | out: |
Lauri Peltonen | 132e010 | 2015-02-12 13:54:33 +0200 | [diff] [blame] | 471 | /* Close input fences. */ |
| 472 | for (i = 0; i < (int)display_contents->numHwLayers; i++) { |
| 473 | layer = &display_contents->hwLayers[i]; |
| 474 | if (layer->acquireFenceFd >= 0) { |
| 475 | close(layer->acquireFenceFd); |
| 476 | layer->acquireFenceFd = -1; |
| 477 | } |
| 478 | } |
| 479 | if (display_contents->outbufAcquireFenceFd >= 0) { |
| 480 | close(display_contents->outbufAcquireFenceFd); |
| 481 | display_contents->outbufAcquireFenceFd = -1; |
| 482 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 483 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays, |
| 488 | hwc_display_contents_1_t** display_contents) |
| 489 | { |
| 490 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 491 | int ret = 0, i; |
| 492 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 493 | for (i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; i++) { |
Sean Paul | dffca95 | 2015-02-04 10:19:55 -0800 | [diff] [blame] | 494 | if (display_contents[i]) |
| 495 | ret = hwc_set_display(ctx, i, display_contents[i]); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 501 | static int hwc_wait_for_vsync(struct hwc_drm_display *hd) |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 502 | { |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 503 | drmVBlank vblank; |
| 504 | int ret; |
| 505 | uint32_t high_crtc; |
| 506 | int64_t timestamp; |
| 507 | |
| 508 | if (hd->active_pipe == -1) |
| 509 | return -EINVAL; |
| 510 | |
| 511 | memset(&vblank, 0, sizeof(vblank)); |
| 512 | |
| 513 | high_crtc = (hd->active_pipe << DRM_VBLANK_HIGH_CRTC_SHIFT); |
| 514 | vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE | |
| 515 | (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK)); |
| 516 | vblank.request.sequence = 1; |
| 517 | |
| 518 | ret = drmWaitVBlank(hd->ctx->fd, &vblank); |
| 519 | if (ret) { |
| 520 | ALOGE("Failed to wait for vblank %d", ret); |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | if (hd->ctx->procs->vsync) { |
| 525 | timestamp = vblank.reply.tval_sec * 1000 * 1000 * 1000 + |
| 526 | vblank.reply.tval_usec * 1000; |
| 527 | hd->ctx->procs->vsync(hd->ctx->procs, hd->display, timestamp); |
| 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static void *hwc_vsync_worker(void *arg) |
| 534 | { |
| 535 | struct hwc_drm_display *hd = (struct hwc_drm_display *)arg; |
| 536 | struct hwc_worker *w = &hd->vsync_worker; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 537 | int ret; |
| 538 | |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 539 | setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY); |
| 540 | |
| 541 | do { |
| 542 | ret = pthread_mutex_lock(&w->lock); |
| 543 | if (ret) { |
| 544 | ALOGE("Failed to lock vsync lock %d", ret); |
| 545 | return NULL; |
| 546 | } |
| 547 | |
| 548 | if (hd->active_pipe == -1) { |
| 549 | ALOGE("Pipe is no longer active, disable events"); |
| 550 | hd->enable_vsync_events = false; |
| 551 | } |
| 552 | |
| 553 | if (!hd->enable_vsync_events) { |
| 554 | ret = pthread_cond_wait(&w->cond, &w->lock); |
| 555 | if (ret) { |
| 556 | ALOGE("Failed to wait on vsync cond %d", ret); |
| 557 | break; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if (w->exit) |
| 562 | break; |
| 563 | |
| 564 | ret = pthread_mutex_unlock(&w->lock); |
| 565 | if (ret) { |
| 566 | ALOGE("Failed to unlock vsync lock %d", ret); |
| 567 | return NULL; |
| 568 | } |
| 569 | |
| 570 | if (!hd->enable_vsync_events) |
| 571 | continue; |
| 572 | |
| 573 | ret = hwc_wait_for_vsync(hd); |
| 574 | if (ret) |
| 575 | ALOGE("Failed to wait for vsync %d", ret); |
| 576 | |
| 577 | } while (true); |
| 578 | |
| 579 | out: |
| 580 | ret = pthread_mutex_unlock(&hd->set_worker.lock); |
| 581 | if (ret) |
| 582 | ALOGE("Failed to unlock set lock while exiting %d", ret); |
| 583 | |
| 584 | return NULL; |
| 585 | } |
| 586 | |
| 587 | static int hwc_event_control(struct hwc_composer_device_1* dev, int display, |
| 588 | int event, int enabled) |
| 589 | { |
| 590 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 591 | struct hwc_drm_display *hd = NULL; |
| 592 | int ret; |
| 593 | |
| 594 | ret = hwc_get_drm_display(ctx, display, &hd); |
| 595 | if (ret) |
| 596 | return ret; |
| 597 | |
| 598 | if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1)) |
| 599 | return -EINVAL; |
| 600 | |
| 601 | if (hd->active_pipe == -1) { |
| 602 | ALOGD("Can't service events for display %d, no pipe", display); |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | |
| 606 | ret = pthread_mutex_lock(&hd->vsync_worker.lock); |
| 607 | if (ret) { |
| 608 | ALOGE("Failed to lock vsync lock %d", ret); |
| 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | hd->enable_vsync_events = !!enabled; |
| 613 | |
| 614 | ret = pthread_cond_signal(&hd->vsync_worker.cond); |
| 615 | if (ret) { |
| 616 | ALOGE("Failed to signal vsync thread %d", ret); |
| 617 | goto out; |
| 618 | } |
| 619 | |
| 620 | ret = pthread_mutex_unlock(&hd->vsync_worker.lock); |
| 621 | if (ret) { |
| 622 | ALOGE("Failed to unlock vsync lock %d", ret); |
| 623 | return ret; |
| 624 | } |
| 625 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 626 | return 0; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 627 | |
| 628 | out: |
| 629 | if (pthread_mutex_unlock(&hd->vsync_worker.lock)) |
| 630 | ALOGE("Failed to unlock vsync worker in error path"); |
| 631 | |
| 632 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static int hwc_set_power_mode(struct hwc_composer_device_1* dev, int display, |
| 636 | int mode) |
| 637 | { |
| 638 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 639 | struct hwc_drm_display *hd = NULL; |
| 640 | drmModeConnectorPtr c; |
| 641 | int ret, i; |
| 642 | uint32_t dpms_prop = 0; |
| 643 | uint64_t dpms_value = 0; |
| 644 | |
| 645 | ret = hwc_get_drm_display(ctx, display, &hd); |
| 646 | if (ret) |
| 647 | return ret; |
| 648 | |
| 649 | c = drmModeGetConnector(ctx->fd, hd->connector_id); |
| 650 | if (!c) { |
| 651 | ALOGE("Failed to get connector %d", display); |
| 652 | return -ENODEV; |
| 653 | } |
| 654 | |
| 655 | for (i = 0; !dpms_prop && i < c->count_props; i++) { |
| 656 | drmModePropertyPtr p; |
| 657 | |
| 658 | p = drmModeGetProperty(ctx->fd, c->props[i]); |
| 659 | if (!p) |
| 660 | continue; |
| 661 | |
| 662 | if (!strcmp(p->name, "DPMS")) |
| 663 | dpms_prop = c->props[i]; |
| 664 | |
| 665 | drmModeFreeProperty(p); |
| 666 | } |
| 667 | if (!dpms_prop) { |
| 668 | ALOGE("Failed to get DPMS property from display %d", display); |
| 669 | ret = -ENOENT; |
| 670 | goto out; |
| 671 | } |
| 672 | |
| 673 | switch(mode) { |
| 674 | case HWC_POWER_MODE_OFF: |
| 675 | dpms_value = DRM_MODE_DPMS_OFF; |
| 676 | break; |
| 677 | |
| 678 | /* We can't support dozing right now, so go full on */ |
| 679 | case HWC_POWER_MODE_DOZE: |
| 680 | case HWC_POWER_MODE_DOZE_SUSPEND: |
| 681 | case HWC_POWER_MODE_NORMAL: |
| 682 | dpms_value = DRM_MODE_DPMS_ON; |
| 683 | break; |
| 684 | }; |
| 685 | |
| 686 | ret = drmModeConnectorSetProperty(ctx->fd, c->connector_id, |
| 687 | dpms_prop, dpms_value); |
| 688 | if (ret) { |
| 689 | ALOGE("Failed to set DPMS property for display %d", display); |
| 690 | goto out; |
| 691 | } |
| 692 | |
| 693 | out: |
| 694 | drmModeFreeConnector(c); |
| 695 | return ret; |
| 696 | } |
| 697 | |
| 698 | static int hwc_query(struct hwc_composer_device_1 */* dev */, int what, |
| 699 | int *value) |
| 700 | { |
| 701 | switch(what) { |
| 702 | case HWC_BACKGROUND_LAYER_SUPPORTED: |
| 703 | *value = 0; /* TODO: We should do this */ |
| 704 | break; |
| 705 | case HWC_VSYNC_PERIOD: |
| 706 | ALOGW("Query for deprecated vsync value, returning 60Hz"); |
| 707 | *value = 1000 * 1000 * 1000 / 60; |
| 708 | break; |
| 709 | case HWC_DISPLAY_TYPES_SUPPORTED: |
| 710 | *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL; |
| 711 | break; |
| 712 | } |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | static void hwc_register_procs(struct hwc_composer_device_1* dev, |
| 717 | hwc_procs_t const* procs) |
| 718 | { |
| 719 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 720 | |
| 721 | ctx->procs = procs; |
| 722 | } |
| 723 | |
| 724 | static int hwc_get_display_configs(struct hwc_composer_device_1* dev, |
| 725 | int display, uint32_t* configs, size_t* numConfigs) |
| 726 | { |
| 727 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 728 | struct hwc_drm_display *hd = NULL; |
| 729 | drmModeConnectorPtr c; |
| 730 | int ret = 0, i; |
| 731 | |
| 732 | if (!*numConfigs) |
| 733 | return 0; |
| 734 | |
| 735 | ret = hwc_get_drm_display(ctx, display, &hd); |
| 736 | if (ret) |
| 737 | return ret; |
| 738 | |
| 739 | c = drmModeGetConnector(ctx->fd, hd->connector_id); |
| 740 | if (!c) { |
| 741 | ALOGE("Failed to get connector %d", display); |
| 742 | return -ENODEV; |
| 743 | } |
| 744 | |
Sean Paul | a4283c5 | 2015-02-04 10:08:00 -0800 | [diff] [blame] | 745 | if (hd->configs) { |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 746 | free(hd->configs); |
Sean Paul | a4283c5 | 2015-02-04 10:08:00 -0800 | [diff] [blame] | 747 | hd->configs = NULL; |
| 748 | } |
| 749 | |
| 750 | if (c->connection == DRM_MODE_DISCONNECTED) { |
| 751 | ret = -ENODEV; |
| 752 | goto out; |
| 753 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 754 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 755 | hd->configs = (drmModeModeInfoPtr)calloc(c->count_modes, |
| 756 | sizeof(*hd->configs)); |
| 757 | if (!hd->configs) { |
| 758 | ALOGE("Failed to allocate config list for display %d", display); |
| 759 | ret = -ENOMEM; |
| 760 | hd->num_configs = 0; |
| 761 | goto out; |
| 762 | } |
| 763 | |
| 764 | for (i = 0; i < c->count_modes; i++) { |
| 765 | drmModeModeInfoPtr m = &hd->configs[i]; |
| 766 | |
| 767 | memcpy(m, &c->modes[i], sizeof(*m)); |
| 768 | |
| 769 | if (i < (int)*numConfigs) |
| 770 | configs[i] = i; |
| 771 | } |
| 772 | |
| 773 | hd->num_configs = c->count_modes; |
| 774 | *numConfigs = MIN(c->count_modes, *numConfigs); |
| 775 | |
| 776 | out: |
| 777 | drmModeFreeConnector(c); |
| 778 | return ret; |
| 779 | } |
| 780 | |
| 781 | static int hwc_check_config_valid(struct hwc_context_t *ctx, |
| 782 | drmModeConnectorPtr connector, int display, |
| 783 | int config_idx) |
| 784 | { |
| 785 | struct hwc_drm_display *hd = NULL; |
| 786 | drmModeModeInfoPtr m = NULL; |
| 787 | int ret = 0, i; |
| 788 | |
| 789 | ret = hwc_get_drm_display(ctx, display, &hd); |
| 790 | if (ret) |
| 791 | return ret; |
| 792 | |
| 793 | /* Make sure the requested config is still valid for the display */ |
| 794 | for (i = 0; i < connector->count_modes; i++) { |
| 795 | if (hwc_mode_is_equal(&connector->modes[i], |
| 796 | &hd->configs[config_idx])) { |
| 797 | m = &hd->configs[config_idx]; |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | if (!m) |
| 802 | return -ENOENT; |
| 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | static int hwc_get_display_attributes(struct hwc_composer_device_1* dev, |
| 808 | int display, uint32_t config, const uint32_t* attributes, |
| 809 | int32_t* values) |
| 810 | { |
| 811 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 812 | struct hwc_drm_display *hd = NULL; |
| 813 | drmModeConnectorPtr c; |
| 814 | drmModeModeInfoPtr m; |
| 815 | int ret, i; |
| 816 | |
| 817 | ret = hwc_get_drm_display(ctx, display, &hd); |
| 818 | if (ret) |
| 819 | return ret; |
| 820 | |
| 821 | if (config >= hd->num_configs) { |
| 822 | ALOGE("Requested config is out-of-bounds %d %d", config, |
| 823 | hd->num_configs); |
| 824 | return -EINVAL; |
| 825 | } |
| 826 | |
| 827 | c = drmModeGetConnector(ctx->fd, hd->connector_id); |
| 828 | if (!c) { |
| 829 | ALOGE("Failed to get connector %d", display); |
| 830 | return -ENODEV; |
| 831 | } |
| 832 | |
| 833 | ret = hwc_check_config_valid(ctx, c, display, (int)config); |
| 834 | if (ret) { |
| 835 | ALOGE("Provided config is no longer valid %u", config); |
| 836 | goto out; |
| 837 | } |
| 838 | |
| 839 | m = &hd->configs[config]; |
| 840 | for (i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; i++) { |
| 841 | switch(attributes[i]) { |
| 842 | case HWC_DISPLAY_VSYNC_PERIOD: |
| 843 | values[i] = 1000 * 1000 * 1000 / m->vrefresh; |
| 844 | break; |
| 845 | case HWC_DISPLAY_WIDTH: |
| 846 | values[i] = m->hdisplay; |
| 847 | break; |
| 848 | case HWC_DISPLAY_HEIGHT: |
| 849 | values[i] = m->vdisplay; |
| 850 | break; |
| 851 | case HWC_DISPLAY_DPI_X: |
| 852 | /* Dots per 1000 inches */ |
| 853 | values[i] = c->mmWidth ? |
| 854 | (m->hdisplay * UM_PER_INCH) / c->mmWidth : 0; |
| 855 | break; |
| 856 | case HWC_DISPLAY_DPI_Y: |
| 857 | /* Dots per 1000 inches */ |
| 858 | values[i] = c->mmHeight ? |
| 859 | (m->vdisplay * UM_PER_INCH) / c->mmHeight : 0; |
| 860 | break; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | out: |
| 865 | drmModeFreeConnector(c); |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | static int hwc_get_active_config(struct hwc_composer_device_1* dev, int display) |
| 870 | { |
| 871 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 872 | struct hwc_drm_display *hd = NULL; |
Sean Paul | fa406a1 | 2015-02-04 10:05:44 -0800 | [diff] [blame] | 873 | int ret, i, index = -1; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 874 | |
| 875 | ret = hwc_get_drm_display(ctx, display, &hd); |
| 876 | if (ret) |
| 877 | return ret; |
| 878 | |
Sean Paul | fa406a1 | 2015-02-04 10:05:44 -0800 | [diff] [blame] | 879 | /* Find the current mode in the config list */ |
| 880 | for (i = 0; i < (int)hd->num_configs; i++) { |
| 881 | if (hwc_mode_is_equal(&hd->configs[i], &hd->active_mode)) { |
| 882 | index = i; |
| 883 | break; |
| 884 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 885 | } |
| 886 | |
Sean Paul | fa406a1 | 2015-02-04 10:05:44 -0800 | [diff] [blame] | 887 | return index; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | static bool hwc_crtc_is_bound(struct hwc_context_t *ctx, uint32_t crtc_id) |
| 891 | { |
| 892 | int i; |
| 893 | |
| 894 | for (i = 0; i < MAX_NUM_DISPLAYS; i++) { |
| 895 | if (ctx->displays[i].active_crtc == crtc_id) |
| 896 | return true; |
| 897 | } |
| 898 | return false; |
| 899 | } |
| 900 | |
| 901 | static int hwc_try_encoder(struct hwc_context_t *ctx, drmModeResPtr r, |
| 902 | uint32_t encoder_id, uint32_t *crtc_id) |
| 903 | { |
| 904 | drmModeEncoderPtr e; |
| 905 | int ret, i; |
| 906 | |
| 907 | e = drmModeGetEncoder(ctx->fd, encoder_id); |
| 908 | if (!e) { |
| 909 | ALOGE("Failed to get encoder for connector %d", encoder_id); |
| 910 | return -ENODEV; |
| 911 | } |
| 912 | |
| 913 | /* First try to use the currently-bound crtc */ |
| 914 | if (e->crtc_id) { |
| 915 | if (!hwc_crtc_is_bound(ctx, e->crtc_id)) { |
| 916 | *crtc_id = e->crtc_id; |
| 917 | ret = 0; |
| 918 | goto out; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | /* Try to find a possible crtc which will work */ |
| 923 | for (i = 0; i < r->count_crtcs; i++) { |
| 924 | if (!(e->possible_crtcs & (1 << i))) |
| 925 | continue; |
| 926 | |
| 927 | /* We've already tried this earlier */ |
| 928 | if (e->crtc_id == r->crtcs[i]) |
| 929 | continue; |
| 930 | |
| 931 | if (!hwc_crtc_is_bound(ctx, r->crtcs[i])) { |
| 932 | *crtc_id = r->crtcs[i]; |
| 933 | ret = 0; |
| 934 | goto out; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | /* We can't use the encoder, but nothing went wrong, try another one */ |
| 939 | ret = -EAGAIN; |
| 940 | |
| 941 | out: |
| 942 | drmModeFreeEncoder(e); |
| 943 | return ret; |
| 944 | } |
| 945 | |
| 946 | static int hwc_set_active_config(struct hwc_composer_device_1* dev, int display, |
| 947 | int index) |
| 948 | { |
| 949 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 950 | struct hwc_drm_display *hd = NULL; |
| 951 | drmModeResPtr r = NULL; |
| 952 | drmModeConnectorPtr c; |
| 953 | uint32_t crtc_id = 0; |
| 954 | int ret, i; |
| 955 | bool new_crtc, new_encoder; |
| 956 | |
| 957 | ret = hwc_get_drm_display(ctx, display, &hd); |
| 958 | if (ret) |
| 959 | return ret; |
| 960 | |
| 961 | c = drmModeGetConnector(ctx->fd, hd->connector_id); |
| 962 | if (!c) { |
| 963 | ALOGE("Failed to get connector %d", display); |
| 964 | return -ENODEV; |
| 965 | } |
| 966 | |
| 967 | if (c->connection == DRM_MODE_DISCONNECTED) { |
| 968 | ALOGE("Tried to configure a disconnected display %d", display); |
| 969 | ret = -ENODEV; |
| 970 | goto out; |
| 971 | } |
| 972 | |
Sean Paul | fa406a1 | 2015-02-04 10:05:44 -0800 | [diff] [blame] | 973 | if (index >= c->count_modes) { |
| 974 | ALOGE("Index is out-of-bounds %d/%d", index, c->count_modes); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 975 | ret = -ENOENT; |
| 976 | goto out; |
| 977 | } |
| 978 | |
| 979 | r = drmModeGetResources(ctx->fd); |
| 980 | if (!r) { |
| 981 | ALOGE("Failed to get drm resources"); |
| 982 | goto out; |
| 983 | } |
| 984 | |
| 985 | /* We no longer have an active_crtc */ |
| 986 | hd->active_crtc = 0; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 987 | hd->active_pipe = -1; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 988 | |
| 989 | /* First, try to use the currently-connected encoder */ |
| 990 | if (c->encoder_id) { |
| 991 | ret = hwc_try_encoder(ctx, r, c->encoder_id, &crtc_id); |
| 992 | if (ret && ret != -EAGAIN) { |
| 993 | ALOGE("Encoder try failed %d", ret); |
| 994 | goto out; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | /* We couldn't find a crtc with the attached encoder, try the others */ |
| 999 | if (!crtc_id) { |
| 1000 | for (i = 0; i < c->count_encoders; i++) { |
| 1001 | ret = hwc_try_encoder(ctx, r, c->encoders[i], &crtc_id); |
| 1002 | if (!ret) { |
| 1003 | break; |
| 1004 | } else if (ret != -EAGAIN) { |
| 1005 | ALOGE("Encoder try failed %d", ret); |
| 1006 | goto out; |
| 1007 | } |
| 1008 | } |
| 1009 | if (!crtc_id) { |
| 1010 | ALOGE("Couldn't find valid crtc to modeset"); |
| 1011 | ret = -EINVAL; |
| 1012 | goto out; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | hd->active_crtc = crtc_id; |
Sean Paul | fa406a1 | 2015-02-04 10:05:44 -0800 | [diff] [blame] | 1017 | |
| 1018 | memcpy(&hd->active_mode, &hd->configs[index], sizeof(hd->active_mode)); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1019 | |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 1020 | /* Find the pipe corresponding to the crtc_id */ |
| 1021 | for (i = 0; i < r->count_crtcs; i++) { |
| 1022 | /* We've already tried this earlier */ |
| 1023 | if (r->crtcs[i] == crtc_id) { |
| 1024 | hd->active_pipe = i; |
| 1025 | break; |
| 1026 | } |
| 1027 | } |
| 1028 | /* This should never happen... hehehe */ |
| 1029 | if (hd->active_pipe == -1) { |
| 1030 | ALOGE("Active crtc was not found in resources!!"); |
| 1031 | ret = -ENODEV; |
| 1032 | goto out; |
| 1033 | } |
| 1034 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1035 | /* TODO: Once we have atomic, set the crtc timing info here */ |
| 1036 | |
| 1037 | out: |
| 1038 | if (r) |
| 1039 | drmModeFreeResources(r); |
| 1040 | |
| 1041 | drmModeFreeConnector(c); |
| 1042 | return ret; |
| 1043 | } |
| 1044 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 1045 | static int hwc_destroy_worker(struct hwc_worker *worker) |
| 1046 | { |
| 1047 | int ret; |
| 1048 | |
| 1049 | ret = pthread_mutex_lock(&worker->lock); |
| 1050 | if (ret) { |
| 1051 | ALOGE("Failed to lock in destroy() %d", ret); |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
| 1055 | worker->exit = true; |
| 1056 | |
| 1057 | ret |= pthread_cond_signal(&worker->cond); |
| 1058 | if (ret) |
| 1059 | ALOGE("Failed to signal cond in destroy() %d", ret); |
| 1060 | |
| 1061 | ret |= pthread_mutex_unlock(&worker->lock); |
| 1062 | if (ret) |
| 1063 | ALOGE("Failed to unlock in destroy() %d", ret); |
| 1064 | |
| 1065 | ret |= pthread_join(worker->thread, NULL); |
| 1066 | if (ret && ret != ESRCH) |
| 1067 | ALOGE("Failed to join thread in destroy() %d", ret); |
| 1068 | |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
| 1072 | static void hwc_destroy_display(struct hwc_drm_display *hd) |
| 1073 | { |
| 1074 | int ret; |
| 1075 | |
| 1076 | if (hwc_destroy_worker(&hd->set_worker)) |
| 1077 | ALOGE("Destroy set worker failed"); |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 1078 | |
| 1079 | if (hwc_destroy_worker(&hd->vsync_worker)) |
| 1080 | ALOGE("Destroy vsync worker failed"); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 1081 | } |
| 1082 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1083 | static int hwc_device_close(struct hw_device_t *dev) |
| 1084 | { |
| 1085 | struct hwc_context_t *ctx = (struct hwc_context_t *)dev; |
Sean Paul | cd36a9e | 2015-01-22 18:01:18 -0500 | [diff] [blame] | 1086 | int ret, i; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1087 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 1088 | for (i = 0; i < MAX_NUM_DISPLAYS; i++) |
| 1089 | hwc_destroy_display(&ctx->displays[i]); |
| 1090 | |
| 1091 | drmClose(ctx->fd); |
Sean Paul | cd36a9e | 2015-01-22 18:01:18 -0500 | [diff] [blame] | 1092 | |
| 1093 | ret = hwc_import_destroy(ctx->import_ctx); |
| 1094 | if (ret) |
| 1095 | ALOGE("Could not destroy import %d", ret); |
| 1096 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1097 | free(ctx); |
| 1098 | |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 1102 | static int hwc_initialize_worker(struct hwc_drm_display *hd, |
| 1103 | struct hwc_worker *worker, void *(*routine)(void*)) |
| 1104 | { |
| 1105 | int ret; |
| 1106 | |
| 1107 | ret = pthread_cond_init(&worker->cond, NULL); |
| 1108 | if (ret) { |
| 1109 | ALOGE("Failed to create worker condition %d", ret); |
| 1110 | return ret; |
| 1111 | } |
| 1112 | |
| 1113 | ret = pthread_mutex_init(&worker->lock, NULL); |
| 1114 | if (ret) { |
| 1115 | ALOGE("Failed to initialize worker lock %d", ret); |
| 1116 | goto err_cond; |
| 1117 | } |
| 1118 | |
| 1119 | worker->exit = false; |
| 1120 | |
| 1121 | ret = pthread_create(&worker->thread, NULL, routine, hd); |
| 1122 | if (ret) { |
| 1123 | ALOGE("Could not create worker thread %d", ret); |
| 1124 | goto err_lock; |
| 1125 | } |
| 1126 | return 0; |
| 1127 | |
| 1128 | err_lock: |
| 1129 | pthread_mutex_destroy(&worker->lock); |
| 1130 | err_cond: |
| 1131 | pthread_cond_destroy(&worker->cond); |
| 1132 | return ret; |
| 1133 | } |
| 1134 | |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 1135 | /* |
| 1136 | * TODO: This function sets the active config to the first one in the list. This |
| 1137 | * should be fixed such that it selects the preferred mode for the display, or |
| 1138 | * some other, saner, method of choosing the config. |
| 1139 | */ |
| 1140 | static int hwc_set_initial_config(struct hwc_drm_display *hd) |
| 1141 | { |
| 1142 | int ret; |
| 1143 | uint32_t config; |
| 1144 | size_t num_configs = 1; |
| 1145 | |
| 1146 | ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config, |
| 1147 | &num_configs); |
Sean Paul | aea15c2 | 2015-02-09 02:24:11 -0500 | [diff] [blame] | 1148 | if (ret || !num_configs) |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 1149 | return 0; |
| 1150 | |
| 1151 | ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0); |
| 1152 | if (ret) { |
| 1153 | ALOGE("Failed to set active config d=%d ret=%d", hd->display, |
| 1154 | ret); |
| 1155 | return ret; |
| 1156 | } |
| 1157 | |
| 1158 | return ret; |
| 1159 | } |
| 1160 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1161 | static int hwc_initialize_display(struct hwc_context_t *ctx, int display, |
| 1162 | uint32_t connector_id) |
| 1163 | { |
| 1164 | struct hwc_drm_display *hd = NULL; |
| 1165 | int ret; |
| 1166 | |
| 1167 | ret = hwc_get_drm_display(ctx, display, &hd); |
| 1168 | if (ret) |
| 1169 | return ret; |
| 1170 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 1171 | hd->ctx = ctx; |
| 1172 | hd->display = display; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 1173 | hd->active_pipe = -1; |
Sean Paul | efb20cb | 2015-02-04 09:29:15 -0800 | [diff] [blame] | 1174 | hd->initial_modeset_required = true; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1175 | hd->connector_id = connector_id; |
| 1176 | |
Sean Paul | f1dc191 | 2015-01-24 01:34:31 -0500 | [diff] [blame] | 1177 | ret = sw_sync_timeline_create(); |
| 1178 | if (ret < 0) { |
| 1179 | ALOGE("Failed to create sw sync timeline %d", ret); |
| 1180 | return ret; |
| 1181 | } |
| 1182 | hd->timeline_fd = ret; |
Sean Paul | e147a2a | 2015-02-22 17:55:43 -0500 | [diff] [blame] | 1183 | |
| 1184 | /* |
| 1185 | * Initialize timeline_next to 1, because point 0 will be the very first |
| 1186 | * set operation. Since we increment every time set() is called, |
| 1187 | * initializing to 0 would cause an off-by-one error where |
| 1188 | * surfaceflinger would composite on the front buffer. |
| 1189 | */ |
| 1190 | hd->timeline_next = 1; |
Sean Paul | f1dc191 | 2015-01-24 01:34:31 -0500 | [diff] [blame] | 1191 | |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 1192 | ret = hwc_set_initial_config(hd); |
| 1193 | if (ret) { |
| 1194 | ALOGE("Failed to set initial config for d=%d ret=%d", display, |
| 1195 | ret); |
| 1196 | return ret; |
| 1197 | } |
| 1198 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 1199 | ret = hwc_initialize_worker(hd, &hd->set_worker, hwc_set_worker); |
| 1200 | if (ret) { |
| 1201 | ALOGE("Failed to create set worker %d\n", ret); |
| 1202 | return ret; |
| 1203 | } |
| 1204 | |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 1205 | ret = hwc_initialize_worker(hd, &hd->vsync_worker, hwc_vsync_worker); |
| 1206 | if (ret) { |
| 1207 | ALOGE("Failed to create vsync worker %d", ret); |
| 1208 | goto err; |
| 1209 | } |
| 1210 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1211 | return 0; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 1212 | |
| 1213 | err: |
| 1214 | if (hwc_destroy_worker(&hd->set_worker)) |
| 1215 | ALOGE("Failed to destroy set worker"); |
| 1216 | |
| 1217 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | static int hwc_enumerate_displays(struct hwc_context_t *ctx) |
| 1221 | { |
| 1222 | struct hwc_drm_display *panel_hd; |
| 1223 | drmModeResPtr res; |
| 1224 | drmModeConnectorPtr *conn_list; |
| 1225 | int ret = 0, i, j; |
| 1226 | |
| 1227 | res = drmModeGetResources(ctx->fd); |
| 1228 | if (!res) { |
| 1229 | ALOGE("Failed to get drm resources"); |
| 1230 | return -ENODEV; |
| 1231 | } |
| 1232 | |
| 1233 | conn_list = (drmModeConnector **)calloc(res->count_connectors, |
| 1234 | sizeof(*conn_list)); |
| 1235 | if (!conn_list) { |
| 1236 | ALOGE("Failed to allocate connector list"); |
| 1237 | ret = -ENOMEM; |
| 1238 | goto out; |
| 1239 | } |
| 1240 | |
| 1241 | for (i = 0; i < res->count_connectors; i++) { |
| 1242 | conn_list[i] = drmModeGetConnector(ctx->fd, res->connectors[i]); |
| 1243 | if (!conn_list[i]) { |
| 1244 | ALOGE("Failed to get connector %d", res->connectors[i]); |
| 1245 | ret = -ENODEV; |
| 1246 | goto out; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | ctx->num_displays = 0; |
| 1251 | |
| 1252 | /* Find a connected, panel type connector for display 0 */ |
| 1253 | for (i = 0; i < res->count_connectors; i++) { |
| 1254 | drmModeConnectorPtr c = conn_list[i]; |
| 1255 | |
| 1256 | for (j = 0; j < ARRAY_SIZE(panel_types); j++) { |
| 1257 | if (c->connector_type == panel_types[j] && |
| 1258 | c->connection == DRM_MODE_CONNECTED) |
| 1259 | break; |
| 1260 | } |
| 1261 | if (j == ARRAY_SIZE(panel_types)) |
| 1262 | continue; |
| 1263 | |
| 1264 | hwc_initialize_display(ctx, ctx->num_displays, c->connector_id); |
| 1265 | ctx->num_displays++; |
| 1266 | break; |
| 1267 | } |
| 1268 | |
| 1269 | ret = hwc_get_drm_display(ctx, 0, &panel_hd); |
| 1270 | if (ret) |
| 1271 | goto out; |
| 1272 | |
| 1273 | /* Fill in the other displays */ |
| 1274 | for (i = 0; i < res->count_connectors; i++) { |
| 1275 | drmModeConnectorPtr c = conn_list[i]; |
| 1276 | |
| 1277 | if (panel_hd->connector_id == c->connector_id) |
| 1278 | continue; |
| 1279 | |
| 1280 | hwc_initialize_display(ctx, ctx->num_displays, c->connector_id); |
| 1281 | ctx->num_displays++; |
| 1282 | } |
| 1283 | |
| 1284 | out: |
| 1285 | for (i = 0; i < res->count_connectors; i++) { |
| 1286 | if (conn_list[i]) |
| 1287 | drmModeFreeConnector(conn_list[i]); |
| 1288 | } |
| 1289 | free(conn_list); |
| 1290 | |
| 1291 | if (res) |
| 1292 | drmModeFreeResources(res); |
| 1293 | |
| 1294 | return ret; |
| 1295 | } |
| 1296 | |
| 1297 | static int hwc_device_open(const struct hw_module_t* module, const char* name, |
| 1298 | struct hw_device_t** dev) |
| 1299 | { |
| 1300 | int ret = 0; |
| 1301 | struct hwc_context_t *ctx; |
Lauri Peltonen | 64717b2 | 2015-02-04 16:55:31 +0200 | [diff] [blame] | 1302 | char path[PROPERTY_VALUE_MAX]; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1303 | |
| 1304 | if (strcmp(name, HWC_HARDWARE_COMPOSER)) { |
| 1305 | ALOGE("Invalid module name- %s", name); |
| 1306 | return -EINVAL; |
| 1307 | } |
| 1308 | |
Sean Paul | 9b1bb84 | 2015-01-23 01:11:58 -0500 | [diff] [blame] | 1309 | ctx = new hwc_context_t(); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1310 | if (!ctx) { |
| 1311 | ALOGE("Failed to allocate hwc context"); |
| 1312 | return -ENOMEM; |
| 1313 | } |
| 1314 | |
Sean Paul | cd36a9e | 2015-01-22 18:01:18 -0500 | [diff] [blame] | 1315 | ret = hwc_import_init(&ctx->import_ctx); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1316 | if (ret) { |
Sean Paul | cd36a9e | 2015-01-22 18:01:18 -0500 | [diff] [blame] | 1317 | ALOGE("Failed to initialize import context"); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1318 | goto out; |
| 1319 | } |
| 1320 | |
Lauri Peltonen | 64717b2 | 2015-02-04 16:55:31 +0200 | [diff] [blame] | 1321 | property_get("hwc.drm.device", path, HWCOMPOSER_DRM_DEVICE); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1322 | /* TODO: Use drmOpenControl here instead */ |
Lauri Peltonen | 64717b2 | 2015-02-04 16:55:31 +0200 | [diff] [blame] | 1323 | ctx->fd = open(path, O_RDWR); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1324 | if (ctx->fd < 0) { |
| 1325 | ALOGE("Failed to open dri- %s", strerror(-errno)); |
| 1326 | goto out; |
| 1327 | } |
| 1328 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1329 | ret = hwc_enumerate_displays(ctx); |
| 1330 | if (ret) { |
| 1331 | ALOGE("Failed to enumerate displays: %s", strerror(ret)); |
| 1332 | goto out; |
| 1333 | } |
| 1334 | |
| 1335 | ctx->device.common.tag = HARDWARE_DEVICE_TAG; |
| 1336 | ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4; |
| 1337 | ctx->device.common.module = const_cast<hw_module_t*>(module); |
| 1338 | ctx->device.common.close = hwc_device_close; |
| 1339 | |
| 1340 | ctx->device.prepare = hwc_prepare; |
| 1341 | ctx->device.set = hwc_set; |
| 1342 | ctx->device.eventControl = hwc_event_control; |
| 1343 | ctx->device.setPowerMode = hwc_set_power_mode; |
| 1344 | ctx->device.query = hwc_query; |
| 1345 | ctx->device.registerProcs = hwc_register_procs; |
| 1346 | ctx->device.getDisplayConfigs = hwc_get_display_configs; |
| 1347 | ctx->device.getDisplayAttributes = hwc_get_display_attributes; |
| 1348 | ctx->device.getActiveConfig = hwc_get_active_config; |
| 1349 | ctx->device.setActiveConfig = hwc_set_active_config; |
| 1350 | ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */ |
| 1351 | |
| 1352 | *dev = &ctx->device.common; |
| 1353 | |
| 1354 | return 0; |
| 1355 | out: |
| 1356 | if (ctx->fd >= 0) |
| 1357 | close(ctx->fd); |
| 1358 | |
| 1359 | free(ctx); |
| 1360 | return ret; |
| 1361 | } |
| 1362 | |
| 1363 | static struct hw_module_methods_t hwc_module_methods = { |
| 1364 | open: hwc_device_open |
| 1365 | }; |
| 1366 | |
| 1367 | hwc_module_t HAL_MODULE_INFO_SYM = { |
| 1368 | common: { |
| 1369 | tag: HARDWARE_MODULE_TAG, |
| 1370 | version_major: 1, |
| 1371 | version_minor: 0, |
| 1372 | id: HWC_HARDWARE_MODULE_ID, |
| 1373 | name: "DRM hwcomposer module", |
| 1374 | author: "The Android Open Source Project", |
| 1375 | methods: &hwc_module_methods, |
| 1376 | dso: NULL, |
| 1377 | reserved: { 0 }, |
| 1378 | } |
| 1379 | }; |