blob: 67793dba0090e8f26a7dafdc055bb173fbbf7ed4 [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"
John Stultz499db602018-03-13 16:51:12 -070020
John Stultz499db602018-03-13 16:51:12 -070021#include <xf86drm.h>
22#include <xf86drmMode.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040023#include <cinttypes>
John Stultz499db602018-03-13 16:51:12 -070024
Sean Paulf72cccd2018-08-27 13:59:08 -040025#include <log/log.h>
John Stultz499db602018-03-13 16:51:12 -070026#include "gralloc_priv.h"
27
John Stultzdf35a812018-05-31 17:37:21 -070028#define MALI_ALIGN(value, base) (((value) + ((base)-1)) & ~((base)-1))
John Stultz499db602018-03-13 16:51:12 -070029
30namespace android {
31
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010032Importer *Importer::CreateInstance(DrmDevice *drm) {
John Stultz499db602018-03-13 16:51:12 -070033 HisiImporter *importer = new HisiImporter(drm);
34 if (!importer)
35 return NULL;
36
37 int ret = importer->Init();
38 if (ret) {
39 ALOGE("Failed to initialize the hisi importer %d", ret);
40 delete importer;
41 return NULL;
42 }
43 return importer;
44}
45
Victor Chonga9ed46a2019-04-11 06:23:28 +010046#if defined(MALI_GRALLOC_INTFMT_AFBC_BASIC) && \
47 defined(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16)
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +000048uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t flags,
49 bool is_rgb) {
50 uint64_t features = 0UL;
51
52 if (flags & MALI_GRALLOC_INTFMT_AFBC_BASIC)
53 features |= AFBC_FORMAT_MOD_BLOCK_SIZE_16x16;
54
55 if (flags & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK)
56 features |= (AFBC_FORMAT_MOD_SPLIT | AFBC_FORMAT_MOD_SPARSE);
57
58 if (flags & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
59 features |= AFBC_FORMAT_MOD_BLOCK_SIZE_32x8;
60
61 if (flags & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
62 features |= AFBC_FORMAT_MOD_TILED;
63
64 if (features) {
65 if (is_rgb)
66 features |= AFBC_FORMAT_MOD_YTR;
67
68 return DRM_FORMAT_MOD_ARM_AFBC(features);
69 }
70
71 return 0;
72}
John Stultz65c988d2019-02-27 23:34:53 -080073#else
74uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t /* flags */,
75 bool /* is_rgb */) {
76 return 0;
77}
78#endif
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +000079
80bool HisiImporter::IsDrmFormatRgb(uint32_t drm_format) {
81 switch (drm_format) {
82 case DRM_FORMAT_ARGB8888:
83 case DRM_FORMAT_XBGR8888:
84 case DRM_FORMAT_ABGR8888:
85 case DRM_FORMAT_BGR888:
86 case DRM_FORMAT_BGR565:
87 return true;
88 case DRM_FORMAT_YVU420:
89 return false;
90 default:
Roman Stratiienko4163efc2019-12-06 12:30:28 +020091 ALOGV("Unsupported format %u assuming rgb?", drm_format);
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +000092 return true;
93 }
94}
95
Roman Stratiienko4163efc2019-12-06 12:30:28 +020096int HisiImporter::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +000097 bool is_rgb;
Sean Paul54589d22018-08-24 16:58:35 -040098
Sean Paulf72cccd2018-08-27 13:59:08 -040099 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
100 handle);
John Stultz499db602018-03-13 16:51:12 -0700101 if (!hnd)
102 return -EINVAL;
103
Sean Paul54589d22018-08-24 16:58:35 -0400104 if (!(hnd->usage & GRALLOC_USAGE_HW_FB))
Alexey Firagoc385afe2018-11-27 14:17:55 +0300105 return -EINVAL;
Sean Paul54589d22018-08-24 16:58:35 -0400106
Roman Stratiienkof63726c2019-11-06 15:03:12 +0200107 uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
108 if (fmt == DRM_FORMAT_INVALID)
109 return -EINVAL;
John Stultz499db602018-03-13 16:51:12 -0700110
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200111 is_rgb = HisiImporter::IsDrmFormatRgb(fmt);
112 bo->modifiers[0] = HisiImporter::
113 ConvertGrallocFormatToDrmModifiers(hnd->internal_format, is_rgb);
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +0000114
John Stultz499db602018-03-13 16:51:12 -0700115 bo->width = hnd->width;
116 bo->height = hnd->height;
John Stultz616fb222018-08-24 16:08:57 -0700117 bo->hal_format = hnd->req_format;
John Stultz499db602018-03-13 16:51:12 -0700118 bo->format = fmt;
119 bo->usage = hnd->usage;
John Stultza4514832018-08-24 16:27:36 -0700120 bo->pixel_stride = hnd->stride;
John Stultz499db602018-03-13 16:51:12 -0700121 bo->pitches[0] = hnd->byte_stride;
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200122 bo->prime_fds[0] = hnd->share_fd;
John Stultz499db602018-03-13 16:51:12 -0700123 bo->offsets[0] = 0;
124
John Stultzdf35a812018-05-31 17:37:21 -0700125 switch (fmt) {
126 case DRM_FORMAT_YVU420: {
127 int align = 128;
128 if (hnd->usage &
129 (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
130 align = 16;
131 int adjusted_height = MALI_ALIGN(hnd->height, 2);
132 int y_size = adjusted_height * hnd->byte_stride;
133 int vu_stride = MALI_ALIGN(hnd->byte_stride / 2, align);
134 int v_size = vu_stride * (adjusted_height / 2);
135
136 /* V plane*/
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200137 bo->prime_fds[1] = hnd->share_fd;
John Stultzdf35a812018-05-31 17:37:21 -0700138 bo->pitches[1] = vu_stride;
139 bo->offsets[1] = y_size;
140 /* U plane */
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200141 bo->prime_fds[2] = hnd->share_fd;
John Stultzdf35a812018-05-31 17:37:21 -0700142 bo->pitches[2] = vu_stride;
143 bo->offsets[2] = y_size + v_size;
144 break;
145 }
146 default:
147 break;
148 }
149
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200150 bo->with_modifiers = true;
Ayan Kumar Haldercc5fca42019-01-14 12:47:12 +0000151
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200152 return 0;
Alexey Firago18ec6882018-11-21 23:47:05 +0300153}
154
Sean Paulf72cccd2018-08-27 13:59:08 -0400155} // namespace android