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 | |
Stéphane Marchesin | be98c8c | 2015-06-23 16:18:10 -0700 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 18 | #define LOG_TAG "hwcomposer-drm" |
| 19 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 20 | #include "drm_hwcomposer.h" |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 21 | #include "drmresources.h" |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 22 | #include "importer.h" |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 23 | #include "virtualcompositorworker.h" |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 24 | #include "vsyncworker.h" |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 25 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | |
| 28 | #include <map> |
| 29 | #include <vector> |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 30 | #include <sstream> |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 31 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 32 | #include <errno.h> |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 33 | #include <fcntl.h> |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 34 | #include <pthread.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 35 | #include <sys/param.h> |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 36 | #include <sys/resource.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 37 | #include <xf86drm.h> |
| 38 | #include <xf86drmMode.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 39 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 40 | #include <cutils/log.h> |
| 41 | #include <cutils/properties.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 42 | #include <hardware/hardware.h> |
| 43 | #include <hardware/hwcomposer.h> |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 44 | #include <sw_sync.h> |
| 45 | #include <sync/sync.h> |
Stéphane Marchesin | be98c8c | 2015-06-23 16:18:10 -0700 | [diff] [blame] | 46 | #include <utils/Trace.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 47 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 48 | #define UM_PER_INCH 25400 |
Stéphane Marchesin | cb3f984 | 2015-06-19 14:50:45 -0700 | [diff] [blame] | 49 | #define HWC_FB_BUFFERS 3 |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 50 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 51 | namespace android { |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 52 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 53 | class DummySwSyncTimeline { |
| 54 | public: |
| 55 | int Init() { |
| 56 | int ret = timeline_fd_.Set(sw_sync_timeline_create()); |
| 57 | if (ret < 0) |
| 58 | return ret; |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | UniqueFd CreateDummyFence() { |
| 63 | int ret = sw_sync_fence_create(timeline_fd_.get(), "dummy fence", |
| 64 | timeline_pt_ + 1); |
| 65 | if (ret < 0) { |
| 66 | ALOGE("Failed to create dummy fence %d", ret); |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | UniqueFd ret_fd(ret); |
| 71 | |
| 72 | ret = sw_sync_timeline_inc(timeline_fd_.get(), 1); |
| 73 | if (ret) { |
| 74 | ALOGE("Failed to increment dummy sync timeline %d", ret); |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | ++timeline_pt_; |
| 79 | return ret_fd; |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | UniqueFd timeline_fd_; |
| 84 | int timeline_pt_ = 0; |
| 85 | }; |
| 86 | |
| 87 | struct CheckedOutputFd { |
| 88 | CheckedOutputFd(int *fd, const char *description, |
| 89 | DummySwSyncTimeline &timeline) |
| 90 | : fd_(fd), description_(description), timeline_(timeline) { |
| 91 | } |
| 92 | CheckedOutputFd(CheckedOutputFd &&rhs) |
| 93 | : description_(rhs.description_), timeline_(rhs.timeline_) { |
| 94 | std::swap(fd_, rhs.fd_); |
| 95 | } |
| 96 | |
| 97 | CheckedOutputFd &operator=(const CheckedOutputFd &rhs) = delete; |
| 98 | |
| 99 | ~CheckedOutputFd() { |
| 100 | if (fd_ == NULL) |
| 101 | return; |
| 102 | |
| 103 | if (*fd_ >= 0) |
| 104 | return; |
| 105 | |
| 106 | *fd_ = timeline_.CreateDummyFence().Release(); |
| 107 | |
| 108 | if (*fd_ < 0) |
| 109 | ALOGE("Failed to fill %s (%p == %d) before destruction", |
| 110 | description_.c_str(), fd_, *fd_); |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | int *fd_ = NULL; |
| 115 | std::string description_; |
| 116 | DummySwSyncTimeline &timeline_; |
| 117 | }; |
| 118 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 119 | typedef struct hwc_drm_display { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 120 | struct hwc_context_t *ctx; |
| 121 | int display; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 122 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 123 | std::vector<uint32_t> config_ids; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 124 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 125 | VSyncWorker vsync_worker; |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 126 | } hwc_drm_display_t; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 127 | |
| 128 | struct hwc_context_t { |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 129 | // map of display:hwc_drm_display_t |
| 130 | typedef std::map<int, hwc_drm_display_t> DisplayMap; |
| 131 | typedef DisplayMap::iterator DisplayMapIter; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 132 | |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 133 | hwc_context_t() : procs(NULL), importer(NULL), use_framebuffer_target(false) { |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | ~hwc_context_t() { |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 137 | virtual_compositor_worker.Exit(); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 138 | delete importer; |
| 139 | } |
| 140 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 141 | hwc_composer_device_1_t device; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 142 | hwc_procs_t const *procs; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 143 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 144 | DisplayMap displays; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 145 | DrmResources drm; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 146 | Importer *importer; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 147 | const gralloc_module_t *gralloc; |
| 148 | DummySwSyncTimeline dummy_timeline; |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 149 | bool use_framebuffer_target; |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 150 | VirtualCompositorWorker virtual_compositor_worker; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 151 | }; |
| 152 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 153 | static native_handle_t *dup_buffer_handle(buffer_handle_t handle) { |
| 154 | native_handle_t *new_handle = |
| 155 | native_handle_create(handle->numFds, handle->numInts); |
| 156 | if (new_handle == NULL) |
| 157 | return NULL; |
| 158 | |
| 159 | const int *old_data = handle->data; |
| 160 | int *new_data = new_handle->data; |
| 161 | for (int i = 0; i < handle->numFds; i++) { |
| 162 | *new_data = dup(*old_data); |
| 163 | old_data++; |
| 164 | new_data++; |
| 165 | } |
| 166 | memcpy(new_data, old_data, sizeof(int) * handle->numInts); |
| 167 | |
| 168 | return new_handle; |
| 169 | } |
| 170 | |
| 171 | static void free_buffer_handle(native_handle_t *handle) { |
| 172 | int ret = native_handle_close(handle); |
| 173 | if (ret) |
| 174 | ALOGE("Failed to close native handle %d", ret); |
| 175 | ret = native_handle_delete(handle); |
| 176 | if (ret) |
| 177 | ALOGE("Failed to delete native handle %d", ret); |
| 178 | } |
| 179 | |
| 180 | OutputFd &OutputFd::operator=(OutputFd &&rhs) { |
| 181 | if (fd_ == NULL) { |
| 182 | std::swap(fd_, rhs.fd_); |
| 183 | } else { |
| 184 | if (*fd_ < 0) { |
| 185 | ALOGE("Failed to fill OutputFd %p before assignment", fd_); |
| 186 | } |
| 187 | fd_ = rhs.fd_; |
| 188 | rhs.fd_ = NULL; |
| 189 | } |
| 190 | |
| 191 | return *this; |
| 192 | } |
| 193 | |
Zach Reizner | f99d53f | 2015-10-09 13:02:55 -0700 | [diff] [blame] | 194 | const hwc_drm_bo *DrmHwcBuffer::operator->() const { |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 195 | if (importer_ == NULL) { |
Zach Reizner | f99d53f | 2015-10-09 13:02:55 -0700 | [diff] [blame] | 196 | ALOGE("Access of non-existent BO"); |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 197 | exit(1); |
| 198 | return NULL; |
| 199 | } |
| 200 | return &bo_; |
| 201 | } |
| 202 | |
| 203 | void DrmHwcBuffer::Clear() { |
| 204 | if (importer_ != NULL) { |
| 205 | importer_->ReleaseBuffer(&bo_); |
| 206 | importer_ = NULL; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) { |
| 211 | hwc_drm_bo tmp_bo; |
| 212 | |
| 213 | int ret = importer->ImportBuffer(handle, &tmp_bo); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | |
| 217 | if (importer_ != NULL) { |
| 218 | importer_->ReleaseBuffer(&bo_); |
| 219 | } |
| 220 | |
| 221 | importer_ = importer; |
| 222 | |
| 223 | bo_ = tmp_bo; |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle, |
| 229 | const gralloc_module_t *gralloc) { |
| 230 | native_handle_t *handle_copy = dup_buffer_handle(handle); |
| 231 | if (handle_copy == NULL) { |
| 232 | ALOGE("Failed to duplicate handle"); |
| 233 | return -ENOMEM; |
| 234 | } |
| 235 | |
| 236 | int ret = gralloc->registerBuffer(gralloc, handle_copy); |
| 237 | if (ret) { |
| 238 | ALOGE("Failed to register buffer handle %d", ret); |
| 239 | free_buffer_handle(handle_copy); |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | Clear(); |
| 244 | |
| 245 | gralloc_ = gralloc; |
| 246 | handle_ = handle_copy; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | DrmHwcNativeHandle::~DrmHwcNativeHandle() { |
| 252 | Clear(); |
| 253 | } |
| 254 | |
| 255 | void DrmHwcNativeHandle::Clear() { |
| 256 | if (gralloc_ != NULL && handle_ != NULL) { |
| 257 | gralloc_->unregisterBuffer(gralloc_, handle_); |
| 258 | free_buffer_handle(handle_); |
| 259 | gralloc_ = NULL; |
| 260 | handle_ = NULL; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | int DrmHwcLayer::InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer, |
| 265 | const gralloc_module_t *gralloc) { |
| 266 | sf_handle = sf_layer->handle; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 267 | alpha = sf_layer->planeAlpha; |
| 268 | |
Zach Reizner | 7e88be9 | 2015-10-12 15:20:33 -0700 | [diff] [blame] | 269 | source_crop = DrmHwcRect<float>( |
| 270 | sf_layer->sourceCropf.left, sf_layer->sourceCropf.top, |
| 271 | sf_layer->sourceCropf.right, sf_layer->sourceCropf.bottom); |
| 272 | display_frame = DrmHwcRect<int>( |
| 273 | sf_layer->displayFrame.left, sf_layer->displayFrame.top, |
| 274 | sf_layer->displayFrame.right, sf_layer->displayFrame.bottom); |
| 275 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 276 | switch (sf_layer->transform) { |
| 277 | case 0: |
| 278 | transform = DrmHwcTransform::kIdentity; |
| 279 | break; |
| 280 | case HWC_TRANSFORM_FLIP_H: |
| 281 | transform = DrmHwcTransform::kFlipH; |
| 282 | break; |
| 283 | case HWC_TRANSFORM_FLIP_V: |
| 284 | transform = DrmHwcTransform::kFlipV; |
| 285 | break; |
| 286 | case HWC_TRANSFORM_ROT_90: |
| 287 | transform = DrmHwcTransform::kRotate90; |
| 288 | break; |
| 289 | case HWC_TRANSFORM_ROT_180: |
| 290 | transform = DrmHwcTransform::kRotate180; |
| 291 | break; |
| 292 | case HWC_TRANSFORM_ROT_270: |
| 293 | transform = DrmHwcTransform::kRotate270; |
| 294 | break; |
| 295 | default: |
| 296 | ALOGE("Invalid transform in hwc_layer_1_t %d", sf_layer->transform); |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | |
| 300 | switch (sf_layer->blending) { |
| 301 | case HWC_BLENDING_NONE: |
| 302 | blending = DrmHwcBlending::kNone; |
| 303 | break; |
| 304 | case HWC_BLENDING_PREMULT: |
| 305 | blending = DrmHwcBlending::kPreMult; |
| 306 | break; |
| 307 | case HWC_BLENDING_COVERAGE: |
| 308 | blending = DrmHwcBlending::kCoverage; |
| 309 | break; |
| 310 | default: |
| 311 | ALOGE("Invalid blending in hwc_layer_1_t %d", sf_layer->blending); |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | |
Zach Reizner | 7e88be9 | 2015-10-12 15:20:33 -0700 | [diff] [blame] | 315 | int ret = buffer.ImportBuffer(sf_layer->handle, importer); |
| 316 | if (ret) |
| 317 | return ret; |
| 318 | |
| 319 | ret = handle.CopyBufferHandle(sf_layer->handle, gralloc); |
| 320 | if (ret) |
| 321 | return ret; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Zach Reizner | c6520e4 | 2015-08-13 14:32:09 -0700 | [diff] [blame] | 326 | static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff, |
Sean Paul | 9046c64 | 2015-06-10 17:27:47 -0400 | [diff] [blame] | 327 | int buff_len) { |
| 328 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 329 | std::ostringstream out; |
| 330 | |
| 331 | ctx->drm.compositor()->Dump(&out); |
| 332 | std::string out_str = out.str(); |
| 333 | strncpy(buff, out_str.c_str(), std::min((size_t)buff_len, out_str.length())); |
| 334 | } |
| 335 | |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 336 | static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 337 | hwc_display_contents_1_t **display_contents) { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 338 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 339 | |
| 340 | char use_framebuffer_target[PROPERTY_VALUE_MAX]; |
| 341 | property_get("hwc.drm.use_framebuffer_target", use_framebuffer_target, "0"); |
| 342 | bool new_use_framebuffer_target = atoi(use_framebuffer_target); |
| 343 | if (ctx->use_framebuffer_target != new_use_framebuffer_target) |
| 344 | ALOGW("Starting to %s HWC_FRAMEBUFFER_TARGET", |
| 345 | new_use_framebuffer_target ? "use" : "not use"); |
| 346 | ctx->use_framebuffer_target = new_use_framebuffer_target; |
| 347 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 348 | for (int i = 0; i < (int)num_displays; ++i) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 349 | if (!display_contents[i]) |
| 350 | continue; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 351 | |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 352 | bool use_framebuffer_target = ctx->use_framebuffer_target; |
| 353 | if (i == HWC_DISPLAY_VIRTUAL) { |
| 354 | use_framebuffer_target = true; |
| 355 | } else { |
| 356 | DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i); |
| 357 | if (!crtc) { |
| 358 | ALOGE("No crtc for display %d", i); |
| 359 | return -ENODEV; |
| 360 | } |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 361 | } |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 362 | |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 363 | int num_layers = display_contents[i]->numHwLayers; |
| 364 | for (int j = 0; j < num_layers; j++) { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 365 | hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j]; |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 366 | |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 367 | if (!use_framebuffer_target) { |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 368 | if (layer->compositionType == HWC_FRAMEBUFFER) |
| 369 | layer->compositionType = HWC_OVERLAY; |
| 370 | } else { |
| 371 | switch (layer->compositionType) { |
| 372 | case HWC_OVERLAY: |
| 373 | case HWC_BACKGROUND: |
| 374 | case HWC_SIDEBAND: |
| 375 | case HWC_CURSOR_OVERLAY: |
| 376 | layer->compositionType = HWC_FRAMEBUFFER; |
| 377 | break; |
| 378 | } |
| 379 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 380 | } |
| 381 | } |
Sean Paul | dffca95 | 2015-02-04 10:19:55 -0800 | [diff] [blame] | 382 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 383 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 384 | } |
| 385 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 386 | static void hwc_add_layer_to_retire_fence( |
| 387 | hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) { |
Sean Paul | 0420612 | 2015-07-16 15:59:24 -0400 | [diff] [blame] | 388 | if (layer->releaseFenceFd < 0) |
| 389 | return; |
| 390 | |
| 391 | if (display_contents->retireFenceFd >= 0) { |
| 392 | int old_retire_fence = display_contents->retireFenceFd; |
Zach Reizner | c6520e4 | 2015-08-13 14:32:09 -0700 | [diff] [blame] | 393 | display_contents->retireFenceFd = |
| 394 | sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd); |
Sean Paul | 0420612 | 2015-07-16 15:59:24 -0400 | [diff] [blame] | 395 | close(old_retire_fence); |
| 396 | } else { |
| 397 | display_contents->retireFenceFd = dup(layer->releaseFenceFd); |
| 398 | } |
| 399 | } |
| 400 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 401 | static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays, |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 402 | hwc_display_contents_1_t **sf_display_contents) { |
Stéphane Marchesin | be98c8c | 2015-06-23 16:18:10 -0700 | [diff] [blame] | 403 | ATRACE_CALL(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 404 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 405 | int ret = 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 406 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 407 | std::vector<CheckedOutputFd> checked_output_fences; |
| 408 | std::vector<DrmHwcDisplayContents> displays_contents; |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 409 | std::vector<DrmCompositionDisplayLayersMap> layers_map; |
| 410 | std::vector<std::vector<size_t>> layers_indices; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 411 | displays_contents.reserve(num_displays); |
| 412 | // layers_map.reserve(num_displays); |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 413 | layers_indices.reserve(num_displays); |
| 414 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 415 | // Phase one does nothing that would cause errors. Only take ownership of FDs. |
| 416 | for (size_t i = 0; i < num_displays; ++i) { |
| 417 | hwc_display_contents_1_t *dc = sf_display_contents[i]; |
| 418 | displays_contents.emplace_back(); |
| 419 | DrmHwcDisplayContents &display_contents = displays_contents.back(); |
Haixia Shi | 7acc59b | 2015-09-30 10:57:54 -0700 | [diff] [blame] | 420 | layers_indices.emplace_back(); |
| 421 | std::vector<size_t> &indices_to_composite = layers_indices.back(); |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 422 | |
| 423 | if (!sf_display_contents[i]) |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 424 | continue; |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 425 | |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 426 | if (i == HWC_DISPLAY_VIRTUAL) { |
| 427 | ctx->virtual_compositor_worker.QueueComposite(dc); |
| 428 | continue; |
| 429 | } |
| 430 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 431 | std::ostringstream display_index_formatter; |
| 432 | display_index_formatter << "retire fence for display " << i; |
| 433 | std::string display_fence_description(display_index_formatter.str()); |
| 434 | checked_output_fences.emplace_back(&dc->retireFenceFd, |
| 435 | display_fence_description.c_str(), |
| 436 | ctx->dummy_timeline); |
| 437 | display_contents.retire_fence = OutputFd(&dc->retireFenceFd); |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 438 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 439 | size_t num_dc_layers = dc->numHwLayers; |
Haixia Shi | 1034bb7 | 2015-09-09 12:08:20 -0700 | [diff] [blame] | 440 | int framebuffer_target_index = -1; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 441 | for (size_t j = 0; j < num_dc_layers; ++j) { |
| 442 | hwc_layer_1_t *sf_layer = &dc->hwLayers[j]; |
| 443 | |
| 444 | display_contents.layers.emplace_back(); |
| 445 | DrmHwcLayer &layer = display_contents.layers.back(); |
| 446 | |
| 447 | if (sf_layer->flags & HWC_SKIP_LAYER) |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 448 | continue; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 449 | |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 450 | if (!ctx->use_framebuffer_target) { |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 451 | if (sf_layer->compositionType == HWC_OVERLAY) |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 452 | indices_to_composite.push_back(j); |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 453 | if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET) |
Haixia Shi | 1034bb7 | 2015-09-09 12:08:20 -0700 | [diff] [blame] | 454 | framebuffer_target_index = j; |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 455 | } else { |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 456 | if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET) |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 457 | indices_to_composite.push_back(j); |
| 458 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 459 | |
| 460 | layer.acquire_fence.Set(sf_layer->acquireFenceFd); |
| 461 | sf_layer->acquireFenceFd = -1; |
| 462 | |
| 463 | std::ostringstream layer_fence_formatter; |
| 464 | layer_fence_formatter << "release fence for layer " << j << " of display " |
| 465 | << i; |
| 466 | std::string layer_fence_description(layer_fence_formatter.str()); |
| 467 | checked_output_fences.emplace_back(&sf_layer->releaseFenceFd, |
| 468 | layer_fence_description.c_str(), |
| 469 | ctx->dummy_timeline); |
| 470 | layer.release_fence = OutputFd(&sf_layer->releaseFenceFd); |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 471 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 472 | |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 473 | if (ctx->use_framebuffer_target) { |
| 474 | if (indices_to_composite.size() != 1) { |
| 475 | ALOGE("Expected 1 (got %d) layer with HWC_FRAMEBUFFER_TARGET", |
| 476 | indices_to_composite.size()); |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 477 | ret = -EINVAL; |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 478 | } |
Haixia Shi | 1034bb7 | 2015-09-09 12:08:20 -0700 | [diff] [blame] | 479 | } else { |
| 480 | if (indices_to_composite.empty() && framebuffer_target_index >= 0) { |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 481 | hwc_layer_1_t *sf_layer = &dc->hwLayers[framebuffer_target_index]; |
| 482 | if (!sf_layer->handle || (sf_layer->flags & HWC_SKIP_LAYER)) { |
| 483 | ALOGE( |
| 484 | "Expected valid layer with HWC_FRAMEBUFFER_TARGET when all " |
| 485 | "HWC_OVERLAY layers are skipped."); |
| 486 | ret = -EINVAL; |
Haixia Shi | 1034bb7 | 2015-09-09 12:08:20 -0700 | [diff] [blame] | 487 | } |
| 488 | indices_to_composite.push_back(framebuffer_target_index); |
| 489 | } |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 490 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 491 | } |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 492 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 493 | if (ret) |
| 494 | return ret; |
| 495 | |
| 496 | for (size_t i = 0; i < num_displays; ++i) { |
| 497 | hwc_display_contents_1_t *dc = sf_display_contents[i]; |
| 498 | DrmHwcDisplayContents &display_contents = displays_contents[i]; |
| 499 | if (!sf_display_contents[i]) |
| 500 | continue; |
| 501 | |
| 502 | layers_map.emplace_back(); |
| 503 | DrmCompositionDisplayLayersMap &map = layers_map.back(); |
Zach Reizner | acba14b | 2015-10-13 18:19:26 -0700 | [diff] [blame] | 504 | map.display = i; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 505 | std::vector<size_t> &indices_to_composite = layers_indices[i]; |
| 506 | for (size_t j : indices_to_composite) { |
| 507 | hwc_layer_1_t *sf_layer = &dc->hwLayers[j]; |
| 508 | |
| 509 | DrmHwcLayer &layer = display_contents.layers[j]; |
| 510 | |
Zach Reizner | 7e88be9 | 2015-10-12 15:20:33 -0700 | [diff] [blame] | 511 | ret = layer.InitFromHwcLayer(sf_layer, ctx->importer, ctx->gralloc); |
| 512 | if (ret) { |
| 513 | ALOGE("Failed to init composition from layer %d", ret); |
| 514 | return ret; |
| 515 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 516 | map.layers.emplace_back(std::move(layer)); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | std::unique_ptr<DrmComposition> composition( |
| 521 | ctx->drm.compositor()->CreateComposition(ctx->importer)); |
| 522 | if (!composition) { |
| 523 | ALOGE("Drm composition init failed"); |
| 524 | return -EINVAL; |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 525 | } |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 526 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 527 | ret = composition->SetLayers(layers_map.size(), layers_map.data()); |
| 528 | if (ret) { |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 529 | return -EINVAL; |
| 530 | } |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 531 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 532 | ret = ctx->drm.compositor()->QueueComposition(std::move(composition)); |
| 533 | if (ret) { |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 534 | return -EINVAL; |
| 535 | } |
| 536 | |
Zach Reizner | 566da2b | 2015-10-06 15:39:09 -0700 | [diff] [blame] | 537 | for (size_t i = 0; i < num_displays; ++i) { |
| 538 | hwc_display_contents_1_t *dc = sf_display_contents[i]; |
| 539 | if (!dc) |
| 540 | continue; |
| 541 | |
| 542 | size_t num_dc_layers = dc->numHwLayers; |
| 543 | for (size_t j = 0; j < num_dc_layers; ++j) { |
| 544 | hwc_layer_1_t *layer = &dc->hwLayers[j]; |
| 545 | if (layer->flags & HWC_SKIP_LAYER) |
| 546 | continue; |
| 547 | hwc_add_layer_to_retire_fence(layer, dc); |
| 548 | } |
| 549 | } |
| 550 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 551 | composition.reset(NULL); |
| 552 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 553 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 554 | } |
| 555 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 556 | static int hwc_event_control(struct hwc_composer_device_1 *dev, int display, |
| 557 | int event, int enabled) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 558 | if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1)) |
| 559 | return -EINVAL; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 560 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 561 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 562 | hwc_drm_display_t *hd = &ctx->displays[display]; |
| 563 | return hd->vsync_worker.VSyncControl(enabled); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 564 | } |
| 565 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 566 | static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display, |
| 567 | int mode) { |
| 568 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 569 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 570 | uint64_t dpmsValue = 0; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 571 | switch (mode) { |
| 572 | case HWC_POWER_MODE_OFF: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 573 | dpmsValue = DRM_MODE_DPMS_OFF; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 574 | break; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 575 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 576 | /* We can't support dozing right now, so go full on */ |
| 577 | case HWC_POWER_MODE_DOZE: |
| 578 | case HWC_POWER_MODE_DOZE_SUSPEND: |
| 579 | case HWC_POWER_MODE_NORMAL: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 580 | dpmsValue = DRM_MODE_DPMS_ON; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 581 | break; |
| 582 | }; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 583 | return ctx->drm.SetDpmsMode(display, dpmsValue); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 584 | } |
| 585 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 586 | static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what, |
| 587 | int *value) { |
| 588 | switch (what) { |
| 589 | case HWC_BACKGROUND_LAYER_SUPPORTED: |
| 590 | *value = 0; /* TODO: We should do this */ |
| 591 | break; |
| 592 | case HWC_VSYNC_PERIOD: |
| 593 | ALOGW("Query for deprecated vsync value, returning 60Hz"); |
| 594 | *value = 1000 * 1000 * 1000 / 60; |
| 595 | break; |
| 596 | case HWC_DISPLAY_TYPES_SUPPORTED: |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 597 | *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL | HWC_DISPLAY_VIRTUAL; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 598 | break; |
| 599 | } |
| 600 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 601 | } |
| 602 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 603 | static void hwc_register_procs(struct hwc_composer_device_1 *dev, |
| 604 | hwc_procs_t const *procs) { |
| 605 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 606 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 607 | ctx->procs = procs; |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 608 | |
| 609 | for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin(); |
| 610 | iter != ctx->displays.end(); ++iter) { |
| 611 | iter->second.vsync_worker.SetProcs(procs); |
| 612 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 613 | } |
| 614 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 615 | static int hwc_get_display_configs(struct hwc_composer_device_1 *dev, |
| 616 | int display, uint32_t *configs, |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 617 | size_t *num_configs) { |
| 618 | if (!*num_configs) |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 619 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 620 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 621 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 622 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 623 | hd->config_ids.clear(); |
| 624 | |
| 625 | DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display); |
| 626 | if (!connector) { |
| 627 | ALOGE("Failed to get connector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 628 | return -ENODEV; |
| 629 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 630 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 631 | int ret = connector->UpdateModes(); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 632 | if (ret) { |
| 633 | ALOGE("Failed to update display modes %d", ret); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 634 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 635 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 636 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 637 | for (DrmConnector::ModeIter iter = connector->begin_modes(); |
| 638 | iter != connector->end_modes(); ++iter) { |
| 639 | size_t idx = hd->config_ids.size(); |
| 640 | if (idx == *num_configs) |
| 641 | break; |
| 642 | hd->config_ids.push_back(iter->id()); |
| 643 | configs[idx] = iter->id(); |
| 644 | } |
| 645 | *num_configs = hd->config_ids.size(); |
| 646 | return *num_configs == 0 ? -1 : 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 647 | } |
| 648 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 649 | static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev, |
| 650 | int display, uint32_t config, |
| 651 | const uint32_t *attributes, |
| 652 | int32_t *values) { |
| 653 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 654 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 655 | if (!c) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 656 | ALOGE("Failed to get DrmConnector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 657 | return -ENODEV; |
| 658 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 659 | DrmMode mode; |
| 660 | for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes(); |
| 661 | ++iter) { |
| 662 | if (iter->id() == config) { |
| 663 | mode = *iter; |
| 664 | break; |
| 665 | } |
| 666 | } |
| 667 | if (mode.id() == 0) { |
| 668 | ALOGE("Failed to find active mode for display %d", display); |
| 669 | return -ENOENT; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 670 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 671 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 672 | uint32_t mm_width = c->mm_width(); |
| 673 | uint32_t mm_height = c->mm_height(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 674 | for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) { |
| 675 | switch (attributes[i]) { |
| 676 | case HWC_DISPLAY_VSYNC_PERIOD: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 677 | values[i] = 1000 * 1000 * 1000 / mode.v_refresh(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 678 | break; |
| 679 | case HWC_DISPLAY_WIDTH: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 680 | values[i] = mode.h_display(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 681 | break; |
| 682 | case HWC_DISPLAY_HEIGHT: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 683 | values[i] = mode.v_display(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 684 | break; |
| 685 | case HWC_DISPLAY_DPI_X: |
| 686 | /* Dots per 1000 inches */ |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 687 | 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] | 688 | break; |
| 689 | case HWC_DISPLAY_DPI_Y: |
| 690 | /* Dots per 1000 inches */ |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 691 | values[i] = |
| 692 | mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 693 | break; |
| 694 | } |
| 695 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 696 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 697 | } |
| 698 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 699 | static int hwc_get_active_config(struct hwc_composer_device_1 *dev, |
| 700 | int display) { |
| 701 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 702 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(display); |
| 703 | if (!c) { |
| 704 | ALOGE("Failed to get DrmConnector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 705 | return -ENODEV; |
| 706 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 707 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 708 | DrmMode mode = c->active_mode(); |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 709 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 710 | for (size_t i = 0; i < hd->config_ids.size(); ++i) { |
| 711 | if (hd->config_ids[i] == mode.id()) |
| 712 | return i; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 713 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 714 | return -1; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 715 | } |
| 716 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 717 | static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display, |
| 718 | int index) { |
| 719 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 720 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 721 | if (index >= (int)hd->config_ids.size()) { |
| 722 | ALOGE("Invalid config index %d passed in", index); |
| 723 | return -EINVAL; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 724 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 725 | |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 726 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(display); |
| 727 | if (!c) { |
| 728 | ALOGE("Failed to get connector for display %d", display); |
| 729 | return -ENODEV; |
| 730 | } |
| 731 | DrmMode mode; |
| 732 | for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes(); |
| 733 | ++iter) { |
| 734 | if (iter->id() == hd->config_ids[index]) { |
| 735 | mode = *iter; |
| 736 | break; |
| 737 | } |
| 738 | } |
| 739 | if (mode.id() != hd->config_ids[index]) { |
| 740 | ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]); |
| 741 | return -ENOENT; |
| 742 | } |
| 743 | int ret = ctx->drm.SetDisplayActiveMode(display, mode); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 744 | if (ret) { |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 745 | ALOGE("Failed to set active config %d", ret); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 746 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [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_device_close(struct hw_device_t *dev) { |
| 752 | struct hwc_context_t *ctx = (struct hwc_context_t *)dev; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 753 | delete ctx; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 754 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 755 | } |
| 756 | |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 757 | /* |
| 758 | * TODO: This function sets the active config to the first one in the list. This |
| 759 | * should be fixed such that it selects the preferred mode for the display, or |
| 760 | * some other, saner, method of choosing the config. |
| 761 | */ |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 762 | static int hwc_set_initial_config(hwc_drm_display_t *hd) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 763 | uint32_t config; |
| 764 | size_t num_configs = 1; |
| 765 | int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config, |
| 766 | &num_configs); |
| 767 | if (ret || !num_configs) |
| 768 | return 0; |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 769 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 770 | ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0); |
| 771 | if (ret) { |
| 772 | ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret); |
| 773 | return ret; |
| 774 | } |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 775 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 776 | return ret; |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 777 | } |
| 778 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 779 | static int hwc_initialize_display(struct hwc_context_t *ctx, int display) { |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 780 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 781 | hd->ctx = ctx; |
| 782 | hd->display = display; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 783 | |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 784 | int ret = hwc_set_initial_config(hd); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 785 | if (ret) { |
| 786 | ALOGE("Failed to set initial config for d=%d ret=%d", display, ret); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 787 | return ret; |
| 788 | } |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 789 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 790 | ret = hd->vsync_worker.Init(&ctx->drm, display); |
| 791 | if (ret) { |
| 792 | ALOGE("Failed to create event worker for display %d %d\n", display, ret); |
| 793 | return ret; |
| 794 | } |
| 795 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 796 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 797 | } |
| 798 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 799 | static int hwc_enumerate_displays(struct hwc_context_t *ctx) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 800 | int ret; |
| 801 | for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors(); |
| 802 | c != ctx->drm.end_connectors(); ++c) { |
| 803 | ret = hwc_initialize_display(ctx, (*c)->display()); |
| 804 | if (ret) { |
| 805 | ALOGE("Failed to initialize display %d", (*c)->display()); |
| 806 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 807 | } |
| 808 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 809 | |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 810 | ret = ctx->virtual_compositor_worker.Init(); |
| 811 | if (ret) { |
| 812 | ALOGE("Failed to initialize virtual compositor worker"); |
| 813 | return ret; |
| 814 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 815 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 816 | } |
| 817 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 818 | static int hwc_device_open(const struct hw_module_t *module, const char *name, |
| 819 | struct hw_device_t **dev) { |
| 820 | if (strcmp(name, HWC_HARDWARE_COMPOSER)) { |
| 821 | ALOGE("Invalid module name- %s", name); |
| 822 | return -EINVAL; |
| 823 | } |
| 824 | |
| 825 | struct hwc_context_t *ctx = new hwc_context_t(); |
| 826 | if (!ctx) { |
| 827 | ALOGE("Failed to allocate hwc context"); |
| 828 | return -ENOMEM; |
| 829 | } |
| 830 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 831 | int ret = ctx->drm.Init(); |
| 832 | if (ret) { |
| 833 | ALOGE("Can't initialize Drm object %d", ret); |
| 834 | delete ctx; |
| 835 | return ret; |
| 836 | } |
| 837 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 838 | ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, |
| 839 | (const hw_module_t **)&ctx->gralloc); |
| 840 | if (ret) { |
| 841 | ALOGE("Failed to open gralloc module %d", ret); |
| 842 | delete ctx; |
| 843 | return ret; |
| 844 | } |
| 845 | |
| 846 | ret = ctx->dummy_timeline.Init(); |
| 847 | if (ret) { |
| 848 | ALOGE("Failed to create dummy sw sync timeline %d", ret); |
| 849 | return ret; |
| 850 | } |
| 851 | |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 852 | ctx->importer = Importer::CreateInstance(&ctx->drm); |
| 853 | if (!ctx->importer) { |
| 854 | ALOGE("Failed to create importer instance"); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 855 | delete ctx; |
| 856 | return ret; |
| 857 | } |
| 858 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 859 | ret = hwc_enumerate_displays(ctx); |
| 860 | if (ret) { |
| 861 | ALOGE("Failed to enumerate displays: %s", strerror(ret)); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 862 | delete ctx; |
| 863 | return ret; |
| 864 | } |
| 865 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 866 | ctx->device.common.tag = HARDWARE_DEVICE_TAG; |
| 867 | ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4; |
| 868 | ctx->device.common.module = const_cast<hw_module_t *>(module); |
| 869 | ctx->device.common.close = hwc_device_close; |
| 870 | |
Sean Paul | 9046c64 | 2015-06-10 17:27:47 -0400 | [diff] [blame] | 871 | ctx->device.dump = hwc_dump; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 872 | ctx->device.prepare = hwc_prepare; |
| 873 | ctx->device.set = hwc_set; |
| 874 | ctx->device.eventControl = hwc_event_control; |
| 875 | ctx->device.setPowerMode = hwc_set_power_mode; |
| 876 | ctx->device.query = hwc_query; |
| 877 | ctx->device.registerProcs = hwc_register_procs; |
| 878 | ctx->device.getDisplayConfigs = hwc_get_display_configs; |
| 879 | ctx->device.getDisplayAttributes = hwc_get_display_attributes; |
| 880 | ctx->device.getActiveConfig = hwc_get_active_config; |
| 881 | ctx->device.setActiveConfig = hwc_set_active_config; |
| 882 | ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */ |
| 883 | |
| 884 | *dev = &ctx->device.common; |
| 885 | |
| 886 | return 0; |
| 887 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 888 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 889 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 890 | static struct hw_module_methods_t hwc_module_methods = { |
| 891 | open : android::hwc_device_open |
| 892 | }; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 893 | |
| 894 | hwc_module_t HAL_MODULE_INFO_SYM = { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 895 | common : { |
| 896 | tag : HARDWARE_MODULE_TAG, |
| 897 | version_major : 1, |
| 898 | version_minor : 0, |
| 899 | id : HWC_HARDWARE_MODULE_ID, |
| 900 | name : "DRM hwcomposer module", |
| 901 | author : "The Android Open Source Project", |
| 902 | methods : &hwc_module_methods, |
| 903 | dso : NULL, |
| 904 | reserved : {0}, |
| 905 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 906 | }; |