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 | |
Zach Reizner | 7642c92 | 2015-10-29 10:11:16 -0700 | [diff] [blame] | 20 | #include "drmhwcomposer.h" |
Sean Paul | 3a69f73 | 2015-10-26 15:37:06 -0400 | [diff] [blame] | 21 | #include "drmeventlistener.h" |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 22 | #include "drmresources.h" |
Sean Paul | 6376996 | 2016-04-21 16:25:06 -0400 | [diff] [blame] | 23 | #include "platform.h" |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 24 | #include "virtualcompositorworker.h" |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 25 | #include "vsyncworker.h" |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 26 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | |
Sean Paul | 3a69f73 | 2015-10-26 15:37:06 -0400 | [diff] [blame] | 29 | #include <cinttypes> |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 30 | #include <map> |
| 31 | #include <vector> |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 32 | #include <sstream> |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 33 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 34 | #include <errno.h> |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 35 | #include <fcntl.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 36 | #include <sys/param.h> |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 37 | #include <sys/resource.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 38 | #include <xf86drm.h> |
| 39 | #include <xf86drmMode.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 40 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 41 | #include <cutils/log.h> |
| 42 | #include <cutils/properties.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 43 | #include <hardware/hardware.h> |
| 44 | #include <hardware/hwcomposer.h> |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 45 | #include <sw_sync.h> |
| 46 | #include <sync/sync.h> |
Stéphane Marchesin | be98c8c | 2015-06-23 16:18:10 -0700 | [diff] [blame] | 47 | #include <utils/Trace.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 48 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 49 | #define UM_PER_INCH 25400 |
| 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 | |
Sean Paul | 3a69f73 | 2015-10-26 15:37:06 -0400 | [diff] [blame] | 128 | class DrmHotplugHandler : public DrmEventHandler { |
| 129 | public: |
| 130 | void Init(DrmResources *drm, const struct hwc_procs *procs) { |
| 131 | drm_ = drm; |
| 132 | procs_ = procs; |
| 133 | } |
| 134 | |
| 135 | void HandleEvent(uint64_t timestamp_us) { |
| 136 | for (auto &conn : drm_->connectors()) { |
| 137 | drmModeConnection old_state = conn->state(); |
| 138 | |
| 139 | conn->UpdateModes(); |
| 140 | |
| 141 | drmModeConnection cur_state = conn->state(); |
| 142 | |
| 143 | if (cur_state == old_state) |
| 144 | continue; |
| 145 | |
| 146 | ALOGI("%s event @%" PRIu64 " for connector %u\n", |
| 147 | cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us, |
| 148 | conn->id()); |
| 149 | |
| 150 | if (cur_state == DRM_MODE_CONNECTED) { |
| 151 | // Take the first one, then look for the preferred |
| 152 | DrmMode mode = *(conn->modes().begin()); |
| 153 | for (auto &m : conn->modes()) { |
| 154 | if (m.type() & DRM_MODE_TYPE_PREFERRED) { |
| 155 | mode = m; |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | ALOGI("Setting mode %dx%d for connector %d\n", mode.h_display(), |
| 160 | mode.v_display(), conn->id()); |
| 161 | int ret = drm_->SetDisplayActiveMode(conn->display(), mode); |
| 162 | if (ret) { |
| 163 | ALOGE("Failed to set active config %d", ret); |
| 164 | return; |
| 165 | } |
| 166 | } else { |
| 167 | int ret = drm_->SetDpmsMode(conn->display(), DRM_MODE_DPMS_OFF); |
| 168 | if (ret) { |
| 169 | ALOGE("Failed to set dpms mode off %d", ret); |
| 170 | return; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | procs_->hotplug(procs_, conn->display(), |
| 175 | cur_state == DRM_MODE_CONNECTED ? 1 : 0); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | private: |
| 180 | DrmResources *drm_ = NULL; |
| 181 | const struct hwc_procs *procs_ = NULL; |
| 182 | }; |
| 183 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 184 | struct hwc_context_t { |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 185 | // map of display:hwc_drm_display_t |
| 186 | typedef std::map<int, hwc_drm_display_t> DisplayMap; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 187 | |
| 188 | ~hwc_context_t() { |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 189 | virtual_compositor_worker.Exit(); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 192 | hwc_composer_device_1_t device; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 193 | hwc_procs_t const *procs = NULL; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 194 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 195 | DisplayMap displays; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 196 | DrmResources drm; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 197 | std::unique_ptr<Importer> importer; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 198 | const gralloc_module_t *gralloc; |
| 199 | DummySwSyncTimeline dummy_timeline; |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 200 | VirtualCompositorWorker virtual_compositor_worker; |
Sean Paul | 3a69f73 | 2015-10-26 15:37:06 -0400 | [diff] [blame] | 201 | DrmHotplugHandler hotplug_handler; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 202 | }; |
| 203 | |
Sean Paul | a5df1de | 2016-02-10 10:00:08 -0800 | [diff] [blame] | 204 | class DrmVsyncCallback : public VsyncCallback { |
| 205 | public: |
| 206 | DrmVsyncCallback(hwc_procs_t const *procs) : procs_(procs) { |
| 207 | } |
| 208 | |
| 209 | void Callback(int display, int64_t timestamp) { |
| 210 | procs_->vsync(procs_, display, timestamp); |
| 211 | } |
| 212 | private: |
| 213 | hwc_procs_t const *procs_; |
| 214 | }; |
| 215 | |
Zach Reizner | c6520e4 | 2015-08-13 14:32:09 -0700 | [diff] [blame] | 216 | static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff, |
Sean Paul | 9046c64 | 2015-06-10 17:27:47 -0400 | [diff] [blame] | 217 | int buff_len) { |
| 218 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 219 | std::ostringstream out; |
| 220 | |
| 221 | ctx->drm.compositor()->Dump(&out); |
| 222 | std::string out_str = out.str(); |
Zach Reizner | fd6dc33 | 2015-10-13 21:12:48 -0700 | [diff] [blame] | 223 | strncpy(buff, out_str.c_str(), |
| 224 | std::min((size_t)buff_len, out_str.length() + 1)); |
| 225 | buff[buff_len - 1] = '\0'; |
Sean Paul | 9046c64 | 2015-06-10 17:27:47 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Sean Paul | bd61c8d | 2015-10-29 15:00:17 -0400 | [diff] [blame] | 228 | static bool hwc_skip_layer(const std::pair<int, int> &indices, int i) { |
| 229 | return indices.first >= 0 && i >= indices.first && i <= indices.second; |
| 230 | } |
| 231 | |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 232 | 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] | 233 | hwc_display_contents_1_t **display_contents) { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 234 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 235 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 236 | for (int i = 0; i < (int)num_displays; ++i) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 237 | if (!display_contents[i]) |
| 238 | continue; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 239 | |
Sean Paul | 6f82f1d | 2015-10-21 20:05:05 -0400 | [diff] [blame] | 240 | bool use_framebuffer_target = false; |
Sean Paul | b100837 | 2015-11-24 11:52:37 -0500 | [diff] [blame] | 241 | DrmMode mode; |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 242 | if (i == HWC_DISPLAY_VIRTUAL) { |
| 243 | use_framebuffer_target = true; |
| 244 | } else { |
Sean Paul | b100837 | 2015-11-24 11:52:37 -0500 | [diff] [blame] | 245 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(i); |
| 246 | if (!c) { |
| 247 | ALOGE("Failed to get DrmConnector for display %d", i); |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 248 | return -ENODEV; |
| 249 | } |
Sean Paul | b100837 | 2015-11-24 11:52:37 -0500 | [diff] [blame] | 250 | mode = c->active_mode(); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 251 | } |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 252 | |
Sean Paul | bd61c8d | 2015-10-29 15:00:17 -0400 | [diff] [blame] | 253 | // Since we can't composite HWC_SKIP_LAYERs by ourselves, we'll let SF |
| 254 | // handle all layers in between the first and last skip layers. So find the |
| 255 | // outer indices and mark everything in between as HWC_FRAMEBUFFER |
| 256 | std::pair<int, int> skip_layer_indices(-1, -1); |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 257 | int num_layers = display_contents[i]->numHwLayers; |
Sean Paul | bd61c8d | 2015-10-29 15:00:17 -0400 | [diff] [blame] | 258 | for (int j = 0; !use_framebuffer_target && j < num_layers; ++j) { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 259 | hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j]; |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 260 | |
Sean Paul | bd61c8d | 2015-10-29 15:00:17 -0400 | [diff] [blame] | 261 | if (!(layer->flags & HWC_SKIP_LAYER)) |
| 262 | continue; |
| 263 | |
| 264 | if (skip_layer_indices.first == -1) |
| 265 | skip_layer_indices.first = j; |
| 266 | skip_layer_indices.second = j; |
| 267 | } |
| 268 | |
| 269 | for (int j = 0; j < num_layers; ++j) { |
| 270 | hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j]; |
| 271 | |
| 272 | if (!use_framebuffer_target && !hwc_skip_layer(skip_layer_indices, j)) { |
Sean Paul | b100837 | 2015-11-24 11:52:37 -0500 | [diff] [blame] | 273 | // If the layer is off the screen, don't earmark it for an overlay. |
| 274 | // We'll leave it as-is, which effectively just drops it from the frame |
| 275 | const hwc_rect_t *frame = &layer->displayFrame; |
| 276 | if ((frame->right - frame->left) <= 0 || |
| 277 | (frame->bottom - frame->top) <= 0 || |
| 278 | frame->right <= 0 || frame->bottom <= 0 || |
| 279 | frame->left >= (int)mode.h_display() || |
| 280 | frame->top >= (int)mode.v_display()) |
| 281 | continue; |
| 282 | |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 283 | if (layer->compositionType == HWC_FRAMEBUFFER) |
| 284 | layer->compositionType = HWC_OVERLAY; |
| 285 | } else { |
| 286 | switch (layer->compositionType) { |
| 287 | case HWC_OVERLAY: |
| 288 | case HWC_BACKGROUND: |
| 289 | case HWC_SIDEBAND: |
| 290 | case HWC_CURSOR_OVERLAY: |
| 291 | layer->compositionType = HWC_FRAMEBUFFER; |
| 292 | break; |
| 293 | } |
| 294 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 295 | } |
| 296 | } |
Sean Paul | dffca95 | 2015-02-04 10:19:55 -0800 | [diff] [blame] | 297 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 298 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 299 | } |
| 300 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 301 | static void hwc_add_layer_to_retire_fence( |
| 302 | hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) { |
Sean Paul | 0420612 | 2015-07-16 15:59:24 -0400 | [diff] [blame] | 303 | if (layer->releaseFenceFd < 0) |
| 304 | return; |
| 305 | |
| 306 | if (display_contents->retireFenceFd >= 0) { |
| 307 | int old_retire_fence = display_contents->retireFenceFd; |
Zach Reizner | c6520e4 | 2015-08-13 14:32:09 -0700 | [diff] [blame] | 308 | display_contents->retireFenceFd = |
| 309 | sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd); |
Sean Paul | 0420612 | 2015-07-16 15:59:24 -0400 | [diff] [blame] | 310 | close(old_retire_fence); |
| 311 | } else { |
| 312 | display_contents->retireFenceFd = dup(layer->releaseFenceFd); |
| 313 | } |
| 314 | } |
| 315 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 316 | 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] | 317 | hwc_display_contents_1_t **sf_display_contents) { |
Stéphane Marchesin | be98c8c | 2015-06-23 16:18:10 -0700 | [diff] [blame] | 318 | ATRACE_CALL(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 319 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 320 | int ret = 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 321 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 322 | std::vector<CheckedOutputFd> checked_output_fences; |
| 323 | std::vector<DrmHwcDisplayContents> displays_contents; |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 324 | std::vector<DrmCompositionDisplayLayersMap> layers_map; |
| 325 | std::vector<std::vector<size_t>> layers_indices; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 326 | displays_contents.reserve(num_displays); |
| 327 | // layers_map.reserve(num_displays); |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 328 | layers_indices.reserve(num_displays); |
| 329 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 330 | // Phase one does nothing that would cause errors. Only take ownership of FDs. |
| 331 | for (size_t i = 0; i < num_displays; ++i) { |
| 332 | hwc_display_contents_1_t *dc = sf_display_contents[i]; |
| 333 | displays_contents.emplace_back(); |
| 334 | DrmHwcDisplayContents &display_contents = displays_contents.back(); |
Haixia Shi | 7acc59b | 2015-09-30 10:57:54 -0700 | [diff] [blame] | 335 | layers_indices.emplace_back(); |
| 336 | std::vector<size_t> &indices_to_composite = layers_indices.back(); |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 337 | |
| 338 | if (!sf_display_contents[i]) |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 339 | continue; |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 340 | |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 341 | if (i == HWC_DISPLAY_VIRTUAL) { |
| 342 | ctx->virtual_compositor_worker.QueueComposite(dc); |
| 343 | continue; |
| 344 | } |
| 345 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 346 | std::ostringstream display_index_formatter; |
| 347 | display_index_formatter << "retire fence for display " << i; |
| 348 | std::string display_fence_description(display_index_formatter.str()); |
| 349 | checked_output_fences.emplace_back(&dc->retireFenceFd, |
| 350 | display_fence_description.c_str(), |
| 351 | ctx->dummy_timeline); |
| 352 | display_contents.retire_fence = OutputFd(&dc->retireFenceFd); |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 353 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 354 | size_t num_dc_layers = dc->numHwLayers; |
Haixia Shi | 1034bb7 | 2015-09-09 12:08:20 -0700 | [diff] [blame] | 355 | int framebuffer_target_index = -1; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 356 | for (size_t j = 0; j < num_dc_layers; ++j) { |
| 357 | hwc_layer_1_t *sf_layer = &dc->hwLayers[j]; |
Sean Paul | bd61c8d | 2015-10-29 15:00:17 -0400 | [diff] [blame] | 358 | if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET) { |
| 359 | framebuffer_target_index = j; |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | for (size_t j = 0; j < num_dc_layers; ++j) { |
| 365 | hwc_layer_1_t *sf_layer = &dc->hwLayers[j]; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 366 | |
| 367 | display_contents.layers.emplace_back(); |
| 368 | DrmHwcLayer &layer = display_contents.layers.back(); |
| 369 | |
Sean Paul | bd61c8d | 2015-10-29 15:00:17 -0400 | [diff] [blame] | 370 | // In prepare() we marked all layers FRAMEBUFFER between SKIP_LAYER's. |
| 371 | // This means we should insert the FB_TARGET layer in the composition |
| 372 | // stack at the location of the first skip layer, and ignore the rest. |
| 373 | if (sf_layer->flags & HWC_SKIP_LAYER) { |
| 374 | if (framebuffer_target_index < 0) |
| 375 | continue; |
| 376 | int idx = framebuffer_target_index; |
| 377 | framebuffer_target_index = -1; |
| 378 | hwc_layer_1_t *fbt_layer = &dc->hwLayers[idx]; |
| 379 | if (!fbt_layer->handle || (fbt_layer->flags & HWC_SKIP_LAYER)) { |
| 380 | ALOGE("Invalid HWC_FRAMEBUFFER_TARGET with HWC_SKIP_LAYER present"); |
| 381 | continue; |
| 382 | } |
| 383 | indices_to_composite.push_back(idx); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 384 | continue; |
Sean Paul | bd61c8d | 2015-10-29 15:00:17 -0400 | [diff] [blame] | 385 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 386 | |
Sean Paul | 6f82f1d | 2015-10-21 20:05:05 -0400 | [diff] [blame] | 387 | if (sf_layer->compositionType == HWC_OVERLAY) |
| 388 | indices_to_composite.push_back(j); |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 389 | |
| 390 | layer.acquire_fence.Set(sf_layer->acquireFenceFd); |
| 391 | sf_layer->acquireFenceFd = -1; |
| 392 | |
| 393 | std::ostringstream layer_fence_formatter; |
| 394 | layer_fence_formatter << "release fence for layer " << j << " of display " |
| 395 | << i; |
| 396 | std::string layer_fence_description(layer_fence_formatter.str()); |
| 397 | checked_output_fences.emplace_back(&sf_layer->releaseFenceFd, |
| 398 | layer_fence_description.c_str(), |
| 399 | ctx->dummy_timeline); |
| 400 | layer.release_fence = OutputFd(&sf_layer->releaseFenceFd); |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 401 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 402 | |
Sean Paul | bd61c8d | 2015-10-29 15:00:17 -0400 | [diff] [blame] | 403 | // This is a catch-all in case we get a frame without any overlay layers, or |
| 404 | // skip layers, but with a value fb_target layer. This _shouldn't_ happen, |
| 405 | // but it's not ruled out by the hwc specification |
Sean Paul | 6f82f1d | 2015-10-21 20:05:05 -0400 | [diff] [blame] | 406 | if (indices_to_composite.empty() && framebuffer_target_index >= 0) { |
| 407 | hwc_layer_1_t *sf_layer = &dc->hwLayers[framebuffer_target_index]; |
| 408 | if (!sf_layer->handle || (sf_layer->flags & HWC_SKIP_LAYER)) { |
| 409 | ALOGE( |
| 410 | "Expected valid layer with HWC_FRAMEBUFFER_TARGET when all " |
| 411 | "HWC_OVERLAY layers are skipped."); |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 412 | ret = -EINVAL; |
Zach Reizner | 1946fa7 | 2015-08-14 11:14:38 -0700 | [diff] [blame] | 413 | } |
Sean Paul | 6f82f1d | 2015-10-21 20:05:05 -0400 | [diff] [blame] | 414 | indices_to_composite.push_back(framebuffer_target_index); |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 415 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 416 | } |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 417 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 418 | if (ret) |
| 419 | return ret; |
| 420 | |
| 421 | for (size_t i = 0; i < num_displays; ++i) { |
| 422 | hwc_display_contents_1_t *dc = sf_display_contents[i]; |
| 423 | DrmHwcDisplayContents &display_contents = displays_contents[i]; |
Haixia Shi | 2fddd37 | 2015-10-15 16:21:48 -0700 | [diff] [blame] | 424 | if (!sf_display_contents[i] || i == HWC_DISPLAY_VIRTUAL) |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 425 | continue; |
| 426 | |
| 427 | layers_map.emplace_back(); |
| 428 | DrmCompositionDisplayLayersMap &map = layers_map.back(); |
Zach Reizner | acba14b | 2015-10-13 18:19:26 -0700 | [diff] [blame] | 429 | map.display = i; |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 430 | map.geometry_changed = |
| 431 | (dc->flags & HWC_GEOMETRY_CHANGED) == HWC_GEOMETRY_CHANGED; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 432 | std::vector<size_t> &indices_to_composite = layers_indices[i]; |
| 433 | for (size_t j : indices_to_composite) { |
| 434 | hwc_layer_1_t *sf_layer = &dc->hwLayers[j]; |
| 435 | |
| 436 | DrmHwcLayer &layer = display_contents.layers[j]; |
| 437 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 438 | ret = layer.InitFromHwcLayer(sf_layer, ctx->importer.get(), ctx->gralloc); |
Zach Reizner | 7e88be9 | 2015-10-12 15:20:33 -0700 | [diff] [blame] | 439 | if (ret) { |
| 440 | ALOGE("Failed to init composition from layer %d", ret); |
| 441 | return ret; |
| 442 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 443 | map.layers.emplace_back(std::move(layer)); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | std::unique_ptr<DrmComposition> composition( |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 448 | ctx->drm.compositor()->CreateComposition(ctx->importer.get())); |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 449 | if (!composition) { |
| 450 | ALOGE("Drm composition init failed"); |
| 451 | return -EINVAL; |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 452 | } |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 453 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 454 | ret = composition->SetLayers(layers_map.size(), layers_map.data()); |
| 455 | if (ret) { |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 456 | return -EINVAL; |
| 457 | } |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 458 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 459 | ret = ctx->drm.compositor()->QueueComposition(std::move(composition)); |
| 460 | if (ret) { |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 461 | return -EINVAL; |
| 462 | } |
| 463 | |
Zach Reizner | 566da2b | 2015-10-06 15:39:09 -0700 | [diff] [blame] | 464 | for (size_t i = 0; i < num_displays; ++i) { |
| 465 | hwc_display_contents_1_t *dc = sf_display_contents[i]; |
| 466 | if (!dc) |
| 467 | continue; |
| 468 | |
| 469 | size_t num_dc_layers = dc->numHwLayers; |
| 470 | for (size_t j = 0; j < num_dc_layers; ++j) { |
| 471 | hwc_layer_1_t *layer = &dc->hwLayers[j]; |
| 472 | if (layer->flags & HWC_SKIP_LAYER) |
| 473 | continue; |
| 474 | hwc_add_layer_to_retire_fence(layer, dc); |
| 475 | } |
| 476 | } |
| 477 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 478 | composition.reset(NULL); |
| 479 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 480 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 481 | } |
| 482 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 483 | static int hwc_event_control(struct hwc_composer_device_1 *dev, int display, |
| 484 | int event, int enabled) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 485 | if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1)) |
| 486 | return -EINVAL; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 487 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 488 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 489 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Adrian Salido | e5c7565 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 490 | hd->vsync_worker.VSyncControl(enabled); |
| 491 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 492 | } |
| 493 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 494 | static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display, |
| 495 | int mode) { |
| 496 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 497 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 498 | uint64_t dpmsValue = 0; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 499 | switch (mode) { |
| 500 | case HWC_POWER_MODE_OFF: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 501 | dpmsValue = DRM_MODE_DPMS_OFF; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 502 | break; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 503 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 504 | /* We can't support dozing right now, so go full on */ |
| 505 | case HWC_POWER_MODE_DOZE: |
| 506 | case HWC_POWER_MODE_DOZE_SUSPEND: |
| 507 | case HWC_POWER_MODE_NORMAL: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 508 | dpmsValue = DRM_MODE_DPMS_ON; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 509 | break; |
| 510 | }; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 511 | return ctx->drm.SetDpmsMode(display, dpmsValue); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 512 | } |
| 513 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 514 | static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what, |
| 515 | int *value) { |
| 516 | switch (what) { |
| 517 | case HWC_BACKGROUND_LAYER_SUPPORTED: |
| 518 | *value = 0; /* TODO: We should do this */ |
| 519 | break; |
| 520 | case HWC_VSYNC_PERIOD: |
| 521 | ALOGW("Query for deprecated vsync value, returning 60Hz"); |
| 522 | *value = 1000 * 1000 * 1000 / 60; |
| 523 | break; |
| 524 | case HWC_DISPLAY_TYPES_SUPPORTED: |
Haixia Shi | 2fddd37 | 2015-10-15 16:21:48 -0700 | [diff] [blame] | 525 | *value = HWC_DISPLAY_PRIMARY_BIT | HWC_DISPLAY_EXTERNAL_BIT | |
| 526 | HWC_DISPLAY_VIRTUAL_BIT; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 527 | break; |
| 528 | } |
| 529 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 530 | } |
| 531 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 532 | static void hwc_register_procs(struct hwc_composer_device_1 *dev, |
| 533 | hwc_procs_t const *procs) { |
| 534 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 535 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 536 | ctx->procs = procs; |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 537 | |
Sean Paul | a5df1de | 2016-02-10 10:00:08 -0800 | [diff] [blame] | 538 | for (std::pair<const int, hwc_drm_display> &display_entry : ctx->displays) { |
| 539 | auto callback = std::make_shared<DrmVsyncCallback>(procs); |
| 540 | display_entry.second.vsync_worker.RegisterCallback(std::move(callback)); |
| 541 | } |
Sean Paul | 3a69f73 | 2015-10-26 15:37:06 -0400 | [diff] [blame] | 542 | |
| 543 | ctx->hotplug_handler.Init(&ctx->drm, procs); |
| 544 | ctx->drm.event_listener()->RegisterHotplugHandler(&ctx->hotplug_handler); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 545 | } |
| 546 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 547 | static int hwc_get_display_configs(struct hwc_composer_device_1 *dev, |
| 548 | int display, uint32_t *configs, |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 549 | size_t *num_configs) { |
| 550 | if (!*num_configs) |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 551 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 552 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 553 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 554 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 555 | hd->config_ids.clear(); |
| 556 | |
| 557 | DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display); |
| 558 | if (!connector) { |
| 559 | ALOGE("Failed to get connector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 560 | return -ENODEV; |
| 561 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 562 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 563 | int ret = connector->UpdateModes(); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 564 | if (ret) { |
| 565 | ALOGE("Failed to update display modes %d", ret); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 566 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 567 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 568 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 569 | for (const DrmMode &mode : connector->modes()) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 570 | size_t idx = hd->config_ids.size(); |
| 571 | if (idx == *num_configs) |
| 572 | break; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 573 | hd->config_ids.push_back(mode.id()); |
| 574 | configs[idx] = mode.id(); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 575 | } |
| 576 | *num_configs = hd->config_ids.size(); |
| 577 | return *num_configs == 0 ? -1 : 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 578 | } |
| 579 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 580 | static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev, |
| 581 | int display, uint32_t config, |
| 582 | const uint32_t *attributes, |
| 583 | int32_t *values) { |
| 584 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 585 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 586 | if (!c) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 587 | ALOGE("Failed to get DrmConnector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 588 | return -ENODEV; |
| 589 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 590 | DrmMode mode; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 591 | for (const DrmMode &conn_mode : c->modes()) { |
| 592 | if (conn_mode.id() == config) { |
| 593 | mode = conn_mode; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 594 | break; |
| 595 | } |
| 596 | } |
| 597 | if (mode.id() == 0) { |
| 598 | ALOGE("Failed to find active mode for display %d", display); |
| 599 | return -ENOENT; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 600 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 601 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 602 | uint32_t mm_width = c->mm_width(); |
| 603 | uint32_t mm_height = c->mm_height(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 604 | for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) { |
| 605 | switch (attributes[i]) { |
| 606 | case HWC_DISPLAY_VSYNC_PERIOD: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 607 | values[i] = 1000 * 1000 * 1000 / mode.v_refresh(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 608 | break; |
| 609 | case HWC_DISPLAY_WIDTH: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 610 | values[i] = mode.h_display(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 611 | break; |
| 612 | case HWC_DISPLAY_HEIGHT: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 613 | values[i] = mode.v_display(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 614 | break; |
| 615 | case HWC_DISPLAY_DPI_X: |
| 616 | /* Dots per 1000 inches */ |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 617 | 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] | 618 | break; |
| 619 | case HWC_DISPLAY_DPI_Y: |
| 620 | /* Dots per 1000 inches */ |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 621 | values[i] = |
| 622 | mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 623 | break; |
| 624 | } |
| 625 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 626 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 627 | } |
| 628 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 629 | static int hwc_get_active_config(struct hwc_composer_device_1 *dev, |
| 630 | int display) { |
| 631 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 632 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(display); |
| 633 | if (!c) { |
| 634 | ALOGE("Failed to get DrmConnector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 635 | return -ENODEV; |
| 636 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 637 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 638 | DrmMode mode = c->active_mode(); |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 639 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 640 | for (size_t i = 0; i < hd->config_ids.size(); ++i) { |
| 641 | if (hd->config_ids[i] == mode.id()) |
| 642 | return i; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 643 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 644 | return -1; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 645 | } |
| 646 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 647 | static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display, |
| 648 | int index) { |
| 649 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 650 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 651 | if (index >= (int)hd->config_ids.size()) { |
| 652 | ALOGE("Invalid config index %d passed in", index); |
| 653 | return -EINVAL; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 654 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 655 | |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 656 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(display); |
| 657 | if (!c) { |
| 658 | ALOGE("Failed to get connector for display %d", display); |
| 659 | return -ENODEV; |
| 660 | } |
Sean Paul | 3a69f73 | 2015-10-26 15:37:06 -0400 | [diff] [blame] | 661 | |
| 662 | if (c->state() != DRM_MODE_CONNECTED) |
| 663 | return -ENODEV; |
| 664 | |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 665 | DrmMode mode; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 666 | for (const DrmMode &conn_mode : c->modes()) { |
| 667 | if (conn_mode.id() == hd->config_ids[index]) { |
| 668 | mode = conn_mode; |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 669 | break; |
| 670 | } |
| 671 | } |
| 672 | if (mode.id() != hd->config_ids[index]) { |
| 673 | ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]); |
| 674 | return -ENOENT; |
| 675 | } |
| 676 | int ret = ctx->drm.SetDisplayActiveMode(display, mode); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 677 | if (ret) { |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 678 | ALOGE("Failed to set active config %d", ret); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 679 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 680 | } |
Sean Paul | 55584b5 | 2016-03-29 10:51:38 -0400 | [diff] [blame] | 681 | ret = ctx->drm.SetDpmsMode(display, DRM_MODE_DPMS_ON); |
| 682 | if (ret) { |
| 683 | ALOGE("Failed to set dpms mode on %d", ret); |
| 684 | return ret; |
| 685 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 686 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 687 | } |
| 688 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 689 | static int hwc_device_close(struct hw_device_t *dev) { |
| 690 | struct hwc_context_t *ctx = (struct hwc_context_t *)dev; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 691 | delete ctx; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 692 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 693 | } |
| 694 | |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 695 | /* |
| 696 | * TODO: This function sets the active config to the first one in the list. This |
| 697 | * should be fixed such that it selects the preferred mode for the display, or |
| 698 | * some other, saner, method of choosing the config. |
| 699 | */ |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 700 | static int hwc_set_initial_config(hwc_drm_display_t *hd) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 701 | uint32_t config; |
| 702 | size_t num_configs = 1; |
| 703 | int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config, |
| 704 | &num_configs); |
| 705 | if (ret || !num_configs) |
| 706 | return 0; |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 707 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 708 | ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0); |
| 709 | if (ret) { |
| 710 | ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret); |
| 711 | return ret; |
| 712 | } |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 713 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 714 | return ret; |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 715 | } |
| 716 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 717 | static int hwc_initialize_display(struct hwc_context_t *ctx, int display) { |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 718 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 719 | hd->ctx = ctx; |
| 720 | hd->display = display; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 721 | |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 722 | int ret = hwc_set_initial_config(hd); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 723 | if (ret) { |
| 724 | 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] | 725 | return ret; |
| 726 | } |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 727 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 728 | ret = hd->vsync_worker.Init(&ctx->drm, display); |
| 729 | if (ret) { |
| 730 | ALOGE("Failed to create event worker for display %d %d\n", display, ret); |
| 731 | return ret; |
| 732 | } |
| 733 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 734 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 735 | } |
| 736 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 737 | static int hwc_enumerate_displays(struct hwc_context_t *ctx) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 738 | int ret; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 739 | for (auto &conn : ctx->drm.connectors()) { |
| 740 | ret = hwc_initialize_display(ctx, conn->display()); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 741 | if (ret) { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 742 | ALOGE("Failed to initialize display %d", conn->display()); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 743 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 744 | } |
| 745 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 746 | |
Haixia Shi | d21f528 | 2015-10-05 14:35:09 -0700 | [diff] [blame] | 747 | ret = ctx->virtual_compositor_worker.Init(); |
| 748 | if (ret) { |
| 749 | ALOGE("Failed to initialize virtual compositor worker"); |
| 750 | return ret; |
| 751 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 752 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 753 | } |
| 754 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 755 | static int hwc_device_open(const struct hw_module_t *module, const char *name, |
| 756 | struct hw_device_t **dev) { |
| 757 | if (strcmp(name, HWC_HARDWARE_COMPOSER)) { |
| 758 | ALOGE("Invalid module name- %s", name); |
| 759 | return -EINVAL; |
| 760 | } |
| 761 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 762 | std::unique_ptr<hwc_context_t> ctx(new hwc_context_t()); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 763 | if (!ctx) { |
| 764 | ALOGE("Failed to allocate hwc context"); |
| 765 | return -ENOMEM; |
| 766 | } |
| 767 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 768 | int ret = ctx->drm.Init(); |
| 769 | if (ret) { |
| 770 | ALOGE("Can't initialize Drm object %d", ret); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 771 | return ret; |
| 772 | } |
| 773 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 774 | ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, |
| 775 | (const hw_module_t **)&ctx->gralloc); |
| 776 | if (ret) { |
| 777 | ALOGE("Failed to open gralloc module %d", ret); |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 778 | return ret; |
| 779 | } |
| 780 | |
| 781 | ret = ctx->dummy_timeline.Init(); |
| 782 | if (ret) { |
| 783 | ALOGE("Failed to create dummy sw sync timeline %d", ret); |
| 784 | return ret; |
| 785 | } |
| 786 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 787 | ctx->importer.reset(Importer::CreateInstance(&ctx->drm)); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 788 | if (!ctx->importer) { |
| 789 | ALOGE("Failed to create importer instance"); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 790 | return ret; |
| 791 | } |
| 792 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 793 | ret = hwc_enumerate_displays(ctx.get()); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 794 | if (ret) { |
| 795 | ALOGE("Failed to enumerate displays: %s", strerror(ret)); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 796 | return ret; |
| 797 | } |
| 798 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 799 | ctx->device.common.tag = HARDWARE_DEVICE_TAG; |
| 800 | ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4; |
| 801 | ctx->device.common.module = const_cast<hw_module_t *>(module); |
| 802 | ctx->device.common.close = hwc_device_close; |
| 803 | |
Sean Paul | 9046c64 | 2015-06-10 17:27:47 -0400 | [diff] [blame] | 804 | ctx->device.dump = hwc_dump; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 805 | ctx->device.prepare = hwc_prepare; |
| 806 | ctx->device.set = hwc_set; |
| 807 | ctx->device.eventControl = hwc_event_control; |
| 808 | ctx->device.setPowerMode = hwc_set_power_mode; |
| 809 | ctx->device.query = hwc_query; |
| 810 | ctx->device.registerProcs = hwc_register_procs; |
| 811 | ctx->device.getDisplayConfigs = hwc_get_display_configs; |
| 812 | ctx->device.getDisplayAttributes = hwc_get_display_attributes; |
| 813 | ctx->device.getActiveConfig = hwc_get_active_config; |
| 814 | ctx->device.setActiveConfig = hwc_set_active_config; |
| 815 | ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */ |
| 816 | |
| 817 | *dev = &ctx->device.common; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 818 | ctx.release(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 819 | |
| 820 | return 0; |
| 821 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 822 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 823 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 824 | static struct hw_module_methods_t hwc_module_methods = { |
Sean Paul | 5325e10 | 2016-03-29 13:55:35 -0400 | [diff] [blame] | 825 | .open = android::hwc_device_open |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 826 | }; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 827 | |
| 828 | hwc_module_t HAL_MODULE_INFO_SYM = { |
Sean Paul | 5325e10 | 2016-03-29 13:55:35 -0400 | [diff] [blame] | 829 | .common = { |
| 830 | .tag = HARDWARE_MODULE_TAG, |
| 831 | .version_major = 1, |
| 832 | .version_minor = 0, |
| 833 | .id = HWC_HARDWARE_MODULE_ID, |
| 834 | .name = "DRM hwcomposer module", |
| 835 | .author = "The Android Open Source Project", |
| 836 | .methods = &hwc_module_methods, |
| 837 | .dso = NULL, |
| 838 | .reserved = {0}, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 839 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 840 | }; |