1 /*
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
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 #define LOG_TAG "hwcomposer-drm"
19
20 #include "drm_hwcomposer.h"
21 #include "drmresources.h"
22 #include "importer.h"
23 #include "virtualcompositorworker.h"
24 #include "vsyncworker.h"
25
26 #include <stdlib.h>
27
28 #include <map>
29 #include <vector>
30 #include <sstream>
31
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <pthread.h>
35 #include <sys/param.h>
36 #include <sys/resource.h>
37 #include <xf86drm.h>
38 #include <xf86drmMode.h>
39
40 #include <cutils/log.h>
41 #include <cutils/properties.h>
42 #include <hardware/hardware.h>
43 #include <hardware/hwcomposer.h>
44 #include <sw_sync.h>
45 #include <sync/sync.h>
46 #include <utils/Trace.h>
47
48 #define UM_PER_INCH 25400
49
50 namespace android {
51
52 class DummySwSyncTimeline {
53 public:
Init()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
CreateDummyFence()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
86 struct CheckedOutputFd {
CheckedOutputFdandroid::CheckedOutputFd87 CheckedOutputFd(int *fd, const char *description,
88 DummySwSyncTimeline &timeline)
89 : fd_(fd), description_(description), timeline_(timeline) {
90 }
CheckedOutputFdandroid::CheckedOutputFd91 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
~CheckedOutputFdandroid::CheckedOutputFd98 ~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
118 typedef struct hwc_drm_display {
119 struct hwc_context_t *ctx;
120 int display;
121
122 std::vector<uint32_t> config_ids;
123
124 VSyncWorker vsync_worker;
125 } hwc_drm_display_t;
126
127 struct hwc_context_t {
128 // map of display:hwc_drm_display_t
129 typedef std::map<int, hwc_drm_display_t> DisplayMap;
130 typedef DisplayMap::iterator DisplayMapIter;
131
hwc_context_tandroid::hwc_context_t132 hwc_context_t() : procs(NULL), importer(NULL) {
133 }
134
~hwc_context_tandroid::hwc_context_t135 ~hwc_context_t() {
136 virtual_compositor_worker.Exit();
137 delete importer;
138 }
139
140 hwc_composer_device_1_t device;
141 hwc_procs_t const *procs;
142
143 DisplayMap displays;
144 DrmResources drm;
145 Importer *importer;
146 const gralloc_module_t *gralloc;
147 DummySwSyncTimeline dummy_timeline;
148 VirtualCompositorWorker virtual_compositor_worker;
149 };
150
dup_buffer_handle(buffer_handle_t handle)151 static 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
free_buffer_handle(native_handle_t * handle)169 static 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
operator =(OutputFd && rhs)178 OutputFd &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
operator ->() const192 const hwc_drm_bo *DrmHwcBuffer::operator->() const {
193 if (importer_ == NULL) {
194 ALOGE("Access of non-existent BO");
195 exit(1);
196 return NULL;
197 }
198 return &bo_;
199 }
200
Clear()201 void DrmHwcBuffer::Clear() {
202 if (importer_ != NULL) {
203 importer_->ReleaseBuffer(&bo_);
204 importer_ = NULL;
205 }
206 }
207
ImportBuffer(buffer_handle_t handle,Importer * importer)208 int 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
CopyBufferHandle(buffer_handle_t handle,const gralloc_module_t * gralloc)226 int 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
~DrmHwcNativeHandle()249 DrmHwcNativeHandle::~DrmHwcNativeHandle() {
250 Clear();
251 }
252
Clear()253 void 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
InitFromHwcLayer(hwc_layer_1_t * sf_layer,Importer * importer,const gralloc_module_t * gralloc)262 int DrmHwcLayer::InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer,
263 const gralloc_module_t *gralloc) {
264 sf_handle = sf_layer->handle;
265 alpha = sf_layer->planeAlpha;
266
267 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
274 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
313 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;
320
321 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
333 return 0;
334 }
335
hwc_dump(struct hwc_composer_device_1 * dev,char * buff,int buff_len)336 static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
337 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();
343 strncpy(buff, out_str.c_str(),
344 std::min((size_t)buff_len, out_str.length() + 1));
345 buff[buff_len - 1] = '\0';
346 }
347
hwc_prepare(hwc_composer_device_1_t * dev,size_t num_displays,hwc_display_contents_1_t ** display_contents)348 static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
349 hwc_display_contents_1_t **display_contents) {
350 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
351
352 for (int i = 0; i < (int)num_displays; ++i) {
353 if (!display_contents[i])
354 continue;
355
356 bool use_framebuffer_target = false;
357 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 }
365 }
366
367 int num_layers = display_contents[i]->numHwLayers;
368 for (int j = 0; j < num_layers; j++) {
369 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
370
371 if (!use_framebuffer_target) {
372 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 }
384 }
385 }
386
387 return 0;
388 }
389
hwc_add_layer_to_retire_fence(hwc_layer_1_t * layer,hwc_display_contents_1_t * display_contents)390 static void hwc_add_layer_to_retire_fence(
391 hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) {
392 if (layer->releaseFenceFd < 0)
393 return;
394
395 if (display_contents->retireFenceFd >= 0) {
396 int old_retire_fence = display_contents->retireFenceFd;
397 display_contents->retireFenceFd =
398 sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd);
399 close(old_retire_fence);
400 } else {
401 display_contents->retireFenceFd = dup(layer->releaseFenceFd);
402 }
403 }
404
hwc_set(hwc_composer_device_1_t * dev,size_t num_displays,hwc_display_contents_1_t ** sf_display_contents)405 static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
406 hwc_display_contents_1_t **sf_display_contents) {
407 ATRACE_CALL();
408 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
409 int ret = 0;
410
411 std::vector<CheckedOutputFd> checked_output_fences;
412 std::vector<DrmHwcDisplayContents> displays_contents;
413 std::vector<DrmCompositionDisplayLayersMap> layers_map;
414 std::vector<std::vector<size_t>> layers_indices;
415 displays_contents.reserve(num_displays);
416 // layers_map.reserve(num_displays);
417 layers_indices.reserve(num_displays);
418
419 // 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();
424 layers_indices.emplace_back();
425 std::vector<size_t> &indices_to_composite = layers_indices.back();
426
427 if (!sf_display_contents[i])
428 continue;
429
430 if (i == HWC_DISPLAY_VIRTUAL) {
431 ctx->virtual_compositor_worker.QueueComposite(dc);
432 continue;
433 }
434
435 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);
442
443 size_t num_dc_layers = dc->numHwLayers;
444 int framebuffer_target_index = -1;
445 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)
452 continue;
453
454 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;
458
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);
470 }
471
472 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.");
478 ret = -EINVAL;
479 }
480 indices_to_composite.push_back(framebuffer_target_index);
481 }
482 }
483
484 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];
490 if (!sf_display_contents[i] || i == HWC_DISPLAY_VIRTUAL)
491 continue;
492
493 layers_map.emplace_back();
494 DrmCompositionDisplayLayersMap &map = layers_map.back();
495 map.display = i;
496 map.geometry_changed =
497 (dc->flags & HWC_GEOMETRY_CHANGED) == HWC_GEOMETRY_CHANGED;
498 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
504 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 }
509 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;
518 }
519
520 ret = composition->SetLayers(layers_map.size(), layers_map.data());
521 if (ret) {
522 return -EINVAL;
523 }
524
525 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
526 if (ret) {
527 return -EINVAL;
528 }
529
530 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
544 composition.reset(NULL);
545
546 return ret;
547 }
548
hwc_event_control(struct hwc_composer_device_1 * dev,int display,int event,int enabled)549 static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
550 int event, int enabled) {
551 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
552 return -EINVAL;
553
554 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);
557 }
558
hwc_set_power_mode(struct hwc_composer_device_1 * dev,int display,int mode)559 static 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;
562
563 uint64_t dpmsValue = 0;
564 switch (mode) {
565 case HWC_POWER_MODE_OFF:
566 dpmsValue = DRM_MODE_DPMS_OFF;
567 break;
568
569 /* 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:
573 dpmsValue = DRM_MODE_DPMS_ON;
574 break;
575 };
576 return ctx->drm.SetDpmsMode(display, dpmsValue);
577 }
578
hwc_query(struct hwc_composer_device_1 *,int what,int * value)579 static 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:
590 *value = HWC_DISPLAY_PRIMARY_BIT | HWC_DISPLAY_EXTERNAL_BIT |
591 HWC_DISPLAY_VIRTUAL_BIT;
592 break;
593 }
594 return 0;
595 }
596
hwc_register_procs(struct hwc_composer_device_1 * dev,hwc_procs_t const * procs)597 static 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;
600
601 ctx->procs = procs;
602
603 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
604 iter != ctx->displays.end(); ++iter) {
605 iter->second.vsync_worker.SetProcs(procs);
606 }
607 }
608
hwc_get_display_configs(struct hwc_composer_device_1 * dev,int display,uint32_t * configs,size_t * num_configs)609 static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
610 int display, uint32_t *configs,
611 size_t *num_configs) {
612 if (!*num_configs)
613 return 0;
614
615 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
616 hwc_drm_display_t *hd = &ctx->displays[display];
617 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);
622 return -ENODEV;
623 }
624
625 int ret = connector->UpdateModes();
626 if (ret) {
627 ALOGE("Failed to update display modes %d", ret);
628 return ret;
629 }
630
631 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;
641 }
642
hwc_get_display_attributes(struct hwc_composer_device_1 * dev,int display,uint32_t config,const uint32_t * attributes,int32_t * values)643 static 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;
648 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
649 if (!c) {
650 ALOGE("Failed to get DrmConnector for display %d", display);
651 return -ENODEV;
652 }
653 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;
664 }
665
666 uint32_t mm_width = c->mm_width();
667 uint32_t mm_height = c->mm_height();
668 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
669 switch (attributes[i]) {
670 case HWC_DISPLAY_VSYNC_PERIOD:
671 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
672 break;
673 case HWC_DISPLAY_WIDTH:
674 values[i] = mode.h_display();
675 break;
676 case HWC_DISPLAY_HEIGHT:
677 values[i] = mode.v_display();
678 break;
679 case HWC_DISPLAY_DPI_X:
680 /* Dots per 1000 inches */
681 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
682 break;
683 case HWC_DISPLAY_DPI_Y:
684 /* Dots per 1000 inches */
685 values[i] =
686 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
687 break;
688 }
689 }
690 return 0;
691 }
692
hwc_get_active_config(struct hwc_composer_device_1 * dev,int display)693 static 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;
696 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
697 if (!c) {
698 ALOGE("Failed to get DrmConnector for display %d", display);
699 return -ENODEV;
700 }
701
702 DrmMode mode = c->active_mode();
703 hwc_drm_display_t *hd = &ctx->displays[display];
704 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
705 if (hd->config_ids[i] == mode.id())
706 return i;
707 }
708 return -1;
709 }
710
hwc_set_active_config(struct hwc_composer_device_1 * dev,int display,int index)711 static 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;
714 hwc_drm_display_t *hd = &ctx->displays[display];
715 if (index >= (int)hd->config_ids.size()) {
716 ALOGE("Invalid config index %d passed in", index);
717 return -EINVAL;
718 }
719
720 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);
738 if (ret) {
739 ALOGE("Failed to set active config %d", ret);
740 return ret;
741 }
742 return ret;
743 }
744
hwc_device_close(struct hw_device_t * dev)745 static int hwc_device_close(struct hw_device_t *dev) {
746 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
747 delete ctx;
748 return 0;
749 }
750
751 /*
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 */
hwc_set_initial_config(hwc_drm_display_t * hd)756 static int hwc_set_initial_config(hwc_drm_display_t *hd) {
757 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;
763
764 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 }
769
770 return ret;
771 }
772
hwc_initialize_display(struct hwc_context_t * ctx,int display)773 static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
774 hwc_drm_display_t *hd = &ctx->displays[display];
775 hd->ctx = ctx;
776 hd->display = display;
777
778 int ret = hwc_set_initial_config(hd);
779 if (ret) {
780 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
781 return ret;
782 }
783
784 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
790 return 0;
791 }
792
hwc_enumerate_displays(struct hwc_context_t * ctx)793 static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
794 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;
801 }
802 }
803
804 ret = ctx->virtual_compositor_worker.Init();
805 if (ret) {
806 ALOGE("Failed to initialize virtual compositor worker");
807 return ret;
808 }
809 return 0;
810 }
811
hwc_device_open(const struct hw_module_t * module,const char * name,struct hw_device_t ** dev)812 static 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
825 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
832 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
846 ctx->importer = Importer::CreateInstance(&ctx->drm);
847 if (!ctx->importer) {
848 ALOGE("Failed to create importer instance");
849 delete ctx;
850 return ret;
851 }
852
853 ret = hwc_enumerate_displays(ctx);
854 if (ret) {
855 ALOGE("Failed to enumerate displays: %s", strerror(ret));
856 delete ctx;
857 return ret;
858 }
859
860 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
865 ctx->device.dump = hwc_dump;
866 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 }
882 }
883
884 static struct hw_module_methods_t hwc_module_methods = {
885 open : android::hwc_device_open
886 };
887
888 hwc_module_t HAL_MODULE_INFO_SYM = {
889 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 }
900 };
901