John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 19 | #include "platformhisi.h" |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 20 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 21 | #include <xf86drm.h> |
| 22 | #include <xf86drmMode.h> |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 23 | #include <cinttypes> |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 24 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 25 | #include <log/log.h> |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 26 | #include "gralloc_priv.h" |
| 27 | |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 28 | #define MALI_ALIGN(value, base) (((value) + ((base)-1)) & ~((base)-1)) |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 32 | Importer *Importer::CreateInstance(DrmDevice *drm) { |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 33 | 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 Chong | a9ed46a | 2019-04-11 06:23:28 +0100 | [diff] [blame] | 46 | #if defined(MALI_GRALLOC_INTFMT_AFBC_BASIC) && \ |
| 47 | defined(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16) |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 48 | uint64_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 Stultz | 65c988d | 2019-02-27 23:34:53 -0800 | [diff] [blame] | 73 | #else |
| 74 | uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t /* flags */, |
| 75 | bool /* is_rgb */) { |
| 76 | return 0; |
| 77 | } |
| 78 | #endif |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 79 | |
| 80 | bool 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 Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame] | 91 | ALOGV("Unsupported format %u assuming rgb?", drm_format); |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | } |
| 95 | |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame] | 96 | int HisiImporter::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) { |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 97 | bool is_rgb; |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 98 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 99 | private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>( |
| 100 | handle); |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 101 | if (!hnd) |
| 102 | return -EINVAL; |
| 103 | |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 104 | if (!(hnd->usage & GRALLOC_USAGE_HW_FB)) |
Alexey Firago | c385afe | 2018-11-27 14:17:55 +0300 | [diff] [blame] | 105 | return -EINVAL; |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 106 | |
Roman Stratiienko | f63726c | 2019-11-06 15:03:12 +0200 | [diff] [blame] | 107 | uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format); |
| 108 | if (fmt == DRM_FORMAT_INVALID) |
| 109 | return -EINVAL; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 110 | |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame] | 111 | is_rgb = HisiImporter::IsDrmFormatRgb(fmt); |
| 112 | bo->modifiers[0] = HisiImporter:: |
| 113 | ConvertGrallocFormatToDrmModifiers(hnd->internal_format, is_rgb); |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 114 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 115 | bo->width = hnd->width; |
| 116 | bo->height = hnd->height; |
John Stultz | 616fb22 | 2018-08-24 16:08:57 -0700 | [diff] [blame] | 117 | bo->hal_format = hnd->req_format; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 118 | bo->format = fmt; |
| 119 | bo->usage = hnd->usage; |
John Stultz | a451483 | 2018-08-24 16:27:36 -0700 | [diff] [blame] | 120 | bo->pixel_stride = hnd->stride; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 121 | bo->pitches[0] = hnd->byte_stride; |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame] | 122 | bo->prime_fds[0] = hnd->share_fd; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 123 | bo->offsets[0] = 0; |
| 124 | |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 125 | 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 Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame] | 137 | bo->prime_fds[1] = hnd->share_fd; |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 138 | bo->pitches[1] = vu_stride; |
| 139 | bo->offsets[1] = y_size; |
| 140 | /* U plane */ |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame] | 141 | bo->prime_fds[2] = hnd->share_fd; |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 142 | bo->pitches[2] = vu_stride; |
| 143 | bo->offsets[2] = y_size + v_size; |
| 144 | break; |
| 145 | } |
| 146 | default: |
| 147 | break; |
| 148 | } |
| 149 | |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame] | 150 | bo->with_modifiers = true; |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 151 | |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame] | 152 | return 0; |
Alexey Firago | 18ec688 | 2018-11-21 23:47:05 +0300 | [diff] [blame] | 153 | } |
| 154 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 155 | } // namespace android |