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> |
Adrian Salido | ee24aca | 2017-07-17 17:58:50 -0700 | [diff] [blame] | 25 | #include <drm/drm_fourcc.h> |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 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 |
| 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 | |
| 51 | NvImporter::NvImporter(DrmResources *drm) : drm_(drm) { |
| 52 | } |
| 53 | |
| 54 | NvImporter::~NvImporter() { |
| 55 | } |
| 56 | |
| 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 | |
| 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) { |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 76 | atomic_fetch_add(&buf->ref, 1); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 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 | } |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 86 | buf->bo.priv = buf; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 87 | buf->importer = this; |
| 88 | |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 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 | |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 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 | |
Zach Reizner | c6520e4 | 2015-08-13 14:32:09 -0700 | [diff] [blame] | 126 | int NvImporter::ReleaseBuffer(hwc_drm_bo_t *bo) { |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 127 | NvBuffer_t *buf = (NvBuffer_t *)bo->priv; |
| 128 | if (!buf) { |
Sean Paul | 5325e10 | 2016-03-29 13:55:35 -0400 | [diff] [blame] | 129 | ALOGE("Freeing bo %" PRIu32 ", buf is NULL!", bo->fb_id); |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | if (atomic_fetch_sub(&buf->ref, 1) > 1) |
| 133 | return 0; |
| 134 | |
| 135 | ReleaseBufferImpl(bo); |
| 136 | delete buf; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | // static |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 141 | void NvImporter::NvGrallocRelease(void *nv_buffer) { |
| 142 | NvBuffer_t *buf = (NvBuffer *)nv_buffer; |
| 143 | buf->importer->ReleaseBuffer(&buf->bo); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 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); |
Isaac Simha | 6e02c9d | 2015-10-14 11:40:29 -0700 | [diff] [blame] | 162 | if (ret) { |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 163 | ALOGE("Failed to close gem handle %d %d", i, ret); |
Isaac Simha | 6e02c9d | 2015-10-14 11:40:29 -0700 | [diff] [blame] | 164 | } else { |
| 165 | /* Clear any duplicate gem handle as well but don't close again */ |
Haixia Shi | 479412c | 2015-10-27 10:40:48 -0700 | [diff] [blame] | 166 | for (int j = i + 1; j < num_gem_handles; j++) |
| 167 | if (bo->gem_handles[j] == bo->gem_handles[i]) |
Isaac Simha | 6e02c9d | 2015-10-14 11:40:29 -0700 | [diff] [blame] | 168 | bo->gem_handles[j] = 0; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 169 | bo->gem_handles[i] = 0; |
Isaac Simha | 6e02c9d | 2015-10-14 11:40:29 -0700 | [diff] [blame] | 170 | } |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 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, |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 178 | handle, NvGrallocRelease, &priv); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 179 | return ret ? NULL : (NvBuffer_t *)priv; |
| 180 | } |
| 181 | |
| 182 | int NvImporter::GrallocSetNvBuffer(buffer_handle_t handle, NvBuffer_t *buf) { |
| 183 | return gralloc_->perform(gralloc_, |
| 184 | GRALLOC_MODULE_PERFORM_SET_IMPORTER_PRIVATE, handle, |
Sean Paul | 419b5e0 | 2015-06-10 14:30:47 -0400 | [diff] [blame] | 185 | NvGrallocRelease, buf); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 186 | } |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 187 | |
| 188 | #ifdef USE_NVIDIA_IMPORTER |
| 189 | // static |
| 190 | std::unique_ptr<Planner> Planner::CreateInstance(DrmResources *) { |
| 191 | std::unique_ptr<Planner> planner(new Planner); |
Adrian Salido | ee24aca | 2017-07-17 17:58:50 -0700 | [diff] [blame] | 192 | planner->AddStage<PlanStageNvLimits>(); |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 193 | planner->AddStage<PlanStageProtectedRotated>(); |
| 194 | planner->AddStage<PlanStageProtected>(); |
Adrian Salido | 4500232 | 2017-04-10 21:44:21 -0700 | [diff] [blame] | 195 | planner->AddStage<PlanStagePrecomp>(); |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 196 | planner->AddStage<PlanStageGreedy>(); |
| 197 | return planner; |
| 198 | } |
| 199 | #endif |
| 200 | |
| 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 | |
| 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 | } |
Adrian Salido | ee24aca | 2017-07-17 17:58:50 -0700 | [diff] [blame] | 277 | |
Adrian Salido | a791258 | 2017-08-21 17:07:18 -0700 | [diff] [blame^] | 278 | bool PlanStageNvLimits::CheckLayer(size_t zorder, DrmHwcLayer *layer) { |
Adrian Salido | ee24aca | 2017-07-17 17:58:50 -0700 | [diff] [blame] | 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) { |
Adrian Salido | a791258 | 2017-08-21 17:07:18 -0700 | [diff] [blame^] | 287 | case DRM_FORMAT_ARGB8888: |
| 288 | case DRM_FORMAT_ABGR8888: |
| 289 | case DRM_FORMAT_XBGR8888: |
| 290 | // tegra driver assumes any layer with alpha channel has premult |
| 291 | // blending, avoid handling it this is not the case. This is not an |
| 292 | // issue for bottom-most layer since there's nothing to blend with |
| 293 | if (zorder > 0 && layer->blending != DrmHwcBlending::kPreMult) |
| 294 | return false; |
| 295 | |
| 296 | v_limit = 2; |
| 297 | break; |
Adrian Salido | ee24aca | 2017-07-17 17:58:50 -0700 | [diff] [blame] | 298 | case DRM_FORMAT_YVU420: |
| 299 | case DRM_FORMAT_BGR565: |
| 300 | v_limit = 4; |
| 301 | break; |
| 302 | default: |
| 303 | v_limit = 2; |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | if (layer->transform & |
| 308 | (DrmHwcTransform::kRotate90 | DrmHwcTransform::kRotate270)) |
| 309 | std::swap(dst_w, dst_h); |
| 310 | |
| 311 | // check for max supported down scaling |
| 312 | if (((src_w / dst_w) > h_limit) || ((src_h / dst_h) > v_limit)) |
| 313 | return false; |
| 314 | |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | int PlanStageNvLimits::ProvisionPlanes( |
| 319 | std::vector<DrmCompositionPlane> *composition, |
| 320 | std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc, |
| 321 | std::vector<DrmPlane *> *planes) { |
| 322 | int ret; |
| 323 | |
| 324 | for (auto i = layers.begin(); i != layers.end();) { |
| 325 | // Skip layer if supported |
Adrian Salido | a791258 | 2017-08-21 17:07:18 -0700 | [diff] [blame^] | 326 | if (CheckLayer(i->first, i->second)) { |
Adrian Salido | ee24aca | 2017-07-17 17:58:50 -0700 | [diff] [blame] | 327 | i++; |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | if (i->second->protected_usage()) { |
| 332 | // Drop the layer if unsupported and protected, this will just display |
| 333 | // black in the area of this layer but it's better than failing miserably |
| 334 | i = layers.erase(i); |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | // If there's no precomp layer already queued, queue one now. |
| 339 | DrmCompositionPlane *precomp = GetPrecomp(composition); |
| 340 | if (precomp) { |
| 341 | precomp->source_layers().emplace_back(i->first); |
| 342 | } else if (!planes->empty()) { |
| 343 | DrmPlane *precomp_plane = planes->back(); |
| 344 | planes->pop_back(); |
| 345 | composition->emplace_back(DrmCompositionPlane::Type::kPrecomp, |
| 346 | precomp_plane, crtc, i->first); |
| 347 | } else { |
| 348 | ALOGE("Not enough planes to reserve for precomp fb"); |
| 349 | } |
| 350 | i = layers.erase(i); |
| 351 | } |
| 352 | |
| 353 | return 0; |
| 354 | } |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 355 | } |