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 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame^] | 19 | #include "drmdevice.h" |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 20 | #include "platform.h" |
| 21 | #include "platformhisi.h" |
| 22 | |
| 23 | |
| 24 | #include <drm/drm_fourcc.h> |
| 25 | #include <cinttypes> |
| 26 | #include <stdatomic.h> |
| 27 | #include <xf86drm.h> |
| 28 | #include <xf86drmMode.h> |
| 29 | |
John Stultz | 9057a6f | 2018-04-26 12:05:55 -0700 | [diff] [blame] | 30 | #include <log/log.h> |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 31 | #include <hardware/gralloc.h> |
| 32 | #include "gralloc_priv.h" |
| 33 | |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 34 | #define MALI_ALIGN(value, base) (((value) + ((base)-1)) & ~((base)-1)) |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame^] | 38 | Importer *Importer::CreateInstance(DrmDevice *drm) { |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 39 | HisiImporter *importer = new HisiImporter(drm); |
| 40 | if (!importer) |
| 41 | return NULL; |
| 42 | |
| 43 | int ret = importer->Init(); |
| 44 | if (ret) { |
| 45 | ALOGE("Failed to initialize the hisi importer %d", ret); |
| 46 | delete importer; |
| 47 | return NULL; |
| 48 | } |
| 49 | return importer; |
| 50 | } |
| 51 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame^] | 52 | HisiImporter::HisiImporter(DrmDevice *drm) |
| 53 | : DrmGenericImporter(drm), drm_(drm) { |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | HisiImporter::~HisiImporter() { |
| 57 | } |
| 58 | |
| 59 | int HisiImporter::Init() { |
| 60 | int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, |
| 61 | (const hw_module_t **)&gralloc_); |
| 62 | if (ret) { |
| 63 | ALOGE("Failed to open gralloc module %d", ret); |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | if (strcasecmp(gralloc_->common.author, "ARM Ltd.")) |
| 68 | ALOGW("Using non-ARM gralloc module: %s/%s\n", gralloc_->common.name, |
| 69 | gralloc_->common.author); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 74 | int HisiImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) { |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 75 | private_handle_t const *hnd = |
| 76 | reinterpret_cast<private_handle_t const *>(handle); |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 77 | if (!hnd) |
| 78 | return -EINVAL; |
| 79 | |
| 80 | uint32_t gem_handle; |
| 81 | int ret = drmPrimeFDToHandle(drm_->fd(), hnd->share_fd, &gem_handle); |
| 82 | if (ret) { |
| 83 | ALOGE("failed to import prime fd %d ret=%d", hnd->share_fd, ret); |
| 84 | return ret; |
| 85 | } |
| 86 | |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 87 | int32_t fmt = ConvertHalFormatToDrm(hnd->req_format); |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 88 | if (fmt < 0) |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 89 | return fmt; |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 90 | |
| 91 | memset(bo, 0, sizeof(hwc_drm_bo_t)); |
| 92 | bo->width = hnd->width; |
| 93 | bo->height = hnd->height; |
| 94 | bo->format = fmt; |
| 95 | bo->usage = hnd->usage; |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 96 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 97 | bo->pitches[0] = hnd->byte_stride; |
| 98 | bo->gem_handles[0] = gem_handle; |
| 99 | bo->offsets[0] = 0; |
| 100 | |
John Stultz | df35a81 | 2018-05-31 17:37:21 -0700 | [diff] [blame] | 101 | switch (fmt) { |
| 102 | case DRM_FORMAT_YVU420: { |
| 103 | int align = 128; |
| 104 | if (hnd->usage & |
| 105 | (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) |
| 106 | align = 16; |
| 107 | int adjusted_height = MALI_ALIGN(hnd->height, 2); |
| 108 | int y_size = adjusted_height * hnd->byte_stride; |
| 109 | int vu_stride = MALI_ALIGN(hnd->byte_stride / 2, align); |
| 110 | int v_size = vu_stride * (adjusted_height / 2); |
| 111 | |
| 112 | /* V plane*/ |
| 113 | bo->gem_handles[1] = gem_handle; |
| 114 | bo->pitches[1] = vu_stride; |
| 115 | bo->offsets[1] = y_size; |
| 116 | /* U plane */ |
| 117 | bo->gem_handles[2] = gem_handle; |
| 118 | bo->pitches[2] = vu_stride; |
| 119 | bo->offsets[2] = y_size + v_size; |
| 120 | break; |
| 121 | } |
| 122 | default: |
| 123 | break; |
| 124 | } |
| 125 | |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 126 | ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format, |
| 127 | bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id, 0); |
| 128 | if (ret) { |
| 129 | ALOGE("could not create drm fb %d", ret); |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame^] | 136 | std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) { |
John Stultz | 499db60 | 2018-03-13 16:51:12 -0700 | [diff] [blame] | 137 | std::unique_ptr<Planner> planner(new Planner); |
| 138 | planner->AddStage<PlanStageGreedy>(); |
| 139 | return planner; |
| 140 | } |
| 141 | } |