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" |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 20 | #include "drmdevice.h" |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 21 | #include "platform.h" |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 22 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 23 | #include <stdatomic.h> |
| 24 | #include <xf86drm.h> |
| 25 | #include <xf86drmMode.h> |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 26 | #include <cinttypes> |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 27 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 28 | #include <hardware/gralloc.h> |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 29 | #include <log/log.h> |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 30 | #include "gralloc_priv.h" |
| 31 | |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 32 | #define MALI_ALIGN(value, base) (((value) + ((base)-1)) & ~((base)-1)) |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 36 | Importer *Importer::CreateInstance(DrmDevice *drm) { |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 37 | 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 Chong | a9ed46a | 2019-04-11 06:23:28 +0100 | [diff] [blame] | 50 | #if defined(MALI_GRALLOC_INTFMT_AFBC_BASIC) && \ |
| 51 | defined(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16) |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 52 | uint64_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 Stultz | 65c988d | 2019-02-27 23:34:53 -0800 | [diff] [blame] | 77 | #else |
| 78 | uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t /* flags */, |
| 79 | bool /* is_rgb */) { |
| 80 | return 0; |
| 81 | } |
| 82 | #endif |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 83 | |
| 84 | bool 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: |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame^] | 95 | ALOGV("Unsupported format %u assuming rgb?", drm_format); |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | } |
| 99 | |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame^] | 100 | 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] | 101 | bool is_rgb; |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 102 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 103 | private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>( |
| 104 | handle); |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 105 | if (!hnd) |
| 106 | return -EINVAL; |
| 107 | |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 108 | if (!(hnd->usage & GRALLOC_USAGE_HW_FB)) |
Alexey Firago | c385afe | 2018-11-27 14:17:55 +0300 | [diff] [blame] | 109 | return -EINVAL; |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 110 | |
Roman Stratiienko | f63726c | 2019-11-06 15:03:12 +0200 | [diff] [blame] | 111 | uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format); |
| 112 | if (fmt == DRM_FORMAT_INVALID) |
| 113 | return -EINVAL; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 114 | |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame^] | 115 | is_rgb = HisiImporter::IsDrmFormatRgb(fmt); |
| 116 | bo->modifiers[0] = HisiImporter:: |
| 117 | ConvertGrallocFormatToDrmModifiers(hnd->internal_format, is_rgb); |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 118 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 119 | bo->width = hnd->width; |
| 120 | bo->height = hnd->height; |
John Stultz | 616fb22 | 2018-08-24 16:08:57 -0700 | [diff] [blame] | 121 | bo->hal_format = hnd->req_format; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 122 | bo->format = fmt; |
| 123 | bo->usage = hnd->usage; |
John Stultz | a451483 | 2018-08-24 16:27:36 -0700 | [diff] [blame] | 124 | bo->pixel_stride = hnd->stride; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 125 | bo->pitches[0] = hnd->byte_stride; |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame^] | 126 | bo->prime_fds[0] = hnd->share_fd; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 127 | bo->offsets[0] = 0; |
| 128 | |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 129 | switch (fmt) { |
| 130 | case DRM_FORMAT_YVU420: { |
| 131 | int align = 128; |
| 132 | if (hnd->usage & |
| 133 | (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) |
| 134 | align = 16; |
| 135 | int adjusted_height = MALI_ALIGN(hnd->height, 2); |
| 136 | int y_size = adjusted_height * hnd->byte_stride; |
| 137 | int vu_stride = MALI_ALIGN(hnd->byte_stride / 2, align); |
| 138 | int v_size = vu_stride * (adjusted_height / 2); |
| 139 | |
| 140 | /* V plane*/ |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame^] | 141 | bo->prime_fds[1] = hnd->share_fd; |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 142 | bo->pitches[1] = vu_stride; |
| 143 | bo->offsets[1] = y_size; |
| 144 | /* U plane */ |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame^] | 145 | bo->prime_fds[2] = hnd->share_fd; |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 146 | bo->pitches[2] = vu_stride; |
| 147 | bo->offsets[2] = y_size + v_size; |
| 148 | break; |
| 149 | } |
| 150 | default: |
| 151 | break; |
| 152 | } |
| 153 | |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame^] | 154 | bo->with_modifiers = true; |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 155 | |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame^] | 156 | return 0; |
Alexey Firago | 18ec688 | 2018-11-21 23:47:05 +0300 | [diff] [blame] | 157 | } |
| 158 | |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 159 | class PlanStageHiSi : public Planner::PlanStage { |
| 160 | public: |
| 161 | int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
| 162 | std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc, |
| 163 | std::vector<DrmPlane *> *planes) { |
John Stultz | 60f5e32 | 2018-08-27 22:24:40 -0700 | [diff] [blame] | 164 | int layers_added = 0; |
Alexey Firago | c385afe | 2018-11-27 14:17:55 +0300 | [diff] [blame] | 165 | // Fill up as many DRM planes as we can with buffers that have HW_FB usage. |
| 166 | // Buffers without HW_FB should have been filtered out with |
| 167 | // CanImportBuffer(), if we meet one here, just skip it. |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 168 | for (auto i = layers.begin(); i != layers.end(); i = layers.erase(i)) { |
| 169 | if (!(i->second->gralloc_buffer_usage & GRALLOC_USAGE_HW_FB)) |
| 170 | continue; |
| 171 | |
| 172 | int ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer, |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 173 | crtc, std::make_pair(i->first, i->second)); |
John Stultz | 60f5e32 | 2018-08-27 22:24:40 -0700 | [diff] [blame] | 174 | layers_added++; |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 175 | // We don't have any planes left |
| 176 | if (ret == -ENOENT) |
| 177 | break; |
Alexandru Gheorghe | 2234d37 | 2018-10-09 16:25:28 +0100 | [diff] [blame] | 178 | else if (ret) { |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 179 | ALOGE("Failed to emplace layer %zu, dropping it", i->first); |
Alexandru Gheorghe | 2234d37 | 2018-10-09 16:25:28 +0100 | [diff] [blame] | 180 | return ret; |
| 181 | } |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 182 | } |
Alexey Firago | c385afe | 2018-11-27 14:17:55 +0300 | [diff] [blame] | 183 | // If we didn't emplace anything, return an error to ensure we force client |
| 184 | // compositing. |
| 185 | if (!layers_added) |
John Stultz | 60f5e32 | 2018-08-27 22:24:40 -0700 | [diff] [blame] | 186 | return -EINVAL; |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | }; |
| 191 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 192 | std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) { |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 193 | std::unique_ptr<Planner> planner(new Planner); |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 194 | planner->AddStage<PlanStageHiSi>(); |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 195 | return planner; |
| 196 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 197 | } // namespace android |