fix code review

Signed-off-by: shegangbin <shegangbin1@huawei.com>
This commit is contained in:
shegangbin 2024-09-09 10:47:44 +08:00
parent 7d56c5fec9
commit 7f9f10af5e
8 changed files with 177 additions and 77 deletions

View File

@ -179,7 +179,7 @@ void OH_NativeImage_Destroy(OH_NativeImage** image)
int32_t OH_NativeImage_AcquireNativeWindowBuffer(OH_NativeImage* image,
OHNativeWindowBuffer** nativeWindowBuffer, int32_t* fenceFd)
{
if (image == nullptr || image->consumer == nullptr || nativeWindowBuffer == nullptr || fenceFd == nullptr) {
if (image == nullptr || image->consumer == nullptr) {
BLOGE("parameter error");
return SURFACE_ERROR_INVALID_PARAM;
}
@ -189,7 +189,7 @@ int32_t OH_NativeImage_AcquireNativeWindowBuffer(OH_NativeImage* image,
int32_t OH_NativeImage_ReleaseNativeWindowBuffer(OH_NativeImage* image,
OHNativeWindowBuffer* nativeWindowBuffer, int32_t fenceFd)
{
if (image == nullptr || image->consumer == nullptr || nativeWindowBuffer == nullptr) {
if (image == nullptr || image->consumer == nullptr) {
BLOGE("parameter error");
return SURFACE_ERROR_INVALID_PARAM;
}

View File

@ -91,6 +91,7 @@ private:
void UpdateReferenceTimeLocked();
void CheckIfFirstRefreshAfterIdleLocked();
void ComputePhaseLocked();
void SetScreenVsyncEnabledInRSMainThreadInternal(SetScreenVsyncEnabledCallback cb, bool enabled);
int64_t period_;
int64_t phase_;

View File

@ -35,8 +35,16 @@ VsyncError VSyncConnectionProxy::RequestNextVSync(const std::string& fromWhom, i
MessageParcel arg;
MessageParcel ret;
arg.WriteInterfaceToken(GetDescriptor());
int res = Remote()->SendRequest(IVSYNC_CONNECTION_REQUEST_NEXT_VSYNC, arg, ret, opt);
if (!arg.WriteInterfaceToken(GetDescriptor())) {
VLOGE("Failed to write interface token");
return VSYNC_ERROR_API_FAILED;
}
auto remote = Remote();
if (remote == nullptr) {
VLOGE("remote is null");
return VSYNC_ERROR_API_FAILED;
}
int res = remote->SendRequest(IVSYNC_CONNECTION_REQUEST_NEXT_VSYNC, arg, ret, opt);
if (res != NO_ERROR) {
VLOGE("ipc send fail, error:%{public}d", res);
return VSYNC_ERROR_BINDER_ERROR;
@ -50,14 +58,25 @@ VsyncError VSyncConnectionProxy::SetUiDvsyncSwitch(bool dvsyncSwitch)
MessageParcel arg;
MessageParcel ret;
arg.WriteInterfaceToken(GetDescriptor());
arg.WriteBool(dvsyncSwitch);
int res = Remote()->SendRequest(IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH, arg, ret, opt);
if (!arg.WriteInterfaceToken(GetDescriptor())) {
VLOGE("Failed to write interface token");
return VSYNC_ERROR_API_FAILED;
}
if (!arg.WriteBool(dvsyncSwitch)) {
VLOGE("Failed to write dvsyncSwitch:%{public}d", dvsyncSwitch);
return VSYNC_ERROR_API_FAILED;
}
auto remote = Remote();
if (remote == nullptr) {
VLOGE("remote is null");
return VSYNC_ERROR_API_FAILED;
}
int res = remote->SendRequest(IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH, arg, ret, opt);
if (res != NO_ERROR) {
VLOGE("ipc send fail, error:%{public}d", res);
return VSYNC_ERROR_UNKOWN;
}
return VSYNC_ERROR_OK;
return static_cast<VsyncError>(ret.ReadInt32());
}
VsyncError VSyncConnectionProxy::SetUiDvsyncConfig(int32_t bufferCount)
@ -66,16 +85,24 @@ VsyncError VSyncConnectionProxy::SetUiDvsyncConfig(int32_t bufferCount)
MessageParcel arg;
MessageParcel ret;
arg.WriteInterfaceToken(GetDescriptor());
if (!arg.WriteInterfaceToken(GetDescriptor())) {
VLOGE("Failed to write interface token");
return VSYNC_ERROR_API_FAILED;
}
if (!arg.WriteInt32(bufferCount)) {
VLOGE("SetUiDvsyncConfig bufferCount error");
return VSYNC_ERROR_UNKOWN;
}
int res = Remote()->SendRequest(IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG, arg, ret, opt);
auto remote = Remote();
if (remote == nullptr) {
VLOGE("remote is null");
return VSYNC_ERROR_API_FAILED;
}
int res = remote->SendRequest(IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG, arg, ret, opt);
if (res != NO_ERROR) {
return VSYNC_ERROR_UNKOWN;
}
return VSYNC_ERROR_OK;
return static_cast<VsyncError>(ret.ReadInt32());
}
VsyncError VSyncConnectionProxy::GetReceiveFd(int32_t &fd)
@ -84,14 +111,26 @@ VsyncError VSyncConnectionProxy::GetReceiveFd(int32_t &fd)
MessageParcel arg;
MessageParcel ret;
arg.WriteInterfaceToken(GetDescriptor());
int res = Remote()->SendRequest(IVSYNC_CONNECTION_GET_RECEIVE_FD, arg, ret, opt);
if (!arg.WriteInterfaceToken(GetDescriptor())) {
VLOGE("Failed to write interface token");
return VSYNC_ERROR_API_FAILED;
}
auto remote = Remote();
if (remote == nullptr) {
VLOGE("remote is null");
return VSYNC_ERROR_API_FAILED;
}
int res = remote->SendRequest(IVSYNC_CONNECTION_GET_RECEIVE_FD, arg, ret, opt);
if (res != NO_ERROR) {
VLOGE("GetReceiveFd Failed, res = %{public}d", res);
return VSYNC_ERROR_BINDER_ERROR;
}
res = ret.ReadInt32();
if (res != VSYNC_ERROR_OK) {
return static_cast<VsyncError>(res);
}
fd = ret.ReadFileDescriptor();
if (fd <= 0) {
if (fd < 0) {
VLOGE("GetReceiveFd Invalid fd:%{public}d", fd);
return VSYNC_ERROR_API_FAILED;
}
@ -107,13 +146,24 @@ VsyncError VSyncConnectionProxy::SetVSyncRate(int32_t rate)
MessageParcel arg;
MessageParcel ret;
arg.WriteInterfaceToken(GetDescriptor());
arg.WriteInt32(rate);
int res = Remote()->SendRequest(IVSYNC_CONNECTION_SET_RATE, arg, ret, opt);
if (!arg.WriteInterfaceToken(GetDescriptor())) {
VLOGE("Failed to write interface token");
return VSYNC_ERROR_API_FAILED;
}
if (!arg.WriteInt32(rate)) {
VLOGE("Failed to write rate:%{public}d", rate);
return VSYNC_ERROR_API_FAILED;
}
auto remote = Remote();
if (remote == nullptr) {
VLOGE("remote is null");
return VSYNC_ERROR_API_FAILED;
}
int res = remote->SendRequest(IVSYNC_CONNECTION_SET_RATE, arg, ret, opt);
if (res != NO_ERROR) {
return VSYNC_ERROR_BINDER_ERROR;
}
return VSYNC_ERROR_OK;
return static_cast<VsyncError>(ret.ReadInt32());
}
VsyncError VSyncConnectionProxy::Destroy()
@ -122,12 +172,20 @@ VsyncError VSyncConnectionProxy::Destroy()
MessageParcel arg;
MessageParcel ret;
arg.WriteInterfaceToken(GetDescriptor());
int res = Remote()->SendRequest(IVSYNC_CONNECTION_DESTROY, arg, ret, opt);
if (!arg.WriteInterfaceToken(GetDescriptor())) {
VLOGE("Failed to write interface token");
return VSYNC_ERROR_API_FAILED;
}
auto remote = Remote();
if (remote == nullptr) {
VLOGE("remote is null");
return VSYNC_ERROR_API_FAILED;
}
int res = remote->SendRequest(IVSYNC_CONNECTION_DESTROY, arg, ret, opt);
if (res != NO_ERROR) {
return VSYNC_ERROR_BINDER_ERROR;
}
return VSYNC_ERROR_OK;
return static_cast<VsyncError>(ret.ReadInt32());
}
} // namespace Vsync
} // namespace OHOS

View File

@ -42,6 +42,7 @@ int32_t VSyncConnectionStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
case IVSYNC_CONNECTION_GET_RECEIVE_FD: {
int32_t fd = -1;
int32_t ret = GetReceiveFd(fd);
reply.WriteInt32(ret);
if (ret != VSYNC_ERROR_OK) {
// check add log
return ret;
@ -52,6 +53,7 @@ int32_t VSyncConnectionStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
case IVSYNC_CONNECTION_SET_RATE: {
int32_t rate = data.ReadInt32();
int32_t ret = SetVSyncRate(rate);
reply.WriteInt32(ret);
if (ret != VSYNC_ERROR_OK) {
// check add log
return ret;
@ -59,22 +61,28 @@ int32_t VSyncConnectionStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
break;
}
case IVSYNC_CONNECTION_DESTROY: {
return Destroy();
int32_t ret = Destroy();
reply.WriteInt32(ret);
return ret;
}
case IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH: {
auto dvsyncOn = data.ReadBool();
return SetUiDvsyncSwitch(dvsyncOn);
int32_t ret = SetUiDvsyncSwitch(dvsyncOn);
reply.WriteInt32(ret);
return ret;
}
case IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG: {
if (!CheckCallingPermission()) {
return VSYNC_ERROR_UNKOWN;
}
int32_t bufferCount = data.ReadInt32();
return SetUiDvsyncConfig(bufferCount);
int32_t ret = SetUiDvsyncConfig(bufferCount);
reply.WriteInt32(ret);
return ret;
}
default: {
// check add log
return VSYNC_ERROR_INVALID_OPERATING;
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}
}
return 0;

View File

@ -138,12 +138,12 @@ VsyncError VSyncConnection::RequestNextVSync(const std::string &fromWhom, int64_
VsyncError VSyncConnection::GetReceiveFd(int32_t &fd)
{
std::unique_lock<std::mutex> locker(mutex_);
if (isDead_) {
if (isDead_ || socketPair_ == nullptr) {
VLOGE("%{public}s VSync Client Connection is dead, name:%{public}s.", __func__, info_.name_.c_str());
return VSYNC_ERROR_API_FAILED;
}
fd = socketPair_->GetReceiveDataFd();
if (fd <= 0) {
if (fd < 0) {
VLOGE("%{public}s socketPair invalid fd:%{public}d.", __func__, fd);
return VSYNC_ERROR_API_FAILED;
}
@ -742,7 +742,7 @@ void VSyncDistributor::SubScribeSystemAbility(const std::string& threadName)
std::string strPid = std::to_string(getpid());
std::string strTid = std::to_string(gettid());
saStatusChangeListener_ = new (std::nothrow)VSyncSystemAbilityListener(threadName, strUid, strPid, strTid);
saStatusChangeListener_ = new VSyncSystemAbilityListener(threadName, strUid, strPid, strTid);
int32_t ret = systemAbilityManager->SubscribeSystemAbility(RES_SCHED_SYS_ABILITY_ID, saStatusChangeListener_);
if (ret != ERR_OK) {
VLOGE("%{public}s subscribe system ability %{public}d failed.", __func__, RES_SCHED_SYS_ABILITY_ID);
@ -825,6 +825,9 @@ void VSyncDistributor::CollectConnectionsLTPO(bool &waitForVSync, int64_t timest
break;
}
int64_t vsyncPulseFreq = static_cast<int64_t>(connections_[i]->vsyncPulseFreq_);
if (vsyncPulseFreq == 0) {
continue;
}
if ((vsyncCount - connections_[i]->referencePulseCount_) % vsyncPulseFreq == 0) {
connections_[i]->triggerThisTime_ = false;
if (connections_[i]->rate_ == 0) {
@ -996,10 +999,10 @@ VsyncError VSyncDistributor::QosGetPidByName(const std::string& name, uint32_t&
return VSYNC_ERROR_INVALID_ARGUMENTS;
}
std::string::size_type pos = name.find("_");
if (pos == std::string::npos) {
if (pos == std::string::npos || (pos + 1) >= name.size()) {
return VSYNC_ERROR_INVALID_ARGUMENTS;
}
pid = (uint32_t)stoi(name.substr(pos + 1));
pid = static_cast<uint32_t>(stoi(name.substr(pos + 1)));
return VSYNC_ERROR_OK;
}
@ -1059,7 +1062,7 @@ VsyncError VSyncDistributor::SetQosVSyncRate(uint64_t windowNodeId, int32_t rate
}
bool isNeedNotify = false;
for (auto& connection : iter->second) {
if (connection && connection->highPriorityRate_ != rate) {
if (connection != nullptr && connection->highPriorityRate_ != rate) {
connection->highPriorityRate_ = rate;
connection->highPriorityState_ = true;
VLOGD("in, conn name:%{public}s, highPriorityRate:%{public}d", connection->info_.name_.c_str(),

View File

@ -201,18 +201,20 @@ void VSyncGenerator::ThreadLoop()
con_.wait(locker);
}
continue;
} else if (vsyncMode_ == VSYNC_MODE_LTPO) {
bool modelChanged = UpdateChangeDataLocked(occurTimestamp, occurReferenceTime, nextTimeStamp);
if (modelChanged) {
ScopedBytrace func("VSyncGenerator: LTPO mode change");
bool clearAllSamplesFlag = clearAllSamplesFlag_;
clearAllSamplesFlag_ = false;
locker.unlock();
ClearAllSamplesInternal(clearAllSamplesFlag);
} else if (vsyncMode_ == VSYNC_MODE_LTPO &&
UpdateChangeDataLocked(occurTimestamp, occurReferenceTime, nextTimeStamp)) {
ScopedBytrace func("VSyncGenerator: LTPO mode change");
bool clearAllSamplesFlag = clearAllSamplesFlag_;
clearAllSamplesFlag_ = false;
locker.unlock();
ClearAllSamplesInternal(clearAllSamplesFlag);
if (appVSyncDistributor_ != nullptr) {
appVSyncDistributor_->RecordVsyncModeChange(currRefreshRate_, period_);
rsVSyncDistributor_->RecordVsyncModeChange(currRefreshRate_, period_);
continue;
}
if (rsVSyncDistributor_ != nullptr) {
rsVSyncDistributor_->RecordVsyncModeChange(currRefreshRate_, period_);
}
continue;
}
}
@ -514,7 +516,7 @@ void VSyncGenerator::SubScribeSystemAbility()
std::string strPid = std::to_string(getpid());
std::string strTid = std::to_string(gettid());
saStatusChangeListener_ = new (std::nothrow)VSyncSystemAbilityListener(threadName, strUid, strPid, strTid);
saStatusChangeListener_ = new VSyncSystemAbilityListener(threadName, strUid, strPid, strTid);
int32_t ret = systemAbilityManager->SubscribeSystemAbility(RES_SCHED_SYS_ABILITY_ID, saStatusChangeListener_);
if (ret != ERR_OK) {
VLOGE("%{public}s subscribe system ability %{public}d failed.", __func__, RES_SCHED_SYS_ABILITY_ID);
@ -763,6 +765,7 @@ VsyncError VSyncGenerator::StartRefresh()
void VSyncGenerator::SetRSDistributor(sptr<VSyncDistributor> &rsVSyncDistributor)
{
std::lock_guard<std::mutex> lock(mutex_);
rsVSyncDistributor_ = rsVSyncDistributor;
}
@ -799,6 +802,7 @@ void VSyncGenerator::PeriodCheckLocked(int64_t hardwareVsyncInterval)
void VSyncGenerator::SetAppDistributor(sptr<VSyncDistributor> &appVSyncDistributor)
{
std::lock_guard<std::mutex> lock(mutex_);
appVSyncDistributor_ = appVSyncDistributor;
}
@ -931,7 +935,9 @@ void VSyncGenerator::SetPendingMode(int64_t period, int64_t timestamp)
std::lock_guard<std::mutex> lock(mutex_);
pendingPeriod_ = period;
pendingReferenceTime_ = timestamp;
rsVSyncDistributor_->UpdatePendingReferenceTime(pendingReferenceTime_);
if (rsVSyncDistributor_ != nullptr) {
rsVSyncDistributor_->UpdatePendingReferenceTime(pendingReferenceTime_);
}
}
void VSyncGenerator::Dump(std::string &result)

View File

@ -165,6 +165,19 @@ VsyncError VSyncReceiver::Init()
return VSYNC_ERROR_NULLPTR;
}
if (looper_ == nullptr) {
std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("OS_VSyncThread");
if (runner == nullptr) {
return VSYNC_ERROR_API_FAILED;
}
looper_ = std::make_shared<AppExecFwk::EventHandler>(runner);
runner->Run();
looper_->PostTask([this] {
SetThreadQos(QOS::QosLevel::QOS_USER_INTERACTIVE);
this->ThreadCreateNotify();
});
}
VsyncError ret = connection_->GetReceiveFd(fd_);
if (ret != VSYNC_ERROR_OK) {
return ret;
@ -178,16 +191,6 @@ VsyncError VSyncReceiver::Init()
listener_->SetName(name_);
if (looper_ == nullptr) {
std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("OS_VSyncThread");
looper_ = std::make_shared<AppExecFwk::EventHandler>(runner);
runner->Run();
looper_->PostTask([this] {
SetThreadQos(QOS::QosLevel::QOS_USER_INTERACTIVE);
this->ThreadCreateNotify();
});
}
looper_->AddFileDescriptorListener(fd_, AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener_, "vSyncTask");
init_ = true;
return VSYNC_ERROR_OK;
@ -319,7 +322,7 @@ void VSyncReceiver::CloseVsyncReceiverFd()
VLOGI("%{public}s looper remove fd listener, fd=%{public}d", __func__, fd_);
}
if (fd_ > 0) {
if (fd_ >= 0) {
close(fd_);
fd_ = INVALID_FD;
}

View File

@ -107,46 +107,64 @@ bool VSyncSampler::GetHardwareVSyncStatus() const
void VSyncSampler::RegSetScreenVsyncEnabledCallback(VSyncSampler::SetScreenVsyncEnabledCallback cb)
{
std::lock_guard<std::mutex> lock(mutex_);
setScreenVsyncEnabledCallback_ = cb;
}
void VSyncSampler::SetScreenVsyncEnabledInRSMainThread(bool enabled)
{
if (setScreenVsyncEnabledCallback_ == nullptr) {
VLOGE("SetScreenVsyncEnabled:%{public}d failed, setScreenVsyncEnabledCallback_ is null", enabled);
SetScreenVsyncEnabledCallback cb;
{
std::lock_guard<std::mutex> lock(mutex_);
cb = setScreenVsyncEnabledCallback_;
}
SetScreenVsyncEnabledInRSMainThreadInternal(cb, enabled);
}
void VSyncSampler::SetScreenVsyncEnabledInRSMainThreadInternal(SetScreenVsyncEnabledCallback cb, bool enabled)
{
if (cb == nullptr) {
VLOGE("SetScreenVsyncEnabled:%{public}d failed, cb is null", enabled);
return;
}
setScreenVsyncEnabledCallback_(enabled);
cb(enabled);
}
bool VSyncSampler::AddSample(int64_t timeStamp)
{
std::lock_guard<std::mutex> lock(mutex_);
if (numSamples_ < MAX_SAMPLES - 1) {
numSamples_++;
} else {
firstSampleIndex_ = (firstSampleIndex_ + 1) % MAX_SAMPLES;
if (timeStamp < 0) {
return true;
}
SetScreenVsyncEnabledCallback cb;
bool shouldDisableScreenVsync;
{
std::lock_guard<std::mutex> lock(mutex_);
if (numSamples_ < MAX_SAMPLES - 1) {
numSamples_++;
} else {
firstSampleIndex_ = (firstSampleIndex_ + 1) % MAX_SAMPLES;
}
if (firstSampleIndex_ + numSamples_ >= 1) {
uint32_t index = (firstSampleIndex_ + numSamples_ - 1) % MAX_SAMPLES;
samples_[index] = timeStamp;
if (firstSampleIndex_ + numSamples_ >= 1) {
uint32_t index = (firstSampleIndex_ + numSamples_ - 1) % MAX_SAMPLES;
samples_[index] = timeStamp;
}
UpdateReferenceTimeLocked();
UpdateModeLocked();
if (numResyncSamplesSincePresent_++ > MAX_SAMPLES_WITHOUT_PRESENT) {
ResetErrorLocked();
}
// 1/2 just a empirical value
shouldDisableScreenVsync = modeUpdated_ && (error_ < ERROR_THRESHOLD / 2);
cb = setScreenVsyncEnabledCallback_;
}
UpdateReferenceTimeLocked();
UpdateModeLocked();
if (numResyncSamplesSincePresent_++ > MAX_SAMPLES_WITHOUT_PRESENT) {
ResetErrorLocked();
}
// 1/2 just a empirical value
bool shouldDisableScreenVsync = modeUpdated_ && (error_ < ERROR_THRESHOLD / 2);
if (shouldDisableScreenVsync) {
// disabled screen vsync in rsMainThread
VLOGD("Disable Screen Vsync");
SetScreenVsyncEnabledInRSMainThread(false);
SetScreenVsyncEnabledInRSMainThreadInternal(cb, false);
}
return !shouldDisableScreenVsync;
@ -257,6 +275,9 @@ void VSyncSampler::UpdateErrorLocked()
bool VSyncSampler::AddPresentFenceTime(int64_t timestamp)
{
if (timestamp < 0) {
return false;
}
std::lock_guard<std::mutex> lock(mutex_);
presentFenceTime_[presentFenceTimeOffset_] = timestamp;