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-nv-importer"
18 
19 #include "drmresources.h"
20 #include "importer.h"
21 #include "nvimporter.h"
22 
23 #include <stdatomic.h>
24 #include <xf86drm.h>
25 #include <xf86drmMode.h>
26 
27 #include <cutils/log.h>
28 #include <hardware/gralloc.h>
29 
30 namespace android {
31 
32 #ifdef USE_NVIDIA_IMPORTER
33 // static
CreateInstance(DrmResources * drm)34 Importer *Importer::CreateInstance(DrmResources *drm) {
35   NvImporter *importer = new NvImporter(drm);
36   if (!importer)
37     return NULL;
38 
39   int ret = importer->Init();
40   if (ret) {
41     ALOGE("Failed to initialize the nv importer %d", ret);
42     delete importer;
43     return NULL;
44   }
45   return importer;
46 }
47 #endif
48 
NvImporter(DrmResources * drm)49 NvImporter::NvImporter(DrmResources *drm) : drm_(drm) {
50 }
51 
~NvImporter()52 NvImporter::~NvImporter() {
53 }
54 
Init()55 int NvImporter::Init() {
56   int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
57                           (const hw_module_t **)&gralloc_);
58   if (ret) {
59     ALOGE("Failed to open gralloc module %d", ret);
60     return ret;
61   }
62 
63   if (strcasecmp(gralloc_->common.author, "NVIDIA"))
64     ALOGW("Using non-NVIDIA gralloc module: %s/%s\n", gralloc_->common.name,
65           gralloc_->common.author);
66 
67   return 0;
68 }
69 
ImportBuffer(buffer_handle_t handle,hwc_drm_bo_t * bo)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);
73   if (buf) {
74     atomic_fetch_add(&buf->ref, 1);
75     *bo = buf->bo;
76     return 0;
77   }
78 
79   buf = new NvBuffer_t();
80   if (!buf) {
81     ALOGE("Failed to allocate new NvBuffer_t");
82     return -ENOMEM;
83   }
84   buf->bo.priv = buf;
85   buf->importer = this;
86 
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);
91 
92   int ret = gralloc_->perform(gralloc_, GRALLOC_MODULE_PERFORM_DRM_IMPORT,
93                               drm_->fd(), handle, &buf->bo);
94   if (ret) {
95     ALOGE("GRALLOC_MODULE_PERFORM_DRM_IMPORT failed %d", ret);
96     delete buf;
97     return ret;
98   }
99 
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,
102                       &buf->bo.fb_id, 0);
103   if (ret) {
104     ALOGE("Failed to add fb %d", ret);
105     ReleaseBufferImpl(&buf->bo);
106     delete buf;
107     return ret;
108   }
109 
110   ret = GrallocSetNvBuffer(handle, buf);
111   if (ret) {
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);
117     delete buf;
118     return ret;
119   }
120   *bo = buf->bo;
121   return 0;
122 }
123 
ReleaseBuffer(hwc_drm_bo_t * bo)124 int NvImporter::ReleaseBuffer(hwc_drm_bo_t * bo) {
125   NvBuffer_t *buf = (NvBuffer_t *)bo->priv;
126   if (!buf) {
127     ALOGE("Freeing bo %ld, buf is NULL!", bo->fb_id);
128     return 0;
129   }
130   if (atomic_fetch_sub(&buf->ref, 1) > 1)
131     return 0;
132 
133   ReleaseBufferImpl(bo);
134   delete buf;
135   return 0;
136 }
137 
138 // static
NvGrallocRelease(void * nv_buffer)139 void NvImporter::NvGrallocRelease(void *nv_buffer) {
140   NvBuffer_t *buf = (NvBuffer *)nv_buffer;
141   buf->importer->ReleaseBuffer(&buf->bo);
142 }
143 
ReleaseBufferImpl(hwc_drm_bo_t * bo)144 void NvImporter::ReleaseBufferImpl(hwc_drm_bo_t *bo) {
145   if (bo->fb_id) {
146     int ret = drmModeRmFB(drm_->fd(), bo->fb_id);
147     if (ret)
148       ALOGE("Failed to rm fb %d", ret);
149   }
150 
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])
156       continue;
157 
158     gem_close.handle = bo->gem_handles[i];
159     int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
160     if (ret)
161       ALOGE("Failed to close gem handle %d %d", i, ret);
162     else
163       bo->gem_handles[i] = 0;
164   }
165 }
166 
GrallocGetNvBuffer(buffer_handle_t handle)167 NvImporter::NvBuffer_t *NvImporter::GrallocGetNvBuffer(buffer_handle_t handle) {
168   void *priv = NULL;
169   int ret =
170       gralloc_->perform(gralloc_, GRALLOC_MODULE_PERFORM_GET_IMPORTER_PRIVATE,
171                         handle, NvGrallocRelease, &priv);
172   return ret ? NULL : (NvBuffer_t *)priv;
173 }
174 
GrallocSetNvBuffer(buffer_handle_t handle,NvBuffer_t * buf)175 int NvImporter::GrallocSetNvBuffer(buffer_handle_t handle, NvBuffer_t *buf) {
176   return gralloc_->perform(gralloc_,
177                            GRALLOC_MODULE_PERFORM_SET_IMPORTER_PRIVATE, handle,
178                            NvGrallocRelease, buf);
179 }
180 }
181