/* * Copyright (C) 2024 Amlogic Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include "MesonDisplayPipe.h" #include "DispAdptLog.h" #define MESON_DRM_DRIVER_NAME "meson" #define MAX_PLANE_LIMIT 4 MesonDisplayPipe::MesonDisplayPipe(int dispId) { mPipeId = dispId; mDrmFd = -1; mEnablePrimaryPlane = false; init(); } MesonDisplayPipe::~MesonDisplayPipe() { if (mDrmFd >= 0) close(mDrmFd); } int32_t MesonDisplayPipe::init() { int ret; ret = openDrmDevice(); DISPLAY_ASSERT(ret == 0, "Open drm device %s failed.", MESON_DRM_DRIVER_NAME); drmDropMaster(mDrmFd); loadDrmResources(); return 0; } int32_t MesonDisplayPipe::openDrmDevice() { if (mDrmFd >= 0) return 0; mDrmFd = drmOpen(MESON_DRM_DRIVER_NAME, NULL); DISPLAY_ASSERT(mDrmFd >= 0, "Open drm device %s failed.", MESON_DRM_DRIVER_NAME); return 0; } int32_t MesonDisplayPipe::loadDrmResources() { int ret = drmSetClientCap(mDrmFd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); DISPLAY_ASSERT(ret == 0, "DRM_CLIENT_CAP_UNIVERSAL_PLANES failed(%d).", ret); ret = drmSetClientCap(mDrmFd, DRM_CLIENT_CAP_ATOMIC, 1); DISPLAY_ASSERT(ret == 0, "DRM_CLIENT_CAP_ATOMIC failed (%d).", ret); /* load crtc & connector & encoder. */ drmModeResPtr drmRes = drmModeGetResources(mDrmFd); DISPLAY_ASSERT(drmRes != 0, "drmModeGetResources failed()."); /* Crtc */ for (int i = 0; i < drmRes->count_crtcs; i++) { drmModeCrtcPtr metadata = drmModeGetCrtc( mDrmFd, drmRes->crtcs[i]); std::shared_ptr crtc = std::make_shared(mDrmFd, metadata, i); mCrtcs.emplace(crtc->getId(), std::move(crtc)); drmModeFreeCrtc(metadata); } /* Encoder */ for (int i = 0; i < drmRes->count_encoders; i++) { drmModeEncoderPtr metadata = drmModeGetEncoder( mDrmFd, drmRes->encoders[i]); std::shared_ptr encoder = std::make_shared(mDrmFd, metadata); mEncoders.emplace(encoder->getId(), std::move(encoder)); drmModeFreeEncoder(metadata); } /*Connectors*/ for (int i = 0; i < drmRes->count_connectors; i ++) { drmModeConnectorPtr metadata = drmModeGetConnector( mDrmFd,drmRes->connectors[i]); std::shared_ptr connector = std::make_shared(mDrmFd, metadata); mConnectors.emplace(connector->getId(), std::move(connector)); drmModeFreeConnector(metadata); } drmModeFreeResources(drmRes); /* load Drm planes */ std::map> planes; bool primary = true; drmModePlaneResPtr planeRes = drmModeGetPlaneResources(mDrmFd); for (uint32_t i = 0; i < planeRes->count_planes; i ++) { drmModePlanePtr metadata = drmModeGetPlane( mDrmFd, planeRes->planes[i]); for (uint32_t pfi= 0; pfi < metadata->count_formats; ++pfi ) { if (DRM_FORMAT_NV21 == metadata->formats[pfi]) { DISPLAY_LOGI("Load a drm video plane %d", pfi); std::shared_ptr plane = std::make_shared(mDrmFd, metadata); plane->setPrimary(primary); if (primary) primary = false; planes.emplace(plane->getId(), std::move(plane)); break; } } drmModeFreePlane(metadata); } drmModeFreePlaneResources(planeRes); std::shared_ptr plane; for (auto planeIt = planes.begin(); planeIt != planes.end(); planeIt++) { plane = planeIt->second; if (plane->getPossibleCrtcs() == (uint32_t)(1 << mPipeId)) { mPlanesForPipe.insert(std::make_pair(mPipeId, plane)); } } return 0; } void MesonDisplayPipe::enablePrimaryPlane() { mEnablePrimaryPlane = true; } int32_t MesonDisplayPipe::getCrtc(std::shared_ptr & crtc) { for (const auto & it : mCrtcs) { if (it.second->getPipe() == (uint32_t)mPipeId) mCrtc = it.second; } crtc = mCrtc; return 0; } void MesonDisplayPipe::getSize(int32_t &width, int32_t &height) { std::shared_ptr crtc; width = 0; height = 0; getCrtc(crtc); if (crtc.get()) { drmModeCrtcPtr metadata = drmModeGetCrtc(mDrmFd, crtc->getId()); if (metadata) { width = metadata->mode.hdisplay; height = metadata->mode.vdisplay; drmModeFreeCrtc(metadata); } } } int32_t MesonDisplayPipe::getPlanes(std::vector> & planes) { planes.clear(); auto plane_rage = mPlanesForPipe.equal_range(mPipeId); for (auto it = plane_rage.first; it != plane_rage.second; ++it) { if (it->second->getPrimary() && mEnablePrimaryPlane) { planes.push_back(it->second); break; } } for (auto it = plane_rage.first; it != plane_rage.second; ++it) { if (!it->second->getPrimary()) planes.push_back(it->second); } return 0; } int32_t MesonDisplayPipe::getConnector(std::shared_ptr & connector) { if (mConnectors.size() > 0) { auto it = mConnectors.begin(); mConnector = it->second; } connector = mConnector; return 0; }