blob: e022010c199e9b7bf4366f389293f321e948769c [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
Victor Chonga9ed46a2019-04-11 06:23:28 +010073#if defined(MALI_GRALLOC_INTFMT_AFBC_BASIC) && \
74 defined(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16)
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +000075uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t flags,
76 bool is_rgb) {
77 uint64_t features = 0UL;
78
79 if (flags & MALI_GRALLOC_INTFMT_AFBC_BASIC)
80 features |= AFBC_FORMAT_MOD_BLOCK_SIZE_16x16;
81
82 if (flags & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK)
83 features |= (AFBC_FORMAT_MOD_SPLIT | AFBC_FORMAT_MOD_SPARSE);
84
85 if (flags & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
86 features |= AFBC_FORMAT_MOD_BLOCK_SIZE_32x8;
87
88 if (flags & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
89 features |= AFBC_FORMAT_MOD_TILED;
90
91 if (features) {
92 if (is_rgb)
93 features |= AFBC_FORMAT_MOD_YTR;
94
95 return DRM_FORMAT_MOD_ARM_AFBC(features);
96 }
97
98 return 0;
99}
John Stultz65c988d2019-02-27 23:34:53 -0800100#else
101uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t /* flags */,
102 bool /* is_rgb */) {
103 return 0;
104}
105#endif
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +0000106
107bool HisiImporter::IsDrmFormatRgb(uint32_t drm_format) {
108 switch (drm_format) {
109 case DRM_FORMAT_ARGB8888:
110 case DRM_FORMAT_XBGR8888:
111 case DRM_FORMAT_ABGR8888:
112 case DRM_FORMAT_BGR888:
113 case DRM_FORMAT_BGR565:
114 return true;
115 case DRM_FORMAT_YVU420:
116 return false;
117 default:
118 ALOGE("Unsupported format %u assuming rgb?", drm_format);
119 return true;
120 }
121}
122
John Stultz499db602018-03-13 16:51:12 -0700123int HisiImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +0000124 bool is_rgb;
125 uint64_t modifiers[4] = {0};
126
Sean Paul54589d22018-08-24 16:58:35 -0400127 memset(bo, 0, sizeof(hwc_drm_bo_t));
128
Sean Paulf72cccd2018-08-27 13:59:08 -0400129 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
130 handle);
John Stultz499db602018-03-13 16:51:12 -0700131 if (!hnd)
132 return -EINVAL;
133
Alexey Firagoc385afe2018-11-27 14:17:55 +0300134 // We can't import these types of buffers.
135 // These buffers should have been filtered out with CanImportBuffer()
Sean Paul54589d22018-08-24 16:58:35 -0400136 if (!(hnd->usage & GRALLOC_USAGE_HW_FB))
Alexey Firagoc385afe2018-11-27 14:17:55 +0300137 return -EINVAL;
Sean Paul54589d22018-08-24 16:58:35 -0400138
John Stultz499db602018-03-13 16:51:12 -0700139 uint32_t gem_handle;
140 int ret = drmPrimeFDToHandle(drm_->fd(), hnd->share_fd, &gem_handle);
141 if (ret) {
142 ALOGE("failed to import prime fd %d ret=%d", hnd->share_fd, ret);
143 return ret;
144 }
145
Rob Herringaf0d9752018-05-04 16:34:19 -0500146 int32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
John Stultz499db602018-03-13 16:51:12 -0700147 if (fmt < 0)
John Stultzdf35a812018-05-31 17:37:21 -0700148 return fmt;
John Stultz499db602018-03-13 16:51:12 -0700149
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +0000150 is_rgb = IsDrmFormatRgb(fmt);
151 modifiers[0] = ConvertGrallocFormatToDrmModifiers(hnd->internal_format,
152 is_rgb);
153
John Stultz499db602018-03-13 16:51:12 -0700154 bo->width = hnd->width;
155 bo->height = hnd->height;
John Stultz616fb222018-08-24 16:08:57 -0700156 bo->hal_format = hnd->req_format;
John Stultz499db602018-03-13 16:51:12 -0700157 bo->format = fmt;
158 bo->usage = hnd->usage;
John Stultza4514832018-08-24 16:27:36 -0700159 bo->pixel_stride = hnd->stride;
John Stultz499db602018-03-13 16:51:12 -0700160 bo->pitches[0] = hnd->byte_stride;
161 bo->gem_handles[0] = gem_handle;
162 bo->offsets[0] = 0;
163
John Stultzdf35a812018-05-31 17:37:21 -0700164 switch (fmt) {
165 case DRM_FORMAT_YVU420: {
166 int align = 128;
167 if (hnd->usage &
168 (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
169 align = 16;
170 int adjusted_height = MALI_ALIGN(hnd->height, 2);
171 int y_size = adjusted_height * hnd->byte_stride;
172 int vu_stride = MALI_ALIGN(hnd->byte_stride / 2, align);
173 int v_size = vu_stride * (adjusted_height / 2);
174
175 /* V plane*/
176 bo->gem_handles[1] = gem_handle;
177 bo->pitches[1] = vu_stride;
178 bo->offsets[1] = y_size;
179 /* U plane */
180 bo->gem_handles[2] = gem_handle;
181 bo->pitches[2] = vu_stride;
182 bo->offsets[2] = y_size + v_size;
183 break;
184 }
185 default:
186 break;
187 }
188
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +0000189 ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
190 bo->format, bo->gem_handles, bo->pitches,
191 bo->offsets, modifiers, &bo->fb_id,
192 modifiers[0] ? DRM_MODE_FB_MODIFIERS : 0);
193
John Stultz499db602018-03-13 16:51:12 -0700194 if (ret) {
195 ALOGE("could not create drm fb %d", ret);
196 return ret;
197 }
198
199 return ret;
200}
201
Alexey Firago18ec6882018-11-21 23:47:05 +0300202bool HisiImporter::CanImportBuffer(buffer_handle_t handle) {
203 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
204 handle);
205 return hnd && (hnd->usage & GRALLOC_USAGE_HW_FB);
206}
207
Sean Paul54589d22018-08-24 16:58:35 -0400208class PlanStageHiSi : public Planner::PlanStage {
209 public:
210 int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition,
211 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
212 std::vector<DrmPlane *> *planes) {
John Stultz60f5e322018-08-27 22:24:40 -0700213 int layers_added = 0;
Alexey Firagoc385afe2018-11-27 14:17:55 +0300214 // Fill up as many DRM planes as we can with buffers that have HW_FB usage.
215 // Buffers without HW_FB should have been filtered out with
216 // CanImportBuffer(), if we meet one here, just skip it.
Sean Paul54589d22018-08-24 16:58:35 -0400217 for (auto i = layers.begin(); i != layers.end(); i = layers.erase(i)) {
218 if (!(i->second->gralloc_buffer_usage & GRALLOC_USAGE_HW_FB))
219 continue;
220
221 int ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer,
Lowry Li9b6cafd2018-08-28 17:58:21 +0800222 crtc, std::make_pair(i->first, i->second));
John Stultz60f5e322018-08-27 22:24:40 -0700223 layers_added++;
Sean Paul54589d22018-08-24 16:58:35 -0400224 // We don't have any planes left
225 if (ret == -ENOENT)
226 break;
Alexandru Gheorghe2234d372018-10-09 16:25:28 +0100227 else if (ret) {
Sean Paul54589d22018-08-24 16:58:35 -0400228 ALOGE("Failed to emplace layer %zu, dropping it", i->first);
Alexandru Gheorghe2234d372018-10-09 16:25:28 +0100229 return ret;
230 }
Sean Paul54589d22018-08-24 16:58:35 -0400231 }
Alexey Firagoc385afe2018-11-27 14:17:55 +0300232 // If we didn't emplace anything, return an error to ensure we force client
233 // compositing.
234 if (!layers_added)
John Stultz60f5e322018-08-27 22:24:40 -0700235 return -EINVAL;
Sean Paul54589d22018-08-24 16:58:35 -0400236
237 return 0;
238 }
239};
240
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100241std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
John Stultz499db602018-03-13 16:51:12 -0700242 std::unique_ptr<Planner> planner(new Planner);
Sean Paul54589d22018-08-24 16:58:35 -0400243 planner->AddStage<PlanStageHiSi>();
John Stultz499db602018-03-13 16:51:12 -0700244 return planner;
245}
Sean Paulf72cccd2018-08-27 13:59:08 -0400246} // namespace android