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