2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "hwc-nv-importer"
19 #include "drmresources.h"
21 #include "nvimporter.h"
23 #include <stdatomic.h>
25 #include <xf86drmMode.h>
27 #include <cutils/log.h>
28 #include <hardware/gralloc.h>
32 #ifdef USE_NVIDIA_IMPORTER
34 Importer *Importer::CreateInstance(DrmResources *drm) {
35 NvImporter *importer = new NvImporter(drm);
39 int ret = importer->Init();
41 ALOGE("Failed to initialize the nv importer %d", ret);
49 NvImporter::NvImporter(DrmResources *drm) : drm_(drm) {
52 NvImporter::~NvImporter() {
55 int NvImporter::Init() {
56 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
57 (const hw_module_t **)&gralloc_);
59 ALOGE("Failed to open gralloc module %d", ret);
63 if (strcasecmp(gralloc_->common.author, "NVIDIA"))
64 ALOGW("Using non-NVIDIA gralloc module: %s/%s\n", gralloc_->common.name,
65 gralloc_->common.author);
70 int NvImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
71 memset(bo, 0, sizeof(hwc_drm_bo_t));
72 NvBuffer_t *buf = GrallocGetNvBuffer(handle);
74 atomic_fetch_add(&buf->ref, 1);
79 buf = new NvBuffer_t();
81 ALOGE("Failed to allocate new NvBuffer_t");
87 // We initialize the reference count to 2 since NvGralloc is still using this
88 // buffer (will be cleared in the NvGrallocRelease), and the other
89 // reference is for HWC (this ImportBuffer call).
90 atomic_init(&buf->ref, 2);
92 int ret = gralloc_->perform(gralloc_, GRALLOC_MODULE_PERFORM_DRM_IMPORT,
93 drm_->fd(), handle, &buf->bo);
95 ALOGE("GRALLOC_MODULE_PERFORM_DRM_IMPORT failed %d", ret);
100 ret = drmModeAddFB2(drm_->fd(), buf->bo.width, buf->bo.height, buf->bo.format,
101 buf->bo.gem_handles, buf->bo.pitches, buf->bo.offsets,
104 ALOGE("Failed to add fb %d", ret);
105 ReleaseBufferImpl(&buf->bo);
110 ret = GrallocSetNvBuffer(handle, buf);
112 /* This will happen is persist.tegra.gpu_mapping_cache is 0/off,
113 * or if NV gralloc runs out of "priv slots" (currently 3 per buffer,
114 * only one of which should be used by drm_hwcomposer). */
115 ALOGE("Failed to register free callback for imported buffer %d", ret);
116 ReleaseBufferImpl(&buf->bo);
124 int NvImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
125 NvBuffer_t *buf = (NvBuffer_t *)bo->priv;
127 ALOGE("Freeing bo %ld, buf is NULL!", bo->fb_id);
130 if (atomic_fetch_sub(&buf->ref, 1) > 1)
133 ReleaseBufferImpl(bo);
139 void NvImporter::NvGrallocRelease(void *nv_buffer) {
140 NvBuffer_t *buf = (NvBuffer *)nv_buffer;
141 buf->importer->ReleaseBuffer(&buf->bo);
144 void NvImporter::ReleaseBufferImpl(hwc_drm_bo_t *bo) {
146 int ret = drmModeRmFB(drm_->fd(), bo->fb_id);
148 ALOGE("Failed to rm fb %d", ret);
151 struct drm_gem_close gem_close;
152 memset(&gem_close, 0, sizeof(gem_close));
153 int num_gem_handles = sizeof(bo->gem_handles) / sizeof(bo->gem_handles[0]);
154 for (int i = 0; i < num_gem_handles; i++) {
155 if (!bo->gem_handles[i])
158 gem_close.handle = bo->gem_handles[i];
159 int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
161 ALOGE("Failed to close gem handle %d %d", i, ret);
163 /* Clear any duplicate gem handle as well but don't close again */
164 for (int j = i + 1; j < num_gem_handles; j++)
165 if (bo->gem_handles[j] == bo->gem_handles[i])
166 bo->gem_handles[j] = 0;
167 bo->gem_handles[i] = 0;
172 NvImporter::NvBuffer_t *NvImporter::GrallocGetNvBuffer(buffer_handle_t handle) {
175 gralloc_->perform(gralloc_, GRALLOC_MODULE_PERFORM_GET_IMPORTER_PRIVATE,
176 handle, NvGrallocRelease, &priv);
177 return ret ? NULL : (NvBuffer_t *)priv;
180 int NvImporter::GrallocSetNvBuffer(buffer_handle_t handle, NvBuffer_t *buf) {
181 return gralloc_->perform(gralloc_,
182 GRALLOC_MODULE_PERFORM_SET_IMPORTER_PRIVATE, handle,
183 NvGrallocRelease, buf);