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-platform-nv"
18
19 #include "drmresources.h"
20 #include "platform.h"
21 #include "platformnv.h"
22
23 #include <cinttypes>
24 #include <stdatomic.h>
25 #include <drm/drm_fourcc.h>
26 #include <xf86drm.h>
27 #include <xf86drmMode.h>
28
29 #include <cutils/log.h>
30 #include <hardware/gralloc.h>
31
32 namespace android {
33
34 #ifdef USE_NVIDIA_IMPORTER
35 // static
CreateInstance(DrmResources * drm)36 Importer *Importer::CreateInstance(DrmResources *drm) {
37 NvImporter *importer = new NvImporter(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
NvImporter(DrmResources * drm)51 NvImporter::NvImporter(DrmResources *drm) : drm_(drm) {
52 }
53
~NvImporter()54 NvImporter::~NvImporter() {
55 }
56
Init()57 int NvImporter::Init() {
58 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
59 (const hw_module_t **)&gralloc_);
60 if (ret) {
61 ALOGE("Failed to open gralloc module %d", ret);
62 return ret;
63 }
64
65 if (strcasecmp(gralloc_->common.author, "NVIDIA"))
66 ALOGW("Using non-NVIDIA gralloc module: %s/%s\n", gralloc_->common.name,
67 gralloc_->common.author);
68
69 return 0;
70 }
71
ImportBuffer(buffer_handle_t handle,hwc_drm_bo_t * bo)72 int NvImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
73 memset(bo, 0, sizeof(hwc_drm_bo_t));
74 NvBuffer_t *buf = GrallocGetNvBuffer(handle);
75 if (buf) {
76 atomic_fetch_add(&buf->ref, 1);
77 *bo = buf->bo;
78 return 0;
79 }
80
81 buf = new NvBuffer_t();
82 if (!buf) {
83 ALOGE("Failed to allocate new NvBuffer_t");
84 return -ENOMEM;
85 }
86 buf->bo.priv = buf;
87 buf->importer = this;
88
89 // We initialize the reference count to 2 since NvGralloc is still using this
90 // buffer (will be cleared in the NvGrallocRelease), and the other
91 // reference is for HWC (this ImportBuffer call).
92 atomic_init(&buf->ref, 2);
93
94 int ret = gralloc_->perform(gralloc_, GRALLOC_MODULE_PERFORM_DRM_IMPORT,
95 drm_->fd(), handle, &buf->bo);
96 if (ret) {
97 ALOGE("GRALLOC_MODULE_PERFORM_DRM_IMPORT failed %d", ret);
98 delete buf;
99 return ret;
100 }
101
102 ret = drmModeAddFB2(drm_->fd(), buf->bo.width, buf->bo.height, buf->bo.format,
103 buf->bo.gem_handles, buf->bo.pitches, buf->bo.offsets,
104 &buf->bo.fb_id, 0);
105 if (ret) {
106 ALOGE("Failed to add fb %d", ret);
107 ReleaseBufferImpl(&buf->bo);
108 delete buf;
109 return ret;
110 }
111
112 ret = GrallocSetNvBuffer(handle, buf);
113 if (ret) {
114 /* This will happen is persist.tegra.gpu_mapping_cache is 0/off,
115 * or if NV gralloc runs out of "priv slots" (currently 3 per buffer,
116 * only one of which should be used by drm_hwcomposer). */
117 ALOGE("Failed to register free callback for imported buffer %d", ret);
118 ReleaseBufferImpl(&buf->bo);
119 delete buf;
120 return ret;
121 }
122 *bo = buf->bo;
123 return 0;
124 }
125
ReleaseBuffer(hwc_drm_bo_t * bo)126 int NvImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
127 NvBuffer_t *buf = (NvBuffer_t *)bo->priv;
128 if (!buf) {
129 ALOGE("Freeing bo %" PRIu32 ", buf is NULL!", bo->fb_id);
130 return 0;
131 }
132 if (atomic_fetch_sub(&buf->ref, 1) > 1)
133 return 0;
134
135 ReleaseBufferImpl(bo);
136 delete buf;
137 return 0;
138 }
139
140 // static
NvGrallocRelease(void * nv_buffer)141 void NvImporter::NvGrallocRelease(void *nv_buffer) {
142 NvBuffer_t *buf = (NvBuffer *)nv_buffer;
143 buf->importer->ReleaseBuffer(&buf->bo);
144 }
145
ReleaseBufferImpl(hwc_drm_bo_t * bo)146 void NvImporter::ReleaseBufferImpl(hwc_drm_bo_t *bo) {
147 if (bo->fb_id) {
148 int ret = drmModeRmFB(drm_->fd(), bo->fb_id);
149 if (ret)
150 ALOGE("Failed to rm fb %d", ret);
151 }
152
153 struct drm_gem_close gem_close;
154 memset(&gem_close, 0, sizeof(gem_close));
155 int num_gem_handles = sizeof(bo->gem_handles) / sizeof(bo->gem_handles[0]);
156 for (int i = 0; i < num_gem_handles; i++) {
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);
162 if (ret) {
163 ALOGE("Failed to close gem handle %d %d", i, ret);
164 } else {
165 /* Clear any duplicate gem handle as well but don't close again */
166 for (int j = i + 1; j < num_gem_handles; j++)
167 if (bo->gem_handles[j] == bo->gem_handles[i])
168 bo->gem_handles[j] = 0;
169 bo->gem_handles[i] = 0;
170 }
171 }
172 }
173
GrallocGetNvBuffer(buffer_handle_t handle)174 NvImporter::NvBuffer_t *NvImporter::GrallocGetNvBuffer(buffer_handle_t handle) {
175 void *priv = NULL;
176 int ret =
177 gralloc_->perform(gralloc_, GRALLOC_MODULE_PERFORM_GET_IMPORTER_PRIVATE,
178 handle, NvGrallocRelease, &priv);
179 return ret ? NULL : (NvBuffer_t *)priv;
180 }
181
GrallocSetNvBuffer(buffer_handle_t handle,NvBuffer_t * buf)182 int NvImporter::GrallocSetNvBuffer(buffer_handle_t handle, NvBuffer_t *buf) {
183 return gralloc_->perform(gralloc_,
184 GRALLOC_MODULE_PERFORM_SET_IMPORTER_PRIVATE, handle,
185 NvGrallocRelease, buf);
186 }
187
188 #ifdef USE_NVIDIA_IMPORTER
189 // static
CreateInstance(DrmResources *)190 std::unique_ptr<Planner> Planner::CreateInstance(DrmResources *) {
191 std::unique_ptr<Planner> planner(new Planner);
192 planner->AddStage<PlanStageNvLimits>();
193 planner->AddStage<PlanStageProtectedRotated>();
194 planner->AddStage<PlanStageProtected>();
195 planner->AddStage<PlanStagePrecomp>();
196 planner->AddStage<PlanStageGreedy>();
197 return planner;
198 }
199 #endif
200
GetCrtcPrimaryPlane(DrmCrtc * crtc,std::vector<DrmPlane * > * planes)201 static DrmPlane *GetCrtcPrimaryPlane(DrmCrtc *crtc,
202 std::vector<DrmPlane *> *planes) {
203 for (auto i = planes->begin(); i != planes->end(); ++i) {
204 if ((*i)->GetCrtcSupported(*crtc)) {
205 DrmPlane *plane = *i;
206 planes->erase(i);
207 return plane;
208 }
209 }
210 return NULL;
211 }
212
ProvisionPlanes(std::vector<DrmCompositionPlane> * composition,std::map<size_t,DrmHwcLayer * > & layers,DrmCrtc * crtc,std::vector<DrmPlane * > * planes)213 int PlanStageProtectedRotated::ProvisionPlanes(
214 std::vector<DrmCompositionPlane> *composition,
215 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
216 std::vector<DrmPlane *> *planes) {
217 int ret;
218 int protected_zorder = -1;
219 for (auto i = layers.begin(); i != layers.end();) {
220 if (!i->second->protected_usage() || !i->second->transform) {
221 ++i;
222 continue;
223 }
224
225 auto primary_iter = planes->begin();
226 for (; primary_iter != planes->end(); ++primary_iter) {
227 if ((*primary_iter)->type() == DRM_PLANE_TYPE_PRIMARY)
228 break;
229 }
230
231 // We cheat a little here. Since there can only be one primary plane per
232 // crtc, we know we'll only hit this case once. So we blindly insert the
233 // protected content at the beginning of the composition, knowing this path
234 // won't be taken a second time during the loop.
235 if (primary_iter != planes->end()) {
236 composition->emplace(composition->begin(),
237 DrmCompositionPlane::Type::kLayer, *primary_iter,
238 crtc, i->first);
239 planes->erase(primary_iter);
240 protected_zorder = i->first;
241 } else {
242 ALOGE("Could not provision primary plane for protected/rotated layer");
243 }
244 i = layers.erase(i);
245 }
246
247 if (protected_zorder == -1)
248 return 0;
249
250 // Add any layers below the protected content to the precomposition since we
251 // need to punch a hole through them.
252 for (auto i = layers.begin(); i != layers.end();) {
253 // Skip layers above the z-order of the protected content
254 if (i->first > static_cast<size_t>(protected_zorder)) {
255 ++i;
256 continue;
257 }
258
259 // If there's no precomp layer already queued, queue one now.
260 DrmCompositionPlane *precomp = GetPrecomp(composition);
261 if (precomp) {
262 precomp->source_layers().emplace_back(i->first);
263 } else {
264 if (planes->size()) {
265 DrmPlane *precomp_plane = planes->back();
266 planes->pop_back();
267 composition->emplace_back(DrmCompositionPlane::Type::kPrecomp,
268 precomp_plane, crtc, i->first);
269 } else {
270 ALOGE("Not enough planes to reserve for precomp fb");
271 }
272 }
273 i = layers.erase(i);
274 }
275 return 0;
276 }
277
CheckLayer(size_t zorder,DrmHwcLayer * layer)278 bool PlanStageNvLimits::CheckLayer(size_t zorder, DrmHwcLayer *layer) {
279 auto src_w = layer->source_crop.width();
280 auto src_h = layer->source_crop.height();
281 auto dst_w = layer->display_frame.width();
282 auto dst_h = layer->display_frame.height();
283 int h_limit = 4;
284 int v_limit;
285
286 switch (layer->buffer->format) {
287 case DRM_FORMAT_ARGB8888:
288 case DRM_FORMAT_ABGR8888:
289 case DRM_FORMAT_XBGR8888:
290 case DRM_FORMAT_XRGB8888:
291 // tegra driver assumes any layer with alpha channel has premult
292 // blending, avoid handling it this is not the case. This is not an
293 // issue for bottom-most layer since there's nothing to blend with
294 if (zorder > 0 && layer->blending != DrmHwcBlending::kPreMult)
295 return false;
296
297 v_limit = 2;
298 break;
299 case DRM_FORMAT_YVU420:
300 case DRM_FORMAT_YUV420:
301 case DRM_FORMAT_YUV422:
302 case DRM_FORMAT_UYVY:
303 case DRM_FORMAT_YUYV:
304 case DRM_FORMAT_NV12:
305 case DRM_FORMAT_NV21:
306 case DRM_FORMAT_RGB565:
307 case DRM_FORMAT_BGR565:
308 v_limit = 4;
309 break;
310 default:
311 v_limit = 2;
312 break;
313 }
314
315 if (layer->transform &
316 (DrmHwcTransform::kRotate90 | DrmHwcTransform::kRotate270))
317 std::swap(dst_w, dst_h);
318
319 // check for max supported down scaling
320 if (((src_w / dst_w) > h_limit) || ((src_h / dst_h) > v_limit))
321 return false;
322
323 return true;
324 }
325
ProvisionPlanes(std::vector<DrmCompositionPlane> * composition,std::map<size_t,DrmHwcLayer * > & layers,DrmCrtc * crtc,std::vector<DrmPlane * > * planes)326 int PlanStageNvLimits::ProvisionPlanes(
327 std::vector<DrmCompositionPlane> *composition,
328 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
329 std::vector<DrmPlane *> *planes) {
330 int ret;
331
332 for (auto i = layers.begin(); i != layers.end();) {
333 // Skip layer if supported
334 if (CheckLayer(i->first, i->second)) {
335 i++;
336 continue;
337 }
338
339 if (i->second->protected_usage()) {
340 // Drop the layer if unsupported and protected, this will just display
341 // black in the area of this layer but it's better than failing miserably
342 i = layers.erase(i);
343 continue;
344 }
345
346 // If there's no precomp layer already queued, queue one now.
347 DrmCompositionPlane *precomp = GetPrecomp(composition);
348 if (precomp) {
349 precomp->source_layers().emplace_back(i->first);
350 } else if (!planes->empty()) {
351 DrmPlane *precomp_plane = planes->back();
352 planes->pop_back();
353 composition->emplace_back(DrmCompositionPlane::Type::kPrecomp,
354 precomp_plane, crtc, i->first);
355 } else {
356 ALOGE("Not enough planes to reserve for precomp fb");
357 }
358 i = layers.erase(i);
359 }
360
361 return 0;
362 }
363 }
364