blob: 4bc4670324fc2c726deb657f97590af1856f3020 [file] [log] [blame]
Sean Paule0c4c3d2015-01-20 16:56:04 -05001/*
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 Marchesinbe98c8c2015-06-23 16:18:10 -070017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Sean Paule0c4c3d2015-01-20 16:56:04 -050018#define LOG_TAG "hwcomposer-drm"
19
Zach Reizner7642c922015-10-29 10:11:16 -070020#include "drmhwcomposer.h"
Sean Paul3a69f732015-10-26 15:37:06 -040021#include "drmeventlistener.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040022#include "drmresources.h"
Sean Paul63769962016-04-21 16:25:06 -040023#include "platform.h"
Haixia Shid21f5282015-10-05 14:35:09 -070024#include "virtualcompositorworker.h"
Sean Paul4057be32015-05-13 06:23:09 -070025#include "vsyncworker.h"
Sean Paulef8f1f92015-04-29 16:05:23 -040026
Zach Reizner09807052015-08-13 14:53:41 -070027#include <stdlib.h>
28
Sean Paul3a69f732015-10-26 15:37:06 -040029#include <cinttypes>
Zach Reizner09807052015-08-13 14:53:41 -070030#include <map>
31#include <vector>
Zach Reizner4a253652015-09-10 18:30:54 -070032#include <sstream>
Zach Reizner09807052015-08-13 14:53:41 -070033
Sean Paule0c4c3d2015-01-20 16:56:04 -050034#include <errno.h>
Sean Paulef8f1f92015-04-29 16:05:23 -040035#include <fcntl.h>
Sean Paulef8f1f92015-04-29 16:05:23 -040036#include <pthread.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050037#include <sys/param.h>
Sean Paul9aa5ad32015-01-22 15:47:54 -050038#include <sys/resource.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050039#include <xf86drm.h>
40#include <xf86drmMode.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050041
Sean Paulef8f1f92015-04-29 16:05:23 -040042#include <cutils/log.h>
43#include <cutils/properties.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050044#include <hardware/hardware.h>
45#include <hardware/hwcomposer.h>
Zach Reizner4a253652015-09-10 18:30:54 -070046#include <sw_sync.h>
47#include <sync/sync.h>
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -070048#include <utils/Trace.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050049
Sean Paule0c4c3d2015-01-20 16:56:04 -050050#define UM_PER_INCH 25400
51
Sean Paul6a55e9f2015-04-30 15:31:06 -040052namespace android {
Sean Paule0c4c3d2015-01-20 16:56:04 -050053
Zach Reizner4a253652015-09-10 18:30:54 -070054class DummySwSyncTimeline {
55 public:
56 int Init() {
57 int ret = timeline_fd_.Set(sw_sync_timeline_create());
58 if (ret < 0)
59 return ret;
60 return 0;
61 }
62
63 UniqueFd CreateDummyFence() {
64 int ret = sw_sync_fence_create(timeline_fd_.get(), "dummy fence",
65 timeline_pt_ + 1);
66 if (ret < 0) {
67 ALOGE("Failed to create dummy fence %d", ret);
68 return ret;
69 }
70
71 UniqueFd ret_fd(ret);
72
73 ret = sw_sync_timeline_inc(timeline_fd_.get(), 1);
74 if (ret) {
75 ALOGE("Failed to increment dummy sync timeline %d", ret);
76 return ret;
77 }
78
79 ++timeline_pt_;
80 return ret_fd;
81 }
82
83 private:
84 UniqueFd timeline_fd_;
85 int timeline_pt_ = 0;
86};
87
88struct CheckedOutputFd {
89 CheckedOutputFd(int *fd, const char *description,
90 DummySwSyncTimeline &timeline)
91 : fd_(fd), description_(description), timeline_(timeline) {
92 }
93 CheckedOutputFd(CheckedOutputFd &&rhs)
94 : description_(rhs.description_), timeline_(rhs.timeline_) {
95 std::swap(fd_, rhs.fd_);
96 }
97
98 CheckedOutputFd &operator=(const CheckedOutputFd &rhs) = delete;
99
100 ~CheckedOutputFd() {
101 if (fd_ == NULL)
102 return;
103
104 if (*fd_ >= 0)
105 return;
106
107 *fd_ = timeline_.CreateDummyFence().Release();
108
109 if (*fd_ < 0)
110 ALOGE("Failed to fill %s (%p == %d) before destruction",
111 description_.c_str(), fd_, *fd_);
112 }
113
114 private:
115 int *fd_ = NULL;
116 std::string description_;
117 DummySwSyncTimeline &timeline_;
118};
119
Sean Paule42febf2015-05-07 11:35:29 -0700120typedef struct hwc_drm_display {
Sean Paulef8f1f92015-04-29 16:05:23 -0400121 struct hwc_context_t *ctx;
122 int display;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500123
Sean Paul6a55e9f2015-04-30 15:31:06 -0400124 std::vector<uint32_t> config_ids;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500125
Sean Paul4057be32015-05-13 06:23:09 -0700126 VSyncWorker vsync_worker;
Sean Paule42febf2015-05-07 11:35:29 -0700127} hwc_drm_display_t;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500128
Sean Paul3a69f732015-10-26 15:37:06 -0400129class DrmHotplugHandler : public DrmEventHandler {
130 public:
131 void Init(DrmResources *drm, const struct hwc_procs *procs) {
132 drm_ = drm;
133 procs_ = procs;
134 }
135
136 void HandleEvent(uint64_t timestamp_us) {
137 for (auto &conn : drm_->connectors()) {
138 drmModeConnection old_state = conn->state();
139
140 conn->UpdateModes();
141
142 drmModeConnection cur_state = conn->state();
143
144 if (cur_state == old_state)
145 continue;
146
147 ALOGI("%s event @%" PRIu64 " for connector %u\n",
148 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
149 conn->id());
150
151 if (cur_state == DRM_MODE_CONNECTED) {
152 // Take the first one, then look for the preferred
153 DrmMode mode = *(conn->modes().begin());
154 for (auto &m : conn->modes()) {
155 if (m.type() & DRM_MODE_TYPE_PREFERRED) {
156 mode = m;
157 break;
158 }
159 }
160 ALOGI("Setting mode %dx%d for connector %d\n", mode.h_display(),
161 mode.v_display(), conn->id());
162 int ret = drm_->SetDisplayActiveMode(conn->display(), mode);
163 if (ret) {
164 ALOGE("Failed to set active config %d", ret);
165 return;
166 }
167 } else {
168 int ret = drm_->SetDpmsMode(conn->display(), DRM_MODE_DPMS_OFF);
169 if (ret) {
170 ALOGE("Failed to set dpms mode off %d", ret);
171 return;
172 }
173 }
174
175 procs_->hotplug(procs_, conn->display(),
176 cur_state == DRM_MODE_CONNECTED ? 1 : 0);
177 }
178 }
179
180 private:
181 DrmResources *drm_ = NULL;
182 const struct hwc_procs *procs_ = NULL;
183};
184
Sean Paule0c4c3d2015-01-20 16:56:04 -0500185struct hwc_context_t {
Sean Paule42febf2015-05-07 11:35:29 -0700186 // map of display:hwc_drm_display_t
187 typedef std::map<int, hwc_drm_display_t> DisplayMap;
Sean Paulda6270d2015-06-01 14:11:52 -0400188
189 ~hwc_context_t() {
Haixia Shid21f5282015-10-05 14:35:09 -0700190 virtual_compositor_worker.Exit();
Sean Paulda6270d2015-06-01 14:11:52 -0400191 }
192
Sean Paule42febf2015-05-07 11:35:29 -0700193 hwc_composer_device_1_t device;
Zach Reiznerff30b522015-10-28 19:08:45 -0700194 hwc_procs_t const *procs = NULL;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500195
Sean Paule42febf2015-05-07 11:35:29 -0700196 DisplayMap displays;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400197 DrmResources drm;
Zach Reiznerff30b522015-10-28 19:08:45 -0700198 std::unique_ptr<Importer> importer;
Zach Reizner4a253652015-09-10 18:30:54 -0700199 const gralloc_module_t *gralloc;
200 DummySwSyncTimeline dummy_timeline;
Haixia Shid21f5282015-10-05 14:35:09 -0700201 VirtualCompositorWorker virtual_compositor_worker;
Sean Paul3a69f732015-10-26 15:37:06 -0400202 DrmHotplugHandler hotplug_handler;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500203};
204
Zach Reizner4a253652015-09-10 18:30:54 -0700205static native_handle_t *dup_buffer_handle(buffer_handle_t handle) {
206 native_handle_t *new_handle =
207 native_handle_create(handle->numFds, handle->numInts);
208 if (new_handle == NULL)
209 return NULL;
210
211 const int *old_data = handle->data;
212 int *new_data = new_handle->data;
213 for (int i = 0; i < handle->numFds; i++) {
214 *new_data = dup(*old_data);
215 old_data++;
216 new_data++;
217 }
218 memcpy(new_data, old_data, sizeof(int) * handle->numInts);
219
220 return new_handle;
221}
222
223static void free_buffer_handle(native_handle_t *handle) {
224 int ret = native_handle_close(handle);
225 if (ret)
226 ALOGE("Failed to close native handle %d", ret);
227 ret = native_handle_delete(handle);
228 if (ret)
229 ALOGE("Failed to delete native handle %d", ret);
230}
231
Zach Reiznerf99d53f2015-10-09 13:02:55 -0700232const hwc_drm_bo *DrmHwcBuffer::operator->() const {
Zach Reizner4a253652015-09-10 18:30:54 -0700233 if (importer_ == NULL) {
Zach Reiznerf99d53f2015-10-09 13:02:55 -0700234 ALOGE("Access of non-existent BO");
Zach Reizner4a253652015-09-10 18:30:54 -0700235 exit(1);
236 return NULL;
237 }
238 return &bo_;
239}
240
241void DrmHwcBuffer::Clear() {
242 if (importer_ != NULL) {
243 importer_->ReleaseBuffer(&bo_);
244 importer_ = NULL;
245 }
246}
247
248int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
249 hwc_drm_bo tmp_bo;
250
251 int ret = importer->ImportBuffer(handle, &tmp_bo);
252 if (ret)
253 return ret;
254
255 if (importer_ != NULL) {
256 importer_->ReleaseBuffer(&bo_);
257 }
258
259 importer_ = importer;
260
261 bo_ = tmp_bo;
262
263 return 0;
264}
265
266int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle,
267 const gralloc_module_t *gralloc) {
268 native_handle_t *handle_copy = dup_buffer_handle(handle);
269 if (handle_copy == NULL) {
270 ALOGE("Failed to duplicate handle");
271 return -ENOMEM;
272 }
273
274 int ret = gralloc->registerBuffer(gralloc, handle_copy);
275 if (ret) {
276 ALOGE("Failed to register buffer handle %d", ret);
277 free_buffer_handle(handle_copy);
278 return ret;
279 }
280
281 Clear();
282
283 gralloc_ = gralloc;
284 handle_ = handle_copy;
285
286 return 0;
287}
288
289DrmHwcNativeHandle::~DrmHwcNativeHandle() {
290 Clear();
291}
292
293void DrmHwcNativeHandle::Clear() {
294 if (gralloc_ != NULL && handle_ != NULL) {
295 gralloc_->unregisterBuffer(gralloc_, handle_);
296 free_buffer_handle(handle_);
297 gralloc_ = NULL;
298 handle_ = NULL;
299 }
300}
301
Sean Paula5df1de2016-02-10 10:00:08 -0800302class DrmVsyncCallback : public VsyncCallback {
303 public:
304 DrmVsyncCallback(hwc_procs_t const *procs) : procs_(procs) {
305 }
306
307 void Callback(int display, int64_t timestamp) {
308 procs_->vsync(procs_, display, timestamp);
309 }
310 private:
311 hwc_procs_t const *procs_;
312};
313
Zach Reizner4a253652015-09-10 18:30:54 -0700314int DrmHwcLayer::InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer,
315 const gralloc_module_t *gralloc) {
316 sf_handle = sf_layer->handle;
Zach Reizner4a253652015-09-10 18:30:54 -0700317 alpha = sf_layer->planeAlpha;
318
Zach Reizner7e88be92015-10-12 15:20:33 -0700319 source_crop = DrmHwcRect<float>(
320 sf_layer->sourceCropf.left, sf_layer->sourceCropf.top,
321 sf_layer->sourceCropf.right, sf_layer->sourceCropf.bottom);
322 display_frame = DrmHwcRect<int>(
323 sf_layer->displayFrame.left, sf_layer->displayFrame.top,
324 sf_layer->displayFrame.right, sf_layer->displayFrame.bottom);
325
Sean Paul04b47ea2015-11-19 21:46:11 -0500326 transform = 0;
327 // 270* and 180* cannot be combined with flips. More specifically, they
328 // already contain both horizontal and vertical flips, so those fields are
329 // redundant in this case. 90* rotation can be combined with either horizontal
330 // flip or vertical flip, so treat it differently
331 if (sf_layer->transform == HWC_TRANSFORM_ROT_270) {
332 transform = DrmHwcTransform::kRotate270;
333 } else if (sf_layer->transform == HWC_TRANSFORM_ROT_180) {
334 transform = DrmHwcTransform::kRotate180;
335 } else {
336 if (sf_layer->transform & HWC_TRANSFORM_FLIP_H)
337 transform |= DrmHwcTransform::kFlipH;
338 if (sf_layer->transform & HWC_TRANSFORM_FLIP_V)
339 transform |= DrmHwcTransform::kFlipV;
340 if (sf_layer->transform & HWC_TRANSFORM_ROT_90)
341 transform |= DrmHwcTransform::kRotate90;
Zach Reizner4a253652015-09-10 18:30:54 -0700342 }
343
344 switch (sf_layer->blending) {
345 case HWC_BLENDING_NONE:
346 blending = DrmHwcBlending::kNone;
347 break;
348 case HWC_BLENDING_PREMULT:
349 blending = DrmHwcBlending::kPreMult;
350 break;
351 case HWC_BLENDING_COVERAGE:
352 blending = DrmHwcBlending::kCoverage;
353 break;
354 default:
355 ALOGE("Invalid blending in hwc_layer_1_t %d", sf_layer->blending);
356 return -EINVAL;
357 }
358
Zach Reizner7e88be92015-10-12 15:20:33 -0700359 int ret = buffer.ImportBuffer(sf_layer->handle, importer);
360 if (ret)
361 return ret;
362
363 ret = handle.CopyBufferHandle(sf_layer->handle, gralloc);
364 if (ret)
365 return ret;
Zach Reizner4a253652015-09-10 18:30:54 -0700366
Zach Reizner36d7c6e2015-10-20 10:58:19 -0700367 ret = gralloc->perform(gralloc, GRALLOC_MODULE_PERFORM_GET_USAGE,
368 handle.get(), &gralloc_buffer_usage);
369 if (ret) {
Zach Reizner36d7c6e2015-10-20 10:58:19 -0700370 ALOGE("Failed to get usage for buffer %p (%d)", handle.get(), ret);
371 return ret;
Zach Reizner36d7c6e2015-10-20 10:58:19 -0700372 }
373
Zach Reizner4a253652015-09-10 18:30:54 -0700374 return 0;
375}
376
Zach Reiznerc6520e42015-08-13 14:32:09 -0700377static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
Sean Paul9046c642015-06-10 17:27:47 -0400378 int buff_len) {
379 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
380 std::ostringstream out;
381
382 ctx->drm.compositor()->Dump(&out);
383 std::string out_str = out.str();
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700384 strncpy(buff, out_str.c_str(),
385 std::min((size_t)buff_len, out_str.length() + 1));
386 buff[buff_len - 1] = '\0';
Sean Paul9046c642015-06-10 17:27:47 -0400387}
388
Sean Paulbd61c8d2015-10-29 15:00:17 -0400389static bool hwc_skip_layer(const std::pair<int, int> &indices, int i) {
390 return indices.first >= 0 && i >= indices.first && i <= indices.second;
391}
392
Sean Paulb386f1b2015-05-13 06:33:23 -0700393static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -0400394 hwc_display_contents_1_t **display_contents) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700395 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Zach Reizner1946fa72015-08-14 11:14:38 -0700396
Sean Paule42febf2015-05-07 11:35:29 -0700397 for (int i = 0; i < (int)num_displays; ++i) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400398 if (!display_contents[i])
399 continue;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500400
Sean Paul6f82f1d2015-10-21 20:05:05 -0400401 bool use_framebuffer_target = false;
Sean Paulb1008372015-11-24 11:52:37 -0500402 DrmMode mode;
Haixia Shid21f5282015-10-05 14:35:09 -0700403 if (i == HWC_DISPLAY_VIRTUAL) {
404 use_framebuffer_target = true;
405 } else {
Sean Paulb1008372015-11-24 11:52:37 -0500406 DrmConnector *c = ctx->drm.GetConnectorForDisplay(i);
407 if (!c) {
408 ALOGE("Failed to get DrmConnector for display %d", i);
Haixia Shid21f5282015-10-05 14:35:09 -0700409 return -ENODEV;
410 }
Sean Paulb1008372015-11-24 11:52:37 -0500411 mode = c->active_mode();
Sean Paulb386f1b2015-05-13 06:33:23 -0700412 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700413
Sean Paulbd61c8d2015-10-29 15:00:17 -0400414 // Since we can't composite HWC_SKIP_LAYERs by ourselves, we'll let SF
415 // handle all layers in between the first and last skip layers. So find the
416 // outer indices and mark everything in between as HWC_FRAMEBUFFER
417 std::pair<int, int> skip_layer_indices(-1, -1);
Zach Reizner45624d32015-06-10 16:03:01 -0700418 int num_layers = display_contents[i]->numHwLayers;
Sean Paulbd61c8d2015-10-29 15:00:17 -0400419 for (int j = 0; !use_framebuffer_target && j < num_layers; ++j) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700420 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700421
Sean Paulbd61c8d2015-10-29 15:00:17 -0400422 if (!(layer->flags & HWC_SKIP_LAYER))
423 continue;
424
425 if (skip_layer_indices.first == -1)
426 skip_layer_indices.first = j;
427 skip_layer_indices.second = j;
428 }
429
430 for (int j = 0; j < num_layers; ++j) {
431 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
432
433 if (!use_framebuffer_target && !hwc_skip_layer(skip_layer_indices, j)) {
Sean Paulb1008372015-11-24 11:52:37 -0500434 // If the layer is off the screen, don't earmark it for an overlay.
435 // We'll leave it as-is, which effectively just drops it from the frame
436 const hwc_rect_t *frame = &layer->displayFrame;
437 if ((frame->right - frame->left) <= 0 ||
438 (frame->bottom - frame->top) <= 0 ||
439 frame->right <= 0 || frame->bottom <= 0 ||
440 frame->left >= (int)mode.h_display() ||
441 frame->top >= (int)mode.v_display())
442 continue;
443
Zach Reizner1946fa72015-08-14 11:14:38 -0700444 if (layer->compositionType == HWC_FRAMEBUFFER)
445 layer->compositionType = HWC_OVERLAY;
446 } else {
447 switch (layer->compositionType) {
448 case HWC_OVERLAY:
449 case HWC_BACKGROUND:
450 case HWC_SIDEBAND:
451 case HWC_CURSOR_OVERLAY:
452 layer->compositionType = HWC_FRAMEBUFFER;
453 break;
454 }
455 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400456 }
457 }
Sean Pauldffca952015-02-04 10:19:55 -0800458
Sean Paulef8f1f92015-04-29 16:05:23 -0400459 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500460}
461
Zach Reizner09807052015-08-13 14:53:41 -0700462static void hwc_add_layer_to_retire_fence(
463 hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) {
Sean Paul04206122015-07-16 15:59:24 -0400464 if (layer->releaseFenceFd < 0)
465 return;
466
467 if (display_contents->retireFenceFd >= 0) {
468 int old_retire_fence = display_contents->retireFenceFd;
Zach Reiznerc6520e42015-08-13 14:32:09 -0700469 display_contents->retireFenceFd =
470 sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd);
Sean Paul04206122015-07-16 15:59:24 -0400471 close(old_retire_fence);
472 } else {
473 display_contents->retireFenceFd = dup(layer->releaseFenceFd);
474 }
475}
476
Sean Paule0c4c3d2015-01-20 16:56:04 -0500477static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
Zach Reizner4a253652015-09-10 18:30:54 -0700478 hwc_display_contents_1_t **sf_display_contents) {
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -0700479 ATRACE_CALL();
Sean Paulef8f1f92015-04-29 16:05:23 -0400480 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Zach Reizner4a253652015-09-10 18:30:54 -0700481 int ret = 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500482
Zach Reizner4a253652015-09-10 18:30:54 -0700483 std::vector<CheckedOutputFd> checked_output_fences;
484 std::vector<DrmHwcDisplayContents> displays_contents;
Zach Reizner09807052015-08-13 14:53:41 -0700485 std::vector<DrmCompositionDisplayLayersMap> layers_map;
486 std::vector<std::vector<size_t>> layers_indices;
Zach Reizner4a253652015-09-10 18:30:54 -0700487 displays_contents.reserve(num_displays);
488 // layers_map.reserve(num_displays);
Zach Reizner09807052015-08-13 14:53:41 -0700489 layers_indices.reserve(num_displays);
490
Zach Reizner4a253652015-09-10 18:30:54 -0700491 // Phase one does nothing that would cause errors. Only take ownership of FDs.
492 for (size_t i = 0; i < num_displays; ++i) {
493 hwc_display_contents_1_t *dc = sf_display_contents[i];
494 displays_contents.emplace_back();
495 DrmHwcDisplayContents &display_contents = displays_contents.back();
Haixia Shi7acc59b2015-09-30 10:57:54 -0700496 layers_indices.emplace_back();
497 std::vector<size_t> &indices_to_composite = layers_indices.back();
Zach Reizner4a253652015-09-10 18:30:54 -0700498
499 if (!sf_display_contents[i])
Sean Paulb386f1b2015-05-13 06:33:23 -0700500 continue;
Zach Reizner09807052015-08-13 14:53:41 -0700501
Haixia Shid21f5282015-10-05 14:35:09 -0700502 if (i == HWC_DISPLAY_VIRTUAL) {
503 ctx->virtual_compositor_worker.QueueComposite(dc);
504 continue;
505 }
506
Zach Reizner4a253652015-09-10 18:30:54 -0700507 std::ostringstream display_index_formatter;
508 display_index_formatter << "retire fence for display " << i;
509 std::string display_fence_description(display_index_formatter.str());
510 checked_output_fences.emplace_back(&dc->retireFenceFd,
511 display_fence_description.c_str(),
512 ctx->dummy_timeline);
513 display_contents.retire_fence = OutputFd(&dc->retireFenceFd);
Zach Reizner09807052015-08-13 14:53:41 -0700514
Zach Reizner4a253652015-09-10 18:30:54 -0700515 size_t num_dc_layers = dc->numHwLayers;
Haixia Shi1034bb72015-09-09 12:08:20 -0700516 int framebuffer_target_index = -1;
Zach Reizner4a253652015-09-10 18:30:54 -0700517 for (size_t j = 0; j < num_dc_layers; ++j) {
518 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
Sean Paulbd61c8d2015-10-29 15:00:17 -0400519 if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET) {
520 framebuffer_target_index = j;
521 break;
522 }
523 }
524
525 for (size_t j = 0; j < num_dc_layers; ++j) {
526 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
Zach Reizner4a253652015-09-10 18:30:54 -0700527
528 display_contents.layers.emplace_back();
529 DrmHwcLayer &layer = display_contents.layers.back();
530
Sean Paulbd61c8d2015-10-29 15:00:17 -0400531 // In prepare() we marked all layers FRAMEBUFFER between SKIP_LAYER's.
532 // This means we should insert the FB_TARGET layer in the composition
533 // stack at the location of the first skip layer, and ignore the rest.
534 if (sf_layer->flags & HWC_SKIP_LAYER) {
535 if (framebuffer_target_index < 0)
536 continue;
537 int idx = framebuffer_target_index;
538 framebuffer_target_index = -1;
539 hwc_layer_1_t *fbt_layer = &dc->hwLayers[idx];
540 if (!fbt_layer->handle || (fbt_layer->flags & HWC_SKIP_LAYER)) {
541 ALOGE("Invalid HWC_FRAMEBUFFER_TARGET with HWC_SKIP_LAYER present");
542 continue;
543 }
544 indices_to_composite.push_back(idx);
Sean Paulb386f1b2015-05-13 06:33:23 -0700545 continue;
Sean Paulbd61c8d2015-10-29 15:00:17 -0400546 }
Zach Reizner4a253652015-09-10 18:30:54 -0700547
Sean Paul6f82f1d2015-10-21 20:05:05 -0400548 if (sf_layer->compositionType == HWC_OVERLAY)
549 indices_to_composite.push_back(j);
Zach Reizner4a253652015-09-10 18:30:54 -0700550
551 layer.acquire_fence.Set(sf_layer->acquireFenceFd);
552 sf_layer->acquireFenceFd = -1;
553
554 std::ostringstream layer_fence_formatter;
555 layer_fence_formatter << "release fence for layer " << j << " of display "
556 << i;
557 std::string layer_fence_description(layer_fence_formatter.str());
558 checked_output_fences.emplace_back(&sf_layer->releaseFenceFd,
559 layer_fence_description.c_str(),
560 ctx->dummy_timeline);
561 layer.release_fence = OutputFd(&sf_layer->releaseFenceFd);
Zach Reizner1946fa72015-08-14 11:14:38 -0700562 }
Zach Reizner4a253652015-09-10 18:30:54 -0700563
Sean Paulbd61c8d2015-10-29 15:00:17 -0400564 // This is a catch-all in case we get a frame without any overlay layers, or
565 // skip layers, but with a value fb_target layer. This _shouldn't_ happen,
566 // but it's not ruled out by the hwc specification
Sean Paul6f82f1d2015-10-21 20:05:05 -0400567 if (indices_to_composite.empty() && framebuffer_target_index >= 0) {
568 hwc_layer_1_t *sf_layer = &dc->hwLayers[framebuffer_target_index];
569 if (!sf_layer->handle || (sf_layer->flags & HWC_SKIP_LAYER)) {
570 ALOGE(
571 "Expected valid layer with HWC_FRAMEBUFFER_TARGET when all "
572 "HWC_OVERLAY layers are skipped.");
Zach Reizner4a253652015-09-10 18:30:54 -0700573 ret = -EINVAL;
Zach Reizner1946fa72015-08-14 11:14:38 -0700574 }
Sean Paul6f82f1d2015-10-21 20:05:05 -0400575 indices_to_composite.push_back(framebuffer_target_index);
Zach Reizner45624d32015-06-10 16:03:01 -0700576 }
Zach Reizner4a253652015-09-10 18:30:54 -0700577 }
Zach Reizner45624d32015-06-10 16:03:01 -0700578
Zach Reizner4a253652015-09-10 18:30:54 -0700579 if (ret)
580 return ret;
581
582 for (size_t i = 0; i < num_displays; ++i) {
583 hwc_display_contents_1_t *dc = sf_display_contents[i];
584 DrmHwcDisplayContents &display_contents = displays_contents[i];
Haixia Shi2fddd372015-10-15 16:21:48 -0700585 if (!sf_display_contents[i] || i == HWC_DISPLAY_VIRTUAL)
Zach Reizner4a253652015-09-10 18:30:54 -0700586 continue;
587
588 layers_map.emplace_back();
589 DrmCompositionDisplayLayersMap &map = layers_map.back();
Zach Reizneracba14b2015-10-13 18:19:26 -0700590 map.display = i;
Zach Reizner5757e822015-10-16 19:06:31 -0700591 map.geometry_changed =
592 (dc->flags & HWC_GEOMETRY_CHANGED) == HWC_GEOMETRY_CHANGED;
Zach Reizner4a253652015-09-10 18:30:54 -0700593 std::vector<size_t> &indices_to_composite = layers_indices[i];
594 for (size_t j : indices_to_composite) {
595 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
596
597 DrmHwcLayer &layer = display_contents.layers[j];
598
Zach Reiznerff30b522015-10-28 19:08:45 -0700599 ret = layer.InitFromHwcLayer(sf_layer, ctx->importer.get(), ctx->gralloc);
Zach Reizner7e88be92015-10-12 15:20:33 -0700600 if (ret) {
601 ALOGE("Failed to init composition from layer %d", ret);
602 return ret;
603 }
Zach Reizner4a253652015-09-10 18:30:54 -0700604 map.layers.emplace_back(std::move(layer));
605 }
606 }
607
608 std::unique_ptr<DrmComposition> composition(
Zach Reiznerff30b522015-10-28 19:08:45 -0700609 ctx->drm.compositor()->CreateComposition(ctx->importer.get()));
Zach Reizner4a253652015-09-10 18:30:54 -0700610 if (!composition) {
611 ALOGE("Drm composition init failed");
612 return -EINVAL;
Zach Reizner09807052015-08-13 14:53:41 -0700613 }
Zach Reizner45624d32015-06-10 16:03:01 -0700614
Zach Reizner09807052015-08-13 14:53:41 -0700615 ret = composition->SetLayers(layers_map.size(), layers_map.data());
616 if (ret) {
Zach Reizner09807052015-08-13 14:53:41 -0700617 return -EINVAL;
618 }
Zach Reizner45624d32015-06-10 16:03:01 -0700619
Zach Reizner09807052015-08-13 14:53:41 -0700620 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
621 if (ret) {
Zach Reizner09807052015-08-13 14:53:41 -0700622 return -EINVAL;
623 }
624
Zach Reizner566da2b2015-10-06 15:39:09 -0700625 for (size_t i = 0; i < num_displays; ++i) {
626 hwc_display_contents_1_t *dc = sf_display_contents[i];
627 if (!dc)
628 continue;
629
630 size_t num_dc_layers = dc->numHwLayers;
631 for (size_t j = 0; j < num_dc_layers; ++j) {
632 hwc_layer_1_t *layer = &dc->hwLayers[j];
633 if (layer->flags & HWC_SKIP_LAYER)
634 continue;
635 hwc_add_layer_to_retire_fence(layer, dc);
636 }
637 }
638
Zach Reizner09807052015-08-13 14:53:41 -0700639 composition.reset(NULL);
640
Sean Paulef8f1f92015-04-29 16:05:23 -0400641 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500642}
643
Sean Paulef8f1f92015-04-29 16:05:23 -0400644static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
645 int event, int enabled) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400646 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
647 return -EINVAL;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500648
Sean Paul4057be32015-05-13 06:23:09 -0700649 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
650 hwc_drm_display_t *hd = &ctx->displays[display];
651 return hd->vsync_worker.VSyncControl(enabled);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500652}
653
Sean Paulef8f1f92015-04-29 16:05:23 -0400654static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
655 int mode) {
656 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500657
Sean Paul6a55e9f2015-04-30 15:31:06 -0400658 uint64_t dpmsValue = 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400659 switch (mode) {
660 case HWC_POWER_MODE_OFF:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400661 dpmsValue = DRM_MODE_DPMS_OFF;
Sean Paulef8f1f92015-04-29 16:05:23 -0400662 break;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500663
Sean Paulef8f1f92015-04-29 16:05:23 -0400664 /* We can't support dozing right now, so go full on */
665 case HWC_POWER_MODE_DOZE:
666 case HWC_POWER_MODE_DOZE_SUSPEND:
667 case HWC_POWER_MODE_NORMAL:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400668 dpmsValue = DRM_MODE_DPMS_ON;
Sean Paulef8f1f92015-04-29 16:05:23 -0400669 break;
670 };
Sean Paul6a55e9f2015-04-30 15:31:06 -0400671 return ctx->drm.SetDpmsMode(display, dpmsValue);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500672}
673
Sean Paulef8f1f92015-04-29 16:05:23 -0400674static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
675 int *value) {
676 switch (what) {
677 case HWC_BACKGROUND_LAYER_SUPPORTED:
678 *value = 0; /* TODO: We should do this */
679 break;
680 case HWC_VSYNC_PERIOD:
681 ALOGW("Query for deprecated vsync value, returning 60Hz");
682 *value = 1000 * 1000 * 1000 / 60;
683 break;
684 case HWC_DISPLAY_TYPES_SUPPORTED:
Haixia Shi2fddd372015-10-15 16:21:48 -0700685 *value = HWC_DISPLAY_PRIMARY_BIT | HWC_DISPLAY_EXTERNAL_BIT |
686 HWC_DISPLAY_VIRTUAL_BIT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400687 break;
688 }
689 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500690}
691
Sean Paulef8f1f92015-04-29 16:05:23 -0400692static void hwc_register_procs(struct hwc_composer_device_1 *dev,
693 hwc_procs_t const *procs) {
694 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500695
Sean Paulef8f1f92015-04-29 16:05:23 -0400696 ctx->procs = procs;
Sean Paul4057be32015-05-13 06:23:09 -0700697
Sean Paula5df1de2016-02-10 10:00:08 -0800698 for (std::pair<const int, hwc_drm_display> &display_entry : ctx->displays) {
699 auto callback = std::make_shared<DrmVsyncCallback>(procs);
700 display_entry.second.vsync_worker.RegisterCallback(std::move(callback));
701 }
Sean Paul3a69f732015-10-26 15:37:06 -0400702
703 ctx->hotplug_handler.Init(&ctx->drm, procs);
704 ctx->drm.event_listener()->RegisterHotplugHandler(&ctx->hotplug_handler);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500705}
706
Sean Paulef8f1f92015-04-29 16:05:23 -0400707static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
708 int display, uint32_t *configs,
Sean Paul6a55e9f2015-04-30 15:31:06 -0400709 size_t *num_configs) {
710 if (!*num_configs)
Sean Paulef8f1f92015-04-29 16:05:23 -0400711 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500712
Sean Paulef8f1f92015-04-29 16:05:23 -0400713 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700714 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400715 hd->config_ids.clear();
716
717 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
718 if (!connector) {
719 ALOGE("Failed to get connector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400720 return -ENODEV;
721 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500722
Sean Paule42febf2015-05-07 11:35:29 -0700723 int ret = connector->UpdateModes();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400724 if (ret) {
725 ALOGE("Failed to update display modes %d", ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400726 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400727 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500728
Zach Reiznerff30b522015-10-28 19:08:45 -0700729 for (const DrmMode &mode : connector->modes()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400730 size_t idx = hd->config_ids.size();
731 if (idx == *num_configs)
732 break;
Zach Reiznerff30b522015-10-28 19:08:45 -0700733 hd->config_ids.push_back(mode.id());
734 configs[idx] = mode.id();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400735 }
736 *num_configs = hd->config_ids.size();
737 return *num_configs == 0 ? -1 : 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500738}
739
Sean Paulef8f1f92015-04-29 16:05:23 -0400740static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
741 int display, uint32_t config,
742 const uint32_t *attributes,
743 int32_t *values) {
744 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400745 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400746 if (!c) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400747 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400748 return -ENODEV;
749 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400750 DrmMode mode;
Zach Reiznerff30b522015-10-28 19:08:45 -0700751 for (const DrmMode &conn_mode : c->modes()) {
752 if (conn_mode.id() == config) {
753 mode = conn_mode;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400754 break;
755 }
756 }
757 if (mode.id() == 0) {
758 ALOGE("Failed to find active mode for display %d", display);
759 return -ENOENT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400760 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500761
Sean Paul6a55e9f2015-04-30 15:31:06 -0400762 uint32_t mm_width = c->mm_width();
763 uint32_t mm_height = c->mm_height();
Sean Paulef8f1f92015-04-29 16:05:23 -0400764 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
765 switch (attributes[i]) {
766 case HWC_DISPLAY_VSYNC_PERIOD:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400767 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
Sean Paulef8f1f92015-04-29 16:05:23 -0400768 break;
769 case HWC_DISPLAY_WIDTH:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400770 values[i] = mode.h_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400771 break;
772 case HWC_DISPLAY_HEIGHT:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400773 values[i] = mode.v_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400774 break;
775 case HWC_DISPLAY_DPI_X:
776 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400777 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400778 break;
779 case HWC_DISPLAY_DPI_Y:
780 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400781 values[i] =
782 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400783 break;
784 }
785 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400786 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500787}
788
Sean Paulef8f1f92015-04-29 16:05:23 -0400789static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
790 int display) {
791 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400792 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
793 if (!c) {
794 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400795 return -ENODEV;
796 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500797
Sean Paul6a55e9f2015-04-30 15:31:06 -0400798 DrmMode mode = c->active_mode();
Sean Paule42febf2015-05-07 11:35:29 -0700799 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400800 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
801 if (hd->config_ids[i] == mode.id())
802 return i;
Sean Paulef8f1f92015-04-29 16:05:23 -0400803 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400804 return -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500805}
806
Sean Paulef8f1f92015-04-29 16:05:23 -0400807static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
808 int index) {
809 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700810 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400811 if (index >= (int)hd->config_ids.size()) {
812 ALOGE("Invalid config index %d passed in", index);
813 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400814 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500815
Sean Paul877be972015-06-03 14:08:27 -0400816 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
817 if (!c) {
818 ALOGE("Failed to get connector for display %d", display);
819 return -ENODEV;
820 }
Sean Paul3a69f732015-10-26 15:37:06 -0400821
822 if (c->state() != DRM_MODE_CONNECTED)
823 return -ENODEV;
824
Sean Paul877be972015-06-03 14:08:27 -0400825 DrmMode mode;
Zach Reiznerff30b522015-10-28 19:08:45 -0700826 for (const DrmMode &conn_mode : c->modes()) {
827 if (conn_mode.id() == hd->config_ids[index]) {
828 mode = conn_mode;
Sean Paul877be972015-06-03 14:08:27 -0400829 break;
830 }
831 }
832 if (mode.id() != hd->config_ids[index]) {
833 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
834 return -ENOENT;
835 }
836 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400837 if (ret) {
Sean Paul877be972015-06-03 14:08:27 -0400838 ALOGE("Failed to set active config %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400839 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400840 }
Sean Paul55584b52016-03-29 10:51:38 -0400841 ret = ctx->drm.SetDpmsMode(display, DRM_MODE_DPMS_ON);
842 if (ret) {
843 ALOGE("Failed to set dpms mode on %d", ret);
844 return ret;
845 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400846 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500847}
848
Sean Paulef8f1f92015-04-29 16:05:23 -0400849static int hwc_device_close(struct hw_device_t *dev) {
850 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulef8f1f92015-04-29 16:05:23 -0400851 delete ctx;
Sean Paulef8f1f92015-04-29 16:05:23 -0400852 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500853}
854
Sean Paul24a26e32015-02-04 10:34:47 -0800855/*
856 * TODO: This function sets the active config to the first one in the list. This
857 * should be fixed such that it selects the preferred mode for the display, or
858 * some other, saner, method of choosing the config.
859 */
Sean Paule42febf2015-05-07 11:35:29 -0700860static int hwc_set_initial_config(hwc_drm_display_t *hd) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400861 uint32_t config;
862 size_t num_configs = 1;
863 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
864 &num_configs);
865 if (ret || !num_configs)
866 return 0;
Sean Paul24a26e32015-02-04 10:34:47 -0800867
Sean Paulef8f1f92015-04-29 16:05:23 -0400868 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
869 if (ret) {
870 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
871 return ret;
872 }
Sean Paul24a26e32015-02-04 10:34:47 -0800873
Sean Paulef8f1f92015-04-29 16:05:23 -0400874 return ret;
Sean Paul24a26e32015-02-04 10:34:47 -0800875}
876
Sean Paul6a55e9f2015-04-30 15:31:06 -0400877static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
Sean Paule42febf2015-05-07 11:35:29 -0700878 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paulef8f1f92015-04-29 16:05:23 -0400879 hd->ctx = ctx;
880 hd->display = display;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500881
Sean Paulb386f1b2015-05-13 06:33:23 -0700882 int ret = hwc_set_initial_config(hd);
Sean Paulef8f1f92015-04-29 16:05:23 -0400883 if (ret) {
884 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400885 return ret;
886 }
Sean Paul24a26e32015-02-04 10:34:47 -0800887
Sean Paul4057be32015-05-13 06:23:09 -0700888 ret = hd->vsync_worker.Init(&ctx->drm, display);
889 if (ret) {
890 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
891 return ret;
892 }
893
Sean Paulef8f1f92015-04-29 16:05:23 -0400894 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500895}
896
Sean Paulef8f1f92015-04-29 16:05:23 -0400897static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400898 int ret;
Zach Reiznerff30b522015-10-28 19:08:45 -0700899 for (auto &conn : ctx->drm.connectors()) {
900 ret = hwc_initialize_display(ctx, conn->display());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400901 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700902 ALOGE("Failed to initialize display %d", conn->display());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400903 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400904 }
905 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400906
Haixia Shid21f5282015-10-05 14:35:09 -0700907 ret = ctx->virtual_compositor_worker.Init();
908 if (ret) {
909 ALOGE("Failed to initialize virtual compositor worker");
910 return ret;
911 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400912 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500913}
914
Sean Paulef8f1f92015-04-29 16:05:23 -0400915static int hwc_device_open(const struct hw_module_t *module, const char *name,
916 struct hw_device_t **dev) {
917 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
918 ALOGE("Invalid module name- %s", name);
919 return -EINVAL;
920 }
921
Zach Reiznerff30b522015-10-28 19:08:45 -0700922 std::unique_ptr<hwc_context_t> ctx(new hwc_context_t());
Sean Paulef8f1f92015-04-29 16:05:23 -0400923 if (!ctx) {
924 ALOGE("Failed to allocate hwc context");
925 return -ENOMEM;
926 }
927
Sean Paul6a55e9f2015-04-30 15:31:06 -0400928 int ret = ctx->drm.Init();
929 if (ret) {
930 ALOGE("Can't initialize Drm object %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400931 return ret;
932 }
933
Zach Reizner4a253652015-09-10 18:30:54 -0700934 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
935 (const hw_module_t **)&ctx->gralloc);
936 if (ret) {
937 ALOGE("Failed to open gralloc module %d", ret);
Zach Reizner4a253652015-09-10 18:30:54 -0700938 return ret;
939 }
940
941 ret = ctx->dummy_timeline.Init();
942 if (ret) {
943 ALOGE("Failed to create dummy sw sync timeline %d", ret);
944 return ret;
945 }
946
Zach Reiznerff30b522015-10-28 19:08:45 -0700947 ctx->importer.reset(Importer::CreateInstance(&ctx->drm));
Sean Paulda6270d2015-06-01 14:11:52 -0400948 if (!ctx->importer) {
949 ALOGE("Failed to create importer instance");
Sean Paulef8f1f92015-04-29 16:05:23 -0400950 return ret;
951 }
952
Zach Reiznerff30b522015-10-28 19:08:45 -0700953 ret = hwc_enumerate_displays(ctx.get());
Sean Paulef8f1f92015-04-29 16:05:23 -0400954 if (ret) {
955 ALOGE("Failed to enumerate displays: %s", strerror(ret));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400956 return ret;
957 }
958
Sean Paulef8f1f92015-04-29 16:05:23 -0400959 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
960 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
961 ctx->device.common.module = const_cast<hw_module_t *>(module);
962 ctx->device.common.close = hwc_device_close;
963
Sean Paul9046c642015-06-10 17:27:47 -0400964 ctx->device.dump = hwc_dump;
Sean Paulef8f1f92015-04-29 16:05:23 -0400965 ctx->device.prepare = hwc_prepare;
966 ctx->device.set = hwc_set;
967 ctx->device.eventControl = hwc_event_control;
968 ctx->device.setPowerMode = hwc_set_power_mode;
969 ctx->device.query = hwc_query;
970 ctx->device.registerProcs = hwc_register_procs;
971 ctx->device.getDisplayConfigs = hwc_get_display_configs;
972 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
973 ctx->device.getActiveConfig = hwc_get_active_config;
974 ctx->device.setActiveConfig = hwc_set_active_config;
975 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
976
977 *dev = &ctx->device.common;
Zach Reiznerff30b522015-10-28 19:08:45 -0700978 ctx.release();
Sean Paulef8f1f92015-04-29 16:05:23 -0400979
980 return 0;
981}
Sean Paul6a55e9f2015-04-30 15:31:06 -0400982}
Sean Paulef8f1f92015-04-29 16:05:23 -0400983
Sean Paul6a55e9f2015-04-30 15:31:06 -0400984static struct hw_module_methods_t hwc_module_methods = {
Sean Paul5325e102016-03-29 13:55:35 -0400985 .open = android::hwc_device_open
Sean Paul6a55e9f2015-04-30 15:31:06 -0400986};
Sean Paule0c4c3d2015-01-20 16:56:04 -0500987
988hwc_module_t HAL_MODULE_INFO_SYM = {
Sean Paul5325e102016-03-29 13:55:35 -0400989 .common = {
990 .tag = HARDWARE_MODULE_TAG,
991 .version_major = 1,
992 .version_minor = 0,
993 .id = HWC_HARDWARE_MODULE_ID,
994 .name = "DRM hwcomposer module",
995 .author = "The Android Open Source Project",
996 .methods = &hwc_module_methods,
997 .dso = NULL,
998 .reserved = {0},
Sean Paulef8f1f92015-04-29 16:05:23 -0400999 }
Sean Paule0c4c3d2015-01-20 16:56:04 -05001000};