Files
BPI-CM9-Linux/aml-comp/vendor/amlogic/meson_videoserver/display/drm/DrmCrtc.cpp
T

218 lines
5.1 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 "DrmCrtc.h"
#include "DispAdptLog.h"
#ifdef VSC_ENABLE
#include "VscalerEvent.h"
#endif
DrmCrtc::DrmCrtc(int drmFd, drmModeCrtcPtr p, uint32_t pipe)
: mDrmFd(drmFd),
mId(p->crtc_id),
mPipe(pipe)
{
#ifdef VSC_ENABLE
mDrmLeaseFd = -1;
mConnectorType = MESON_CONNECTOR_RESERVED;
mBgColor = 0;
#endif
}
#ifdef VSC_ENABLE
void DrmCrtc::setDrmLeaseFd(int fd)
{
mDrmLeaseFd = fd;
}
void DrmCrtc::setConnectorType(uint32_t type)
{
mConnectorType = (MESON_CONNECTOR_TYPE)type;
}
int DrmCrtc::setBgColor(uint64_t value)
{
int ret = -1;
if (mDrmLeaseFd < 0)
return ret;
drmModeAtomicReqPtr req = drmModeAtomicAlloc();
if (req == NULL)
return ret;
ret = meson_drm_setBackGroundColor(mDrmLeaseFd, req, value, mConnectorType);
if (ret == 0) {
ret = drmModeAtomicCommit(mDrmLeaseFd,
req,
DRM_MODE_ATOMIC_NONBLOCK,
NULL);
mBgColor = value;
}
drmModeAtomicFree(req);
return ret;
}
int DrmCrtc::getBgColor()
{
return mBgColor;
}
int DrmCrtc::getHdmiTxMode(DisplayMode& mode)
{
if (mDrmLeaseFd < 0)
return -1;
return meson_drm_getModeInfo(mDrmLeaseFd, mConnectorType, &mode);
}
int DrmCrtc::setHdmiTxMode(DisplayMode mode)
{
std::lock_guard<std::mutex> hdmiLock(mHdmiMutex);
int ret = -1;
if (mDrmLeaseFd < 0)
return ret;
drmModeAtomicReqPtr req = drmModeAtomicAlloc();
if (req == NULL)
return ret;
ret = meson_drm_changeMode(mDrmLeaseFd, req, &mode, mConnectorType);
if (ret == 0)
ret = drmModeAtomicCommit(mDrmLeaseFd,
req,
DRM_MODE_ATOMIC_ALLOW_MODESET,
NULL);
drmModeAtomicFree(req);
return ret;
}
int DrmCrtc::getHdmitxHdrPolicy(int &value) {
value = meson_drm_getHDRPolicy(mDrmLeaseFd, mConnectorType);
return 0;
}
int DrmCrtc::setHdmitxHdrPolicy(ENUM_MESON_HDR_POLICY policy)
{
std::lock_guard<std::mutex> hdmiLock(mHdmiMutex);
int ret = -1;
if (mDrmLeaseFd < 0)
return ret;
drmModeAtomicReqPtr req = drmModeAtomicAlloc();
if (req == NULL)
return ret;
ret = meson_drm_setHDRPolicy(mDrmLeaseFd, req, policy, mConnectorType);
if (ret == 0)
ret = drmModeAtomicCommit(mDrmLeaseFd,
req,
DRM_MODE_ATOMIC_ALLOW_MODESET,
NULL);
drmModeAtomicFree(req);
return ret;
}
int DrmCrtc::setHdmitxAvMute(bool mute)
{
std::lock_guard<std::mutex> hdmiLock(mHdmiMutex);
int ret = -1;
if (mDrmLeaseFd < 0)
return ret;
drmModeAtomicReqPtr req = drmModeAtomicAlloc();
if (req == NULL)
return ret;
ret = meson_drm_setAVMute(mDrmLeaseFd, req, mute, mConnectorType);
if (ret == 0)
ret = drmModeAtomicCommit(mDrmLeaseFd,
req,
DRM_MODE_ATOMIC_NONBLOCK,
NULL);
drmModeAtomicFree(req);
return ret;
}
int DrmCrtc::setHdmitxHdcpAuth(bool enable)
{
std::lock_guard<std::mutex> hdmiLock(mHdmiMutex);
int ret = -1;
if (mDrmLeaseFd < 0)
return ret;
drmModeAtomicReqPtr req = drmModeAtomicAlloc();
if (req == NULL)
return ret;
ret = meson_drm_setHDCPEnable(mDrmLeaseFd, req, enable, mConnectorType);
if (ret == 0)
ret = drmModeAtomicCommit(mDrmLeaseFd,
req,
DRM_MODE_ATOMIC_NONBLOCK,
NULL);
drmModeAtomicFree(req);
return ret;
}
#endif
int32_t DrmCrtc::waitTime(int clock, nsecs_t timeout)
{
struct timespec next = {0, 0};
clock_gettime(clock, &next);
next.tv_sec += timeout / 1000000000LL;
next.tv_nsec += timeout % 1000000000LL;
if (next.tv_nsec > 1000000000LL) {
next.tv_sec++;
next.tv_nsec -= 1000000000LL;
}
int err;
do {
err = clock_nanosleep(clock, TIMER_ABSTIME, &next, NULL);
} while (err == EINTR);
return err;
}
int32_t DrmCrtc::waitVBlank(nsecs_t & timestamp)
{
uint32_t reqType = DRM_VBLANK_RELATIVE | (mPipe == 0 ? 0 : (1 << mPipe));
drmVBlank vbl;
vbl.request.type = (drmVBlankSeqType)reqType;
vbl.request.sequence = 1;
int32_t ret = drmWaitVBlank(mDrmFd, &vbl);
if (!ret) {
timestamp = vbl.reply.tval_sec * 1000000000LL + vbl.reply.tval_usec *1000LL;
} else {
DISPLAY_LOGD("waitVBlank failed crtc[%d]ret[%d]", mId, ret);
#ifdef VSC_ENABLE
VscalerEvent *vEvent = &(VscalerEvent::getInstance());
vEvent->makeBspError("waitVBlank failed");
#endif
ret = waitTime(CLOCK_MONOTONIC, 16000000);
}
return ret;
}