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