blob: 7b64bf2d80c510323d64fdb917c638d838657dd7 [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
321 return 0;
322}
323
Zach Reiznerc6520e42015-08-13 14:32:09 -0700324static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
Sean Paul9046c642015-06-10 17:27:47 -0400325 int buff_len) {
326 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
327 std::ostringstream out;
328
329 ctx->drm.compositor()->Dump(&out);
330 std::string out_str = out.str();
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700331 strncpy(buff, out_str.c_str(),
332 std::min((size_t)buff_len, out_str.length() + 1));
333 buff[buff_len - 1] = '\0';
Sean Paul9046c642015-06-10 17:27:47 -0400334}
335
Sean Paulb386f1b2015-05-13 06:33:23 -0700336static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -0400337 hwc_display_contents_1_t **display_contents) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700338 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Zach Reizner1946fa72015-08-14 11:14:38 -0700339
Sean Paule42febf2015-05-07 11:35:29 -0700340 for (int i = 0; i < (int)num_displays; ++i) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400341 if (!display_contents[i])
342 continue;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500343
Sean Paul6f82f1d2015-10-21 20:05:05 -0400344 bool use_framebuffer_target = false;
Haixia Shid21f5282015-10-05 14:35:09 -0700345 if (i == HWC_DISPLAY_VIRTUAL) {
346 use_framebuffer_target = true;
347 } else {
348 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
349 if (!crtc) {
350 ALOGE("No crtc for display %d", i);
351 return -ENODEV;
352 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700353 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700354
Zach Reizner45624d32015-06-10 16:03:01 -0700355 int num_layers = display_contents[i]->numHwLayers;
356 for (int j = 0; j < num_layers; j++) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700357 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700358
Haixia Shid21f5282015-10-05 14:35:09 -0700359 if (!use_framebuffer_target) {
Zach Reizner1946fa72015-08-14 11:14:38 -0700360 if (layer->compositionType == HWC_FRAMEBUFFER)
361 layer->compositionType = HWC_OVERLAY;
362 } else {
363 switch (layer->compositionType) {
364 case HWC_OVERLAY:
365 case HWC_BACKGROUND:
366 case HWC_SIDEBAND:
367 case HWC_CURSOR_OVERLAY:
368 layer->compositionType = HWC_FRAMEBUFFER;
369 break;
370 }
371 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400372 }
373 }
Sean Pauldffca952015-02-04 10:19:55 -0800374
Sean Paulef8f1f92015-04-29 16:05:23 -0400375 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500376}
377
Zach Reizner09807052015-08-13 14:53:41 -0700378static void hwc_add_layer_to_retire_fence(
379 hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) {
Sean Paul04206122015-07-16 15:59:24 -0400380 if (layer->releaseFenceFd < 0)
381 return;
382
383 if (display_contents->retireFenceFd >= 0) {
384 int old_retire_fence = display_contents->retireFenceFd;
Zach Reiznerc6520e42015-08-13 14:32:09 -0700385 display_contents->retireFenceFd =
386 sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd);
Sean Paul04206122015-07-16 15:59:24 -0400387 close(old_retire_fence);
388 } else {
389 display_contents->retireFenceFd = dup(layer->releaseFenceFd);
390 }
391}
392
Sean Paule0c4c3d2015-01-20 16:56:04 -0500393static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
Zach Reizner4a253652015-09-10 18:30:54 -0700394 hwc_display_contents_1_t **sf_display_contents) {
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -0700395 ATRACE_CALL();
Sean Paulef8f1f92015-04-29 16:05:23 -0400396 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Zach Reizner4a253652015-09-10 18:30:54 -0700397 int ret = 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500398
Zach Reizner4a253652015-09-10 18:30:54 -0700399 std::vector<CheckedOutputFd> checked_output_fences;
400 std::vector<DrmHwcDisplayContents> displays_contents;
Zach Reizner09807052015-08-13 14:53:41 -0700401 std::vector<DrmCompositionDisplayLayersMap> layers_map;
402 std::vector<std::vector<size_t>> layers_indices;
Zach Reizner4a253652015-09-10 18:30:54 -0700403 displays_contents.reserve(num_displays);
404 // layers_map.reserve(num_displays);
Zach Reizner09807052015-08-13 14:53:41 -0700405 layers_indices.reserve(num_displays);
406
Zach Reizner4a253652015-09-10 18:30:54 -0700407 // Phase one does nothing that would cause errors. Only take ownership of FDs.
408 for (size_t i = 0; i < num_displays; ++i) {
409 hwc_display_contents_1_t *dc = sf_display_contents[i];
410 displays_contents.emplace_back();
411 DrmHwcDisplayContents &display_contents = displays_contents.back();
Haixia Shi7acc59b2015-09-30 10:57:54 -0700412 layers_indices.emplace_back();
413 std::vector<size_t> &indices_to_composite = layers_indices.back();
Zach Reizner4a253652015-09-10 18:30:54 -0700414
415 if (!sf_display_contents[i])
Sean Paulb386f1b2015-05-13 06:33:23 -0700416 continue;
Zach Reizner09807052015-08-13 14:53:41 -0700417
Haixia Shid21f5282015-10-05 14:35:09 -0700418 if (i == HWC_DISPLAY_VIRTUAL) {
419 ctx->virtual_compositor_worker.QueueComposite(dc);
420 continue;
421 }
422
Zach Reizner4a253652015-09-10 18:30:54 -0700423 std::ostringstream display_index_formatter;
424 display_index_formatter << "retire fence for display " << i;
425 std::string display_fence_description(display_index_formatter.str());
426 checked_output_fences.emplace_back(&dc->retireFenceFd,
427 display_fence_description.c_str(),
428 ctx->dummy_timeline);
429 display_contents.retire_fence = OutputFd(&dc->retireFenceFd);
Zach Reizner09807052015-08-13 14:53:41 -0700430
Zach Reizner4a253652015-09-10 18:30:54 -0700431 size_t num_dc_layers = dc->numHwLayers;
Haixia Shi1034bb72015-09-09 12:08:20 -0700432 int framebuffer_target_index = -1;
Zach Reizner4a253652015-09-10 18:30:54 -0700433 for (size_t j = 0; j < num_dc_layers; ++j) {
434 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
435
436 display_contents.layers.emplace_back();
437 DrmHwcLayer &layer = display_contents.layers.back();
438
439 if (sf_layer->flags & HWC_SKIP_LAYER)
Sean Paulb386f1b2015-05-13 06:33:23 -0700440 continue;
Zach Reizner4a253652015-09-10 18:30:54 -0700441
Sean Paul6f82f1d2015-10-21 20:05:05 -0400442 if (sf_layer->compositionType == HWC_OVERLAY)
443 indices_to_composite.push_back(j);
444 if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET)
445 framebuffer_target_index = j;
Zach Reizner4a253652015-09-10 18:30:54 -0700446
447 layer.acquire_fence.Set(sf_layer->acquireFenceFd);
448 sf_layer->acquireFenceFd = -1;
449
450 std::ostringstream layer_fence_formatter;
451 layer_fence_formatter << "release fence for layer " << j << " of display "
452 << i;
453 std::string layer_fence_description(layer_fence_formatter.str());
454 checked_output_fences.emplace_back(&sf_layer->releaseFenceFd,
455 layer_fence_description.c_str(),
456 ctx->dummy_timeline);
457 layer.release_fence = OutputFd(&sf_layer->releaseFenceFd);
Zach Reizner1946fa72015-08-14 11:14:38 -0700458 }
Zach Reizner4a253652015-09-10 18:30:54 -0700459
Sean Paul6f82f1d2015-10-21 20:05:05 -0400460 if (indices_to_composite.empty() && framebuffer_target_index >= 0) {
461 hwc_layer_1_t *sf_layer = &dc->hwLayers[framebuffer_target_index];
462 if (!sf_layer->handle || (sf_layer->flags & HWC_SKIP_LAYER)) {
463 ALOGE(
464 "Expected valid layer with HWC_FRAMEBUFFER_TARGET when all "
465 "HWC_OVERLAY layers are skipped.");
Zach Reizner4a253652015-09-10 18:30:54 -0700466 ret = -EINVAL;
Zach Reizner1946fa72015-08-14 11:14:38 -0700467 }
Sean Paul6f82f1d2015-10-21 20:05:05 -0400468 indices_to_composite.push_back(framebuffer_target_index);
Zach Reizner45624d32015-06-10 16:03:01 -0700469 }
Zach Reizner4a253652015-09-10 18:30:54 -0700470 }
Zach Reizner45624d32015-06-10 16:03:01 -0700471
Zach Reizner4a253652015-09-10 18:30:54 -0700472 if (ret)
473 return ret;
474
475 for (size_t i = 0; i < num_displays; ++i) {
476 hwc_display_contents_1_t *dc = sf_display_contents[i];
477 DrmHwcDisplayContents &display_contents = displays_contents[i];
Haixia Shi2fddd372015-10-15 16:21:48 -0700478 if (!sf_display_contents[i] || i == HWC_DISPLAY_VIRTUAL)
Zach Reizner4a253652015-09-10 18:30:54 -0700479 continue;
480
481 layers_map.emplace_back();
482 DrmCompositionDisplayLayersMap &map = layers_map.back();
Zach Reizneracba14b2015-10-13 18:19:26 -0700483 map.display = i;
Zach Reizner5757e822015-10-16 19:06:31 -0700484 map.geometry_changed =
485 (dc->flags & HWC_GEOMETRY_CHANGED) == HWC_GEOMETRY_CHANGED;
Zach Reizner4a253652015-09-10 18:30:54 -0700486 std::vector<size_t> &indices_to_composite = layers_indices[i];
487 for (size_t j : indices_to_composite) {
488 hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
489
490 DrmHwcLayer &layer = display_contents.layers[j];
491
Zach Reizner7e88be92015-10-12 15:20:33 -0700492 ret = layer.InitFromHwcLayer(sf_layer, ctx->importer, ctx->gralloc);
493 if (ret) {
494 ALOGE("Failed to init composition from layer %d", ret);
495 return ret;
496 }
Zach Reizner4a253652015-09-10 18:30:54 -0700497 map.layers.emplace_back(std::move(layer));
498 }
499 }
500
501 std::unique_ptr<DrmComposition> composition(
502 ctx->drm.compositor()->CreateComposition(ctx->importer));
503 if (!composition) {
504 ALOGE("Drm composition init failed");
505 return -EINVAL;
Zach Reizner09807052015-08-13 14:53:41 -0700506 }
Zach Reizner45624d32015-06-10 16:03:01 -0700507
Zach Reizner09807052015-08-13 14:53:41 -0700508 ret = composition->SetLayers(layers_map.size(), layers_map.data());
509 if (ret) {
Zach Reizner09807052015-08-13 14:53:41 -0700510 return -EINVAL;
511 }
Zach Reizner45624d32015-06-10 16:03:01 -0700512
Zach Reizner09807052015-08-13 14:53:41 -0700513 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
514 if (ret) {
Zach Reizner09807052015-08-13 14:53:41 -0700515 return -EINVAL;
516 }
517
Zach Reizner566da2b2015-10-06 15:39:09 -0700518 for (size_t i = 0; i < num_displays; ++i) {
519 hwc_display_contents_1_t *dc = sf_display_contents[i];
520 if (!dc)
521 continue;
522
523 size_t num_dc_layers = dc->numHwLayers;
524 for (size_t j = 0; j < num_dc_layers; ++j) {
525 hwc_layer_1_t *layer = &dc->hwLayers[j];
526 if (layer->flags & HWC_SKIP_LAYER)
527 continue;
528 hwc_add_layer_to_retire_fence(layer, dc);
529 }
530 }
531
Zach Reizner09807052015-08-13 14:53:41 -0700532 composition.reset(NULL);
533
Sean Paulef8f1f92015-04-29 16:05:23 -0400534 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500535}
536
Sean Paulef8f1f92015-04-29 16:05:23 -0400537static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
538 int event, int enabled) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400539 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
540 return -EINVAL;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500541
Sean Paul4057be32015-05-13 06:23:09 -0700542 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
543 hwc_drm_display_t *hd = &ctx->displays[display];
544 return hd->vsync_worker.VSyncControl(enabled);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500545}
546
Sean Paulef8f1f92015-04-29 16:05:23 -0400547static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
548 int mode) {
549 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500550
Sean Paul6a55e9f2015-04-30 15:31:06 -0400551 uint64_t dpmsValue = 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400552 switch (mode) {
553 case HWC_POWER_MODE_OFF:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400554 dpmsValue = DRM_MODE_DPMS_OFF;
Sean Paulef8f1f92015-04-29 16:05:23 -0400555 break;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500556
Sean Paulef8f1f92015-04-29 16:05:23 -0400557 /* We can't support dozing right now, so go full on */
558 case HWC_POWER_MODE_DOZE:
559 case HWC_POWER_MODE_DOZE_SUSPEND:
560 case HWC_POWER_MODE_NORMAL:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400561 dpmsValue = DRM_MODE_DPMS_ON;
Sean Paulef8f1f92015-04-29 16:05:23 -0400562 break;
563 };
Sean Paul6a55e9f2015-04-30 15:31:06 -0400564 return ctx->drm.SetDpmsMode(display, dpmsValue);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500565}
566
Sean Paulef8f1f92015-04-29 16:05:23 -0400567static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
568 int *value) {
569 switch (what) {
570 case HWC_BACKGROUND_LAYER_SUPPORTED:
571 *value = 0; /* TODO: We should do this */
572 break;
573 case HWC_VSYNC_PERIOD:
574 ALOGW("Query for deprecated vsync value, returning 60Hz");
575 *value = 1000 * 1000 * 1000 / 60;
576 break;
577 case HWC_DISPLAY_TYPES_SUPPORTED:
Haixia Shi2fddd372015-10-15 16:21:48 -0700578 *value = HWC_DISPLAY_PRIMARY_BIT | HWC_DISPLAY_EXTERNAL_BIT |
579 HWC_DISPLAY_VIRTUAL_BIT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400580 break;
581 }
582 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500583}
584
Sean Paulef8f1f92015-04-29 16:05:23 -0400585static void hwc_register_procs(struct hwc_composer_device_1 *dev,
586 hwc_procs_t const *procs) {
587 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500588
Sean Paulef8f1f92015-04-29 16:05:23 -0400589 ctx->procs = procs;
Sean Paul4057be32015-05-13 06:23:09 -0700590
591 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
592 iter != ctx->displays.end(); ++iter) {
593 iter->second.vsync_worker.SetProcs(procs);
594 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500595}
596
Sean Paulef8f1f92015-04-29 16:05:23 -0400597static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
598 int display, uint32_t *configs,
Sean Paul6a55e9f2015-04-30 15:31:06 -0400599 size_t *num_configs) {
600 if (!*num_configs)
Sean Paulef8f1f92015-04-29 16:05:23 -0400601 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500602
Sean Paulef8f1f92015-04-29 16:05:23 -0400603 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700604 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400605 hd->config_ids.clear();
606
607 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
608 if (!connector) {
609 ALOGE("Failed to get connector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400610 return -ENODEV;
611 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500612
Sean Paule42febf2015-05-07 11:35:29 -0700613 int ret = connector->UpdateModes();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400614 if (ret) {
615 ALOGE("Failed to update display modes %d", ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400616 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400617 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500618
Sean Paul6a55e9f2015-04-30 15:31:06 -0400619 for (DrmConnector::ModeIter iter = connector->begin_modes();
620 iter != connector->end_modes(); ++iter) {
621 size_t idx = hd->config_ids.size();
622 if (idx == *num_configs)
623 break;
624 hd->config_ids.push_back(iter->id());
625 configs[idx] = iter->id();
626 }
627 *num_configs = hd->config_ids.size();
628 return *num_configs == 0 ? -1 : 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500629}
630
Sean Paulef8f1f92015-04-29 16:05:23 -0400631static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
632 int display, uint32_t config,
633 const uint32_t *attributes,
634 int32_t *values) {
635 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400636 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400637 if (!c) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400638 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400639 return -ENODEV;
640 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400641 DrmMode mode;
642 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
643 ++iter) {
644 if (iter->id() == config) {
645 mode = *iter;
646 break;
647 }
648 }
649 if (mode.id() == 0) {
650 ALOGE("Failed to find active mode for display %d", display);
651 return -ENOENT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400652 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500653
Sean Paul6a55e9f2015-04-30 15:31:06 -0400654 uint32_t mm_width = c->mm_width();
655 uint32_t mm_height = c->mm_height();
Sean Paulef8f1f92015-04-29 16:05:23 -0400656 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
657 switch (attributes[i]) {
658 case HWC_DISPLAY_VSYNC_PERIOD:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400659 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
Sean Paulef8f1f92015-04-29 16:05:23 -0400660 break;
661 case HWC_DISPLAY_WIDTH:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400662 values[i] = mode.h_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400663 break;
664 case HWC_DISPLAY_HEIGHT:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400665 values[i] = mode.v_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400666 break;
667 case HWC_DISPLAY_DPI_X:
668 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400669 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400670 break;
671 case HWC_DISPLAY_DPI_Y:
672 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400673 values[i] =
674 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400675 break;
676 }
677 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400678 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500679}
680
Sean Paulef8f1f92015-04-29 16:05:23 -0400681static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
682 int display) {
683 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400684 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
685 if (!c) {
686 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400687 return -ENODEV;
688 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500689
Sean Paul6a55e9f2015-04-30 15:31:06 -0400690 DrmMode mode = c->active_mode();
Sean Paule42febf2015-05-07 11:35:29 -0700691 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400692 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
693 if (hd->config_ids[i] == mode.id())
694 return i;
Sean Paulef8f1f92015-04-29 16:05:23 -0400695 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400696 return -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500697}
698
Sean Paulef8f1f92015-04-29 16:05:23 -0400699static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
700 int index) {
701 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700702 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400703 if (index >= (int)hd->config_ids.size()) {
704 ALOGE("Invalid config index %d passed in", index);
705 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400706 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500707
Sean Paul877be972015-06-03 14:08:27 -0400708 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
709 if (!c) {
710 ALOGE("Failed to get connector for display %d", display);
711 return -ENODEV;
712 }
713 DrmMode mode;
714 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
715 ++iter) {
716 if (iter->id() == hd->config_ids[index]) {
717 mode = *iter;
718 break;
719 }
720 }
721 if (mode.id() != hd->config_ids[index]) {
722 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
723 return -ENOENT;
724 }
725 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400726 if (ret) {
Sean Paul877be972015-06-03 14:08:27 -0400727 ALOGE("Failed to set active config %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400728 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400729 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400730 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500731}
732
Sean Paulef8f1f92015-04-29 16:05:23 -0400733static int hwc_device_close(struct hw_device_t *dev) {
734 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulef8f1f92015-04-29 16:05:23 -0400735 delete ctx;
Sean Paulef8f1f92015-04-29 16:05:23 -0400736 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500737}
738
Sean Paul24a26e32015-02-04 10:34:47 -0800739/*
740 * TODO: This function sets the active config to the first one in the list. This
741 * should be fixed such that it selects the preferred mode for the display, or
742 * some other, saner, method of choosing the config.
743 */
Sean Paule42febf2015-05-07 11:35:29 -0700744static int hwc_set_initial_config(hwc_drm_display_t *hd) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400745 uint32_t config;
746 size_t num_configs = 1;
747 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
748 &num_configs);
749 if (ret || !num_configs)
750 return 0;
Sean Paul24a26e32015-02-04 10:34:47 -0800751
Sean Paulef8f1f92015-04-29 16:05:23 -0400752 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
753 if (ret) {
754 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
755 return ret;
756 }
Sean Paul24a26e32015-02-04 10:34:47 -0800757
Sean Paulef8f1f92015-04-29 16:05:23 -0400758 return ret;
Sean Paul24a26e32015-02-04 10:34:47 -0800759}
760
Sean Paul6a55e9f2015-04-30 15:31:06 -0400761static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
Sean Paule42febf2015-05-07 11:35:29 -0700762 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paulef8f1f92015-04-29 16:05:23 -0400763 hd->ctx = ctx;
764 hd->display = display;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500765
Sean Paulb386f1b2015-05-13 06:33:23 -0700766 int ret = hwc_set_initial_config(hd);
Sean Paulef8f1f92015-04-29 16:05:23 -0400767 if (ret) {
768 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400769 return ret;
770 }
Sean Paul24a26e32015-02-04 10:34:47 -0800771
Sean Paul4057be32015-05-13 06:23:09 -0700772 ret = hd->vsync_worker.Init(&ctx->drm, display);
773 if (ret) {
774 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
775 return ret;
776 }
777
Sean Paulef8f1f92015-04-29 16:05:23 -0400778 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500779}
780
Sean Paulef8f1f92015-04-29 16:05:23 -0400781static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400782 int ret;
783 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
784 c != ctx->drm.end_connectors(); ++c) {
785 ret = hwc_initialize_display(ctx, (*c)->display());
786 if (ret) {
787 ALOGE("Failed to initialize display %d", (*c)->display());
788 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400789 }
790 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400791
Haixia Shid21f5282015-10-05 14:35:09 -0700792 ret = ctx->virtual_compositor_worker.Init();
793 if (ret) {
794 ALOGE("Failed to initialize virtual compositor worker");
795 return ret;
796 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400797 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500798}
799
Sean Paulef8f1f92015-04-29 16:05:23 -0400800static int hwc_device_open(const struct hw_module_t *module, const char *name,
801 struct hw_device_t **dev) {
802 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
803 ALOGE("Invalid module name- %s", name);
804 return -EINVAL;
805 }
806
807 struct hwc_context_t *ctx = new hwc_context_t();
808 if (!ctx) {
809 ALOGE("Failed to allocate hwc context");
810 return -ENOMEM;
811 }
812
Sean Paul6a55e9f2015-04-30 15:31:06 -0400813 int ret = ctx->drm.Init();
814 if (ret) {
815 ALOGE("Can't initialize Drm object %d", ret);
816 delete ctx;
817 return ret;
818 }
819
Zach Reizner4a253652015-09-10 18:30:54 -0700820 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
821 (const hw_module_t **)&ctx->gralloc);
822 if (ret) {
823 ALOGE("Failed to open gralloc module %d", ret);
824 delete ctx;
825 return ret;
826 }
827
828 ret = ctx->dummy_timeline.Init();
829 if (ret) {
830 ALOGE("Failed to create dummy sw sync timeline %d", ret);
831 return ret;
832 }
833
Sean Paulda6270d2015-06-01 14:11:52 -0400834 ctx->importer = Importer::CreateInstance(&ctx->drm);
835 if (!ctx->importer) {
836 ALOGE("Failed to create importer instance");
Sean Paulef8f1f92015-04-29 16:05:23 -0400837 delete ctx;
838 return ret;
839 }
840
Sean Paulef8f1f92015-04-29 16:05:23 -0400841 ret = hwc_enumerate_displays(ctx);
842 if (ret) {
843 ALOGE("Failed to enumerate displays: %s", strerror(ret));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400844 delete ctx;
845 return ret;
846 }
847
Sean Paulef8f1f92015-04-29 16:05:23 -0400848 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
849 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
850 ctx->device.common.module = const_cast<hw_module_t *>(module);
851 ctx->device.common.close = hwc_device_close;
852
Sean Paul9046c642015-06-10 17:27:47 -0400853 ctx->device.dump = hwc_dump;
Sean Paulef8f1f92015-04-29 16:05:23 -0400854 ctx->device.prepare = hwc_prepare;
855 ctx->device.set = hwc_set;
856 ctx->device.eventControl = hwc_event_control;
857 ctx->device.setPowerMode = hwc_set_power_mode;
858 ctx->device.query = hwc_query;
859 ctx->device.registerProcs = hwc_register_procs;
860 ctx->device.getDisplayConfigs = hwc_get_display_configs;
861 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
862 ctx->device.getActiveConfig = hwc_get_active_config;
863 ctx->device.setActiveConfig = hwc_set_active_config;
864 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
865
866 *dev = &ctx->device.common;
867
868 return 0;
869}
Sean Paul6a55e9f2015-04-30 15:31:06 -0400870}
Sean Paulef8f1f92015-04-29 16:05:23 -0400871
Sean Paul6a55e9f2015-04-30 15:31:06 -0400872static struct hw_module_methods_t hwc_module_methods = {
873 open : android::hwc_device_open
874};
Sean Paule0c4c3d2015-01-20 16:56:04 -0500875
876hwc_module_t HAL_MODULE_INFO_SYM = {
Sean Paulef8f1f92015-04-29 16:05:23 -0400877 common : {
878 tag : HARDWARE_MODULE_TAG,
879 version_major : 1,
880 version_minor : 0,
881 id : HWC_HARDWARE_MODULE_ID,
882 name : "DRM hwcomposer module",
883 author : "The Android Open Source Project",
884 methods : &hwc_module_methods,
885 dso : NULL,
886 reserved : {0},
887 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500888};