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 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 19 | #include "drm_hwcomposer.h" |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 20 | #include "drmresources.h" |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 21 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 22 | #include <errno.h> |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 23 | #include <fcntl.h> |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 24 | #include <pthread.h> |
Dan Albert | c5255b3 | 2015-05-07 23:42:54 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 26 | #include <sys/param.h> |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 27 | #include <sys/resource.h> |
Dan Albert | c5255b3 | 2015-05-07 23:42:54 -0700 | [diff] [blame] | 28 | |
| 29 | #include <list> |
| 30 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 31 | #include <xf86drm.h> |
| 32 | #include <xf86drmMode.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 33 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 34 | #include <cutils/log.h> |
| 35 | #include <cutils/properties.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 36 | #include <hardware/hardware.h> |
| 37 | #include <hardware/hwcomposer.h> |
Sean Paul | f1dc191 | 2015-01-24 01:34:31 -0500 | [diff] [blame] | 38 | #include <sw_sync.h> |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 39 | #include <sync/sync.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 40 | |
| 41 | #define ARRAY_SIZE(arr) (int)(sizeof(arr) / sizeof((arr)[0])) |
| 42 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 43 | #define MAX_NUM_DISPLAYS 3 |
| 44 | #define UM_PER_INCH 25400 |
| 45 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 46 | namespace android { |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 47 | |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 48 | struct hwc_worker { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 49 | pthread_t thread; |
| 50 | pthread_mutex_t lock; |
| 51 | pthread_cond_t cond; |
| 52 | bool exit; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 53 | }; |
| 54 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 55 | struct hwc_drm_display { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 56 | struct hwc_context_t *ctx; |
| 57 | int display; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 58 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 59 | std::vector<uint32_t> config_ids; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 60 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 61 | struct hwc_worker set_worker; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 62 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 63 | std::list<struct hwc_drm_bo> buf_queue; |
| 64 | struct hwc_drm_bo front; |
| 65 | pthread_mutex_t flip_lock; |
| 66 | pthread_cond_t flip_cond; |
Sean Paul | f1dc191 | 2015-01-24 01:34:31 -0500 | [diff] [blame] | 67 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 68 | int timeline_fd; |
| 69 | unsigned timeline_next; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 70 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 71 | bool enable_vsync_events; |
| 72 | unsigned int vsync_sequence; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | struct hwc_context_t { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 76 | hwc_composer_device_1_t device; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 77 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 78 | hwc_procs_t const *procs; |
| 79 | struct hwc_import_context *import_ctx; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 80 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 81 | struct hwc_drm_display displays[MAX_NUM_DISPLAYS]; |
| 82 | int num_displays; |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 83 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 84 | struct hwc_worker event_worker; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 85 | |
| 86 | DrmResources drm; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | static int hwc_get_drm_display(struct hwc_context_t *ctx, int display, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 90 | struct hwc_drm_display **hd) { |
| 91 | if (display >= MAX_NUM_DISPLAYS) { |
| 92 | ALOGE("Requested display is out-of-bounds %d %d", display, |
| 93 | MAX_NUM_DISPLAYS); |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | *hd = &ctx->displays[display]; |
| 97 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 100 | static int hwc_prepare_layer(hwc_layer_1_t *layer) { |
| 101 | /* TODO: We can't handle background right now, defer to sufaceFlinger */ |
| 102 | if (layer->compositionType == HWC_BACKGROUND) { |
| 103 | layer->compositionType = HWC_FRAMEBUFFER; |
| 104 | ALOGV("Can't handle background layers yet"); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 105 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 106 | /* TODO: Support sideband compositions */ |
| 107 | } else if (layer->compositionType == HWC_SIDEBAND) { |
| 108 | layer->compositionType = HWC_FRAMEBUFFER; |
| 109 | ALOGV("Can't handle sideband content yet"); |
| 110 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 111 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 112 | layer->hints = 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 113 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 114 | /* TODO: Handle cursor by setting compositionType=HWC_CURSOR_OVERLAY */ |
| 115 | if (layer->flags & HWC_IS_CURSOR_LAYER) { |
| 116 | ALOGV("Can't handle async cursors yet"); |
| 117 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 118 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 119 | /* TODO: Handle transformations */ |
| 120 | if (layer->transform) { |
| 121 | ALOGV("Can't handle transformations yet"); |
| 122 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 123 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 124 | /* TODO: Handle blending & plane alpha*/ |
| 125 | if (layer->blending == HWC_BLENDING_PREMULT || |
| 126 | layer->blending == HWC_BLENDING_COVERAGE) { |
| 127 | ALOGV("Can't handle blending yet"); |
| 128 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 129 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 130 | /* TODO: Handle cropping & scaling */ |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 131 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 132 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 135 | static int hwc_prepare(hwc_composer_device_1_t * /* dev */, size_t num_displays, |
| 136 | hwc_display_contents_1_t **display_contents) { |
| 137 | /* TODO: Check flags for HWC_GEOMETRY_CHANGED */ |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 138 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 139 | for (int i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; ++i) { |
| 140 | if (!display_contents[i]) |
| 141 | continue; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 142 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 143 | for (int j = 0; j < (int)display_contents[i]->numHwLayers; ++j) { |
| 144 | int ret = hwc_prepare_layer(&display_contents[i]->hwLayers[j]); |
| 145 | if (ret) { |
| 146 | ALOGE("Failed to prepare layer %d:%d", j, i); |
| 147 | return ret; |
| 148 | } |
| 149 | } |
| 150 | } |
Sean Paul | dffca95 | 2015-02-04 10:19:55 -0800 | [diff] [blame] | 151 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 152 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 155 | static int hwc_queue_vblank_event(struct hwc_drm_display *hd) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 156 | DrmCrtc *crtc = hd->ctx->drm.GetCrtcForDisplay(hd->display); |
| 157 | if (!crtc) { |
| 158 | ALOGE("Failed to get crtc for display"); |
| 159 | return -ENODEV; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 160 | } |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 161 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 162 | drmVBlank vblank; |
| 163 | memset(&vblank, 0, sizeof(vblank)); |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 164 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 165 | uint32_t high_crtc = (crtc->pipe() << DRM_VBLANK_HIGH_CRTC_SHIFT); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 166 | vblank.request.type = (drmVBlankSeqType)( |
| 167 | DRM_VBLANK_ABSOLUTE | DRM_VBLANK_NEXTONMISS | DRM_VBLANK_EVENT | |
| 168 | (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK)); |
| 169 | vblank.request.signal = (unsigned long)hd; |
| 170 | vblank.request.sequence = hd->vsync_sequence + 1; |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 171 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 172 | int ret = drmWaitVBlank(hd->ctx->drm.fd(), &vblank); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 173 | if (ret) { |
| 174 | ALOGE("Failed to wait for vblank %d", ret); |
| 175 | return ret; |
| 176 | } |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 177 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 178 | return 0; |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void hwc_vblank_event_handler(int /* fd */, unsigned int sequence, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 182 | unsigned int tv_sec, unsigned int tv_usec, |
| 183 | void *user_data) { |
| 184 | struct hwc_drm_display *hd = (struct hwc_drm_display *)user_data; |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 185 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 186 | if (!hd->enable_vsync_events || !hd->ctx->procs->vsync) |
| 187 | return; |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 188 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 189 | /* |
| 190 | * Discard duplicate vsync (can happen when enabling vsync events while |
| 191 | * already processing vsyncs). |
| 192 | */ |
| 193 | if (sequence <= hd->vsync_sequence) |
| 194 | return; |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 195 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 196 | hd->vsync_sequence = sequence; |
| 197 | int ret = hwc_queue_vblank_event(hd); |
| 198 | if (ret) |
| 199 | ALOGE("Failed to queue vblank event ret=%d", ret); |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 200 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 201 | int64_t timestamp = |
| 202 | (int64_t)tv_sec * 1000 * 1000 * 1000 + (int64_t)tv_usec * 1000; |
| 203 | hd->ctx->procs->vsync(hd->ctx->procs, hd->display, timestamp); |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void hwc_flip_event_handler(int /* fd */, unsigned int /* sequence */, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 207 | unsigned int /* tv_sec */, |
| 208 | unsigned int /* tv_usec */, |
| 209 | void *user_data) { |
| 210 | struct hwc_drm_display *hd = (struct hwc_drm_display *)user_data; |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 211 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 212 | int ret = pthread_mutex_lock(&hd->flip_lock); |
| 213 | if (ret) { |
| 214 | ALOGE("Failed to lock flip lock ret=%d", ret); |
| 215 | return; |
| 216 | } |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 217 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 218 | ret = pthread_cond_signal(&hd->flip_cond); |
| 219 | if (ret) |
| 220 | ALOGE("Failed to signal flip condition ret=%d", ret); |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 221 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 222 | ret = pthread_mutex_unlock(&hd->flip_lock); |
| 223 | if (ret) { |
| 224 | ALOGE("Failed to unlock flip lock ret=%d", ret); |
| 225 | return; |
| 226 | } |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 229 | static void *hwc_event_worker(void *arg) { |
| 230 | setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY); |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 231 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 232 | struct hwc_context_t *ctx = (struct hwc_context_t *)arg; |
| 233 | do { |
| 234 | fd_set fds; |
| 235 | FD_ZERO(&fds); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 236 | FD_SET(ctx->drm.fd(), &fds); |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 237 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 238 | drmEventContext event_context; |
| 239 | event_context.version = DRM_EVENT_CONTEXT_VERSION; |
| 240 | event_context.page_flip_handler = hwc_flip_event_handler; |
| 241 | event_context.vblank_handler = hwc_vblank_event_handler; |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 242 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 243 | int ret; |
| 244 | do { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 245 | ret = select(ctx->drm.fd() + 1, &fds, NULL, NULL, NULL); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 246 | } while (ret == -1 && errno == EINTR); |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 247 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 248 | if (ret != 1) { |
| 249 | ALOGE("Failed waiting for drm event\n"); |
| 250 | continue; |
| 251 | } |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 252 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 253 | drmHandleEvent(ctx->drm.fd(), &event_context); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 254 | } while (true); |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 255 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 256 | return NULL; |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 257 | } |
| 258 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 259 | static bool hwc_mode_is_equal(drmModeModeInfoPtr a, drmModeModeInfoPtr b) { |
| 260 | return a->clock == b->clock && a->hdisplay == b->hdisplay && |
| 261 | a->hsync_start == b->hsync_start && a->hsync_end == b->hsync_end && |
| 262 | a->htotal == b->htotal && a->hskew == b->hskew && |
| 263 | a->vdisplay == b->vdisplay && a->vsync_start == b->vsync_start && |
| 264 | a->vsync_end == b->vsync_end && a->vtotal == b->vtotal && |
| 265 | a->vscan == b->vscan && a->vrefresh == b->vrefresh && |
| 266 | a->flags == b->flags && a->type == b->type && |
| 267 | !strcmp(a->name, b->name); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 268 | } |
| 269 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 270 | static int hwc_flip(struct hwc_drm_display *hd, struct hwc_drm_bo *buf) { |
| 271 | DrmCrtc *crtc = hd->ctx->drm.GetCrtcForDisplay(hd->display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 272 | if (!crtc) { |
| 273 | ALOGE("Failed to get crtc for display %d", hd->display); |
| 274 | return -ENODEV; |
| 275 | } |
Sean Paul | efb20cb | 2015-02-04 09:29:15 -0800 | [diff] [blame] | 276 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 277 | DrmConnector *connector = hd->ctx->drm.GetConnectorForDisplay(hd->display); |
| 278 | if (!connector) { |
| 279 | ALOGE("Failed to get connector for display %d", hd->display); |
| 280 | return -ENODEV; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 281 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 282 | |
| 283 | int ret; |
| 284 | if (crtc->requires_modeset()) { |
| 285 | drmModeModeInfo drm_mode; |
| 286 | connector->active_mode().ToModeModeInfo(&drm_mode); |
| 287 | uint32_t connector_id = connector->id(); |
| 288 | ret = drmModeSetCrtc(hd->ctx->drm.fd(), crtc->id(), buf->fb_id, 0, 0, |
| 289 | &connector_id, 1, &drm_mode); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 290 | if (ret) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 291 | ALOGE("Modeset failed for crtc %d", crtc->id()); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 292 | return ret; |
| 293 | } |
| 294 | return 0; |
| 295 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 296 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 297 | ret = drmModePageFlip(hd->ctx->drm.fd(), crtc->id(), buf->fb_id, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 298 | DRM_MODE_PAGE_FLIP_EVENT, hd); |
| 299 | if (ret) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 300 | ALOGE("Failed to flip buffer for crtc %d", crtc->id()); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 301 | return ret; |
| 302 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 303 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 304 | ret = pthread_cond_wait(&hd->flip_cond, &hd->flip_lock); |
| 305 | if (ret) { |
| 306 | ALOGE("Failed to wait on condition %d", ret); |
| 307 | return ret; |
| 308 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 309 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 310 | return 0; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 313 | static int hwc_wait_and_set(struct hwc_drm_display *hd, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 314 | struct hwc_drm_bo *buf) { |
| 315 | int ret; |
| 316 | if (buf->acquire_fence_fd >= 0) { |
| 317 | ret = sync_wait(buf->acquire_fence_fd, -1); |
| 318 | close(buf->acquire_fence_fd); |
| 319 | buf->acquire_fence_fd = -1; |
| 320 | if (ret) { |
| 321 | ALOGE("Failed to wait for acquire %d", ret); |
| 322 | return ret; |
| 323 | } |
| 324 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 325 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 326 | ret = hwc_flip(hd, buf); |
| 327 | if (ret) { |
| 328 | ALOGE("Failed to perform flip\n"); |
| 329 | return ret; |
| 330 | } |
Lauri Peltonen | 132e010 | 2015-02-12 13:54:33 +0200 | [diff] [blame] | 331 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 332 | if (hwc_import_bo_release(hd->ctx->drm.fd(), hd->ctx->import_ctx, |
| 333 | &hd->front)) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 334 | struct drm_gem_close args; |
| 335 | memset(&args, 0, sizeof(args)); |
| 336 | for (int i = 0; i < ARRAY_SIZE(hd->front.gem_handles); ++i) { |
| 337 | if (!hd->front.gem_handles[i]) |
| 338 | continue; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 339 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 340 | ret = pthread_mutex_lock(&hd->set_worker.lock); |
| 341 | if (ret) { |
| 342 | ALOGE("Failed to lock set lock in wait_and_set() %d", ret); |
| 343 | continue; |
| 344 | } |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 345 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 346 | /* check for duplicate handle in buf_queue */ |
| 347 | bool found = false; |
| 348 | for (std::list<struct hwc_drm_bo>::iterator bi = hd->buf_queue.begin(); |
| 349 | bi != hd->buf_queue.end(); ++bi) |
| 350 | for (int j = 0; j < ARRAY_SIZE(bi->gem_handles); ++j) |
| 351 | if (hd->front.gem_handles[i] == bi->gem_handles[j]) |
| 352 | found = true; |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 353 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 354 | for (int j = 0; j < ARRAY_SIZE(buf->gem_handles); ++j) |
| 355 | if (hd->front.gem_handles[i] == buf->gem_handles[j]) |
| 356 | found = true; |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 357 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 358 | if (!found) { |
| 359 | args.handle = hd->front.gem_handles[i]; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 360 | drmIoctl(hd->ctx->drm.fd(), DRM_IOCTL_GEM_CLOSE, &args); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 361 | } |
| 362 | if (pthread_mutex_unlock(&hd->set_worker.lock)) |
| 363 | ALOGE("Failed to unlock set lock in wait_and_set() %d", ret); |
| 364 | } |
| 365 | } |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 366 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 367 | hd->front = *buf; |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 368 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 369 | return ret; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 370 | } |
| 371 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 372 | static void *hwc_set_worker(void *arg) { |
| 373 | setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 374 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 375 | struct hwc_drm_display *hd = (struct hwc_drm_display *)arg; |
| 376 | int ret = pthread_mutex_lock(&hd->flip_lock); |
| 377 | if (ret) { |
| 378 | ALOGE("Failed to lock flip lock ret=%d", ret); |
| 379 | return NULL; |
| 380 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 381 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 382 | do { |
| 383 | ret = pthread_mutex_lock(&hd->set_worker.lock); |
| 384 | if (ret) { |
| 385 | ALOGE("Failed to lock set lock %d", ret); |
| 386 | return NULL; |
| 387 | } |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 388 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 389 | if (hd->set_worker.exit) |
| 390 | break; |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 391 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 392 | if (hd->buf_queue.empty()) { |
| 393 | ret = pthread_cond_wait(&hd->set_worker.cond, &hd->set_worker.lock); |
| 394 | if (ret) { |
| 395 | ALOGE("Failed to wait on condition %d", ret); |
| 396 | break; |
| 397 | } |
| 398 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 399 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 400 | struct hwc_drm_bo buf; |
| 401 | buf = hd->buf_queue.front(); |
| 402 | hd->buf_queue.pop_front(); |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 403 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 404 | ret = pthread_mutex_unlock(&hd->set_worker.lock); |
| 405 | if (ret) { |
| 406 | ALOGE("Failed to unlock set lock %d", ret); |
| 407 | return NULL; |
| 408 | } |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 409 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 410 | ret = hwc_wait_and_set(hd, &buf); |
| 411 | if (ret) |
| 412 | ALOGE("Failed to wait and set %d", ret); |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 413 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 414 | ret = sw_sync_timeline_inc(hd->timeline_fd, 1); |
| 415 | if (ret) |
| 416 | ALOGE("Failed to increment sync timeline %d", ret); |
| 417 | } while (true); |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 418 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 419 | ret = pthread_mutex_unlock(&hd->set_worker.lock); |
| 420 | if (ret) |
| 421 | ALOGE("Failed to unlock set lock while exiting %d", ret); |
Sean Paul | f1dc191 | 2015-01-24 01:34:31 -0500 | [diff] [blame] | 422 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 423 | ret = pthread_mutex_unlock(&hd->flip_lock); |
| 424 | if (ret) |
| 425 | ALOGE("Failed to unlock flip lock ret=%d", ret); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 426 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 427 | return NULL; |
| 428 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 429 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 430 | static void hwc_close_fences(hwc_display_contents_1_t *display_contents) { |
| 431 | for (int i = 0; i < (int)display_contents->numHwLayers; ++i) { |
| 432 | hwc_layer_1_t *layer = &display_contents->hwLayers[i]; |
| 433 | if (layer->acquireFenceFd >= 0) { |
| 434 | close(layer->acquireFenceFd); |
| 435 | layer->acquireFenceFd = -1; |
| 436 | } |
| 437 | } |
| 438 | if (display_contents->outbufAcquireFenceFd >= 0) { |
| 439 | close(display_contents->outbufAcquireFenceFd); |
| 440 | display_contents->outbufAcquireFenceFd = -1; |
| 441 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 442 | } |
| 443 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 444 | static int hwc_set_display(hwc_context_t *ctx, int display, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 445 | hwc_display_contents_1_t *display_contents) { |
| 446 | struct hwc_drm_display *hd = NULL; |
| 447 | int ret = hwc_get_drm_display(ctx, display, &hd); |
| 448 | if (ret) { |
| 449 | hwc_close_fences(display_contents); |
| 450 | return ret; |
| 451 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 452 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 453 | DrmCrtc *crtc = hd->ctx->drm.GetCrtcForDisplay(display); |
| 454 | if (!crtc) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 455 | ALOGE("There is no active crtc for display %d", display); |
| 456 | hwc_close_fences(display_contents); |
| 457 | return -ENOENT; |
| 458 | } |
Sean Paul | 9b1bb84 | 2015-01-23 01:11:58 -0500 | [diff] [blame] | 459 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 460 | /* |
| 461 | * TODO: We can only support one hw layer atm, so choose either the |
| 462 | * first one or the framebuffer target. |
| 463 | */ |
| 464 | hwc_layer_1_t *layer = NULL; |
| 465 | if (!display_contents->numHwLayers) { |
| 466 | return 0; |
| 467 | } else if (display_contents->numHwLayers == 1) { |
| 468 | layer = &display_contents->hwLayers[0]; |
| 469 | } else { |
| 470 | int i; |
| 471 | for (i = 0; i < (int)display_contents->numHwLayers; ++i) { |
| 472 | layer = &display_contents->hwLayers[i]; |
| 473 | if (layer->compositionType == HWC_FRAMEBUFFER_TARGET) |
| 474 | break; |
| 475 | } |
| 476 | if (i == (int)display_contents->numHwLayers) { |
| 477 | ALOGE("Could not find a suitable layer for display %d", display); |
| 478 | } |
| 479 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 480 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 481 | ret = pthread_mutex_lock(&hd->set_worker.lock); |
| 482 | if (ret) { |
| 483 | ALOGE("Failed to lock set lock in set() %d", ret); |
| 484 | hwc_close_fences(display_contents); |
| 485 | return ret; |
| 486 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 487 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 488 | struct hwc_drm_bo buf; |
| 489 | memset(&buf, 0, sizeof(buf)); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 490 | ret = |
| 491 | hwc_import_bo_create(ctx->drm.fd(), ctx->import_ctx, layer->handle, &buf); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 492 | if (ret) { |
| 493 | ALOGE("Failed to import handle to drm bo %d", ret); |
| 494 | hwc_close_fences(display_contents); |
| 495 | return ret; |
| 496 | } |
| 497 | buf.acquire_fence_fd = layer->acquireFenceFd; |
| 498 | layer->acquireFenceFd = -1; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 499 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 500 | /* |
| 501 | * TODO: Retire and release can use the same sync point here b/c hwc is |
| 502 | * restricted to one layer. Once that is no longer true, this will need |
| 503 | * to change |
| 504 | */ |
| 505 | ++hd->timeline_next; |
| 506 | display_contents->retireFenceFd = sw_sync_fence_create( |
| 507 | hd->timeline_fd, "drm_hwc_retire", hd->timeline_next); |
| 508 | layer->releaseFenceFd = sw_sync_fence_create( |
| 509 | hd->timeline_fd, "drm_hwc_release", hd->timeline_next); |
| 510 | hd->buf_queue.push_back(buf); |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 511 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 512 | ret = pthread_cond_signal(&hd->set_worker.cond); |
| 513 | if (ret) |
| 514 | ALOGE("Failed to signal set worker %d", ret); |
Allen Martin | 3d3f70a | 2015-02-21 21:20:17 -0800 | [diff] [blame] | 515 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 516 | if (pthread_mutex_unlock(&hd->set_worker.lock)) |
| 517 | ALOGE("Failed to unlock set lock in set()"); |
Sean Paul | 3bc48e8 | 2015-01-23 01:41:13 -0500 | [diff] [blame] | 518 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 519 | hwc_close_fences(display_contents); |
| 520 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 524 | hwc_display_contents_1_t **display_contents) { |
| 525 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 526 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 527 | int ret = 0; |
| 528 | for (int i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; ++i) { |
| 529 | if (display_contents[i]) |
| 530 | ret = hwc_set_display(ctx, i, display_contents[i]); |
| 531 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 532 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 533 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 534 | } |
| 535 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 536 | static int hwc_event_control(struct hwc_composer_device_1 *dev, int display, |
| 537 | int event, int enabled) { |
| 538 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 539 | struct hwc_drm_display *hd = NULL; |
| 540 | int ret = hwc_get_drm_display(ctx, display, &hd); |
| 541 | if (ret) |
| 542 | return ret; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 543 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 544 | if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1)) |
| 545 | return -EINVAL; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 546 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 547 | DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(display); |
| 548 | if (!crtc) { |
| 549 | ALOGD("Can't service events for display %d, no crtc", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 550 | return -EINVAL; |
| 551 | } |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 552 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 553 | hd->enable_vsync_events = !!enabled; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 554 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 555 | if (!hd->enable_vsync_events) |
| 556 | return 0; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 557 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 558 | /* |
| 559 | * Note that it's possible that the event worker is already waiting for |
| 560 | * a vsync, and this will be a duplicate request. In that event, we'll |
| 561 | * end up firing the event handler twice, and it will discard the second |
| 562 | * event. Not ideal, but not worth introducing a bunch of additional |
| 563 | * logic/locks/state for. |
| 564 | */ |
| 565 | ret = hwc_queue_vblank_event(hd); |
| 566 | if (ret) { |
| 567 | ALOGE("Failed to queue vblank event ret=%d", ret); |
| 568 | return ret; |
| 569 | } |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 570 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 571 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 572 | } |
| 573 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 574 | static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display, |
| 575 | int mode) { |
| 576 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 577 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 578 | uint64_t dpmsValue = 0; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 579 | switch (mode) { |
| 580 | case HWC_POWER_MODE_OFF: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 581 | dpmsValue = DRM_MODE_DPMS_OFF; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 582 | break; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 583 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 584 | /* We can't support dozing right now, so go full on */ |
| 585 | case HWC_POWER_MODE_DOZE: |
| 586 | case HWC_POWER_MODE_DOZE_SUSPEND: |
| 587 | case HWC_POWER_MODE_NORMAL: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 588 | dpmsValue = DRM_MODE_DPMS_ON; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 589 | break; |
| 590 | }; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 591 | return ctx->drm.SetDpmsMode(display, dpmsValue); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 592 | } |
| 593 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 594 | static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what, |
| 595 | int *value) { |
| 596 | switch (what) { |
| 597 | case HWC_BACKGROUND_LAYER_SUPPORTED: |
| 598 | *value = 0; /* TODO: We should do this */ |
| 599 | break; |
| 600 | case HWC_VSYNC_PERIOD: |
| 601 | ALOGW("Query for deprecated vsync value, returning 60Hz"); |
| 602 | *value = 1000 * 1000 * 1000 / 60; |
| 603 | break; |
| 604 | case HWC_DISPLAY_TYPES_SUPPORTED: |
| 605 | *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL; |
| 606 | break; |
| 607 | } |
| 608 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 609 | } |
| 610 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 611 | static void hwc_register_procs(struct hwc_composer_device_1 *dev, |
| 612 | hwc_procs_t const *procs) { |
| 613 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 614 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 615 | ctx->procs = procs; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 616 | } |
| 617 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 618 | static int hwc_get_display_configs(struct hwc_composer_device_1 *dev, |
| 619 | int display, uint32_t *configs, |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 620 | size_t *num_configs) { |
| 621 | if (!*num_configs) |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 622 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 623 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 624 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 625 | struct hwc_drm_display *hd = NULL; |
| 626 | int ret = hwc_get_drm_display(ctx, display, &hd); |
| 627 | if (ret) |
| 628 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 629 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 630 | hd->config_ids.clear(); |
| 631 | |
| 632 | DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display); |
| 633 | if (!connector) { |
| 634 | ALOGE("Failed to get connector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 635 | return -ENODEV; |
| 636 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 637 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 638 | ret = connector->UpdateModes(); |
| 639 | if (ret) { |
| 640 | ALOGE("Failed to update display modes %d", ret); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 641 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 642 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 643 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 644 | for (DrmConnector::ModeIter iter = connector->begin_modes(); |
| 645 | iter != connector->end_modes(); ++iter) { |
| 646 | size_t idx = hd->config_ids.size(); |
| 647 | if (idx == *num_configs) |
| 648 | break; |
| 649 | hd->config_ids.push_back(iter->id()); |
| 650 | configs[idx] = iter->id(); |
| 651 | } |
| 652 | *num_configs = hd->config_ids.size(); |
| 653 | return *num_configs == 0 ? -1 : 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 654 | } |
| 655 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 656 | static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev, |
| 657 | int display, uint32_t config, |
| 658 | const uint32_t *attributes, |
| 659 | int32_t *values) { |
| 660 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 661 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 662 | if (!c) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 663 | ALOGE("Failed to get DrmConnector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 664 | return -ENODEV; |
| 665 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 666 | DrmMode mode; |
| 667 | for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes(); |
| 668 | ++iter) { |
| 669 | if (iter->id() == config) { |
| 670 | mode = *iter; |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | if (mode.id() == 0) { |
| 675 | ALOGE("Failed to find active mode for display %d", display); |
| 676 | return -ENOENT; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 677 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 678 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 679 | uint32_t mm_width = c->mm_width(); |
| 680 | uint32_t mm_height = c->mm_height(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 681 | for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) { |
| 682 | switch (attributes[i]) { |
| 683 | case HWC_DISPLAY_VSYNC_PERIOD: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 684 | values[i] = 1000 * 1000 * 1000 / mode.v_refresh(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 685 | break; |
| 686 | case HWC_DISPLAY_WIDTH: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 687 | values[i] = mode.h_display(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 688 | break; |
| 689 | case HWC_DISPLAY_HEIGHT: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 690 | values[i] = mode.v_display(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 691 | break; |
| 692 | case HWC_DISPLAY_DPI_X: |
| 693 | /* Dots per 1000 inches */ |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 694 | values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 695 | break; |
| 696 | case HWC_DISPLAY_DPI_Y: |
| 697 | /* Dots per 1000 inches */ |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 698 | values[i] = |
| 699 | mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 700 | break; |
| 701 | } |
| 702 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 703 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 704 | } |
| 705 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 706 | static int hwc_get_active_config(struct hwc_composer_device_1 *dev, |
| 707 | int display) { |
| 708 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 709 | struct hwc_drm_display *hd = NULL; |
| 710 | int ret = hwc_get_drm_display(ctx, display, &hd); |
| 711 | if (ret) |
| 712 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 713 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 714 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(display); |
| 715 | if (!c) { |
| 716 | ALOGE("Failed to get DrmConnector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 717 | return -ENODEV; |
| 718 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 719 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 720 | DrmMode mode = c->active_mode(); |
| 721 | for (size_t i = 0; i < hd->config_ids.size(); ++i) { |
| 722 | if (hd->config_ids[i] == mode.id()) |
| 723 | return i; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 724 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 725 | return -1; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 726 | } |
| 727 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 728 | static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display, |
| 729 | int index) { |
| 730 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 731 | struct hwc_drm_display *hd = NULL; |
| 732 | int ret = hwc_get_drm_display(ctx, display, &hd); |
| 733 | if (ret) |
| 734 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 735 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 736 | if (index >= (int)hd->config_ids.size()) { |
| 737 | ALOGE("Invalid config index %d passed in", index); |
| 738 | return -EINVAL; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 739 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 740 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 741 | ret = |
| 742 | ctx->drm.SetDisplayActiveMode(display, hd->config_ids[index]); |
| 743 | if (ret) { |
| 744 | ALOGE("Failed to set config for display %d", display); |
| 745 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 746 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 747 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 748 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 749 | } |
| 750 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 751 | static int hwc_destroy_worker(struct hwc_worker *worker) { |
| 752 | int ret = pthread_mutex_lock(&worker->lock); |
| 753 | if (ret) { |
| 754 | ALOGE("Failed to lock in destroy() %d", ret); |
| 755 | return ret; |
| 756 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 757 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 758 | worker->exit = true; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 759 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 760 | ret |= pthread_cond_signal(&worker->cond); |
| 761 | if (ret) |
| 762 | ALOGE("Failed to signal cond in destroy() %d", ret); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 763 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 764 | ret |= pthread_mutex_unlock(&worker->lock); |
| 765 | if (ret) |
| 766 | ALOGE("Failed to unlock in destroy() %d", ret); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 767 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 768 | ret |= pthread_join(worker->thread, NULL); |
| 769 | if (ret && ret != ESRCH) |
| 770 | ALOGE("Failed to join thread in destroy() %d", ret); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 771 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 772 | return ret; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 773 | } |
| 774 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 775 | static void hwc_destroy_display(struct hwc_drm_display *hd) { |
| 776 | if (hwc_destroy_worker(&hd->set_worker)) |
| 777 | ALOGE("Destroy set worker failed"); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 778 | } |
| 779 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 780 | static int hwc_device_close(struct hw_device_t *dev) { |
| 781 | struct hwc_context_t *ctx = (struct hwc_context_t *)dev; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 782 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 783 | for (int i = 0; i < MAX_NUM_DISPLAYS; ++i) |
| 784 | hwc_destroy_display(&ctx->displays[i]); |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 785 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 786 | if (hwc_destroy_worker(&ctx->event_worker)) |
| 787 | ALOGE("Destroy event worker failed"); |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 788 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 789 | int ret = hwc_import_destroy(ctx->import_ctx); |
| 790 | if (ret) |
| 791 | ALOGE("Could not destroy import %d", ret); |
Sean Paul | cd36a9e | 2015-01-22 18:01:18 -0500 | [diff] [blame] | 792 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 793 | delete ctx; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 794 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 795 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 796 | } |
| 797 | |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 798 | static int hwc_initialize_worker(struct hwc_worker *worker, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 799 | void *(*routine)(void *), void *arg) { |
| 800 | int ret = pthread_cond_init(&worker->cond, NULL); |
| 801 | if (ret) { |
| 802 | ALOGE("Failed to create worker condition %d", ret); |
| 803 | return ret; |
| 804 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 805 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 806 | ret = pthread_mutex_init(&worker->lock, NULL); |
| 807 | if (ret) { |
| 808 | ALOGE("Failed to initialize worker lock %d", ret); |
| 809 | pthread_cond_destroy(&worker->cond); |
| 810 | return ret; |
| 811 | } |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 812 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 813 | worker->exit = false; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 814 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 815 | ret = pthread_create(&worker->thread, NULL, routine, arg); |
| 816 | if (ret) { |
| 817 | ALOGE("Could not create worker thread %d", ret); |
| 818 | pthread_mutex_destroy(&worker->lock); |
| 819 | pthread_cond_destroy(&worker->cond); |
| 820 | return ret; |
| 821 | } |
| 822 | return 0; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 823 | } |
| 824 | |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 825 | /* |
| 826 | * TODO: This function sets the active config to the first one in the list. This |
| 827 | * should be fixed such that it selects the preferred mode for the display, or |
| 828 | * some other, saner, method of choosing the config. |
| 829 | */ |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 830 | static int hwc_set_initial_config(struct hwc_drm_display *hd) { |
| 831 | uint32_t config; |
| 832 | size_t num_configs = 1; |
| 833 | int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config, |
| 834 | &num_configs); |
| 835 | if (ret || !num_configs) |
| 836 | return 0; |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 837 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 838 | ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0); |
| 839 | if (ret) { |
| 840 | ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret); |
| 841 | return ret; |
| 842 | } |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 843 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 844 | return ret; |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 845 | } |
| 846 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 847 | static int hwc_initialize_display(struct hwc_context_t *ctx, int display) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 848 | struct hwc_drm_display *hd = NULL; |
| 849 | int ret = hwc_get_drm_display(ctx, display, &hd); |
| 850 | if (ret) |
| 851 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 852 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 853 | hd->ctx = ctx; |
| 854 | hd->display = display; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 855 | hd->enable_vsync_events = false; |
| 856 | hd->vsync_sequence = 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 857 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 858 | ret = pthread_mutex_init(&hd->flip_lock, NULL); |
| 859 | if (ret) { |
| 860 | ALOGE("Failed to initialize flip lock %d", ret); |
| 861 | return ret; |
| 862 | } |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 863 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 864 | ret = pthread_cond_init(&hd->flip_cond, NULL); |
| 865 | if (ret) { |
| 866 | ALOGE("Failed to intiialize flip condition %d", ret); |
| 867 | pthread_mutex_destroy(&hd->flip_lock); |
| 868 | return ret; |
| 869 | } |
Sean Paul | 814bddb | 2015-03-03 17:46:19 -0500 | [diff] [blame] | 870 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 871 | ret = sw_sync_timeline_create(); |
| 872 | if (ret < 0) { |
| 873 | ALOGE("Failed to create sw sync timeline %d", ret); |
| 874 | pthread_cond_destroy(&hd->flip_cond); |
| 875 | pthread_mutex_destroy(&hd->flip_lock); |
| 876 | return ret; |
| 877 | } |
| 878 | hd->timeline_fd = ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 879 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 880 | /* |
| 881 | * Initialize timeline_next to 1, because point 0 will be the very first |
| 882 | * set operation. Since we increment every time set() is called, |
| 883 | * initializing to 0 would cause an off-by-one error where |
| 884 | * surfaceflinger would composite on the front buffer. |
| 885 | */ |
| 886 | hd->timeline_next = 1; |
Sean Paul | e147a2a | 2015-02-22 17:55:43 -0500 | [diff] [blame] | 887 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 888 | ret = hwc_set_initial_config(hd); |
| 889 | if (ret) { |
| 890 | ALOGE("Failed to set initial config for d=%d ret=%d", display, ret); |
| 891 | close(hd->timeline_fd); |
| 892 | pthread_cond_destroy(&hd->flip_cond); |
| 893 | pthread_mutex_destroy(&hd->flip_lock); |
| 894 | return ret; |
| 895 | } |
Sean Paul | f1dc191 | 2015-01-24 01:34:31 -0500 | [diff] [blame] | 896 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 897 | ret = hwc_initialize_worker(&hd->set_worker, hwc_set_worker, hd); |
| 898 | if (ret) { |
| 899 | ALOGE("Failed to create set worker %d\n", ret); |
| 900 | close(hd->timeline_fd); |
| 901 | pthread_cond_destroy(&hd->flip_cond); |
| 902 | pthread_mutex_destroy(&hd->flip_lock); |
| 903 | return ret; |
| 904 | } |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 905 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 906 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 907 | } |
| 908 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 909 | static void hwc_free_conn_list(drmModeConnectorPtr *conn_list, int num_conn) { |
| 910 | for (int i = 0; i < num_conn; ++i) { |
| 911 | if (conn_list[i]) |
| 912 | drmModeFreeConnector(conn_list[i]); |
| 913 | } |
| 914 | free(conn_list); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 915 | } |
| 916 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 917 | static int hwc_enumerate_displays(struct hwc_context_t *ctx) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 918 | int ret; |
| 919 | for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors(); |
| 920 | c != ctx->drm.end_connectors(); ++c) { |
| 921 | ret = hwc_initialize_display(ctx, (*c)->display()); |
| 922 | if (ret) { |
| 923 | ALOGE("Failed to initialize display %d", (*c)->display()); |
| 924 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 925 | } |
| 926 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 927 | |
| 928 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 929 | } |
| 930 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 931 | static int hwc_device_open(const struct hw_module_t *module, const char *name, |
| 932 | struct hw_device_t **dev) { |
| 933 | if (strcmp(name, HWC_HARDWARE_COMPOSER)) { |
| 934 | ALOGE("Invalid module name- %s", name); |
| 935 | return -EINVAL; |
| 936 | } |
| 937 | |
| 938 | struct hwc_context_t *ctx = new hwc_context_t(); |
| 939 | if (!ctx) { |
| 940 | ALOGE("Failed to allocate hwc context"); |
| 941 | return -ENOMEM; |
| 942 | } |
| 943 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 944 | int ret = ctx->drm.Init(); |
| 945 | if (ret) { |
| 946 | ALOGE("Can't initialize Drm object %d", ret); |
| 947 | delete ctx; |
| 948 | return ret; |
| 949 | } |
| 950 | |
| 951 | ret = hwc_import_init(&ctx->import_ctx); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 952 | if (ret) { |
| 953 | ALOGE("Failed to initialize import context"); |
| 954 | delete ctx; |
| 955 | return ret; |
| 956 | } |
| 957 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 958 | ret = hwc_enumerate_displays(ctx); |
| 959 | if (ret) { |
| 960 | ALOGE("Failed to enumerate displays: %s", strerror(ret)); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 961 | delete ctx; |
| 962 | return ret; |
| 963 | } |
| 964 | |
| 965 | ret = hwc_initialize_worker(&ctx->event_worker, hwc_event_worker, ctx); |
| 966 | if (ret) { |
| 967 | ALOGE("Failed to create event worker %d\n", ret); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 968 | delete ctx; |
| 969 | return ret; |
| 970 | } |
| 971 | |
| 972 | ctx->device.common.tag = HARDWARE_DEVICE_TAG; |
| 973 | ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4; |
| 974 | ctx->device.common.module = const_cast<hw_module_t *>(module); |
| 975 | ctx->device.common.close = hwc_device_close; |
| 976 | |
| 977 | ctx->device.prepare = hwc_prepare; |
| 978 | ctx->device.set = hwc_set; |
| 979 | ctx->device.eventControl = hwc_event_control; |
| 980 | ctx->device.setPowerMode = hwc_set_power_mode; |
| 981 | ctx->device.query = hwc_query; |
| 982 | ctx->device.registerProcs = hwc_register_procs; |
| 983 | ctx->device.getDisplayConfigs = hwc_get_display_configs; |
| 984 | ctx->device.getDisplayAttributes = hwc_get_display_attributes; |
| 985 | ctx->device.getActiveConfig = hwc_get_active_config; |
| 986 | ctx->device.setActiveConfig = hwc_set_active_config; |
| 987 | ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */ |
| 988 | |
| 989 | *dev = &ctx->device.common; |
| 990 | |
| 991 | return 0; |
| 992 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 993 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 994 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 995 | static struct hw_module_methods_t hwc_module_methods = { |
| 996 | open : android::hwc_device_open |
| 997 | }; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 998 | |
| 999 | hwc_module_t HAL_MODULE_INFO_SYM = { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 1000 | common : { |
| 1001 | tag : HARDWARE_MODULE_TAG, |
| 1002 | version_major : 1, |
| 1003 | version_minor : 0, |
| 1004 | id : HWC_HARDWARE_MODULE_ID, |
| 1005 | name : "DRM hwcomposer module", |
| 1006 | author : "The Android Open Source Project", |
| 1007 | methods : &hwc_module_methods, |
| 1008 | dso : NULL, |
| 1009 | reserved : {0}, |
| 1010 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1011 | }; |