blob: de5c66fbff9b9d7e9bb97cf1b9cd086a5364a0c6 [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
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;
130 typedef DisplayMap::iterator DisplayMapIter;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500131
Sean Paul6f82f1d2015-10-21 20:05:05 -0400132 hwc_context_t() : procs(NULL), importer(NULL) {
Sean Paulda6270d2015-06-01 14:11:52 -0400133 }
134
135 ~hwc_context_t() {
Haixia Shid21f5282015-10-05 14:35:09 -0700136 virtual_compositor_worker.Exit();
Sean Paulda6270d2015-06-01 14:11:52 -0400137 delete importer;
138 }
139
Sean Paule42febf2015-05-07 11:35:29 -0700140 hwc_composer_device_1_t device;
Sean Paulef8f1f92015-04-29 16:05:23 -0400141 hwc_procs_t const *procs;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500142
Sean Paule42febf2015-05-07 11:35:29 -0700143 DisplayMap displays;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400144 DrmResources drm;
Sean Paulda6270d2015-06-01 14:11:52 -0400145 Importer *importer;
Zach Reizner4a253652015-09-10 18:30:54 -0700146 const gralloc_module_t *gralloc;
147 DummySwSyncTimeline dummy_timeline;
Haixia Shid21f5282015-10-05 14:35:09 -0700148 VirtualCompositorWorker virtual_compositor_worker;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500149};
150
Zach Reizner4a253652015-09-10 18:30:54 -0700151static native_handle_t *dup_buffer_handle(buffer_handle_t handle) {
152 native_handle_t *new_handle =
153 native_handle_create(handle->numFds, handle->numInts);
154 if (new_handle == NULL)
155 return NULL;
156
157 const int *old_data = handle->data;
158 int *new_data = new_handle->data;
159 for (int i = 0; i < handle->numFds; i++) {
160 *new_data = dup(*old_data);
161 old_data++;
162 new_data++;
163 }
164 memcpy(new_data, old_data, sizeof(int) * handle->numInts);
165
166 return new_handle;
167}
168
169static void free_buffer_handle(native_handle_t *handle) {
170 int ret = native_handle_close(handle);
171 if (ret)
172 ALOGE("Failed to close native handle %d", ret);
173 ret = native_handle_delete(handle);
174 if (ret)
175 ALOGE("Failed to delete native handle %d", ret);
176}
177
178OutputFd &OutputFd::operator=(OutputFd &&rhs) {
179 if (fd_ == NULL) {
180 std::swap(fd_, rhs.fd_);
181 } else {
182 if (*fd_ < 0) {
183 ALOGE("Failed to fill OutputFd %p before assignment", fd_);
184 }
185 fd_ = rhs.fd_;
186 rhs.fd_ = NULL;
187 }
188
189 return *this;
190}
191
Zach Reiznerf99d53f2015-10-09 13:02:55 -0700192const hwc_drm_bo *DrmHwcBuffer::operator->() const {
Zach Reizner4a253652015-09-10 18:30:54 -0700193 if (importer_ == NULL) {
Zach Reiznerf99d53f2015-10-09 13:02:55 -0700194 ALOGE("Access of non-existent BO");
Zach Reizner4a253652015-09-10 18:30:54 -0700195 exit(1);
196 return NULL;
197 }
198 return &bo_;
199}
200
201void DrmHwcBuffer::Clear() {
202 if (importer_ != NULL) {
203 importer_->ReleaseBuffer(&bo_);
204 importer_ = NULL;
205 }
206}
207
208int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
209 hwc_drm_bo tmp_bo;
210
211 int ret = importer->ImportBuffer(handle, &tmp_bo);
212 if (ret)
213 return ret;
214
215 if (importer_ != NULL) {
216 importer_->ReleaseBuffer(&bo_);
217 }
218
219 importer_ = importer;
220
221 bo_ = tmp_bo;
222
223 return 0;
224}
225
226int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle,
227 const gralloc_module_t *gralloc) {
228 native_handle_t *handle_copy = dup_buffer_handle(handle);
229 if (handle_copy == NULL) {
230 ALOGE("Failed to duplicate handle");
231 return -ENOMEM;
232 }
233
234 int ret = gralloc->registerBuffer(gralloc, handle_copy);
235 if (ret) {
236 ALOGE("Failed to register buffer handle %d", ret);
237 free_buffer_handle(handle_copy);
238 return ret;
239 }
240
241 Clear();
242
243 gralloc_ = gralloc;
244 handle_ = handle_copy;
245
246 return 0;
247}
248
249DrmHwcNativeHandle::~DrmHwcNativeHandle() {
250 Clear();
251}
252
253void DrmHwcNativeHandle::Clear() {
254 if (gralloc_ != NULL && handle_ != NULL) {
255 gralloc_->unregisterBuffer(gralloc_, handle_);
256 free_buffer_handle(handle_);
257 gralloc_ = NULL;
258 handle_ = NULL;
259 }
260}
261
262int DrmHwcLayer::InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer,
263 const gralloc_module_t *gralloc) {
264 sf_handle = sf_layer->handle;
Zach Reizner4a253652015-09-10 18:30:54 -0700265 alpha = sf_layer->planeAlpha;
266
Zach Reizner7e88be92015-10-12 15:20:33 -0700267 source_crop = DrmHwcRect<float>(
268 sf_layer->sourceCropf.left, sf_layer->sourceCropf.top,
269 sf_layer->sourceCropf.right, sf_layer->sourceCropf.bottom);
270 display_frame = DrmHwcRect<int>(
271 sf_layer->displayFrame.left, sf_layer->displayFrame.top,
272 sf_layer->displayFrame.right, sf_layer->displayFrame.bottom);
273
Zach Reizner4a253652015-09-10 18:30:54 -0700274 switch (sf_layer->transform) {
275 case 0:
276 transform = DrmHwcTransform::kIdentity;
277 break;
278 case HWC_TRANSFORM_FLIP_H:
279 transform = DrmHwcTransform::kFlipH;
280 break;
281 case HWC_TRANSFORM_FLIP_V:
282 transform = DrmHwcTransform::kFlipV;
283 break;
284 case HWC_TRANSFORM_ROT_90:
285 transform = DrmHwcTransform::kRotate90;
286 break;
287 case HWC_TRANSFORM_ROT_180:
288 transform = DrmHwcTransform::kRotate180;
289 break;
290 case HWC_TRANSFORM_ROT_270:
291 transform = DrmHwcTransform::kRotate270;
292 break;
293 default:
294 ALOGE("Invalid transform in hwc_layer_1_t %d", sf_layer->transform);
295 return -EINVAL;
296 }
297
298 switch (sf_layer->blending) {
299 case HWC_BLENDING_NONE:
300 blending = DrmHwcBlending::kNone;
301 break;
302 case HWC_BLENDING_PREMULT:
303 blending = DrmHwcBlending::kPreMult;
304 break;
305 case HWC_BLENDING_COVERAGE:
306 blending = DrmHwcBlending::kCoverage;
307 break;
308 default:
309 ALOGE("Invalid blending in hwc_layer_1_t %d", sf_layer->blending);
310 return -EINVAL;
311 }
312
Zach Reizner7e88be92015-10-12 15:20:33 -0700313 int ret = buffer.ImportBuffer(sf_layer->handle, importer);
314 if (ret)
315 return ret;
316
317 ret = handle.CopyBufferHandle(sf_layer->handle, gralloc);
318 if (ret)
319 return ret;
Zach Reizner4a253652015-09-10 18:30:54 -0700320
Zach Reizner36d7c6e2015-10-20 10:58:19 -0700321 ret = gralloc->perform(gralloc, GRALLOC_MODULE_PERFORM_GET_USAGE,
322 handle.get(), &gralloc_buffer_usage);
323 if (ret) {
324 // TODO(zachr): Once GRALLOC_MODULE_PERFORM_GET_USAGE is implemented, remove
325 // "ret = 0" and enable the error logging code.
326 ret = 0;
327#if 0
328 ALOGE("Failed to get usage for buffer %p (%d)", handle.get(), ret);
329 return ret;
330#endif
331 }
332
Zach Reizner4a253652015-09-10 18:30:54 -0700333 return 0;
334}
335
Zach Reiznerc6520e42015-08-13 14:32:09 -0700336static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
Sean Paul9046c642015-06-10 17:27:47 -0400337 int buff_len) {
338 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
339 std::ostringstream out;
340
341 ctx->drm.compositor()->Dump(&out);
342 std::string out_str = out.str();
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700343 strncpy(buff, out_str.c_str(),
344 std::min((size_t)buff_len, out_str.length() + 1));
345 buff[buff_len - 1] = '\0';
Sean Paul9046c642015-06-10 17:27:47 -0400346}
347
Sean Paulb386f1b2015-05-13 06:33:23 -0700348static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -0400349 hwc_display_contents_1_t **display_contents) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700350 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Zach Reizner1946fa72015-08-14 11:14:38 -0700351
Sean Paule42febf2015-05-07 11:35:29 -0700352 for (int i = 0; i < (int)num_displays; ++i) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400353 if (!display_contents[i])
354 continue;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500355
Sean Paul6f82f1d2015-10-21 20:05:05 -0400356 bool use_framebuffer_target = false;
Haixia Shid21f5282015-10-05 14:35:09 -0700357 if (i == HWC_DISPLAY_VIRTUAL) {
358 use_framebuffer_target = true;
359 } else {
360 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
361 if (!crtc) {
362 ALOGE("No crtc for display %d", i);
363 return -ENODEV;
364 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700365 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700366
Zach Reizner45624d32015-06-10 16:03:01 -0700367 int num_layers = display_contents[i]->numHwLayers;
368 for (int j = 0; j < num_layers; j++) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700369 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700370
Haixia Shid21f5282015-10-05 14:35:09 -0700371 if (!use_framebuffer_target) {
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];
447
448 display_contents.layers.emplace_back();
449 DrmHwcLayer &layer = display_contents.layers.back();
450
451 if (sf_layer->flags & HWC_SKIP_LAYER)
Sean Paulb386f1b2015-05-13 06:33:23 -0700452 continue;
Zach Reizner4a253652015-09-10 18:30:54 -0700453
Sean Paul6f82f1d2015-10-21 20:05:05 -0400454 if (sf_layer->compositionType == HWC_OVERLAY)
455 indices_to_composite.push_back(j);
456 if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET)
457 framebuffer_target_index = j;
Zach Reizner4a253652015-09-10 18:30:54 -0700458
459 layer.acquire_fence.Set(sf_layer->acquireFenceFd);
460 sf_layer->acquireFenceFd = -1;
461
462 std::ostringstream layer_fence_formatter;
463 layer_fence_formatter << "release fence for layer " << j << " of display "
464 << i;
465 std::string layer_fence_description(layer_fence_formatter.str());
466 checked_output_fences.emplace_back(&sf_layer->releaseFenceFd,
467 layer_fence_description.c_str(),
468 ctx->dummy_timeline);
469 layer.release_fence = OutputFd(&sf_layer->releaseFenceFd);
Zach Reizner1946fa72015-08-14 11:14:38 -0700470 }
Zach Reizner4a253652015-09-10 18:30:54 -0700471
Sean Paul6f82f1d2015-10-21 20:05:05 -0400472 if (indices_to_composite.empty() && framebuffer_target_index >= 0) {
473 hwc_layer_1_t *sf_layer = &dc->hwLayers[framebuffer_target_index];
474 if (!sf_layer->handle || (sf_layer->flags & HWC_SKIP_LAYER)) {
475 ALOGE(
476 "Expected valid layer with HWC_FRAMEBUFFER_TARGET when all "
477 "HWC_OVERLAY layers are skipped.");
Zach Reizner4a253652015-09-10 18:30:54 -0700478 ret = -EINVAL;
Zach Reizner1946fa72015-08-14 11:14:38 -0700479 }
Sean Paul6f82f1d2015-10-21 20:05:05 -0400480 indices_to_composite.push_back(framebuffer_target_index);
Zach Reizner45624d32015-06-10 16:03:01 -0700481 }
Zach Reizner4a253652015-09-10 18:30:54 -0700482 }
Zach Reizner45624d32015-06-10 16:03:01 -0700483
Zach Reizner4a253652015-09-10 18:30:54 -0700484 if (ret)
485 return ret;
486
487 for (size_t i = 0; i < num_displays; ++i) {
488 hwc_display_contents_1_t *dc = sf_display_contents[i];
489 DrmHwcDisplayContents &display_contents = displays_contents[i];
Haixia Shi2fddd372015-10-15 16:21:48 -0700490 if (!sf_display_contents[i] || i == HWC_DISPLAY_VIRTUAL)
Zach Reizner4a253652015-09-10 18:30:54 -0700491 continue;
492
493 layers_map.emplace_back();
494 DrmCompositionDisplayLayersMap &map = layers_map.back();
Zach Reizneracba14b2015-10-13 18:19:26 -0700495 map.display = i;
Zach Reizner5757e822015-10-16 19:06:31 -0700496 map.geometry_changed =
497 (dc->flags & HWC_GEOMETRY_CHANGED) == HWC_GEOMETRY_CHANGED;
Zach Reizner4a253652015-09-10 18:30:54 -0700498 std::vector<size_t> &indices_to_composite = layers_indices[i];
499 for (size_t j : indices_to_composite) {
500 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
501
502 DrmHwcLayer &layer = display_contents.layers[j];
503
Zach Reizner7e88be92015-10-12 15:20:33 -0700504 ret = layer.InitFromHwcLayer(sf_layer, ctx->importer, ctx->gralloc);
505 if (ret) {
506 ALOGE("Failed to init composition from layer %d", ret);
507 return ret;
508 }
Zach Reizner4a253652015-09-10 18:30:54 -0700509 map.layers.emplace_back(std::move(layer));
510 }
511 }
512
513 std::unique_ptr<DrmComposition> composition(
514 ctx->drm.compositor()->CreateComposition(ctx->importer));
515 if (!composition) {
516 ALOGE("Drm composition init failed");
517 return -EINVAL;
Zach Reizner09807052015-08-13 14:53:41 -0700518 }
Zach Reizner45624d32015-06-10 16:03:01 -0700519
Zach Reizner09807052015-08-13 14:53:41 -0700520 ret = composition->SetLayers(layers_map.size(), layers_map.data());
521 if (ret) {
Zach Reizner09807052015-08-13 14:53:41 -0700522 return -EINVAL;
523 }
Zach Reizner45624d32015-06-10 16:03:01 -0700524
Zach Reizner09807052015-08-13 14:53:41 -0700525 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
526 if (ret) {
Zach Reizner09807052015-08-13 14:53:41 -0700527 return -EINVAL;
528 }
529
Zach Reizner566da2b2015-10-06 15:39:09 -0700530 for (size_t i = 0; i < num_displays; ++i) {
531 hwc_display_contents_1_t *dc = sf_display_contents[i];
532 if (!dc)
533 continue;
534
535 size_t num_dc_layers = dc->numHwLayers;
536 for (size_t j = 0; j < num_dc_layers; ++j) {
537 hwc_layer_1_t *layer = &dc->hwLayers[j];
538 if (layer->flags & HWC_SKIP_LAYER)
539 continue;
540 hwc_add_layer_to_retire_fence(layer, dc);
541 }
542 }
543
Zach Reizner09807052015-08-13 14:53:41 -0700544 composition.reset(NULL);
545
Sean Paulef8f1f92015-04-29 16:05:23 -0400546 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500547}
548
Sean Paulef8f1f92015-04-29 16:05:23 -0400549static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
550 int event, int enabled) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400551 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
552 return -EINVAL;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500553
Sean Paul4057be32015-05-13 06:23:09 -0700554 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
555 hwc_drm_display_t *hd = &ctx->displays[display];
556 return hd->vsync_worker.VSyncControl(enabled);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500557}
558
Sean Paulef8f1f92015-04-29 16:05:23 -0400559static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
560 int mode) {
561 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500562
Sean Paul6a55e9f2015-04-30 15:31:06 -0400563 uint64_t dpmsValue = 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400564 switch (mode) {
565 case HWC_POWER_MODE_OFF:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400566 dpmsValue = DRM_MODE_DPMS_OFF;
Sean Paulef8f1f92015-04-29 16:05:23 -0400567 break;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500568
Sean Paulef8f1f92015-04-29 16:05:23 -0400569 /* We can't support dozing right now, so go full on */
570 case HWC_POWER_MODE_DOZE:
571 case HWC_POWER_MODE_DOZE_SUSPEND:
572 case HWC_POWER_MODE_NORMAL:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400573 dpmsValue = DRM_MODE_DPMS_ON;
Sean Paulef8f1f92015-04-29 16:05:23 -0400574 break;
575 };
Sean Paul6a55e9f2015-04-30 15:31:06 -0400576 return ctx->drm.SetDpmsMode(display, dpmsValue);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500577}
578
Sean Paulef8f1f92015-04-29 16:05:23 -0400579static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
580 int *value) {
581 switch (what) {
582 case HWC_BACKGROUND_LAYER_SUPPORTED:
583 *value = 0; /* TODO: We should do this */
584 break;
585 case HWC_VSYNC_PERIOD:
586 ALOGW("Query for deprecated vsync value, returning 60Hz");
587 *value = 1000 * 1000 * 1000 / 60;
588 break;
589 case HWC_DISPLAY_TYPES_SUPPORTED:
Haixia Shi2fddd372015-10-15 16:21:48 -0700590 *value = HWC_DISPLAY_PRIMARY_BIT | HWC_DISPLAY_EXTERNAL_BIT |
591 HWC_DISPLAY_VIRTUAL_BIT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400592 break;
593 }
594 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500595}
596
Sean Paulef8f1f92015-04-29 16:05:23 -0400597static void hwc_register_procs(struct hwc_composer_device_1 *dev,
598 hwc_procs_t const *procs) {
599 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500600
Sean Paulef8f1f92015-04-29 16:05:23 -0400601 ctx->procs = procs;
Sean Paul4057be32015-05-13 06:23:09 -0700602
603 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
604 iter != ctx->displays.end(); ++iter) {
605 iter->second.vsync_worker.SetProcs(procs);
606 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500607}
608
Sean Paulef8f1f92015-04-29 16:05:23 -0400609static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
610 int display, uint32_t *configs,
Sean Paul6a55e9f2015-04-30 15:31:06 -0400611 size_t *num_configs) {
612 if (!*num_configs)
Sean Paulef8f1f92015-04-29 16:05:23 -0400613 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500614
Sean Paulef8f1f92015-04-29 16:05:23 -0400615 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700616 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400617 hd->config_ids.clear();
618
619 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
620 if (!connector) {
621 ALOGE("Failed to get connector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400622 return -ENODEV;
623 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500624
Sean Paule42febf2015-05-07 11:35:29 -0700625 int ret = connector->UpdateModes();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400626 if (ret) {
627 ALOGE("Failed to update display modes %d", ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400628 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400629 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500630
Sean Paul6a55e9f2015-04-30 15:31:06 -0400631 for (DrmConnector::ModeIter iter = connector->begin_modes();
632 iter != connector->end_modes(); ++iter) {
633 size_t idx = hd->config_ids.size();
634 if (idx == *num_configs)
635 break;
636 hd->config_ids.push_back(iter->id());
637 configs[idx] = iter->id();
638 }
639 *num_configs = hd->config_ids.size();
640 return *num_configs == 0 ? -1 : 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500641}
642
Sean Paulef8f1f92015-04-29 16:05:23 -0400643static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
644 int display, uint32_t config,
645 const uint32_t *attributes,
646 int32_t *values) {
647 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400648 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400649 if (!c) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400650 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400651 return -ENODEV;
652 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400653 DrmMode mode;
654 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
655 ++iter) {
656 if (iter->id() == config) {
657 mode = *iter;
658 break;
659 }
660 }
661 if (mode.id() == 0) {
662 ALOGE("Failed to find active mode for display %d", display);
663 return -ENOENT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400664 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500665
Sean Paul6a55e9f2015-04-30 15:31:06 -0400666 uint32_t mm_width = c->mm_width();
667 uint32_t mm_height = c->mm_height();
Sean Paulef8f1f92015-04-29 16:05:23 -0400668 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
669 switch (attributes[i]) {
670 case HWC_DISPLAY_VSYNC_PERIOD:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400671 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
Sean Paulef8f1f92015-04-29 16:05:23 -0400672 break;
673 case HWC_DISPLAY_WIDTH:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400674 values[i] = mode.h_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400675 break;
676 case HWC_DISPLAY_HEIGHT:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400677 values[i] = mode.v_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400678 break;
679 case HWC_DISPLAY_DPI_X:
680 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400681 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400682 break;
683 case HWC_DISPLAY_DPI_Y:
684 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400685 values[i] =
686 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400687 break;
688 }
689 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400690 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500691}
692
Sean Paulef8f1f92015-04-29 16:05:23 -0400693static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
694 int display) {
695 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400696 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
697 if (!c) {
698 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400699 return -ENODEV;
700 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500701
Sean Paul6a55e9f2015-04-30 15:31:06 -0400702 DrmMode mode = c->active_mode();
Sean Paule42febf2015-05-07 11:35:29 -0700703 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400704 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
705 if (hd->config_ids[i] == mode.id())
706 return i;
Sean Paulef8f1f92015-04-29 16:05:23 -0400707 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400708 return -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500709}
710
Sean Paulef8f1f92015-04-29 16:05:23 -0400711static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
712 int index) {
713 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 if (index >= (int)hd->config_ids.size()) {
716 ALOGE("Invalid config index %d passed in", index);
717 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400718 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500719
Sean Paul877be972015-06-03 14:08:27 -0400720 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
721 if (!c) {
722 ALOGE("Failed to get connector for display %d", display);
723 return -ENODEV;
724 }
725 DrmMode mode;
726 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
727 ++iter) {
728 if (iter->id() == hd->config_ids[index]) {
729 mode = *iter;
730 break;
731 }
732 }
733 if (mode.id() != hd->config_ids[index]) {
734 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
735 return -ENOENT;
736 }
737 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400738 if (ret) {
Sean Paul877be972015-06-03 14:08:27 -0400739 ALOGE("Failed to set active config %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400740 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400741 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400742 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500743}
744
Sean Paulef8f1f92015-04-29 16:05:23 -0400745static int hwc_device_close(struct hw_device_t *dev) {
746 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulef8f1f92015-04-29 16:05:23 -0400747 delete ctx;
Sean Paulef8f1f92015-04-29 16:05:23 -0400748 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500749}
750
Sean Paul24a26e32015-02-04 10:34:47 -0800751/*
752 * TODO: This function sets the active config to the first one in the list. This
753 * should be fixed such that it selects the preferred mode for the display, or
754 * some other, saner, method of choosing the config.
755 */
Sean Paule42febf2015-05-07 11:35:29 -0700756static int hwc_set_initial_config(hwc_drm_display_t *hd) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400757 uint32_t config;
758 size_t num_configs = 1;
759 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
760 &num_configs);
761 if (ret || !num_configs)
762 return 0;
Sean Paul24a26e32015-02-04 10:34:47 -0800763
Sean Paulef8f1f92015-04-29 16:05:23 -0400764 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
765 if (ret) {
766 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
767 return ret;
768 }
Sean Paul24a26e32015-02-04 10:34:47 -0800769
Sean Paulef8f1f92015-04-29 16:05:23 -0400770 return ret;
Sean Paul24a26e32015-02-04 10:34:47 -0800771}
772
Sean Paul6a55e9f2015-04-30 15:31:06 -0400773static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
Sean Paule42febf2015-05-07 11:35:29 -0700774 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paulef8f1f92015-04-29 16:05:23 -0400775 hd->ctx = ctx;
776 hd->display = display;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500777
Sean Paulb386f1b2015-05-13 06:33:23 -0700778 int ret = hwc_set_initial_config(hd);
Sean Paulef8f1f92015-04-29 16:05:23 -0400779 if (ret) {
780 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400781 return ret;
782 }
Sean Paul24a26e32015-02-04 10:34:47 -0800783
Sean Paul4057be32015-05-13 06:23:09 -0700784 ret = hd->vsync_worker.Init(&ctx->drm, display);
785 if (ret) {
786 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
787 return ret;
788 }
789
Sean Paulef8f1f92015-04-29 16:05:23 -0400790 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500791}
792
Sean Paulef8f1f92015-04-29 16:05:23 -0400793static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400794 int ret;
795 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
796 c != ctx->drm.end_connectors(); ++c) {
797 ret = hwc_initialize_display(ctx, (*c)->display());
798 if (ret) {
799 ALOGE("Failed to initialize display %d", (*c)->display());
800 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400801 }
802 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400803
Haixia Shid21f5282015-10-05 14:35:09 -0700804 ret = ctx->virtual_compositor_worker.Init();
805 if (ret) {
806 ALOGE("Failed to initialize virtual compositor worker");
807 return ret;
808 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400809 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500810}
811
Sean Paulef8f1f92015-04-29 16:05:23 -0400812static int hwc_device_open(const struct hw_module_t *module, const char *name,
813 struct hw_device_t **dev) {
814 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
815 ALOGE("Invalid module name- %s", name);
816 return -EINVAL;
817 }
818
819 struct hwc_context_t *ctx = new hwc_context_t();
820 if (!ctx) {
821 ALOGE("Failed to allocate hwc context");
822 return -ENOMEM;
823 }
824
Sean Paul6a55e9f2015-04-30 15:31:06 -0400825 int ret = ctx->drm.Init();
826 if (ret) {
827 ALOGE("Can't initialize Drm object %d", ret);
828 delete ctx;
829 return ret;
830 }
831
Zach Reizner4a253652015-09-10 18:30:54 -0700832 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
833 (const hw_module_t **)&ctx->gralloc);
834 if (ret) {
835 ALOGE("Failed to open gralloc module %d", ret);
836 delete ctx;
837 return ret;
838 }
839
840 ret = ctx->dummy_timeline.Init();
841 if (ret) {
842 ALOGE("Failed to create dummy sw sync timeline %d", ret);
843 return ret;
844 }
845
Sean Paulda6270d2015-06-01 14:11:52 -0400846 ctx->importer = Importer::CreateInstance(&ctx->drm);
847 if (!ctx->importer) {
848 ALOGE("Failed to create importer instance");
Sean Paulef8f1f92015-04-29 16:05:23 -0400849 delete ctx;
850 return ret;
851 }
852
Sean Paulef8f1f92015-04-29 16:05:23 -0400853 ret = hwc_enumerate_displays(ctx);
854 if (ret) {
855 ALOGE("Failed to enumerate displays: %s", strerror(ret));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400856 delete ctx;
857 return ret;
858 }
859
Sean Paulef8f1f92015-04-29 16:05:23 -0400860 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
861 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
862 ctx->device.common.module = const_cast<hw_module_t *>(module);
863 ctx->device.common.close = hwc_device_close;
864
Sean Paul9046c642015-06-10 17:27:47 -0400865 ctx->device.dump = hwc_dump;
Sean Paulef8f1f92015-04-29 16:05:23 -0400866 ctx->device.prepare = hwc_prepare;
867 ctx->device.set = hwc_set;
868 ctx->device.eventControl = hwc_event_control;
869 ctx->device.setPowerMode = hwc_set_power_mode;
870 ctx->device.query = hwc_query;
871 ctx->device.registerProcs = hwc_register_procs;
872 ctx->device.getDisplayConfigs = hwc_get_display_configs;
873 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
874 ctx->device.getActiveConfig = hwc_get_active_config;
875 ctx->device.setActiveConfig = hwc_set_active_config;
876 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
877
878 *dev = &ctx->device.common;
879
880 return 0;
881}
Sean Paul6a55e9f2015-04-30 15:31:06 -0400882}
Sean Paulef8f1f92015-04-29 16:05:23 -0400883
Sean Paul6a55e9f2015-04-30 15:31:06 -0400884static struct hw_module_methods_t hwc_module_methods = {
885 open : android::hwc_device_open
886};
Sean Paule0c4c3d2015-01-20 16:56:04 -0500887
888hwc_module_t HAL_MODULE_INFO_SYM = {
Sean Paulef8f1f92015-04-29 16:05:23 -0400889 common : {
890 tag : HARDWARE_MODULE_TAG,
891 version_major : 1,
892 version_minor : 0,
893 id : HWC_HARDWARE_MODULE_ID,
894 name : "DRM hwcomposer module",
895 author : "The Android Open Source Project",
896 methods : &hwc_module_methods,
897 dso : NULL,
898 reserved : {0},
899 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500900};