blob: f19407a40c50368c8c8adc9bd6f5c6290c0c1dcf [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 Paul6a55e9f2015-04-30 15:31:06 -040021#include "drmresources.h"
Sean Paulda6270d2015-06-01 14:11:52 -040022#include "importer.h"
Haixia Shid21f5282015-10-05 14:35:09 -070023#include "virtualcompositorworker.h"
Sean Paul4057be32015-05-13 06:23:09 -070024#include "vsyncworker.h"
Sean Paulef8f1f92015-04-29 16:05:23 -040025
Zach Reizner09807052015-08-13 14:53:41 -070026#include <stdlib.h>
27
28#include <map>
29#include <vector>
Zach Reizner4a253652015-09-10 18:30:54 -070030#include <sstream>
Zach Reizner09807052015-08-13 14:53:41 -070031
Sean Paule0c4c3d2015-01-20 16:56:04 -050032#include <errno.h>
Sean Paulef8f1f92015-04-29 16:05:23 -040033#include <fcntl.h>
Sean Paulef8f1f92015-04-29 16:05:23 -040034#include <pthread.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050035#include <sys/param.h>
Sean Paul9aa5ad32015-01-22 15:47:54 -050036#include <sys/resource.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050037#include <xf86drm.h>
38#include <xf86drmMode.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050039
Sean Paulef8f1f92015-04-29 16:05:23 -040040#include <cutils/log.h>
41#include <cutils/properties.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050042#include <hardware/hardware.h>
43#include <hardware/hwcomposer.h>
Zach Reizner4a253652015-09-10 18:30:54 -070044#include <sw_sync.h>
45#include <sync/sync.h>
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -070046#include <utils/Trace.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050047
Sean Paule0c4c3d2015-01-20 16:56:04 -050048#define UM_PER_INCH 25400
49
Sean Paul6a55e9f2015-04-30 15:31:06 -040050namespace android {
Sean Paule0c4c3d2015-01-20 16:56:04 -050051
Zach Reizner4a253652015-09-10 18:30:54 -070052class DummySwSyncTimeline {
53 public:
54 int Init() {
55 int ret = timeline_fd_.Set(sw_sync_timeline_create());
56 if (ret < 0)
57 return ret;
58 return 0;
59 }
60
61 UniqueFd CreateDummyFence() {
62 int ret = sw_sync_fence_create(timeline_fd_.get(), "dummy fence",
63 timeline_pt_ + 1);
64 if (ret < 0) {
65 ALOGE("Failed to create dummy fence %d", ret);
66 return ret;
67 }
68
69 UniqueFd ret_fd(ret);
70
71 ret = sw_sync_timeline_inc(timeline_fd_.get(), 1);
72 if (ret) {
73 ALOGE("Failed to increment dummy sync timeline %d", ret);
74 return ret;
75 }
76
77 ++timeline_pt_;
78 return ret_fd;
79 }
80
81 private:
82 UniqueFd timeline_fd_;
83 int timeline_pt_ = 0;
84};
85
86struct CheckedOutputFd {
87 CheckedOutputFd(int *fd, const char *description,
88 DummySwSyncTimeline &timeline)
89 : fd_(fd), description_(description), timeline_(timeline) {
90 }
91 CheckedOutputFd(CheckedOutputFd &&rhs)
92 : description_(rhs.description_), timeline_(rhs.timeline_) {
93 std::swap(fd_, rhs.fd_);
94 }
95
96 CheckedOutputFd &operator=(const CheckedOutputFd &rhs) = delete;
97
98 ~CheckedOutputFd() {
99 if (fd_ == NULL)
100 return;
101
102 if (*fd_ >= 0)
103 return;
104
105 *fd_ = timeline_.CreateDummyFence().Release();
106
107 if (*fd_ < 0)
108 ALOGE("Failed to fill %s (%p == %d) before destruction",
109 description_.c_str(), fd_, *fd_);
110 }
111
112 private:
113 int *fd_ = NULL;
114 std::string description_;
115 DummySwSyncTimeline &timeline_;
116};
117
Sean Paule42febf2015-05-07 11:35:29 -0700118typedef struct hwc_drm_display {
Sean Paulef8f1f92015-04-29 16:05:23 -0400119 struct hwc_context_t *ctx;
120 int display;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500121
Sean Paul6a55e9f2015-04-30 15:31:06 -0400122 std::vector<uint32_t> config_ids;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500123
Sean Paul4057be32015-05-13 06:23:09 -0700124 VSyncWorker vsync_worker;
Sean Paule42febf2015-05-07 11:35:29 -0700125} hwc_drm_display_t;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500126
127struct hwc_context_t {
Sean Paule42febf2015-05-07 11:35:29 -0700128 // map of display:hwc_drm_display_t
129 typedef std::map<int, hwc_drm_display_t> DisplayMap;
Sean Paulda6270d2015-06-01 14:11:52 -0400130
131 ~hwc_context_t() {
Haixia Shid21f5282015-10-05 14:35:09 -0700132 virtual_compositor_worker.Exit();
Sean Paulda6270d2015-06-01 14:11:52 -0400133 }
134
Sean Paule42febf2015-05-07 11:35:29 -0700135 hwc_composer_device_1_t device;
Zach Reiznerff30b522015-10-28 19:08:45 -0700136 hwc_procs_t const *procs = NULL;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500137
Sean Paule42febf2015-05-07 11:35:29 -0700138 DisplayMap displays;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400139 DrmResources drm;
Zach Reiznerff30b522015-10-28 19:08:45 -0700140 std::unique_ptr<Importer> importer;
Zach Reizner4a253652015-09-10 18:30:54 -0700141 const gralloc_module_t *gralloc;
142 DummySwSyncTimeline dummy_timeline;
Haixia Shid21f5282015-10-05 14:35:09 -0700143 VirtualCompositorWorker virtual_compositor_worker;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500144};
145
Zach Reizner4a253652015-09-10 18:30:54 -0700146static native_handle_t *dup_buffer_handle(buffer_handle_t handle) {
147 native_handle_t *new_handle =
148 native_handle_create(handle->numFds, handle->numInts);
149 if (new_handle == NULL)
150 return NULL;
151
152 const int *old_data = handle->data;
153 int *new_data = new_handle->data;
154 for (int i = 0; i < handle->numFds; i++) {
155 *new_data = dup(*old_data);
156 old_data++;
157 new_data++;
158 }
159 memcpy(new_data, old_data, sizeof(int) * handle->numInts);
160
161 return new_handle;
162}
163
164static void free_buffer_handle(native_handle_t *handle) {
165 int ret = native_handle_close(handle);
166 if (ret)
167 ALOGE("Failed to close native handle %d", ret);
168 ret = native_handle_delete(handle);
169 if (ret)
170 ALOGE("Failed to delete native handle %d", ret);
171}
172
Zach Reiznerf99d53f2015-10-09 13:02:55 -0700173const hwc_drm_bo *DrmHwcBuffer::operator->() const {
Zach Reizner4a253652015-09-10 18:30:54 -0700174 if (importer_ == NULL) {
Zach Reiznerf99d53f2015-10-09 13:02:55 -0700175 ALOGE("Access of non-existent BO");
Zach Reizner4a253652015-09-10 18:30:54 -0700176 exit(1);
177 return NULL;
178 }
179 return &bo_;
180}
181
182void DrmHwcBuffer::Clear() {
183 if (importer_ != NULL) {
184 importer_->ReleaseBuffer(&bo_);
185 importer_ = NULL;
186 }
187}
188
189int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
190 hwc_drm_bo tmp_bo;
191
192 int ret = importer->ImportBuffer(handle, &tmp_bo);
193 if (ret)
194 return ret;
195
196 if (importer_ != NULL) {
197 importer_->ReleaseBuffer(&bo_);
198 }
199
200 importer_ = importer;
201
202 bo_ = tmp_bo;
203
204 return 0;
205}
206
207int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle,
208 const gralloc_module_t *gralloc) {
209 native_handle_t *handle_copy = dup_buffer_handle(handle);
210 if (handle_copy == NULL) {
211 ALOGE("Failed to duplicate handle");
212 return -ENOMEM;
213 }
214
215 int ret = gralloc->registerBuffer(gralloc, handle_copy);
216 if (ret) {
217 ALOGE("Failed to register buffer handle %d", ret);
218 free_buffer_handle(handle_copy);
219 return ret;
220 }
221
222 Clear();
223
224 gralloc_ = gralloc;
225 handle_ = handle_copy;
226
227 return 0;
228}
229
230DrmHwcNativeHandle::~DrmHwcNativeHandle() {
231 Clear();
232}
233
234void DrmHwcNativeHandle::Clear() {
235 if (gralloc_ != NULL && handle_ != NULL) {
236 gralloc_->unregisterBuffer(gralloc_, handle_);
237 free_buffer_handle(handle_);
238 gralloc_ = NULL;
239 handle_ = NULL;
240 }
241}
242
243int DrmHwcLayer::InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer,
244 const gralloc_module_t *gralloc) {
245 sf_handle = sf_layer->handle;
Zach Reizner4a253652015-09-10 18:30:54 -0700246 alpha = sf_layer->planeAlpha;
247
Zach Reizner7e88be92015-10-12 15:20:33 -0700248 source_crop = DrmHwcRect<float>(
249 sf_layer->sourceCropf.left, sf_layer->sourceCropf.top,
250 sf_layer->sourceCropf.right, sf_layer->sourceCropf.bottom);
251 display_frame = DrmHwcRect<int>(
252 sf_layer->displayFrame.left, sf_layer->displayFrame.top,
253 sf_layer->displayFrame.right, sf_layer->displayFrame.bottom);
254
Zach Reizner4a253652015-09-10 18:30:54 -0700255 switch (sf_layer->transform) {
256 case 0:
257 transform = DrmHwcTransform::kIdentity;
258 break;
259 case HWC_TRANSFORM_FLIP_H:
260 transform = DrmHwcTransform::kFlipH;
261 break;
262 case HWC_TRANSFORM_FLIP_V:
263 transform = DrmHwcTransform::kFlipV;
264 break;
265 case HWC_TRANSFORM_ROT_90:
266 transform = DrmHwcTransform::kRotate90;
267 break;
268 case HWC_TRANSFORM_ROT_180:
269 transform = DrmHwcTransform::kRotate180;
270 break;
271 case HWC_TRANSFORM_ROT_270:
272 transform = DrmHwcTransform::kRotate270;
273 break;
274 default:
275 ALOGE("Invalid transform in hwc_layer_1_t %d", sf_layer->transform);
276 return -EINVAL;
277 }
278
279 switch (sf_layer->blending) {
280 case HWC_BLENDING_NONE:
281 blending = DrmHwcBlending::kNone;
282 break;
283 case HWC_BLENDING_PREMULT:
284 blending = DrmHwcBlending::kPreMult;
285 break;
286 case HWC_BLENDING_COVERAGE:
287 blending = DrmHwcBlending::kCoverage;
288 break;
289 default:
290 ALOGE("Invalid blending in hwc_layer_1_t %d", sf_layer->blending);
291 return -EINVAL;
292 }
293
Zach Reizner7e88be92015-10-12 15:20:33 -0700294 int ret = buffer.ImportBuffer(sf_layer->handle, importer);
295 if (ret)
296 return ret;
297
298 ret = handle.CopyBufferHandle(sf_layer->handle, gralloc);
299 if (ret)
300 return ret;
Zach Reizner4a253652015-09-10 18:30:54 -0700301
Zach Reizner36d7c6e2015-10-20 10:58:19 -0700302 ret = gralloc->perform(gralloc, GRALLOC_MODULE_PERFORM_GET_USAGE,
303 handle.get(), &gralloc_buffer_usage);
304 if (ret) {
305 // TODO(zachr): Once GRALLOC_MODULE_PERFORM_GET_USAGE is implemented, remove
306 // "ret = 0" and enable the error logging code.
307 ret = 0;
308#if 0
309 ALOGE("Failed to get usage for buffer %p (%d)", handle.get(), ret);
310 return ret;
311#endif
312 }
313
Zach Reizner4a253652015-09-10 18:30:54 -0700314 return 0;
315}
316
Zach Reiznerc6520e42015-08-13 14:32:09 -0700317static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
Sean Paul9046c642015-06-10 17:27:47 -0400318 int buff_len) {
319 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
320 std::ostringstream out;
321
322 ctx->drm.compositor()->Dump(&out);
323 std::string out_str = out.str();
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700324 strncpy(buff, out_str.c_str(),
325 std::min((size_t)buff_len, out_str.length() + 1));
326 buff[buff_len - 1] = '\0';
Sean Paul9046c642015-06-10 17:27:47 -0400327}
328
Sean Paulbd61c8d2015-10-29 15:00:17 -0400329static bool hwc_skip_layer(const std::pair<int, int> &indices, int i) {
330 return indices.first >= 0 && i >= indices.first && i <= indices.second;
331}
332
Sean Paulb386f1b2015-05-13 06:33:23 -0700333static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -0400334 hwc_display_contents_1_t **display_contents) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700335 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Zach Reizner1946fa72015-08-14 11:14:38 -0700336
Sean Paule42febf2015-05-07 11:35:29 -0700337 for (int i = 0; i < (int)num_displays; ++i) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400338 if (!display_contents[i])
339 continue;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500340
Sean Paul6f82f1d2015-10-21 20:05:05 -0400341 bool use_framebuffer_target = false;
Haixia Shid21f5282015-10-05 14:35:09 -0700342 if (i == HWC_DISPLAY_VIRTUAL) {
343 use_framebuffer_target = true;
344 } else {
345 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
346 if (!crtc) {
347 ALOGE("No crtc for display %d", i);
348 return -ENODEV;
349 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700350 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700351
Sean Paulbd61c8d2015-10-29 15:00:17 -0400352 // Since we can't composite HWC_SKIP_LAYERs by ourselves, we'll let SF
353 // handle all layers in between the first and last skip layers. So find the
354 // outer indices and mark everything in between as HWC_FRAMEBUFFER
355 std::pair<int, int> skip_layer_indices(-1, -1);
Zach Reizner45624d32015-06-10 16:03:01 -0700356 int num_layers = display_contents[i]->numHwLayers;
Sean Paulbd61c8d2015-10-29 15:00:17 -0400357 for (int j = 0; !use_framebuffer_target && j < num_layers; ++j) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700358 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700359
Sean Paulbd61c8d2015-10-29 15:00:17 -0400360 if (!(layer->flags & HWC_SKIP_LAYER))
361 continue;
362
363 if (skip_layer_indices.first == -1)
364 skip_layer_indices.first = j;
365 skip_layer_indices.second = j;
366 }
367
368 for (int j = 0; j < num_layers; ++j) {
369 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
370
371 if (!use_framebuffer_target && !hwc_skip_layer(skip_layer_indices, j)) {
Zach Reizner1946fa72015-08-14 11:14:38 -0700372 if (layer->compositionType == HWC_FRAMEBUFFER)
373 layer->compositionType = HWC_OVERLAY;
374 } else {
375 switch (layer->compositionType) {
376 case HWC_OVERLAY:
377 case HWC_BACKGROUND:
378 case HWC_SIDEBAND:
379 case HWC_CURSOR_OVERLAY:
380 layer->compositionType = HWC_FRAMEBUFFER;
381 break;
382 }
383 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400384 }
385 }
Sean Pauldffca952015-02-04 10:19:55 -0800386
Sean Paulef8f1f92015-04-29 16:05:23 -0400387 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500388}
389
Zach Reizner09807052015-08-13 14:53:41 -0700390static void hwc_add_layer_to_retire_fence(
391 hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) {
Sean Paul04206122015-07-16 15:59:24 -0400392 if (layer->releaseFenceFd < 0)
393 return;
394
395 if (display_contents->retireFenceFd >= 0) {
396 int old_retire_fence = display_contents->retireFenceFd;
Zach Reiznerc6520e42015-08-13 14:32:09 -0700397 display_contents->retireFenceFd =
398 sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd);
Sean Paul04206122015-07-16 15:59:24 -0400399 close(old_retire_fence);
400 } else {
401 display_contents->retireFenceFd = dup(layer->releaseFenceFd);
402 }
403}
404
Sean Paule0c4c3d2015-01-20 16:56:04 -0500405static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
Zach Reizner4a253652015-09-10 18:30:54 -0700406 hwc_display_contents_1_t **sf_display_contents) {
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -0700407 ATRACE_CALL();
Sean Paulef8f1f92015-04-29 16:05:23 -0400408 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Zach Reizner4a253652015-09-10 18:30:54 -0700409 int ret = 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500410
Zach Reizner4a253652015-09-10 18:30:54 -0700411 std::vector<CheckedOutputFd> checked_output_fences;
412 std::vector<DrmHwcDisplayContents> displays_contents;
Zach Reizner09807052015-08-13 14:53:41 -0700413 std::vector<DrmCompositionDisplayLayersMap> layers_map;
414 std::vector<std::vector<size_t>> layers_indices;
Zach Reizner4a253652015-09-10 18:30:54 -0700415 displays_contents.reserve(num_displays);
416 // layers_map.reserve(num_displays);
Zach Reizner09807052015-08-13 14:53:41 -0700417 layers_indices.reserve(num_displays);
418
Zach Reizner4a253652015-09-10 18:30:54 -0700419 // Phase one does nothing that would cause errors. Only take ownership of FDs.
420 for (size_t i = 0; i < num_displays; ++i) {
421 hwc_display_contents_1_t *dc = sf_display_contents[i];
422 displays_contents.emplace_back();
423 DrmHwcDisplayContents &display_contents = displays_contents.back();
Haixia Shi7acc59b2015-09-30 10:57:54 -0700424 layers_indices.emplace_back();
425 std::vector<size_t> &indices_to_composite = layers_indices.back();
Zach Reizner4a253652015-09-10 18:30:54 -0700426
427 if (!sf_display_contents[i])
Sean Paulb386f1b2015-05-13 06:33:23 -0700428 continue;
Zach Reizner09807052015-08-13 14:53:41 -0700429
Haixia Shid21f5282015-10-05 14:35:09 -0700430 if (i == HWC_DISPLAY_VIRTUAL) {
431 ctx->virtual_compositor_worker.QueueComposite(dc);
432 continue;
433 }
434
Zach Reizner4a253652015-09-10 18:30:54 -0700435 std::ostringstream display_index_formatter;
436 display_index_formatter << "retire fence for display " << i;
437 std::string display_fence_description(display_index_formatter.str());
438 checked_output_fences.emplace_back(&dc->retireFenceFd,
439 display_fence_description.c_str(),
440 ctx->dummy_timeline);
441 display_contents.retire_fence = OutputFd(&dc->retireFenceFd);
Zach Reizner09807052015-08-13 14:53:41 -0700442
Zach Reizner4a253652015-09-10 18:30:54 -0700443 size_t num_dc_layers = dc->numHwLayers;
Haixia Shi1034bb72015-09-09 12:08:20 -0700444 int framebuffer_target_index = -1;
Zach Reizner4a253652015-09-10 18:30:54 -0700445 for (size_t j = 0; j < num_dc_layers; ++j) {
446 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
Sean Paulbd61c8d2015-10-29 15:00:17 -0400447 if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET) {
448 framebuffer_target_index = j;
449 break;
450 }
451 }
452
453 for (size_t j = 0; j < num_dc_layers; ++j) {
454 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
Zach Reizner4a253652015-09-10 18:30:54 -0700455
456 display_contents.layers.emplace_back();
457 DrmHwcLayer &layer = display_contents.layers.back();
458
Sean Paulbd61c8d2015-10-29 15:00:17 -0400459 // In prepare() we marked all layers FRAMEBUFFER between SKIP_LAYER's.
460 // This means we should insert the FB_TARGET layer in the composition
461 // stack at the location of the first skip layer, and ignore the rest.
462 if (sf_layer->flags & HWC_SKIP_LAYER) {
463 if (framebuffer_target_index < 0)
464 continue;
465 int idx = framebuffer_target_index;
466 framebuffer_target_index = -1;
467 hwc_layer_1_t *fbt_layer = &dc->hwLayers[idx];
468 if (!fbt_layer->handle || (fbt_layer->flags & HWC_SKIP_LAYER)) {
469 ALOGE("Invalid HWC_FRAMEBUFFER_TARGET with HWC_SKIP_LAYER present");
470 continue;
471 }
472 indices_to_composite.push_back(idx);
Sean Paulb386f1b2015-05-13 06:33:23 -0700473 continue;
Sean Paulbd61c8d2015-10-29 15:00:17 -0400474 }
Zach Reizner4a253652015-09-10 18:30:54 -0700475
Sean Paul6f82f1d2015-10-21 20:05:05 -0400476 if (sf_layer->compositionType == HWC_OVERLAY)
477 indices_to_composite.push_back(j);
Zach Reizner4a253652015-09-10 18:30:54 -0700478
479 layer.acquire_fence.Set(sf_layer->acquireFenceFd);
480 sf_layer->acquireFenceFd = -1;
481
482 std::ostringstream layer_fence_formatter;
483 layer_fence_formatter << "release fence for layer " << j << " of display "
484 << i;
485 std::string layer_fence_description(layer_fence_formatter.str());
486 checked_output_fences.emplace_back(&sf_layer->releaseFenceFd,
487 layer_fence_description.c_str(),
488 ctx->dummy_timeline);
489 layer.release_fence = OutputFd(&sf_layer->releaseFenceFd);
Zach Reizner1946fa72015-08-14 11:14:38 -0700490 }
Zach Reizner4a253652015-09-10 18:30:54 -0700491
Sean Paulbd61c8d2015-10-29 15:00:17 -0400492 // This is a catch-all in case we get a frame without any overlay layers, or
493 // skip layers, but with a value fb_target layer. This _shouldn't_ happen,
494 // but it's not ruled out by the hwc specification
Sean Paul6f82f1d2015-10-21 20:05:05 -0400495 if (indices_to_composite.empty() && framebuffer_target_index >= 0) {
496 hwc_layer_1_t *sf_layer = &dc->hwLayers[framebuffer_target_index];
497 if (!sf_layer->handle || (sf_layer->flags & HWC_SKIP_LAYER)) {
498 ALOGE(
499 "Expected valid layer with HWC_FRAMEBUFFER_TARGET when all "
500 "HWC_OVERLAY layers are skipped.");
Zach Reizner4a253652015-09-10 18:30:54 -0700501 ret = -EINVAL;
Zach Reizner1946fa72015-08-14 11:14:38 -0700502 }
Sean Paul6f82f1d2015-10-21 20:05:05 -0400503 indices_to_composite.push_back(framebuffer_target_index);
Zach Reizner45624d32015-06-10 16:03:01 -0700504 }
Zach Reizner4a253652015-09-10 18:30:54 -0700505 }
Zach Reizner45624d32015-06-10 16:03:01 -0700506
Zach Reizner4a253652015-09-10 18:30:54 -0700507 if (ret)
508 return ret;
509
510 for (size_t i = 0; i < num_displays; ++i) {
511 hwc_display_contents_1_t *dc = sf_display_contents[i];
512 DrmHwcDisplayContents &display_contents = displays_contents[i];
Haixia Shi2fddd372015-10-15 16:21:48 -0700513 if (!sf_display_contents[i] || i == HWC_DISPLAY_VIRTUAL)
Zach Reizner4a253652015-09-10 18:30:54 -0700514 continue;
515
516 layers_map.emplace_back();
517 DrmCompositionDisplayLayersMap &map = layers_map.back();
Zach Reizneracba14b2015-10-13 18:19:26 -0700518 map.display = i;
Zach Reizner5757e822015-10-16 19:06:31 -0700519 map.geometry_changed =
520 (dc->flags & HWC_GEOMETRY_CHANGED) == HWC_GEOMETRY_CHANGED;
Zach Reizner4a253652015-09-10 18:30:54 -0700521 std::vector<size_t> &indices_to_composite = layers_indices[i];
522 for (size_t j : indices_to_composite) {
523 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
524
525 DrmHwcLayer &layer = display_contents.layers[j];
526
Zach Reiznerff30b522015-10-28 19:08:45 -0700527 ret = layer.InitFromHwcLayer(sf_layer, ctx->importer.get(), ctx->gralloc);
Zach Reizner7e88be92015-10-12 15:20:33 -0700528 if (ret) {
529 ALOGE("Failed to init composition from layer %d", ret);
530 return ret;
531 }
Zach Reizner4a253652015-09-10 18:30:54 -0700532 map.layers.emplace_back(std::move(layer));
533 }
534 }
535
536 std::unique_ptr<DrmComposition> composition(
Zach Reiznerff30b522015-10-28 19:08:45 -0700537 ctx->drm.compositor()->CreateComposition(ctx->importer.get()));
Zach Reizner4a253652015-09-10 18:30:54 -0700538 if (!composition) {
539 ALOGE("Drm composition init failed");
540 return -EINVAL;
Zach Reizner09807052015-08-13 14:53:41 -0700541 }
Zach Reizner45624d32015-06-10 16:03:01 -0700542
Zach Reizner09807052015-08-13 14:53:41 -0700543 ret = composition->SetLayers(layers_map.size(), layers_map.data());
544 if (ret) {
Zach Reizner09807052015-08-13 14:53:41 -0700545 return -EINVAL;
546 }
Zach Reizner45624d32015-06-10 16:03:01 -0700547
Zach Reizner09807052015-08-13 14:53:41 -0700548 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
549 if (ret) {
Zach Reizner09807052015-08-13 14:53:41 -0700550 return -EINVAL;
551 }
552
Zach Reizner566da2b2015-10-06 15:39:09 -0700553 for (size_t i = 0; i < num_displays; ++i) {
554 hwc_display_contents_1_t *dc = sf_display_contents[i];
555 if (!dc)
556 continue;
557
558 size_t num_dc_layers = dc->numHwLayers;
559 for (size_t j = 0; j < num_dc_layers; ++j) {
560 hwc_layer_1_t *layer = &dc->hwLayers[j];
561 if (layer->flags & HWC_SKIP_LAYER)
562 continue;
563 hwc_add_layer_to_retire_fence(layer, dc);
564 }
565 }
566
Zach Reizner09807052015-08-13 14:53:41 -0700567 composition.reset(NULL);
568
Sean Paulef8f1f92015-04-29 16:05:23 -0400569 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500570}
571
Sean Paulef8f1f92015-04-29 16:05:23 -0400572static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
573 int event, int enabled) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400574 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
575 return -EINVAL;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500576
Sean Paul4057be32015-05-13 06:23:09 -0700577 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
578 hwc_drm_display_t *hd = &ctx->displays[display];
579 return hd->vsync_worker.VSyncControl(enabled);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500580}
581
Sean Paulef8f1f92015-04-29 16:05:23 -0400582static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
583 int mode) {
584 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500585
Sean Paul6a55e9f2015-04-30 15:31:06 -0400586 uint64_t dpmsValue = 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400587 switch (mode) {
588 case HWC_POWER_MODE_OFF:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400589 dpmsValue = DRM_MODE_DPMS_OFF;
Sean Paulef8f1f92015-04-29 16:05:23 -0400590 break;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500591
Sean Paulef8f1f92015-04-29 16:05:23 -0400592 /* We can't support dozing right now, so go full on */
593 case HWC_POWER_MODE_DOZE:
594 case HWC_POWER_MODE_DOZE_SUSPEND:
595 case HWC_POWER_MODE_NORMAL:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400596 dpmsValue = DRM_MODE_DPMS_ON;
Sean Paulef8f1f92015-04-29 16:05:23 -0400597 break;
598 };
Sean Paul6a55e9f2015-04-30 15:31:06 -0400599 return ctx->drm.SetDpmsMode(display, dpmsValue);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500600}
601
Sean Paulef8f1f92015-04-29 16:05:23 -0400602static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
603 int *value) {
604 switch (what) {
605 case HWC_BACKGROUND_LAYER_SUPPORTED:
606 *value = 0; /* TODO: We should do this */
607 break;
608 case HWC_VSYNC_PERIOD:
609 ALOGW("Query for deprecated vsync value, returning 60Hz");
610 *value = 1000 * 1000 * 1000 / 60;
611 break;
612 case HWC_DISPLAY_TYPES_SUPPORTED:
Haixia Shi2fddd372015-10-15 16:21:48 -0700613 *value = HWC_DISPLAY_PRIMARY_BIT | HWC_DISPLAY_EXTERNAL_BIT |
614 HWC_DISPLAY_VIRTUAL_BIT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400615 break;
616 }
617 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500618}
619
Sean Paulef8f1f92015-04-29 16:05:23 -0400620static void hwc_register_procs(struct hwc_composer_device_1 *dev,
621 hwc_procs_t const *procs) {
622 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500623
Sean Paulef8f1f92015-04-29 16:05:23 -0400624 ctx->procs = procs;
Sean Paul4057be32015-05-13 06:23:09 -0700625
Zach Reiznerff30b522015-10-28 19:08:45 -0700626 for (std::pair<const int, hwc_drm_display> &display_entry : ctx->displays)
627 display_entry.second.vsync_worker.SetProcs(procs);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500628}
629
Sean Paulef8f1f92015-04-29 16:05:23 -0400630static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
631 int display, uint32_t *configs,
Sean Paul6a55e9f2015-04-30 15:31:06 -0400632 size_t *num_configs) {
633 if (!*num_configs)
Sean Paulef8f1f92015-04-29 16:05:23 -0400634 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500635
Sean Paulef8f1f92015-04-29 16:05:23 -0400636 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700637 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400638 hd->config_ids.clear();
639
640 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
641 if (!connector) {
642 ALOGE("Failed to get connector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400643 return -ENODEV;
644 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500645
Sean Paule42febf2015-05-07 11:35:29 -0700646 int ret = connector->UpdateModes();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400647 if (ret) {
648 ALOGE("Failed to update display modes %d", ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400649 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400650 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500651
Zach Reiznerff30b522015-10-28 19:08:45 -0700652 for (const DrmMode &mode : connector->modes()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400653 size_t idx = hd->config_ids.size();
654 if (idx == *num_configs)
655 break;
Zach Reiznerff30b522015-10-28 19:08:45 -0700656 hd->config_ids.push_back(mode.id());
657 configs[idx] = mode.id();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400658 }
659 *num_configs = hd->config_ids.size();
660 return *num_configs == 0 ? -1 : 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500661}
662
Sean Paulef8f1f92015-04-29 16:05:23 -0400663static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
664 int display, uint32_t config,
665 const uint32_t *attributes,
666 int32_t *values) {
667 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400668 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400669 if (!c) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400670 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400671 return -ENODEV;
672 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400673 DrmMode mode;
Zach Reiznerff30b522015-10-28 19:08:45 -0700674 for (const DrmMode &conn_mode : c->modes()) {
675 if (conn_mode.id() == config) {
676 mode = conn_mode;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400677 break;
678 }
679 }
680 if (mode.id() == 0) {
681 ALOGE("Failed to find active mode for display %d", display);
682 return -ENOENT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400683 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500684
Sean Paul6a55e9f2015-04-30 15:31:06 -0400685 uint32_t mm_width = c->mm_width();
686 uint32_t mm_height = c->mm_height();
Sean Paulef8f1f92015-04-29 16:05:23 -0400687 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
688 switch (attributes[i]) {
689 case HWC_DISPLAY_VSYNC_PERIOD:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400690 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
Sean Paulef8f1f92015-04-29 16:05:23 -0400691 break;
692 case HWC_DISPLAY_WIDTH:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400693 values[i] = mode.h_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400694 break;
695 case HWC_DISPLAY_HEIGHT:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400696 values[i] = mode.v_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400697 break;
698 case HWC_DISPLAY_DPI_X:
699 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400700 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400701 break;
702 case HWC_DISPLAY_DPI_Y:
703 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400704 values[i] =
705 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400706 break;
707 }
708 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400709 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500710}
711
Sean Paulef8f1f92015-04-29 16:05:23 -0400712static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
713 int display) {
714 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400715 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
716 if (!c) {
717 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400718 return -ENODEV;
719 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500720
Sean Paul6a55e9f2015-04-30 15:31:06 -0400721 DrmMode mode = c->active_mode();
Sean Paule42febf2015-05-07 11:35:29 -0700722 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400723 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
724 if (hd->config_ids[i] == mode.id())
725 return i;
Sean Paulef8f1f92015-04-29 16:05:23 -0400726 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400727 return -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500728}
729
Sean Paulef8f1f92015-04-29 16:05:23 -0400730static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
731 int index) {
732 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700733 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400734 if (index >= (int)hd->config_ids.size()) {
735 ALOGE("Invalid config index %d passed in", index);
736 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400737 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500738
Sean Paul877be972015-06-03 14:08:27 -0400739 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
740 if (!c) {
741 ALOGE("Failed to get connector for display %d", display);
742 return -ENODEV;
743 }
744 DrmMode mode;
Zach Reiznerff30b522015-10-28 19:08:45 -0700745 for (const DrmMode &conn_mode : c->modes()) {
746 if (conn_mode.id() == hd->config_ids[index]) {
747 mode = conn_mode;
Sean Paul877be972015-06-03 14:08:27 -0400748 break;
749 }
750 }
751 if (mode.id() != hd->config_ids[index]) {
752 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
753 return -ENOENT;
754 }
755 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400756 if (ret) {
Sean Paul877be972015-06-03 14:08:27 -0400757 ALOGE("Failed to set active config %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400758 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400759 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400760 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500761}
762
Sean Paulef8f1f92015-04-29 16:05:23 -0400763static int hwc_device_close(struct hw_device_t *dev) {
764 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulef8f1f92015-04-29 16:05:23 -0400765 delete ctx;
Sean Paulef8f1f92015-04-29 16:05:23 -0400766 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500767}
768
Sean Paul24a26e32015-02-04 10:34:47 -0800769/*
770 * TODO: This function sets the active config to the first one in the list. This
771 * should be fixed such that it selects the preferred mode for the display, or
772 * some other, saner, method of choosing the config.
773 */
Sean Paule42febf2015-05-07 11:35:29 -0700774static int hwc_set_initial_config(hwc_drm_display_t *hd) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400775 uint32_t config;
776 size_t num_configs = 1;
777 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
778 &num_configs);
779 if (ret || !num_configs)
780 return 0;
Sean Paul24a26e32015-02-04 10:34:47 -0800781
Sean Paulef8f1f92015-04-29 16:05:23 -0400782 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
783 if (ret) {
784 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
785 return ret;
786 }
Sean Paul24a26e32015-02-04 10:34:47 -0800787
Sean Paulef8f1f92015-04-29 16:05:23 -0400788 return ret;
Sean Paul24a26e32015-02-04 10:34:47 -0800789}
790
Sean Paul6a55e9f2015-04-30 15:31:06 -0400791static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
Sean Paule42febf2015-05-07 11:35:29 -0700792 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paulef8f1f92015-04-29 16:05:23 -0400793 hd->ctx = ctx;
794 hd->display = display;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500795
Sean Paulb386f1b2015-05-13 06:33:23 -0700796 int ret = hwc_set_initial_config(hd);
Sean Paulef8f1f92015-04-29 16:05:23 -0400797 if (ret) {
798 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400799 return ret;
800 }
Sean Paul24a26e32015-02-04 10:34:47 -0800801
Sean Paul4057be32015-05-13 06:23:09 -0700802 ret = hd->vsync_worker.Init(&ctx->drm, display);
803 if (ret) {
804 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
805 return ret;
806 }
807
Sean Paulef8f1f92015-04-29 16:05:23 -0400808 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500809}
810
Sean Paulef8f1f92015-04-29 16:05:23 -0400811static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400812 int ret;
Zach Reiznerff30b522015-10-28 19:08:45 -0700813 for (auto &conn : ctx->drm.connectors()) {
814 ret = hwc_initialize_display(ctx, conn->display());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400815 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700816 ALOGE("Failed to initialize display %d", conn->display());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400817 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400818 }
819 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400820
Haixia Shid21f5282015-10-05 14:35:09 -0700821 ret = ctx->virtual_compositor_worker.Init();
822 if (ret) {
823 ALOGE("Failed to initialize virtual compositor worker");
824 return ret;
825 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400826 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500827}
828
Sean Paulef8f1f92015-04-29 16:05:23 -0400829static int hwc_device_open(const struct hw_module_t *module, const char *name,
830 struct hw_device_t **dev) {
831 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
832 ALOGE("Invalid module name- %s", name);
833 return -EINVAL;
834 }
835
Zach Reiznerff30b522015-10-28 19:08:45 -0700836 std::unique_ptr<hwc_context_t> ctx(new hwc_context_t());
Sean Paulef8f1f92015-04-29 16:05:23 -0400837 if (!ctx) {
838 ALOGE("Failed to allocate hwc context");
839 return -ENOMEM;
840 }
841
Sean Paul6a55e9f2015-04-30 15:31:06 -0400842 int ret = ctx->drm.Init();
843 if (ret) {
844 ALOGE("Can't initialize Drm object %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400845 return ret;
846 }
847
Zach Reizner4a253652015-09-10 18:30:54 -0700848 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
849 (const hw_module_t **)&ctx->gralloc);
850 if (ret) {
851 ALOGE("Failed to open gralloc module %d", ret);
Zach Reizner4a253652015-09-10 18:30:54 -0700852 return ret;
853 }
854
855 ret = ctx->dummy_timeline.Init();
856 if (ret) {
857 ALOGE("Failed to create dummy sw sync timeline %d", ret);
858 return ret;
859 }
860
Zach Reiznerff30b522015-10-28 19:08:45 -0700861 ctx->importer.reset(Importer::CreateInstance(&ctx->drm));
Sean Paulda6270d2015-06-01 14:11:52 -0400862 if (!ctx->importer) {
863 ALOGE("Failed to create importer instance");
Sean Paulef8f1f92015-04-29 16:05:23 -0400864 return ret;
865 }
866
Zach Reiznerff30b522015-10-28 19:08:45 -0700867 ret = hwc_enumerate_displays(ctx.get());
Sean Paulef8f1f92015-04-29 16:05:23 -0400868 if (ret) {
869 ALOGE("Failed to enumerate displays: %s", strerror(ret));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400870 return ret;
871 }
872
Sean Paulef8f1f92015-04-29 16:05:23 -0400873 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
874 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
875 ctx->device.common.module = const_cast<hw_module_t *>(module);
876 ctx->device.common.close = hwc_device_close;
877
Sean Paul9046c642015-06-10 17:27:47 -0400878 ctx->device.dump = hwc_dump;
Sean Paulef8f1f92015-04-29 16:05:23 -0400879 ctx->device.prepare = hwc_prepare;
880 ctx->device.set = hwc_set;
881 ctx->device.eventControl = hwc_event_control;
882 ctx->device.setPowerMode = hwc_set_power_mode;
883 ctx->device.query = hwc_query;
884 ctx->device.registerProcs = hwc_register_procs;
885 ctx->device.getDisplayConfigs = hwc_get_display_configs;
886 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
887 ctx->device.getActiveConfig = hwc_get_active_config;
888 ctx->device.setActiveConfig = hwc_set_active_config;
889 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
890
891 *dev = &ctx->device.common;
Zach Reiznerff30b522015-10-28 19:08:45 -0700892 ctx.release();
Sean Paulef8f1f92015-04-29 16:05:23 -0400893
894 return 0;
895}
Sean Paul6a55e9f2015-04-30 15:31:06 -0400896}
Sean Paulef8f1f92015-04-29 16:05:23 -0400897
Sean Paul6a55e9f2015-04-30 15:31:06 -0400898static struct hw_module_methods_t hwc_module_methods = {
899 open : android::hwc_device_open
900};
Sean Paule0c4c3d2015-01-20 16:56:04 -0500901
902hwc_module_t HAL_MODULE_INFO_SYM = {
Sean Paulef8f1f92015-04-29 16:05:23 -0400903 common : {
904 tag : HARDWARE_MODULE_TAG,
905 version_major : 1,
906 version_minor : 0,
907 id : HWC_HARDWARE_MODULE_ID,
908 name : "DRM hwcomposer module",
909 author : "The Android Open Source Project",
910 methods : &hwc_module_methods,
911 dso : NULL,
912 reserved : {0},
913 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500914};