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 | |
| 23 | #include <drm/drm_fourcc.h> |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 24 | #include <stdatomic.h> |
| 25 | #include <xf86drm.h> |
| 26 | #include <xf86drmMode.h> |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 27 | #include <cinttypes> |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 28 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 29 | #include <hardware/gralloc.h> |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 30 | #include <log/log.h> |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 31 | #include "gralloc_priv.h" |
| 32 | |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 33 | #define MALI_ALIGN(value, base) (((value) + ((base)-1)) & ~((base)-1)) |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 37 | Importer *Importer::CreateInstance(DrmDevice *drm) { |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 38 | 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 Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 51 | HisiImporter::HisiImporter(DrmDevice *drm) |
| 52 | : DrmGenericImporter(drm), drm_(drm) { |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | HisiImporter::~HisiImporter() { |
| 56 | } |
| 57 | |
| 58 | int 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 | |
John Stultz | 65c988d | 2019-02-27 23:34:53 -0800 | [diff] [blame^] | 73 | #ifdef MALI_GRALLOC_INTFMT_AFBC_BASIC |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 74 | uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t flags, |
| 75 | bool is_rgb) { |
| 76 | uint64_t features = 0UL; |
| 77 | |
| 78 | if (flags & MALI_GRALLOC_INTFMT_AFBC_BASIC) |
| 79 | features |= AFBC_FORMAT_MOD_BLOCK_SIZE_16x16; |
| 80 | |
| 81 | if (flags & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK) |
| 82 | features |= (AFBC_FORMAT_MOD_SPLIT | AFBC_FORMAT_MOD_SPARSE); |
| 83 | |
| 84 | if (flags & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK) |
| 85 | features |= AFBC_FORMAT_MOD_BLOCK_SIZE_32x8; |
| 86 | |
| 87 | if (flags & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS) |
| 88 | features |= AFBC_FORMAT_MOD_TILED; |
| 89 | |
| 90 | if (features) { |
| 91 | if (is_rgb) |
| 92 | features |= AFBC_FORMAT_MOD_YTR; |
| 93 | |
| 94 | return DRM_FORMAT_MOD_ARM_AFBC(features); |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
John Stultz | 65c988d | 2019-02-27 23:34:53 -0800 | [diff] [blame^] | 99 | #else |
| 100 | uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t /* flags */, |
| 101 | bool /* is_rgb */) { |
| 102 | return 0; |
| 103 | } |
| 104 | #endif |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 105 | |
| 106 | bool HisiImporter::IsDrmFormatRgb(uint32_t drm_format) { |
| 107 | switch (drm_format) { |
| 108 | case DRM_FORMAT_ARGB8888: |
| 109 | case DRM_FORMAT_XBGR8888: |
| 110 | case DRM_FORMAT_ABGR8888: |
| 111 | case DRM_FORMAT_BGR888: |
| 112 | case DRM_FORMAT_BGR565: |
| 113 | return true; |
| 114 | case DRM_FORMAT_YVU420: |
| 115 | return false; |
| 116 | default: |
| 117 | ALOGE("Unsupported format %u assuming rgb?", drm_format); |
| 118 | return true; |
| 119 | } |
| 120 | } |
| 121 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 122 | int HisiImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) { |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 123 | bool is_rgb; |
| 124 | uint64_t modifiers[4] = {0}; |
| 125 | |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 126 | memset(bo, 0, sizeof(hwc_drm_bo_t)); |
| 127 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 128 | private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>( |
| 129 | handle); |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 130 | if (!hnd) |
| 131 | return -EINVAL; |
| 132 | |
Alexey Firago | c385afe | 2018-11-27 14:17:55 +0300 | [diff] [blame] | 133 | // We can't import these types of buffers. |
| 134 | // These buffers should have been filtered out with CanImportBuffer() |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 135 | if (!(hnd->usage & GRALLOC_USAGE_HW_FB)) |
Alexey Firago | c385afe | 2018-11-27 14:17:55 +0300 | [diff] [blame] | 136 | return -EINVAL; |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 137 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 138 | uint32_t gem_handle; |
| 139 | int ret = drmPrimeFDToHandle(drm_->fd(), hnd->share_fd, &gem_handle); |
| 140 | if (ret) { |
| 141 | ALOGE("failed to import prime fd %d ret=%d", hnd->share_fd, ret); |
| 142 | return ret; |
| 143 | } |
| 144 | |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 145 | int32_t fmt = ConvertHalFormatToDrm(hnd->req_format); |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 146 | if (fmt < 0) |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 147 | return fmt; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 148 | |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 149 | is_rgb = IsDrmFormatRgb(fmt); |
| 150 | modifiers[0] = ConvertGrallocFormatToDrmModifiers(hnd->internal_format, |
| 151 | is_rgb); |
| 152 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 153 | bo->width = hnd->width; |
| 154 | bo->height = hnd->height; |
John Stultz | 616fb22 | 2018-08-24 16:08:57 -0700 | [diff] [blame] | 155 | bo->hal_format = hnd->req_format; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 156 | bo->format = fmt; |
| 157 | bo->usage = hnd->usage; |
John Stultz | a451483 | 2018-08-24 16:27:36 -0700 | [diff] [blame] | 158 | bo->pixel_stride = hnd->stride; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 159 | bo->pitches[0] = hnd->byte_stride; |
| 160 | bo->gem_handles[0] = gem_handle; |
| 161 | bo->offsets[0] = 0; |
| 162 | |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 163 | switch (fmt) { |
| 164 | case DRM_FORMAT_YVU420: { |
| 165 | int align = 128; |
| 166 | if (hnd->usage & |
| 167 | (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) |
| 168 | align = 16; |
| 169 | int adjusted_height = MALI_ALIGN(hnd->height, 2); |
| 170 | int y_size = adjusted_height * hnd->byte_stride; |
| 171 | int vu_stride = MALI_ALIGN(hnd->byte_stride / 2, align); |
| 172 | int v_size = vu_stride * (adjusted_height / 2); |
| 173 | |
| 174 | /* V plane*/ |
| 175 | bo->gem_handles[1] = gem_handle; |
| 176 | bo->pitches[1] = vu_stride; |
| 177 | bo->offsets[1] = y_size; |
| 178 | /* U plane */ |
| 179 | bo->gem_handles[2] = gem_handle; |
| 180 | bo->pitches[2] = vu_stride; |
| 181 | bo->offsets[2] = y_size + v_size; |
| 182 | break; |
| 183 | } |
| 184 | default: |
| 185 | break; |
| 186 | } |
| 187 | |
Ayan Kumar Halder | cc5fca4 | 2019-01-14 12:47:12 +0000 | [diff] [blame] | 188 | ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height, |
| 189 | bo->format, bo->gem_handles, bo->pitches, |
| 190 | bo->offsets, modifiers, &bo->fb_id, |
| 191 | modifiers[0] ? DRM_MODE_FB_MODIFIERS : 0); |
| 192 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 193 | if (ret) { |
| 194 | ALOGE("could not create drm fb %d", ret); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
Alexey Firago | 18ec688 | 2018-11-21 23:47:05 +0300 | [diff] [blame] | 201 | bool HisiImporter::CanImportBuffer(buffer_handle_t handle) { |
| 202 | private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>( |
| 203 | handle); |
| 204 | return hnd && (hnd->usage & GRALLOC_USAGE_HW_FB); |
| 205 | } |
| 206 | |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 207 | class PlanStageHiSi : public Planner::PlanStage { |
| 208 | public: |
| 209 | int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
| 210 | std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc, |
| 211 | std::vector<DrmPlane *> *planes) { |
John Stultz | 60f5e32 | 2018-08-27 22:24:40 -0700 | [diff] [blame] | 212 | int layers_added = 0; |
Alexey Firago | c385afe | 2018-11-27 14:17:55 +0300 | [diff] [blame] | 213 | // Fill up as many DRM planes as we can with buffers that have HW_FB usage. |
| 214 | // Buffers without HW_FB should have been filtered out with |
| 215 | // CanImportBuffer(), if we meet one here, just skip it. |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 216 | for (auto i = layers.begin(); i != layers.end(); i = layers.erase(i)) { |
| 217 | if (!(i->second->gralloc_buffer_usage & GRALLOC_USAGE_HW_FB)) |
| 218 | continue; |
| 219 | |
| 220 | int ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer, |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 221 | crtc, std::make_pair(i->first, i->second)); |
John Stultz | 60f5e32 | 2018-08-27 22:24:40 -0700 | [diff] [blame] | 222 | layers_added++; |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 223 | // We don't have any planes left |
| 224 | if (ret == -ENOENT) |
| 225 | break; |
Alexandru Gheorghe | 2234d37 | 2018-10-09 16:25:28 +0100 | [diff] [blame] | 226 | else if (ret) { |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 227 | ALOGE("Failed to emplace layer %zu, dropping it", i->first); |
Alexandru Gheorghe | 2234d37 | 2018-10-09 16:25:28 +0100 | [diff] [blame] | 228 | return ret; |
| 229 | } |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 230 | } |
Alexey Firago | c385afe | 2018-11-27 14:17:55 +0300 | [diff] [blame] | 231 | // If we didn't emplace anything, return an error to ensure we force client |
| 232 | // compositing. |
| 233 | if (!layers_added) |
John Stultz | 60f5e32 | 2018-08-27 22:24:40 -0700 | [diff] [blame] | 234 | return -EINVAL; |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | }; |
| 239 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 240 | std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) { |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 241 | std::unique_ptr<Planner> planner(new Planner); |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 242 | planner->AddStage<PlanStageHiSi>(); |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 243 | return planner; |
| 244 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 245 | } // namespace android |