Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [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 | |
Sean Paul | 0aee6b2 | 2016-05-10 04:08:10 -0400 | [diff] [blame] | 17 | #define LOG_TAG "hwc-platform-drm-generic" |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 18 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 19 | #include "platformdrmgeneric.h" |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 20 | #include "drmdevice.h" |
Sean Paul | 6376996 | 2016-04-21 16:25:06 -0400 | [diff] [blame] | 21 | #include "platform.h" |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 22 | |
| 23 | #include <drm/drm_fourcc.h> |
| 24 | #include <xf86drm.h> |
| 25 | #include <xf86drmMode.h> |
| 26 | |
Mykhailo Sopiha | 1693bc3 | 2019-07-08 18:28:56 +0300 | [diff] [blame] | 27 | #include <cutils/properties.h> |
John Stultz | d12274d | 2018-04-02 20:57:21 -0700 | [diff] [blame] | 28 | #include <gralloc_handle.h> |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 29 | #include <hardware/gralloc.h> |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 30 | #include <log/log.h> |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | #ifdef USE_DRM_GENERIC_IMPORTER |
| 35 | // static |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 36 | Importer *Importer::CreateInstance(DrmDevice *drm) { |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 37 | DrmGenericImporter *importer = new DrmGenericImporter(drm); |
| 38 | if (!importer) |
| 39 | return NULL; |
| 40 | |
| 41 | int ret = importer->Init(); |
| 42 | if (ret) { |
| 43 | ALOGE("Failed to initialize the nv importer %d", ret); |
| 44 | delete importer; |
| 45 | return NULL; |
| 46 | } |
| 47 | return importer; |
| 48 | } |
| 49 | #endif |
| 50 | |
Mykhailo Sopiha | 1693bc3 | 2019-07-08 18:28:56 +0300 | [diff] [blame] | 51 | DrmGenericImporter::DrmGenericImporter(DrmDevice *drm) |
| 52 | : drm_(drm), exclude_non_hwfb_(false) { |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | DrmGenericImporter::~DrmGenericImporter() { |
| 56 | } |
| 57 | |
| 58 | int DrmGenericImporter::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"); |
| 63 | return ret; |
| 64 | } |
Mykhailo Sopiha | ad43886 | 2019-06-06 14:45:27 +0300 | [diff] [blame] | 65 | |
| 66 | ALOGI("Using %s gralloc module: %s\n", gralloc_->common.name, |
| 67 | gralloc_->common.author); |
| 68 | |
Mykhailo Sopiha | 1693bc3 | 2019-07-08 18:28:56 +0300 | [diff] [blame] | 69 | char exclude_non_hwfb_prop[PROPERTY_VALUE_MAX]; |
| 70 | property_get("hwc.drm.exclude_non_hwfb_imports", exclude_non_hwfb_prop, "0"); |
| 71 | exclude_non_hwfb_ = static_cast<bool>(strncmp(exclude_non_hwfb_prop, "0", 1)); |
| 72 | |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | uint32_t DrmGenericImporter::ConvertHalFormatToDrm(uint32_t hal_format) { |
| 77 | switch (hal_format) { |
| 78 | case HAL_PIXEL_FORMAT_RGB_888: |
| 79 | return DRM_FORMAT_BGR888; |
| 80 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 81 | return DRM_FORMAT_ARGB8888; |
| 82 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 83 | return DRM_FORMAT_XBGR8888; |
| 84 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 85 | return DRM_FORMAT_ABGR8888; |
| 86 | case HAL_PIXEL_FORMAT_RGB_565: |
| 87 | return DRM_FORMAT_BGR565; |
| 88 | case HAL_PIXEL_FORMAT_YV12: |
| 89 | return DRM_FORMAT_YVU420; |
| 90 | default: |
| 91 | ALOGE("Cannot convert hal format to drm format %u", hal_format); |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | } |
| 95 | |
John Stultz | a451483 | 2018-08-24 16:27:36 -0700 | [diff] [blame] | 96 | uint32_t DrmGenericImporter::DrmFormatToBitsPerPixel(uint32_t drm_format) { |
| 97 | switch (drm_format) { |
| 98 | case DRM_FORMAT_ARGB8888: |
| 99 | case DRM_FORMAT_XBGR8888: |
| 100 | case DRM_FORMAT_ABGR8888: |
| 101 | return 32; |
| 102 | case DRM_FORMAT_BGR888: |
| 103 | return 24; |
| 104 | case DRM_FORMAT_BGR565: |
| 105 | return 16; |
| 106 | case DRM_FORMAT_YVU420: |
| 107 | return 12; |
| 108 | default: |
| 109 | ALOGE("Cannot convert hal format %u to bpp (returning 32)", drm_format); |
| 110 | return 32; |
| 111 | } |
| 112 | } |
| 113 | |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 114 | int DrmGenericImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) { |
John Stultz | d12274d | 2018-04-02 20:57:21 -0700 | [diff] [blame] | 115 | gralloc_handle_t *gr_handle = gralloc_handle(handle); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 116 | if (!gr_handle) |
| 117 | return -EINVAL; |
| 118 | |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 119 | uint32_t gem_handle; |
| 120 | int ret = drmPrimeFDToHandle(drm_->fd(), gr_handle->prime_fd, &gem_handle); |
| 121 | if (ret) { |
| 122 | ALOGE("failed to import prime fd %d ret=%d", gr_handle->prime_fd, ret); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | memset(bo, 0, sizeof(hwc_drm_bo_t)); |
| 127 | bo->width = gr_handle->width; |
| 128 | bo->height = gr_handle->height; |
John Stultz | 616fb22 | 2018-08-24 16:08:57 -0700 | [diff] [blame] | 129 | bo->hal_format = gr_handle->format; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 130 | bo->format = ConvertHalFormatToDrm(gr_handle->format); |
Rob Herring | aeccd89 | 2017-10-06 17:20:05 -0500 | [diff] [blame] | 131 | bo->usage = gr_handle->usage; |
John Stultz | a451483 | 2018-08-24 16:27:36 -0700 | [diff] [blame] | 132 | bo->pixel_stride = (gr_handle->stride * 8) / |
| 133 | DrmFormatToBitsPerPixel(bo->format); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 134 | bo->pitches[0] = gr_handle->stride; |
| 135 | bo->gem_handles[0] = gem_handle; |
| 136 | bo->offsets[0] = 0; |
| 137 | |
| 138 | ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format, |
| 139 | bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id, 0); |
| 140 | if (ret) { |
| 141 | ALOGE("could not create drm fb %d", ret); |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) { |
| 149 | if (bo->fb_id) |
| 150 | if (drmModeRmFB(drm_->fd(), bo->fb_id)) |
| 151 | ALOGE("Failed to rm fb"); |
| 152 | |
| 153 | struct drm_gem_close gem_close; |
| 154 | memset(&gem_close, 0, sizeof(gem_close)); |
John Stultz | ee18b0e | 2018-08-27 10:53:07 -0700 | [diff] [blame] | 155 | |
| 156 | for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) { |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 157 | if (!bo->gem_handles[i]) |
| 158 | continue; |
| 159 | |
| 160 | gem_close.handle = bo->gem_handles[i]; |
| 161 | int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close); |
John Stultz | 53cb4ef | 2018-05-31 17:46:50 -0700 | [diff] [blame] | 162 | if (ret) { |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 163 | ALOGE("Failed to close gem handle %d %d", i, ret); |
John Stultz | 53cb4ef | 2018-05-31 17:46:50 -0700 | [diff] [blame] | 164 | } else { |
John Stultz | ee18b0e | 2018-08-27 10:53:07 -0700 | [diff] [blame] | 165 | for (int j = i + 1; j < HWC_DRM_BO_MAX_PLANES; j++) |
John Stultz | 53cb4ef | 2018-05-31 17:46:50 -0700 | [diff] [blame] | 166 | if (bo->gem_handles[j] == bo->gem_handles[i]) |
| 167 | bo->gem_handles[j] = 0; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 168 | bo->gem_handles[i] = 0; |
John Stultz | 53cb4ef | 2018-05-31 17:46:50 -0700 | [diff] [blame] | 169 | } |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 170 | } |
| 171 | return 0; |
| 172 | } |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 173 | |
Alexey Firago | 18ec688 | 2018-11-21 23:47:05 +0300 | [diff] [blame] | 174 | bool DrmGenericImporter::CanImportBuffer(buffer_handle_t handle) { |
| 175 | if (handle == NULL) |
| 176 | return false; |
Mykhailo Sopiha | 1693bc3 | 2019-07-08 18:28:56 +0300 | [diff] [blame] | 177 | |
| 178 | if (exclude_non_hwfb_) { |
| 179 | gralloc_handle_t *hnd = gralloc_handle(handle); |
| 180 | return hnd->usage & GRALLOC_USAGE_HW_FB; |
| 181 | } |
| 182 | |
Alexey Firago | 18ec688 | 2018-11-21 23:47:05 +0300 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 186 | #ifdef USE_DRM_GENERIC_IMPORTER |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 187 | std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) { |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 188 | std::unique_ptr<Planner> planner(new Planner); |
| 189 | planner->AddStage<PlanStageGreedy>(); |
| 190 | return planner; |
| 191 | } |
| 192 | #endif |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 193 | } |