blob: f1d6fbb13ddbcdd86684b3eb91e765fd4eb1ca0f [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
Sean Paulef8f1f92015-04-29 16:05:23 -040020#include "drm_hwcomposer.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
Stéphane Marchesincb3f9842015-06-19 14:50:45 -070049#define HWC_FB_BUFFERS 3
Sean Paule0c4c3d2015-01-20 16:56:04 -050050
Sean Paul6a55e9f2015-04-30 15:31:06 -040051namespace android {
Sean Paule0c4c3d2015-01-20 16:56:04 -050052
Zach Reizner4a253652015-09-10 18:30:54 -070053class 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
87struct 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 Paule42febf2015-05-07 11:35:29 -0700119typedef struct hwc_drm_display {
Sean Paulef8f1f92015-04-29 16:05:23 -0400120 struct hwc_context_t *ctx;
121 int display;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500122
Sean Paul6a55e9f2015-04-30 15:31:06 -0400123 std::vector<uint32_t> config_ids;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500124
Sean Paul4057be32015-05-13 06:23:09 -0700125 VSyncWorker vsync_worker;
Sean Paule42febf2015-05-07 11:35:29 -0700126} hwc_drm_display_t;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500127
128struct hwc_context_t {
Sean Paule42febf2015-05-07 11:35:29 -0700129 // map of display:hwc_drm_display_t
130 typedef std::map<int, hwc_drm_display_t> DisplayMap;
131 typedef DisplayMap::iterator DisplayMapIter;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500132
Sean Paul6f82f1d2015-10-21 20:05:05 -0400133 hwc_context_t() : procs(NULL), importer(NULL) {
Sean Paulda6270d2015-06-01 14:11:52 -0400134 }
135
136 ~hwc_context_t() {
Haixia Shid21f5282015-10-05 14:35:09 -0700137 virtual_compositor_worker.Exit();
Sean Paulda6270d2015-06-01 14:11:52 -0400138 delete importer;
139 }
140
Sean Paule42febf2015-05-07 11:35:29 -0700141 hwc_composer_device_1_t device;
Sean Paulef8f1f92015-04-29 16:05:23 -0400142 hwc_procs_t const *procs;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500143
Sean Paule42febf2015-05-07 11:35:29 -0700144 DisplayMap displays;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400145 DrmResources drm;
Sean Paulda6270d2015-06-01 14:11:52 -0400146 Importer *importer;
Zach Reizner4a253652015-09-10 18:30:54 -0700147 const gralloc_module_t *gralloc;
148 DummySwSyncTimeline dummy_timeline;
Haixia Shid21f5282015-10-05 14:35:09 -0700149 VirtualCompositorWorker virtual_compositor_worker;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500150};
151
Zach Reizner4a253652015-09-10 18:30:54 -0700152static native_handle_t *dup_buffer_handle(buffer_handle_t handle) {
153 native_handle_t *new_handle =
154 native_handle_create(handle->numFds, handle->numInts);
155 if (new_handle == NULL)
156 return NULL;
157
158 const int *old_data = handle->data;
159 int *new_data = new_handle->data;
160 for (int i = 0; i < handle->numFds; i++) {
161 *new_data = dup(*old_data);
162 old_data++;
163 new_data++;
164 }
165 memcpy(new_data, old_data, sizeof(int) * handle->numInts);
166
167 return new_handle;
168}
169
170static void free_buffer_handle(native_handle_t *handle) {
171 int ret = native_handle_close(handle);
172 if (ret)
173 ALOGE("Failed to close native handle %d", ret);
174 ret = native_handle_delete(handle);
175 if (ret)
176 ALOGE("Failed to delete native handle %d", ret);
177}
178
179OutputFd &OutputFd::operator=(OutputFd &&rhs) {
180 if (fd_ == NULL) {
181 std::swap(fd_, rhs.fd_);
182 } else {
183 if (*fd_ < 0) {
184 ALOGE("Failed to fill OutputFd %p before assignment", fd_);
185 }
186 fd_ = rhs.fd_;
187 rhs.fd_ = NULL;
188 }
189
190 return *this;
191}
192
Zach Reiznerf99d53f2015-10-09 13:02:55 -0700193const hwc_drm_bo *DrmHwcBuffer::operator->() const {
Zach Reizner4a253652015-09-10 18:30:54 -0700194 if (importer_ == NULL) {
Zach Reiznerf99d53f2015-10-09 13:02:55 -0700195 ALOGE("Access of non-existent BO");
Zach Reizner4a253652015-09-10 18:30:54 -0700196 exit(1);
197 return NULL;
198 }
199 return &bo_;
200}
201
202void DrmHwcBuffer::Clear() {
203 if (importer_ != NULL) {
204 importer_->ReleaseBuffer(&bo_);
205 importer_ = NULL;
206 }
207}
208
209int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
210 hwc_drm_bo tmp_bo;
211
212 int ret = importer->ImportBuffer(handle, &tmp_bo);
213 if (ret)
214 return ret;
215
216 if (importer_ != NULL) {
217 importer_->ReleaseBuffer(&bo_);
218 }
219
220 importer_ = importer;
221
222 bo_ = tmp_bo;
223
224 return 0;
225}
226
227int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle,
228 const gralloc_module_t *gralloc) {
229 native_handle_t *handle_copy = dup_buffer_handle(handle);
230 if (handle_copy == NULL) {
231 ALOGE("Failed to duplicate handle");
232 return -ENOMEM;
233 }
234
235 int ret = gralloc->registerBuffer(gralloc, handle_copy);
236 if (ret) {
237 ALOGE("Failed to register buffer handle %d", ret);
238 free_buffer_handle(handle_copy);
239 return ret;
240 }
241
242 Clear();
243
244 gralloc_ = gralloc;
245 handle_ = handle_copy;
246
247 return 0;
248}
249
250DrmHwcNativeHandle::~DrmHwcNativeHandle() {
251 Clear();
252}
253
254void DrmHwcNativeHandle::Clear() {
255 if (gralloc_ != NULL && handle_ != NULL) {
256 gralloc_->unregisterBuffer(gralloc_, handle_);
257 free_buffer_handle(handle_);
258 gralloc_ = NULL;
259 handle_ = NULL;
260 }
261}
262
263int DrmHwcLayer::InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer,
264 const gralloc_module_t *gralloc) {
265 sf_handle = sf_layer->handle;
Zach Reizner4a253652015-09-10 18:30:54 -0700266 alpha = sf_layer->planeAlpha;
267
Zach Reizner7e88be92015-10-12 15:20:33 -0700268 source_crop = DrmHwcRect<float>(
269 sf_layer->sourceCropf.left, sf_layer->sourceCropf.top,
270 sf_layer->sourceCropf.right, sf_layer->sourceCropf.bottom);
271 display_frame = DrmHwcRect<int>(
272 sf_layer->displayFrame.left, sf_layer->displayFrame.top,
273 sf_layer->displayFrame.right, sf_layer->displayFrame.bottom);
274
Zach Reizner4a253652015-09-10 18:30:54 -0700275 switch (sf_layer->transform) {
276 case 0:
277 transform = DrmHwcTransform::kIdentity;
278 break;
279 case HWC_TRANSFORM_FLIP_H:
280 transform = DrmHwcTransform::kFlipH;
281 break;
282 case HWC_TRANSFORM_FLIP_V:
283 transform = DrmHwcTransform::kFlipV;
284 break;
285 case HWC_TRANSFORM_ROT_90:
286 transform = DrmHwcTransform::kRotate90;
287 break;
288 case HWC_TRANSFORM_ROT_180:
289 transform = DrmHwcTransform::kRotate180;
290 break;
291 case HWC_TRANSFORM_ROT_270:
292 transform = DrmHwcTransform::kRotate270;
293 break;
294 default:
295 ALOGE("Invalid transform in hwc_layer_1_t %d", sf_layer->transform);
296 return -EINVAL;
297 }
298
299 switch (sf_layer->blending) {
300 case HWC_BLENDING_NONE:
301 blending = DrmHwcBlending::kNone;
302 break;
303 case HWC_BLENDING_PREMULT:
304 blending = DrmHwcBlending::kPreMult;
305 break;
306 case HWC_BLENDING_COVERAGE:
307 blending = DrmHwcBlending::kCoverage;
308 break;
309 default:
310 ALOGE("Invalid blending in hwc_layer_1_t %d", sf_layer->blending);
311 return -EINVAL;
312 }
313
Zach Reizner7e88be92015-10-12 15:20:33 -0700314 int ret = buffer.ImportBuffer(sf_layer->handle, importer);
315 if (ret)
316 return ret;
317
318 ret = handle.CopyBufferHandle(sf_layer->handle, gralloc);
319 if (ret)
320 return ret;
Zach Reizner4a253652015-09-10 18:30:54 -0700321
322 return 0;
323}
324
Zach Reiznerc6520e42015-08-13 14:32:09 -0700325static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
Sean Paul9046c642015-06-10 17:27:47 -0400326 int buff_len) {
327 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
328 std::ostringstream out;
329
330 ctx->drm.compositor()->Dump(&out);
331 std::string out_str = out.str();
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700332 strncpy(buff, out_str.c_str(),
333 std::min((size_t)buff_len, out_str.length() + 1));
334 buff[buff_len - 1] = '\0';
Sean Paul9046c642015-06-10 17:27:47 -0400335}
336
Sean Paulb386f1b2015-05-13 06:33:23 -0700337static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -0400338 hwc_display_contents_1_t **display_contents) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700339 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Zach Reizner1946fa72015-08-14 11:14:38 -0700340
Sean Paule42febf2015-05-07 11:35:29 -0700341 for (int i = 0; i < (int)num_displays; ++i) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400342 if (!display_contents[i])
343 continue;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500344
Sean Paul6f82f1d2015-10-21 20:05:05 -0400345 bool use_framebuffer_target = false;
Haixia Shid21f5282015-10-05 14:35:09 -0700346 if (i == HWC_DISPLAY_VIRTUAL) {
347 use_framebuffer_target = true;
348 } else {
349 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
350 if (!crtc) {
351 ALOGE("No crtc for display %d", i);
352 return -ENODEV;
353 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700354 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700355
Zach Reizner45624d32015-06-10 16:03:01 -0700356 int num_layers = display_contents[i]->numHwLayers;
357 for (int j = 0; 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
Haixia Shid21f5282015-10-05 14:35:09 -0700360 if (!use_framebuffer_target) {
Zach Reizner1946fa72015-08-14 11:14:38 -0700361 if (layer->compositionType == HWC_FRAMEBUFFER)
362 layer->compositionType = HWC_OVERLAY;
363 } else {
364 switch (layer->compositionType) {
365 case HWC_OVERLAY:
366 case HWC_BACKGROUND:
367 case HWC_SIDEBAND:
368 case HWC_CURSOR_OVERLAY:
369 layer->compositionType = HWC_FRAMEBUFFER;
370 break;
371 }
372 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400373 }
374 }
Sean Pauldffca952015-02-04 10:19:55 -0800375
Sean Paulef8f1f92015-04-29 16:05:23 -0400376 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500377}
378
Zach Reizner09807052015-08-13 14:53:41 -0700379static void hwc_add_layer_to_retire_fence(
380 hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) {
Sean Paul04206122015-07-16 15:59:24 -0400381 if (layer->releaseFenceFd < 0)
382 return;
383
384 if (display_contents->retireFenceFd >= 0) {
385 int old_retire_fence = display_contents->retireFenceFd;
Zach Reiznerc6520e42015-08-13 14:32:09 -0700386 display_contents->retireFenceFd =
387 sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd);
Sean Paul04206122015-07-16 15:59:24 -0400388 close(old_retire_fence);
389 } else {
390 display_contents->retireFenceFd = dup(layer->releaseFenceFd);
391 }
392}
393
Sean Paule0c4c3d2015-01-20 16:56:04 -0500394static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
Zach Reizner4a253652015-09-10 18:30:54 -0700395 hwc_display_contents_1_t **sf_display_contents) {
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -0700396 ATRACE_CALL();
Sean Paulef8f1f92015-04-29 16:05:23 -0400397 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Zach Reizner4a253652015-09-10 18:30:54 -0700398 int ret = 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500399
Zach Reizner4a253652015-09-10 18:30:54 -0700400 std::vector<CheckedOutputFd> checked_output_fences;
401 std::vector<DrmHwcDisplayContents> displays_contents;
Zach Reizner09807052015-08-13 14:53:41 -0700402 std::vector<DrmCompositionDisplayLayersMap> layers_map;
403 std::vector<std::vector<size_t>> layers_indices;
Zach Reizner4a253652015-09-10 18:30:54 -0700404 displays_contents.reserve(num_displays);
405 // layers_map.reserve(num_displays);
Zach Reizner09807052015-08-13 14:53:41 -0700406 layers_indices.reserve(num_displays);
407
Zach Reizner4a253652015-09-10 18:30:54 -0700408 // Phase one does nothing that would cause errors. Only take ownership of FDs.
409 for (size_t i = 0; i < num_displays; ++i) {
410 hwc_display_contents_1_t *dc = sf_display_contents[i];
411 displays_contents.emplace_back();
412 DrmHwcDisplayContents &display_contents = displays_contents.back();
Haixia Shi7acc59b2015-09-30 10:57:54 -0700413 layers_indices.emplace_back();
414 std::vector<size_t> &indices_to_composite = layers_indices.back();
Zach Reizner4a253652015-09-10 18:30:54 -0700415
416 if (!sf_display_contents[i])
Sean Paulb386f1b2015-05-13 06:33:23 -0700417 continue;
Zach Reizner09807052015-08-13 14:53:41 -0700418
Haixia Shid21f5282015-10-05 14:35:09 -0700419 if (i == HWC_DISPLAY_VIRTUAL) {
420 ctx->virtual_compositor_worker.QueueComposite(dc);
421 continue;
422 }
423
Zach Reizner4a253652015-09-10 18:30:54 -0700424 std::ostringstream display_index_formatter;
425 display_index_formatter << "retire fence for display " << i;
426 std::string display_fence_description(display_index_formatter.str());
427 checked_output_fences.emplace_back(&dc->retireFenceFd,
428 display_fence_description.c_str(),
429 ctx->dummy_timeline);
430 display_contents.retire_fence = OutputFd(&dc->retireFenceFd);
Zach Reizner09807052015-08-13 14:53:41 -0700431
Zach Reizner4a253652015-09-10 18:30:54 -0700432 size_t num_dc_layers = dc->numHwLayers;
Haixia Shi1034bb72015-09-09 12:08:20 -0700433 int framebuffer_target_index = -1;
Zach Reizner4a253652015-09-10 18:30:54 -0700434 for (size_t j = 0; j < num_dc_layers; ++j) {
435 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
436
437 display_contents.layers.emplace_back();
438 DrmHwcLayer &layer = display_contents.layers.back();
439
440 if (sf_layer->flags & HWC_SKIP_LAYER)
Sean Paulb386f1b2015-05-13 06:33:23 -0700441 continue;
Zach Reizner4a253652015-09-10 18:30:54 -0700442
Sean Paul6f82f1d2015-10-21 20:05:05 -0400443 if (sf_layer->compositionType == HWC_OVERLAY)
444 indices_to_composite.push_back(j);
445 if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET)
446 framebuffer_target_index = j;
Zach Reizner4a253652015-09-10 18:30:54 -0700447
448 layer.acquire_fence.Set(sf_layer->acquireFenceFd);
449 sf_layer->acquireFenceFd = -1;
450
451 std::ostringstream layer_fence_formatter;
452 layer_fence_formatter << "release fence for layer " << j << " of display "
453 << i;
454 std::string layer_fence_description(layer_fence_formatter.str());
455 checked_output_fences.emplace_back(&sf_layer->releaseFenceFd,
456 layer_fence_description.c_str(),
457 ctx->dummy_timeline);
458 layer.release_fence = OutputFd(&sf_layer->releaseFenceFd);
Zach Reizner1946fa72015-08-14 11:14:38 -0700459 }
Zach Reizner4a253652015-09-10 18:30:54 -0700460
Sean Paul6f82f1d2015-10-21 20:05:05 -0400461 if (indices_to_composite.empty() && framebuffer_target_index >= 0) {
462 hwc_layer_1_t *sf_layer = &dc->hwLayers[framebuffer_target_index];
463 if (!sf_layer->handle || (sf_layer->flags & HWC_SKIP_LAYER)) {
464 ALOGE(
465 "Expected valid layer with HWC_FRAMEBUFFER_TARGET when all "
466 "HWC_OVERLAY layers are skipped.");
Zach Reizner4a253652015-09-10 18:30:54 -0700467 ret = -EINVAL;
Zach Reizner1946fa72015-08-14 11:14:38 -0700468 }
Sean Paul6f82f1d2015-10-21 20:05:05 -0400469 indices_to_composite.push_back(framebuffer_target_index);
Zach Reizner45624d32015-06-10 16:03:01 -0700470 }
Zach Reizner4a253652015-09-10 18:30:54 -0700471 }
Zach Reizner45624d32015-06-10 16:03:01 -0700472
Zach Reizner4a253652015-09-10 18:30:54 -0700473 if (ret)
474 return ret;
475
476 for (size_t i = 0; i < num_displays; ++i) {
477 hwc_display_contents_1_t *dc = sf_display_contents[i];
478 DrmHwcDisplayContents &display_contents = displays_contents[i];
Haixia Shi2fddd372015-10-15 16:21:48 -0700479 if (!sf_display_contents[i] || i == HWC_DISPLAY_VIRTUAL)
Zach Reizner4a253652015-09-10 18:30:54 -0700480 continue;
481
482 layers_map.emplace_back();
483 DrmCompositionDisplayLayersMap &map = layers_map.back();
Zach Reizneracba14b2015-10-13 18:19:26 -0700484 map.display = i;
Zach Reizner4a253652015-09-10 18:30:54 -0700485 std::vector<size_t> &indices_to_composite = layers_indices[i];
486 for (size_t j : indices_to_composite) {
487 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
488
489 DrmHwcLayer &layer = display_contents.layers[j];
490
Zach Reizner7e88be92015-10-12 15:20:33 -0700491 ret = layer.InitFromHwcLayer(sf_layer, ctx->importer, ctx->gralloc);
492 if (ret) {
493 ALOGE("Failed to init composition from layer %d", ret);
494 return ret;
495 }
Zach Reizner4a253652015-09-10 18:30:54 -0700496 map.layers.emplace_back(std::move(layer));
497 }
498 }
499
500 std::unique_ptr<DrmComposition> composition(
501 ctx->drm.compositor()->CreateComposition(ctx->importer));
502 if (!composition) {
503 ALOGE("Drm composition init failed");
504 return -EINVAL;
Zach Reizner09807052015-08-13 14:53:41 -0700505 }
Zach Reizner45624d32015-06-10 16:03:01 -0700506
Zach Reizner09807052015-08-13 14:53:41 -0700507 ret = composition->SetLayers(layers_map.size(), layers_map.data());
508 if (ret) {
Zach Reizner09807052015-08-13 14:53:41 -0700509 return -EINVAL;
510 }
Zach Reizner45624d32015-06-10 16:03:01 -0700511
Zach Reizner09807052015-08-13 14:53:41 -0700512 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
513 if (ret) {
Zach Reizner09807052015-08-13 14:53:41 -0700514 return -EINVAL;
515 }
516
Zach Reizner566da2b2015-10-06 15:39:09 -0700517 for (size_t i = 0; i < num_displays; ++i) {
518 hwc_display_contents_1_t *dc = sf_display_contents[i];
519 if (!dc)
520 continue;
521
522 size_t num_dc_layers = dc->numHwLayers;
523 for (size_t j = 0; j < num_dc_layers; ++j) {
524 hwc_layer_1_t *layer = &dc->hwLayers[j];
525 if (layer->flags & HWC_SKIP_LAYER)
526 continue;
527 hwc_add_layer_to_retire_fence(layer, dc);
528 }
529 }
530
Zach Reizner09807052015-08-13 14:53:41 -0700531 composition.reset(NULL);
532
Sean Paulef8f1f92015-04-29 16:05:23 -0400533 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500534}
535
Sean Paulef8f1f92015-04-29 16:05:23 -0400536static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
537 int event, int enabled) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400538 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
539 return -EINVAL;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500540
Sean Paul4057be32015-05-13 06:23:09 -0700541 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
542 hwc_drm_display_t *hd = &ctx->displays[display];
543 return hd->vsync_worker.VSyncControl(enabled);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500544}
545
Sean Paulef8f1f92015-04-29 16:05:23 -0400546static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
547 int mode) {
548 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500549
Sean Paul6a55e9f2015-04-30 15:31:06 -0400550 uint64_t dpmsValue = 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400551 switch (mode) {
552 case HWC_POWER_MODE_OFF:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400553 dpmsValue = DRM_MODE_DPMS_OFF;
Sean Paulef8f1f92015-04-29 16:05:23 -0400554 break;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500555
Sean Paulef8f1f92015-04-29 16:05:23 -0400556 /* We can't support dozing right now, so go full on */
557 case HWC_POWER_MODE_DOZE:
558 case HWC_POWER_MODE_DOZE_SUSPEND:
559 case HWC_POWER_MODE_NORMAL:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400560 dpmsValue = DRM_MODE_DPMS_ON;
Sean Paulef8f1f92015-04-29 16:05:23 -0400561 break;
562 };
Sean Paul6a55e9f2015-04-30 15:31:06 -0400563 return ctx->drm.SetDpmsMode(display, dpmsValue);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500564}
565
Sean Paulef8f1f92015-04-29 16:05:23 -0400566static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
567 int *value) {
568 switch (what) {
569 case HWC_BACKGROUND_LAYER_SUPPORTED:
570 *value = 0; /* TODO: We should do this */
571 break;
572 case HWC_VSYNC_PERIOD:
573 ALOGW("Query for deprecated vsync value, returning 60Hz");
574 *value = 1000 * 1000 * 1000 / 60;
575 break;
576 case HWC_DISPLAY_TYPES_SUPPORTED:
Haixia Shi2fddd372015-10-15 16:21:48 -0700577 *value = HWC_DISPLAY_PRIMARY_BIT | HWC_DISPLAY_EXTERNAL_BIT |
578 HWC_DISPLAY_VIRTUAL_BIT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400579 break;
580 }
581 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500582}
583
Sean Paulef8f1f92015-04-29 16:05:23 -0400584static void hwc_register_procs(struct hwc_composer_device_1 *dev,
585 hwc_procs_t const *procs) {
586 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500587
Sean Paulef8f1f92015-04-29 16:05:23 -0400588 ctx->procs = procs;
Sean Paul4057be32015-05-13 06:23:09 -0700589
590 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
591 iter != ctx->displays.end(); ++iter) {
592 iter->second.vsync_worker.SetProcs(procs);
593 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500594}
595
Sean Paulef8f1f92015-04-29 16:05:23 -0400596static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
597 int display, uint32_t *configs,
Sean Paul6a55e9f2015-04-30 15:31:06 -0400598 size_t *num_configs) {
599 if (!*num_configs)
Sean Paulef8f1f92015-04-29 16:05:23 -0400600 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500601
Sean Paulef8f1f92015-04-29 16:05:23 -0400602 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700603 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400604 hd->config_ids.clear();
605
606 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
607 if (!connector) {
608 ALOGE("Failed to get connector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400609 return -ENODEV;
610 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500611
Sean Paule42febf2015-05-07 11:35:29 -0700612 int ret = connector->UpdateModes();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400613 if (ret) {
614 ALOGE("Failed to update display modes %d", ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400615 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400616 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500617
Sean Paul6a55e9f2015-04-30 15:31:06 -0400618 for (DrmConnector::ModeIter iter = connector->begin_modes();
619 iter != connector->end_modes(); ++iter) {
620 size_t idx = hd->config_ids.size();
621 if (idx == *num_configs)
622 break;
623 hd->config_ids.push_back(iter->id());
624 configs[idx] = iter->id();
625 }
626 *num_configs = hd->config_ids.size();
627 return *num_configs == 0 ? -1 : 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500628}
629
Sean Paulef8f1f92015-04-29 16:05:23 -0400630static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
631 int display, uint32_t config,
632 const uint32_t *attributes,
633 int32_t *values) {
634 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400635 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400636 if (!c) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400637 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400638 return -ENODEV;
639 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400640 DrmMode mode;
641 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
642 ++iter) {
643 if (iter->id() == config) {
644 mode = *iter;
645 break;
646 }
647 }
648 if (mode.id() == 0) {
649 ALOGE("Failed to find active mode for display %d", display);
650 return -ENOENT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400651 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500652
Sean Paul6a55e9f2015-04-30 15:31:06 -0400653 uint32_t mm_width = c->mm_width();
654 uint32_t mm_height = c->mm_height();
Sean Paulef8f1f92015-04-29 16:05:23 -0400655 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
656 switch (attributes[i]) {
657 case HWC_DISPLAY_VSYNC_PERIOD:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400658 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
Sean Paulef8f1f92015-04-29 16:05:23 -0400659 break;
660 case HWC_DISPLAY_WIDTH:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400661 values[i] = mode.h_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400662 break;
663 case HWC_DISPLAY_HEIGHT:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400664 values[i] = mode.v_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400665 break;
666 case HWC_DISPLAY_DPI_X:
667 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400668 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400669 break;
670 case HWC_DISPLAY_DPI_Y:
671 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400672 values[i] =
673 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400674 break;
675 }
676 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400677 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500678}
679
Sean Paulef8f1f92015-04-29 16:05:23 -0400680static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
681 int display) {
682 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400683 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
684 if (!c) {
685 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400686 return -ENODEV;
687 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500688
Sean Paul6a55e9f2015-04-30 15:31:06 -0400689 DrmMode mode = c->active_mode();
Sean Paule42febf2015-05-07 11:35:29 -0700690 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400691 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
692 if (hd->config_ids[i] == mode.id())
693 return i;
Sean Paulef8f1f92015-04-29 16:05:23 -0400694 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400695 return -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500696}
697
Sean Paulef8f1f92015-04-29 16:05:23 -0400698static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
699 int index) {
700 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700701 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400702 if (index >= (int)hd->config_ids.size()) {
703 ALOGE("Invalid config index %d passed in", index);
704 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400705 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500706
Sean Paul877be972015-06-03 14:08:27 -0400707 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
708 if (!c) {
709 ALOGE("Failed to get connector for display %d", display);
710 return -ENODEV;
711 }
712 DrmMode mode;
713 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
714 ++iter) {
715 if (iter->id() == hd->config_ids[index]) {
716 mode = *iter;
717 break;
718 }
719 }
720 if (mode.id() != hd->config_ids[index]) {
721 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
722 return -ENOENT;
723 }
724 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400725 if (ret) {
Sean Paul877be972015-06-03 14:08:27 -0400726 ALOGE("Failed to set active config %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400727 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400728 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400729 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500730}
731
Sean Paulef8f1f92015-04-29 16:05:23 -0400732static int hwc_device_close(struct hw_device_t *dev) {
733 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulef8f1f92015-04-29 16:05:23 -0400734 delete ctx;
Sean Paulef8f1f92015-04-29 16:05:23 -0400735 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500736}
737
Sean Paul24a26e32015-02-04 10:34:47 -0800738/*
739 * TODO: This function sets the active config to the first one in the list. This
740 * should be fixed such that it selects the preferred mode for the display, or
741 * some other, saner, method of choosing the config.
742 */
Sean Paule42febf2015-05-07 11:35:29 -0700743static int hwc_set_initial_config(hwc_drm_display_t *hd) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400744 uint32_t config;
745 size_t num_configs = 1;
746 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
747 &num_configs);
748 if (ret || !num_configs)
749 return 0;
Sean Paul24a26e32015-02-04 10:34:47 -0800750
Sean Paulef8f1f92015-04-29 16:05:23 -0400751 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
752 if (ret) {
753 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
754 return ret;
755 }
Sean Paul24a26e32015-02-04 10:34:47 -0800756
Sean Paulef8f1f92015-04-29 16:05:23 -0400757 return ret;
Sean Paul24a26e32015-02-04 10:34:47 -0800758}
759
Sean Paul6a55e9f2015-04-30 15:31:06 -0400760static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
Sean Paule42febf2015-05-07 11:35:29 -0700761 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paulef8f1f92015-04-29 16:05:23 -0400762 hd->ctx = ctx;
763 hd->display = display;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500764
Sean Paulb386f1b2015-05-13 06:33:23 -0700765 int ret = hwc_set_initial_config(hd);
Sean Paulef8f1f92015-04-29 16:05:23 -0400766 if (ret) {
767 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400768 return ret;
769 }
Sean Paul24a26e32015-02-04 10:34:47 -0800770
Sean Paul4057be32015-05-13 06:23:09 -0700771 ret = hd->vsync_worker.Init(&ctx->drm, display);
772 if (ret) {
773 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
774 return ret;
775 }
776
Sean Paulef8f1f92015-04-29 16:05:23 -0400777 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500778}
779
Sean Paulef8f1f92015-04-29 16:05:23 -0400780static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400781 int ret;
782 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
783 c != ctx->drm.end_connectors(); ++c) {
784 ret = hwc_initialize_display(ctx, (*c)->display());
785 if (ret) {
786 ALOGE("Failed to initialize display %d", (*c)->display());
787 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400788 }
789 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400790
Haixia Shid21f5282015-10-05 14:35:09 -0700791 ret = ctx->virtual_compositor_worker.Init();
792 if (ret) {
793 ALOGE("Failed to initialize virtual compositor worker");
794 return ret;
795 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400796 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500797}
798
Sean Paulef8f1f92015-04-29 16:05:23 -0400799static int hwc_device_open(const struct hw_module_t *module, const char *name,
800 struct hw_device_t **dev) {
801 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
802 ALOGE("Invalid module name- %s", name);
803 return -EINVAL;
804 }
805
806 struct hwc_context_t *ctx = new hwc_context_t();
807 if (!ctx) {
808 ALOGE("Failed to allocate hwc context");
809 return -ENOMEM;
810 }
811
Sean Paul6a55e9f2015-04-30 15:31:06 -0400812 int ret = ctx->drm.Init();
813 if (ret) {
814 ALOGE("Can't initialize Drm object %d", ret);
815 delete ctx;
816 return ret;
817 }
818
Zach Reizner4a253652015-09-10 18:30:54 -0700819 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
820 (const hw_module_t **)&ctx->gralloc);
821 if (ret) {
822 ALOGE("Failed to open gralloc module %d", ret);
823 delete ctx;
824 return ret;
825 }
826
827 ret = ctx->dummy_timeline.Init();
828 if (ret) {
829 ALOGE("Failed to create dummy sw sync timeline %d", ret);
830 return ret;
831 }
832
Sean Paulda6270d2015-06-01 14:11:52 -0400833 ctx->importer = Importer::CreateInstance(&ctx->drm);
834 if (!ctx->importer) {
835 ALOGE("Failed to create importer instance");
Sean Paulef8f1f92015-04-29 16:05:23 -0400836 delete ctx;
837 return ret;
838 }
839
Sean Paulef8f1f92015-04-29 16:05:23 -0400840 ret = hwc_enumerate_displays(ctx);
841 if (ret) {
842 ALOGE("Failed to enumerate displays: %s", strerror(ret));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400843 delete ctx;
844 return ret;
845 }
846
Sean Paulef8f1f92015-04-29 16:05:23 -0400847 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
848 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
849 ctx->device.common.module = const_cast<hw_module_t *>(module);
850 ctx->device.common.close = hwc_device_close;
851
Sean Paul9046c642015-06-10 17:27:47 -0400852 ctx->device.dump = hwc_dump;
Sean Paulef8f1f92015-04-29 16:05:23 -0400853 ctx->device.prepare = hwc_prepare;
854 ctx->device.set = hwc_set;
855 ctx->device.eventControl = hwc_event_control;
856 ctx->device.setPowerMode = hwc_set_power_mode;
857 ctx->device.query = hwc_query;
858 ctx->device.registerProcs = hwc_register_procs;
859 ctx->device.getDisplayConfigs = hwc_get_display_configs;
860 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
861 ctx->device.getActiveConfig = hwc_get_active_config;
862 ctx->device.setActiveConfig = hwc_set_active_config;
863 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
864
865 *dev = &ctx->device.common;
866
867 return 0;
868}
Sean Paul6a55e9f2015-04-30 15:31:06 -0400869}
Sean Paulef8f1f92015-04-29 16:05:23 -0400870
Sean Paul6a55e9f2015-04-30 15:31:06 -0400871static struct hw_module_methods_t hwc_module_methods = {
872 open : android::hwc_device_open
873};
Sean Paule0c4c3d2015-01-20 16:56:04 -0500874
875hwc_module_t HAL_MODULE_INFO_SYM = {
Sean Paulef8f1f92015-04-29 16:05:23 -0400876 common : {
877 tag : HARDWARE_MODULE_TAG,
878 version_major : 1,
879 version_minor : 0,
880 id : HWC_HARDWARE_MODULE_ID,
881 name : "DRM hwcomposer module",
882 author : "The Android Open Source Project",
883 methods : &hwc_module_methods,
884 dso : NULL,
885 reserved : {0},
886 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500887};