Neil Armstrong | 17d3c8b | 2019-04-25 15:17:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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-meson" |
| 18 | |
| 19 | #include "platformmeson.h" |
| 20 | #include "drmdevice.h" |
| 21 | #include "platform.h" |
| 22 | |
| 23 | #include <drm/drm_fourcc.h> |
| 24 | #include <stdatomic.h> |
| 25 | #include <xf86drm.h> |
| 26 | #include <xf86drmMode.h> |
| 27 | #include <cinttypes> |
| 28 | |
| 29 | #include <hardware/gralloc.h> |
| 30 | #include <log/log.h> |
| 31 | #include "gralloc_priv.h" |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | Importer *Importer::CreateInstance(DrmDevice *drm) { |
| 36 | MesonImporter *importer = new MesonImporter(drm); |
| 37 | if (!importer) |
| 38 | return NULL; |
| 39 | |
| 40 | int ret = importer->Init(); |
| 41 | if (ret) { |
| 42 | ALOGE("Failed to initialize the meson importer %d", ret); |
| 43 | delete importer; |
| 44 | return NULL; |
| 45 | } |
| 46 | return importer; |
| 47 | } |
| 48 | |
| 49 | #if defined(MALI_GRALLOC_INTFMT_AFBC_BASIC) && \ |
| 50 | defined(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16) |
| 51 | uint64_t MesonImporter::ConvertGrallocFormatToDrmModifiers(uint64_t flags) { |
| 52 | uint64_t features = 0UL; |
| 53 | |
Neil Armstrong | 6a1c38d | 2019-10-10 09:35:57 +0000 | [diff] [blame] | 54 | if (flags & MALI_GRALLOC_INTFMT_AFBC_BASIC) { |
| 55 | if (flags & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK) |
| 56 | features |= AFBC_FORMAT_MOD_BLOCK_SIZE_32x8; |
| 57 | else |
| 58 | features |= AFBC_FORMAT_MOD_BLOCK_SIZE_16x16; |
| 59 | } |
Neil Armstrong | 17d3c8b | 2019-04-25 15:17:22 +0000 | [diff] [blame] | 60 | |
| 61 | if (flags & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK) |
| 62 | features |= (AFBC_FORMAT_MOD_SPLIT | AFBC_FORMAT_MOD_SPARSE); |
| 63 | |
Neil Armstrong | 17d3c8b | 2019-04-25 15:17:22 +0000 | [diff] [blame] | 64 | if (flags & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS) |
| 65 | features |= AFBC_FORMAT_MOD_TILED; |
| 66 | |
| 67 | if (features) |
| 68 | return DRM_FORMAT_MOD_ARM_AFBC(features | AFBC_FORMAT_MOD_YTR); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | #else |
| 73 | uint64_t MesonImporter::ConvertGrallocFormatToDrmModifiers( |
| 74 | uint64_t /* flags */) { |
| 75 | return 0; |
| 76 | } |
| 77 | #endif |
| 78 | |
| 79 | int MesonImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) { |
| 80 | uint64_t modifiers[4] = {0}; |
| 81 | |
| 82 | memset(bo, 0, sizeof(hwc_drm_bo_t)); |
| 83 | |
| 84 | private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>( |
| 85 | handle); |
| 86 | if (!hnd) |
| 87 | return -EINVAL; |
| 88 | |
| 89 | // We can't import these types of buffers. |
| 90 | // These buffers should have been filtered out with CanImportBuffer() |
| 91 | if (!(hnd->usage & GRALLOC_USAGE_HW_FB)) |
| 92 | return -EINVAL; |
| 93 | |
| 94 | uint32_t gem_handle; |
| 95 | int ret = drmPrimeFDToHandle(drm_->fd(), hnd->share_fd, &gem_handle); |
| 96 | if (ret) { |
| 97 | ALOGE("failed to import prime fd %d ret=%d", hnd->share_fd, ret); |
| 98 | return ret; |
| 99 | } |
| 100 | |
Roman Stratiienko | f63726c | 2019-11-06 15:03:12 +0200 | [diff] [blame^] | 101 | uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format); |
| 102 | if (fmt == DRM_FORMAT_INVALID) |
| 103 | return -EINVAL; |
Neil Armstrong | 17d3c8b | 2019-04-25 15:17:22 +0000 | [diff] [blame] | 104 | |
| 105 | modifiers[0] = ConvertGrallocFormatToDrmModifiers(hnd->internal_format); |
| 106 | |
| 107 | bo->width = hnd->width; |
| 108 | bo->height = hnd->height; |
| 109 | bo->hal_format = hnd->req_format; |
| 110 | bo->format = fmt; |
| 111 | bo->usage = hnd->usage; |
| 112 | bo->pixel_stride = hnd->stride; |
| 113 | bo->pitches[0] = hnd->byte_stride; |
| 114 | bo->gem_handles[0] = gem_handle; |
| 115 | bo->offsets[0] = 0; |
| 116 | |
| 117 | ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height, |
| 118 | bo->format, bo->gem_handles, bo->pitches, |
| 119 | bo->offsets, modifiers, &bo->fb_id, |
| 120 | modifiers[0] ? DRM_MODE_FB_MODIFIERS : 0); |
| 121 | |
| 122 | if (ret) { |
| 123 | ALOGE("could not create drm fb %d", ret); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | bool MesonImporter::CanImportBuffer(buffer_handle_t handle) { |
| 131 | private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>( |
| 132 | handle); |
| 133 | return hnd && (hnd->usage & GRALLOC_USAGE_HW_FB); |
| 134 | } |
| 135 | |
| 136 | std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) { |
| 137 | std::unique_ptr<Planner> planner(new Planner); |
| 138 | planner->AddStage<PlanStageGreedy>(); |
| 139 | return planner; |
| 140 | } |
| 141 | } // namespace android |