blob: 81d8039b4a8f7270930e134a44b8b22505f0fa3e [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
John Stultz499db602018-03-13 16:51:12 -070023#include <stdatomic.h>
24#include <xf86drm.h>
25#include <xf86drmMode.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040026#include <cinttypes>
John Stultz499db602018-03-13 16:51:12 -070027
John Stultz499db602018-03-13 16:51:12 -070028#include <hardware/gralloc.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040029#include <log/log.h>
John Stultz499db602018-03-13 16:51:12 -070030#include "gralloc_priv.h"
31
John Stultzdf35a812018-05-31 17:37:21 -070032#define MALI_ALIGN(value, base) (((value) + ((base)-1)) & ~((base)-1))
John Stultz499db602018-03-13 16:51:12 -070033
34namespace android {
35
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010036Importer *Importer::CreateInstance(DrmDevice *drm) {
John Stultz499db602018-03-13 16:51:12 -070037 HisiImporter *importer = new HisiImporter(drm);
38 if (!importer)
39 return NULL;
40
41 int ret = importer->Init();
42 if (ret) {
43 ALOGE("Failed to initialize the hisi importer %d", ret);
44 delete importer;
45 return NULL;
46 }
47 return importer;
48}
49
Victor Chonga9ed46a2019-04-11 06:23:28 +010050#if defined(MALI_GRALLOC_INTFMT_AFBC_BASIC) && \
51 defined(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16)
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +000052uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t flags,
53 bool is_rgb) {
54 uint64_t features = 0UL;
55
56 if (flags & MALI_GRALLOC_INTFMT_AFBC_BASIC)
57 features |= AFBC_FORMAT_MOD_BLOCK_SIZE_16x16;
58
59 if (flags & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK)
60 features |= (AFBC_FORMAT_MOD_SPLIT | AFBC_FORMAT_MOD_SPARSE);
61
62 if (flags & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
63 features |= AFBC_FORMAT_MOD_BLOCK_SIZE_32x8;
64
65 if (flags & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
66 features |= AFBC_FORMAT_MOD_TILED;
67
68 if (features) {
69 if (is_rgb)
70 features |= AFBC_FORMAT_MOD_YTR;
71
72 return DRM_FORMAT_MOD_ARM_AFBC(features);
73 }
74
75 return 0;
76}
John Stultz65c988d2019-02-27 23:34:53 -080077#else
78uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t /* flags */,
79 bool /* is_rgb */) {
80 return 0;
81}
82#endif
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +000083
84bool HisiImporter::IsDrmFormatRgb(uint32_t drm_format) {
85 switch (drm_format) {
86 case DRM_FORMAT_ARGB8888:
87 case DRM_FORMAT_XBGR8888:
88 case DRM_FORMAT_ABGR8888:
89 case DRM_FORMAT_BGR888:
90 case DRM_FORMAT_BGR565:
91 return true;
92 case DRM_FORMAT_YVU420:
93 return false;
94 default:
95 ALOGE("Unsupported format %u assuming rgb?", drm_format);
96 return true;
97 }
98}
99
John Stultz499db602018-03-13 16:51:12 -0700100int HisiImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +0000101 bool is_rgb;
102 uint64_t modifiers[4] = {0};
103
Sean Paul54589d22018-08-24 16:58:35 -0400104 memset(bo, 0, sizeof(hwc_drm_bo_t));
105
Sean Paulf72cccd2018-08-27 13:59:08 -0400106 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
107 handle);
John Stultz499db602018-03-13 16:51:12 -0700108 if (!hnd)
109 return -EINVAL;
110
Alexey Firagoc385afe2018-11-27 14:17:55 +0300111 // We can't import these types of buffers.
112 // These buffers should have been filtered out with CanImportBuffer()
Sean Paul54589d22018-08-24 16:58:35 -0400113 if (!(hnd->usage & GRALLOC_USAGE_HW_FB))
Alexey Firagoc385afe2018-11-27 14:17:55 +0300114 return -EINVAL;
Sean Paul54589d22018-08-24 16:58:35 -0400115
John Stultz499db602018-03-13 16:51:12 -0700116 uint32_t gem_handle;
117 int ret = drmPrimeFDToHandle(drm_->fd(), hnd->share_fd, &gem_handle);
118 if (ret) {
119 ALOGE("failed to import prime fd %d ret=%d", hnd->share_fd, ret);
120 return ret;
121 }
122
Roman Stratiienkof63726c2019-11-06 15:03:12 +0200123 uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
124 if (fmt == DRM_FORMAT_INVALID)
125 return -EINVAL;
John Stultz499db602018-03-13 16:51:12 -0700126
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +0000127 is_rgb = IsDrmFormatRgb(fmt);
128 modifiers[0] = ConvertGrallocFormatToDrmModifiers(hnd->internal_format,
129 is_rgb);
130
John Stultz499db602018-03-13 16:51:12 -0700131 bo->width = hnd->width;
132 bo->height = hnd->height;
John Stultz616fb222018-08-24 16:08:57 -0700133 bo->hal_format = hnd->req_format;
John Stultz499db602018-03-13 16:51:12 -0700134 bo->format = fmt;
135 bo->usage = hnd->usage;
John Stultza4514832018-08-24 16:27:36 -0700136 bo->pixel_stride = hnd->stride;
John Stultz499db602018-03-13 16:51:12 -0700137 bo->pitches[0] = hnd->byte_stride;
138 bo->gem_handles[0] = gem_handle;
139 bo->offsets[0] = 0;
140
John Stultzdf35a812018-05-31 17:37:21 -0700141 switch (fmt) {
142 case DRM_FORMAT_YVU420: {
143 int align = 128;
144 if (hnd->usage &
145 (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
146 align = 16;
147 int adjusted_height = MALI_ALIGN(hnd->height, 2);
148 int y_size = adjusted_height * hnd->byte_stride;
149 int vu_stride = MALI_ALIGN(hnd->byte_stride / 2, align);
150 int v_size = vu_stride * (adjusted_height / 2);
151
152 /* V plane*/
153 bo->gem_handles[1] = gem_handle;
154 bo->pitches[1] = vu_stride;
155 bo->offsets[1] = y_size;
156 /* U plane */
157 bo->gem_handles[2] = gem_handle;
158 bo->pitches[2] = vu_stride;
159 bo->offsets[2] = y_size + v_size;
160 break;
161 }
162 default:
163 break;
164 }
165
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +0000166 ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
167 bo->format, bo->gem_handles, bo->pitches,
168 bo->offsets, modifiers, &bo->fb_id,
169 modifiers[0] ? DRM_MODE_FB_MODIFIERS : 0);
170
John Stultz499db602018-03-13 16:51:12 -0700171 if (ret) {
172 ALOGE("could not create drm fb %d", ret);
173 return ret;
174 }
175
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100176 ImportHandle(gem_handle);
177
John Stultz499db602018-03-13 16:51:12 -0700178 return ret;
179}
180
Alexey Firago18ec6882018-11-21 23:47:05 +0300181bool HisiImporter::CanImportBuffer(buffer_handle_t handle) {
182 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
183 handle);
184 return hnd && (hnd->usage & GRALLOC_USAGE_HW_FB);
185}
186
Sean Paul54589d22018-08-24 16:58:35 -0400187class PlanStageHiSi : public Planner::PlanStage {
188 public:
189 int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition,
190 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
191 std::vector<DrmPlane *> *planes) {
John Stultz60f5e322018-08-27 22:24:40 -0700192 int layers_added = 0;
Alexey Firagoc385afe2018-11-27 14:17:55 +0300193 // Fill up as many DRM planes as we can with buffers that have HW_FB usage.
194 // Buffers without HW_FB should have been filtered out with
195 // CanImportBuffer(), if we meet one here, just skip it.
Sean Paul54589d22018-08-24 16:58:35 -0400196 for (auto i = layers.begin(); i != layers.end(); i = layers.erase(i)) {
197 if (!(i->second->gralloc_buffer_usage & GRALLOC_USAGE_HW_FB))
198 continue;
199
200 int ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer,
Lowry Li9b6cafd2018-08-28 17:58:21 +0800201 crtc, std::make_pair(i->first, i->second));
John Stultz60f5e322018-08-27 22:24:40 -0700202 layers_added++;
Sean Paul54589d22018-08-24 16:58:35 -0400203 // We don't have any planes left
204 if (ret == -ENOENT)
205 break;
Alexandru Gheorghe2234d372018-10-09 16:25:28 +0100206 else if (ret) {
Sean Paul54589d22018-08-24 16:58:35 -0400207 ALOGE("Failed to emplace layer %zu, dropping it", i->first);
Alexandru Gheorghe2234d372018-10-09 16:25:28 +0100208 return ret;
209 }
Sean Paul54589d22018-08-24 16:58:35 -0400210 }
Alexey Firagoc385afe2018-11-27 14:17:55 +0300211 // If we didn't emplace anything, return an error to ensure we force client
212 // compositing.
213 if (!layers_added)
John Stultz60f5e322018-08-27 22:24:40 -0700214 return -EINVAL;
Sean Paul54589d22018-08-24 16:58:35 -0400215
216 return 0;
217 }
218};
219
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100220std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
John Stultz499db602018-03-13 16:51:12 -0700221 std::unique_ptr<Planner> planner(new Planner);
Sean Paul54589d22018-08-24 16:58:35 -0400222 planner->AddStage<PlanStageHiSi>();
John Stultz499db602018-03-13 16:51:12 -0700223 return planner;
224}
Sean Paulf72cccd2018-08-27 13:59:08 -0400225} // namespace android