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-nv" |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 18 | |
| 19 | #include "drmresources.h" |
Sean Paul | 6376996 | 2016-04-21 16:25:06 -0400 | [diff] [blame] | 20 | #include "platform.h" |
Sean Paul | ea045b7 | 2016-04-21 16:39:02 -0400 | [diff] [blame] | 21 | #include "platformnv.h" |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 22 | |
Sean Paul | 5325e10 | 2016-03-29 13:55:35 -0400 | [diff] [blame] | 23 | #include <cinttypes> |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 24 | #include <stdatomic.h> |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 25 | #include <xf86drm.h> |
| 26 | #include <xf86drmMode.h> |
| 27 | |
| 28 | #include <cutils/log.h> |
| 29 | #include <hardware/gralloc.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | #ifdef USE_NVIDIA_IMPORTER |
| 34 | // static |
| 35 | Importer *Importer::CreateInstance(DrmResources *drm) { |
| 36 | NvImporter *importer = new NvImporter(drm); |
| 37 | if (!importer) |
| 38 | return NULL; |
| 39 | |
| 40 | int ret = importer->Init(); |
| 41 | if (ret) { |
| 42 | ALOGE("Failed to initialize the nv importer %d", ret); |
| 43 | delete importer; |
| 44 | return NULL; |
| 45 | } |
| 46 | return importer; |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | NvImporter::NvImporter(DrmResources *drm) : drm_(drm) { |
| 51 | } |
| 52 | |
| 53 | NvImporter::~NvImporter() { |
| 54 | } |
| 55 | |
| 56 | int NvImporter::Init() { |
| 57 | int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, |
| 58 | (const hw_module_t **)&gralloc_); |
| 59 | if (ret) { |
| 60 | ALOGE("Failed to open gralloc module %d", ret); |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | if (strcasecmp(gralloc_->common.author, "NVIDIA")) |
| 65 | ALOGW("Using non-NVIDIA gralloc module: %s/%s\n", gralloc_->common.name, |
| 66 | gralloc_->common.author); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int NvImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) { |
| 72 | memset(bo, 0, sizeof(hwc_drm_bo_t)); |
| 73 | NvBuffer_t *buf = GrallocGetNvBuffer(handle); |
| 74 | if (buf) { |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 75 | atomic_fetch_add(&buf->ref, 1); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 76 | *bo = buf->bo; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | buf = new NvBuffer_t(); |
| 81 | if (!buf) { |
| 82 | ALOGE("Failed to allocate new NvBuffer_t"); |
| 83 | return -ENOMEM; |
| 84 | } |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 85 | buf->bo.priv = buf; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 86 | buf->importer = this; |
| 87 | |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 88 | // We initialize the reference count to 2 since NvGralloc is still using this |
| 89 | // buffer (will be cleared in the NvGrallocRelease), and the other |
| 90 | // reference is for HWC (this ImportBuffer call). |
| 91 | atomic_init(&buf->ref, 2); |
| 92 | |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 93 | int ret = gralloc_->perform(gralloc_, GRALLOC_MODULE_PERFORM_DRM_IMPORT, |
| 94 | drm_->fd(), handle, &buf->bo); |
| 95 | if (ret) { |
| 96 | ALOGE("GRALLOC_MODULE_PERFORM_DRM_IMPORT failed %d", ret); |
| 97 | delete buf; |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | ret = drmModeAddFB2(drm_->fd(), buf->bo.width, buf->bo.height, buf->bo.format, |
| 102 | buf->bo.gem_handles, buf->bo.pitches, buf->bo.offsets, |
| 103 | &buf->bo.fb_id, 0); |
| 104 | if (ret) { |
| 105 | ALOGE("Failed to add fb %d", ret); |
| 106 | ReleaseBufferImpl(&buf->bo); |
| 107 | delete buf; |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | ret = GrallocSetNvBuffer(handle, buf); |
| 112 | if (ret) { |
| 113 | /* This will happen is persist.tegra.gpu_mapping_cache is 0/off, |
| 114 | * or if NV gralloc runs out of "priv slots" (currently 3 per buffer, |
| 115 | * only one of which should be used by drm_hwcomposer). */ |
| 116 | ALOGE("Failed to register free callback for imported buffer %d", ret); |
| 117 | ReleaseBufferImpl(&buf->bo); |
| 118 | delete buf; |
| 119 | return ret; |
| 120 | } |
| 121 | *bo = buf->bo; |
| 122 | return 0; |
| 123 | } |
| 124 | |
Zach Reizner | c6520e4 | 2015-08-13 14:32:09 -0700 | [diff] [blame] | 125 | int NvImporter::ReleaseBuffer(hwc_drm_bo_t *bo) { |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 126 | NvBuffer_t *buf = (NvBuffer_t *)bo->priv; |
| 127 | if (!buf) { |
Sean Paul | 5325e10 | 2016-03-29 13:55:35 -0400 | [diff] [blame] | 128 | ALOGE("Freeing bo %" PRIu32 ", buf is NULL!", bo->fb_id); |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | if (atomic_fetch_sub(&buf->ref, 1) > 1) |
| 132 | return 0; |
| 133 | |
| 134 | ReleaseBufferImpl(bo); |
| 135 | delete buf; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | // static |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 140 | void NvImporter::NvGrallocRelease(void *nv_buffer) { |
| 141 | NvBuffer_t *buf = (NvBuffer *)nv_buffer; |
| 142 | buf->importer->ReleaseBuffer(&buf->bo); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void NvImporter::ReleaseBufferImpl(hwc_drm_bo_t *bo) { |
| 146 | if (bo->fb_id) { |
| 147 | int ret = drmModeRmFB(drm_->fd(), bo->fb_id); |
| 148 | if (ret) |
| 149 | ALOGE("Failed to rm fb %d", ret); |
| 150 | } |
| 151 | |
| 152 | struct drm_gem_close gem_close; |
| 153 | memset(&gem_close, 0, sizeof(gem_close)); |
| 154 | int num_gem_handles = sizeof(bo->gem_handles) / sizeof(bo->gem_handles[0]); |
| 155 | for (int i = 0; i < num_gem_handles; i++) { |
| 156 | if (!bo->gem_handles[i]) |
| 157 | continue; |
| 158 | |
| 159 | gem_close.handle = bo->gem_handles[i]; |
| 160 | int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close); |
Isaac Simha | 6e02c9d | 2015-10-14 11:40:29 -0700 | [diff] [blame] | 161 | if (ret) { |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 162 | ALOGE("Failed to close gem handle %d %d", i, ret); |
Isaac Simha | 6e02c9d | 2015-10-14 11:40:29 -0700 | [diff] [blame] | 163 | } else { |
| 164 | /* Clear any duplicate gem handle as well but don't close again */ |
Haixia Shi | 479412c | 2015-10-27 10:40:48 -0700 | [diff] [blame] | 165 | for (int j = i + 1; j < num_gem_handles; j++) |
| 166 | if (bo->gem_handles[j] == bo->gem_handles[i]) |
Isaac Simha | 6e02c9d | 2015-10-14 11:40:29 -0700 | [diff] [blame] | 167 | bo->gem_handles[j] = 0; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 168 | bo->gem_handles[i] = 0; |
Isaac Simha | 6e02c9d | 2015-10-14 11:40:29 -0700 | [diff] [blame] | 169 | } |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | NvImporter::NvBuffer_t *NvImporter::GrallocGetNvBuffer(buffer_handle_t handle) { |
| 174 | void *priv = NULL; |
| 175 | int ret = |
| 176 | gralloc_->perform(gralloc_, GRALLOC_MODULE_PERFORM_GET_IMPORTER_PRIVATE, |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 177 | handle, NvGrallocRelease, &priv); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 178 | return ret ? NULL : (NvBuffer_t *)priv; |
| 179 | } |
| 180 | |
| 181 | int NvImporter::GrallocSetNvBuffer(buffer_handle_t handle, NvBuffer_t *buf) { |
| 182 | return gralloc_->perform(gralloc_, |
| 183 | GRALLOC_MODULE_PERFORM_SET_IMPORTER_PRIVATE, handle, |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 184 | NvGrallocRelease, buf); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 185 | } |
| 186 | } |