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