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