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 | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 73 | int HisiImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) { |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 74 | memset(bo, 0, sizeof(hwc_drm_bo_t)); |
| 75 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 76 | private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>( |
| 77 | handle); |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 78 | if (!hnd) |
| 79 | return -EINVAL; |
| 80 | |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 81 | // We can't import these types of buffers, so pretend we did and rely on the |
| 82 | // planner to skip them when choosing layers for planes |
| 83 | if (!(hnd->usage & GRALLOC_USAGE_HW_FB)) |
| 84 | return 0; |
| 85 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 86 | uint32_t gem_handle; |
| 87 | int ret = drmPrimeFDToHandle(drm_->fd(), hnd->share_fd, &gem_handle); |
| 88 | if (ret) { |
| 89 | ALOGE("failed to import prime fd %d ret=%d", hnd->share_fd, ret); |
| 90 | return ret; |
| 91 | } |
| 92 | |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 93 | int32_t fmt = ConvertHalFormatToDrm(hnd->req_format); |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 94 | if (fmt < 0) |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 95 | return fmt; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 96 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 97 | bo->width = hnd->width; |
| 98 | bo->height = hnd->height; |
John Stultz | 616fb22 | 2018-08-24 16:08:57 -0700 | [diff] [blame] | 99 | bo->hal_format = hnd->req_format; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 100 | bo->format = fmt; |
| 101 | bo->usage = hnd->usage; |
John Stultz | a451483 | 2018-08-24 16:27:36 -0700 | [diff] [blame^] | 102 | bo->pixel_stride = hnd->stride; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 103 | bo->pitches[0] = hnd->byte_stride; |
| 104 | bo->gem_handles[0] = gem_handle; |
| 105 | bo->offsets[0] = 0; |
| 106 | |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 107 | switch (fmt) { |
| 108 | case DRM_FORMAT_YVU420: { |
| 109 | int align = 128; |
| 110 | if (hnd->usage & |
| 111 | (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) |
| 112 | align = 16; |
| 113 | int adjusted_height = MALI_ALIGN(hnd->height, 2); |
| 114 | int y_size = adjusted_height * hnd->byte_stride; |
| 115 | int vu_stride = MALI_ALIGN(hnd->byte_stride / 2, align); |
| 116 | int v_size = vu_stride * (adjusted_height / 2); |
| 117 | |
| 118 | /* V plane*/ |
| 119 | bo->gem_handles[1] = gem_handle; |
| 120 | bo->pitches[1] = vu_stride; |
| 121 | bo->offsets[1] = y_size; |
| 122 | /* U plane */ |
| 123 | bo->gem_handles[2] = gem_handle; |
| 124 | bo->pitches[2] = vu_stride; |
| 125 | bo->offsets[2] = y_size + v_size; |
| 126 | break; |
| 127 | } |
| 128 | default: |
| 129 | break; |
| 130 | } |
| 131 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 132 | ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format, |
| 133 | bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id, 0); |
| 134 | if (ret) { |
| 135 | ALOGE("could not create drm fb %d", ret); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 142 | class PlanStageHiSi : public Planner::PlanStage { |
| 143 | public: |
| 144 | int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
| 145 | std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc, |
| 146 | std::vector<DrmPlane *> *planes) { |
John Stultz | 60f5e32 | 2018-08-27 22:24:40 -0700 | [diff] [blame] | 147 | int layers_added = 0; |
| 148 | int initial_layers = layers.size(); |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 149 | // Fill up as many planes as we can with buffers that do not have HW_FB |
| 150 | // usage |
| 151 | for (auto i = layers.begin(); i != layers.end(); i = layers.erase(i)) { |
| 152 | if (!(i->second->gralloc_buffer_usage & GRALLOC_USAGE_HW_FB)) |
| 153 | continue; |
| 154 | |
| 155 | int ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer, |
| 156 | crtc, i->first); |
John Stultz | 60f5e32 | 2018-08-27 22:24:40 -0700 | [diff] [blame] | 157 | layers_added++; |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 158 | // We don't have any planes left |
| 159 | if (ret == -ENOENT) |
| 160 | break; |
| 161 | else if (ret) |
| 162 | ALOGE("Failed to emplace layer %zu, dropping it", i->first); |
| 163 | } |
John Stultz | 60f5e32 | 2018-08-27 22:24:40 -0700 | [diff] [blame] | 164 | /* |
| 165 | * If we only have one layer, but we didn't emplace anything, we |
| 166 | * can run into trouble, as we might try to device composite a |
| 167 | * buffer we fake-imported, which can cause things to jamb up. |
| 168 | * So return an error in this case to ensure we force client |
| 169 | * compositing. |
| 170 | */ |
| 171 | if (!layers_added && (initial_layers <= 1)) |
| 172 | return -EINVAL; |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | }; |
| 177 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 178 | std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) { |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 179 | std::unique_ptr<Planner> planner(new Planner); |
Sean Paul | 54589d2 | 2018-08-24 16:58:35 -0400 | [diff] [blame] | 180 | planner->AddStage<PlanStageHiSi>(); |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 181 | return planner; |
| 182 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 183 | } // namespace android |