blob: 76fe1e7915944875afe01cc6920d5d997fef9cd1 [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 Paul54589d22018-08-24 16:58:35 -040074 memset(bo, 0, sizeof(hwc_drm_bo_t));
75
Sean Paulf72cccd2018-08-27 13:59:08 -040076 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
77 handle);
John Stultz499db602018-03-13 16:51:12 -070078 if (!hnd)
79 return -EINVAL;
80
Alexey Firagoc385afe2018-11-27 14:17:55 +030081 // We can't import these types of buffers.
82 // These buffers should have been filtered out with CanImportBuffer()
Sean Paul54589d22018-08-24 16:58:35 -040083 if (!(hnd->usage & GRALLOC_USAGE_HW_FB))
Alexey Firagoc385afe2018-11-27 14:17:55 +030084 return -EINVAL;
Sean Paul54589d22018-08-24 16:58:35 -040085
John Stultz499db602018-03-13 16:51:12 -070086 uint32_t gem_handle;
87 int ret = drmPrimeFDToHandle(drm_->fd(), hnd->share_fd, &gem_handle);
88 if (ret) {
89 ALOGE("failed to import prime fd %d ret=%d", hnd->share_fd, ret);
90 return ret;
91 }
92
Rob Herringaf0d9752018-05-04 16:34:19 -050093 int32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
John Stultz499db602018-03-13 16:51:12 -070094 if (fmt < 0)
John Stultzdf35a812018-05-31 17:37:21 -070095 return fmt;
John Stultz499db602018-03-13 16:51:12 -070096
John Stultz499db602018-03-13 16:51:12 -070097 bo->width = hnd->width;
98 bo->height = hnd->height;
John Stultz616fb222018-08-24 16:08:57 -070099 bo->hal_format = hnd->req_format;
John Stultz499db602018-03-13 16:51:12 -0700100 bo->format = fmt;
101 bo->usage = hnd->usage;
John Stultza4514832018-08-24 16:27:36 -0700102 bo->pixel_stride = hnd->stride;
John Stultz499db602018-03-13 16:51:12 -0700103 bo->pitches[0] = hnd->byte_stride;
104 bo->gem_handles[0] = gem_handle;
105 bo->offsets[0] = 0;
106
John Stultzdf35a812018-05-31 17:37:21 -0700107 switch (fmt) {
108 case DRM_FORMAT_YVU420: {
109 int align = 128;
110 if (hnd->usage &
111 (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
112 align = 16;
113 int adjusted_height = MALI_ALIGN(hnd->height, 2);
114 int y_size = adjusted_height * hnd->byte_stride;
115 int vu_stride = MALI_ALIGN(hnd->byte_stride / 2, align);
116 int v_size = vu_stride * (adjusted_height / 2);
117
118 /* V plane*/
119 bo->gem_handles[1] = gem_handle;
120 bo->pitches[1] = vu_stride;
121 bo->offsets[1] = y_size;
122 /* U plane */
123 bo->gem_handles[2] = gem_handle;
124 bo->pitches[2] = vu_stride;
125 bo->offsets[2] = y_size + v_size;
126 break;
127 }
128 default:
129 break;
130 }
131
John Stultz499db602018-03-13 16:51:12 -0700132 ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
133 bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id, 0);
134 if (ret) {
135 ALOGE("could not create drm fb %d", ret);
136 return ret;
137 }
138
139 return ret;
140}
141
Alexey Firago18ec6882018-11-21 23:47:05 +0300142bool HisiImporter::CanImportBuffer(buffer_handle_t handle) {
143 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
144 handle);
145 return hnd && (hnd->usage & GRALLOC_USAGE_HW_FB);
146}
147
Sean Paul54589d22018-08-24 16:58:35 -0400148class PlanStageHiSi : public Planner::PlanStage {
149 public:
150 int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition,
151 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
152 std::vector<DrmPlane *> *planes) {
John Stultz60f5e322018-08-27 22:24:40 -0700153 int layers_added = 0;
Alexey Firagoc385afe2018-11-27 14:17:55 +0300154 // Fill up as many DRM planes as we can with buffers that have HW_FB usage.
155 // Buffers without HW_FB should have been filtered out with
156 // CanImportBuffer(), if we meet one here, just skip it.
Sean Paul54589d22018-08-24 16:58:35 -0400157 for (auto i = layers.begin(); i != layers.end(); i = layers.erase(i)) {
158 if (!(i->second->gralloc_buffer_usage & GRALLOC_USAGE_HW_FB))
159 continue;
160
161 int ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer,
Lowry Li9b6cafd2018-08-28 17:58:21 +0800162 crtc, std::make_pair(i->first, i->second));
John Stultz60f5e322018-08-27 22:24:40 -0700163 layers_added++;
Sean Paul54589d22018-08-24 16:58:35 -0400164 // We don't have any planes left
165 if (ret == -ENOENT)
166 break;
Alexandru Gheorghe2234d372018-10-09 16:25:28 +0100167 else if (ret) {
Sean Paul54589d22018-08-24 16:58:35 -0400168 ALOGE("Failed to emplace layer %zu, dropping it", i->first);
Alexandru Gheorghe2234d372018-10-09 16:25:28 +0100169 return ret;
170 }
Sean Paul54589d22018-08-24 16:58:35 -0400171 }
Alexey Firagoc385afe2018-11-27 14:17:55 +0300172 // If we didn't emplace anything, return an error to ensure we force client
173 // compositing.
174 if (!layers_added)
John Stultz60f5e322018-08-27 22:24:40 -0700175 return -EINVAL;
Sean Paul54589d22018-08-24 16:58:35 -0400176
177 return 0;
178 }
179};
180
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100181std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
John Stultz499db602018-03-13 16:51:12 -0700182 std::unique_ptr<Planner> planner(new Planner);
Sean Paul54589d22018-08-24 16:58:35 -0400183 planner->AddStage<PlanStageHiSi>();
John Stultz499db602018-03-13 16:51:12 -0700184 return planner;
185}
Sean Paulf72cccd2018-08-27 13:59:08 -0400186} // namespace android