Files
BPI-CM9-Linux/aml-comp/vendor/amlogic/meson_videoserver/display/MesonDisplayPipe.cpp
T
2026-08-24 10:03:24 +08:00

195 lines
5.8 KiB
C++

/*
* 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 <drm.h>
#include <drm_fourcc.h>
#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<DrmCrtc> crtc =
std::make_shared<DrmCrtc>(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<DrmEncoder> encoder =
std::make_shared<DrmEncoder>(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<DrmConnector> connector =
std::make_shared<DrmConnector>(mDrmFd, metadata);
mConnectors.emplace(connector->getId(), std::move(connector));
drmModeFreeConnector(metadata);
}
drmModeFreeResources(drmRes);
/* load Drm planes */
std::map<uint32_t, std::shared_ptr<DrmVideoPlane>> 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<DrmVideoPlane> plane =
std::make_shared<DrmVideoPlane>(mDrmFd, metadata);
plane->setPrimary(primary);
if (primary)
primary = false;
planes.emplace(plane->getId(), std::move(plane));
break;
}
}
drmModeFreePlane(metadata);
}
drmModeFreePlaneResources(planeRes);
std::shared_ptr<DrmVideoPlane> 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<DrmCrtc> & 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<DrmCrtc> 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<std::shared_ptr<DrmVideoPlane>> & 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<DrmConnector> & connector)
{
if (mConnectors.size() > 0) {
auto it = mConnectors.begin();
mConnector = it->second;
}
connector = mConnector;
return 0;
}