blob: 5a1ac1b41314cee41d207d1c7ea881501138012e [file] [log] [blame]
John Stultz499db602018-03-13 16:51:12 -07001/*
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 LOG_TAG "hwc-platform-hisi"
18
Sean Paulf72cccd2018-08-27 13:59:08 -040019#include "platformhisi.h"
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010020#include "drmdevice.h"
John Stultz499db602018-03-13 16:51:12 -070021#include "platform.h"
John Stultz499db602018-03-13 16:51:12 -070022
23#include <drm/drm_fourcc.h>
John Stultz499db602018-03-13 16:51:12 -070024#include <stdatomic.h>
25#include <xf86drm.h>
26#include <xf86drmMode.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040027#include <cinttypes>
John Stultz499db602018-03-13 16:51:12 -070028
John Stultz499db602018-03-13 16:51:12 -070029#include <hardware/gralloc.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040030#include <log/log.h>
John Stultz499db602018-03-13 16:51:12 -070031#include "gralloc_priv.h"
32
John Stultzdf35a812018-05-31 17:37:21 -070033#define MALI_ALIGN(value, base) (((value) + ((base)-1)) & ~((base)-1))
John Stultz499db602018-03-13 16:51:12 -070034
35namespace android {
36
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010037Importer *Importer::CreateInstance(DrmDevice *drm) {
John Stultz499db602018-03-13 16:51:12 -070038 HisiImporter *importer = new HisiImporter(drm);
39 if (!importer)
40 return NULL;
41
42 int ret = importer->Init();
43 if (ret) {
44 ALOGE("Failed to initialize the hisi importer %d", ret);
45 delete importer;
46 return NULL;
47 }
48 return importer;
49}
50
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010051HisiImporter::HisiImporter(DrmDevice *drm)
52 : DrmGenericImporter(drm), drm_(drm) {
John Stultz499db602018-03-13 16:51:12 -070053}
54
55HisiImporter::~HisiImporter() {
56}
57
58int HisiImporter::Init() {
59 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
60 (const hw_module_t **)&gralloc_);
61 if (ret) {
62 ALOGE("Failed to open gralloc module %d", ret);
63 return ret;
64 }
65
66 if (strcasecmp(gralloc_->common.author, "ARM Ltd."))
67 ALOGW("Using non-ARM gralloc module: %s/%s\n", gralloc_->common.name,
68 gralloc_->common.author);
69
70 return 0;
71}
72
John Stultz499db602018-03-13 16:51:12 -070073int HisiImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
Sean Paulf72cccd2018-08-27 13:59:08 -040074 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
75 handle);
John Stultz499db602018-03-13 16:51:12 -070076 if (!hnd)
77 return -EINVAL;
78
79 uint32_t gem_handle;
80 int ret = drmPrimeFDToHandle(drm_->fd(), hnd->share_fd, &gem_handle);
81 if (ret) {
82 ALOGE("failed to import prime fd %d ret=%d", hnd->share_fd, ret);
83 return ret;
84 }
85
Rob Herringaf0d9752018-05-04 16:34:19 -050086 int32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
John Stultz499db602018-03-13 16:51:12 -070087 if (fmt < 0)
John Stultzdf35a812018-05-31 17:37:21 -070088 return fmt;
John Stultz499db602018-03-13 16:51:12 -070089
90 memset(bo, 0, sizeof(hwc_drm_bo_t));
91 bo->width = hnd->width;
92 bo->height = hnd->height;
93 bo->format = fmt;
94 bo->usage = hnd->usage;
John Stultzdf35a812018-05-31 17:37:21 -070095
John Stultz499db602018-03-13 16:51:12 -070096 bo->pitches[0] = hnd->byte_stride;
97 bo->gem_handles[0] = gem_handle;
98 bo->offsets[0] = 0;
99
John Stultzdf35a812018-05-31 17:37:21 -0700100 switch (fmt) {
101 case DRM_FORMAT_YVU420: {
102 int align = 128;
103 if (hnd->usage &
104 (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
105 align = 16;
106 int adjusted_height = MALI_ALIGN(hnd->height, 2);
107 int y_size = adjusted_height * hnd->byte_stride;
108 int vu_stride = MALI_ALIGN(hnd->byte_stride / 2, align);
109 int v_size = vu_stride * (adjusted_height / 2);
110
111 /* V plane*/
112 bo->gem_handles[1] = gem_handle;
113 bo->pitches[1] = vu_stride;
114 bo->offsets[1] = y_size;
115 /* U plane */
116 bo->gem_handles[2] = gem_handle;
117 bo->pitches[2] = vu_stride;
118 bo->offsets[2] = y_size + v_size;
119 break;
120 }
121 default:
122 break;
123 }
124
John Stultz499db602018-03-13 16:51:12 -0700125 ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
126 bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id, 0);
127 if (ret) {
128 ALOGE("could not create drm fb %d", ret);
129 return ret;
130 }
131
132 return ret;
133}
134
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100135std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
John Stultz499db602018-03-13 16:51:12 -0700136 std::unique_ptr<Planner> planner(new Planner);
137 planner->AddStage<PlanStageGreedy>();
138 return planner;
139}
Sean Paulf72cccd2018-08-27 13:59:08 -0400140} // namespace android