1、Modify function return value

2、fix rs crash bug
Signed-off-by: BruceXuXu <xufeilong2@huawei.com>
Change-Id: I562390bedb16a04f9281052d95e6037d4c3c7740

Change-Id: I64691af90bd9befcba475e0f5731dd721e57e1aa
This commit is contained in:
BruceXuXu 2022-02-15 10:19:55 +08:00
parent 58a940336b
commit 7279f71abe
10 changed files with 182 additions and 50 deletions

View File

@ -34,6 +34,7 @@ config("composer_public_config") {
"//foundation/graphic/standard/interfaces/innerkits/common",
"//foundation/graphic/standard/rosen/modules/composer/hdi_backend/include",
"//foundation/graphic/standard/rosen/modules/composer/vsync/include",
"//foundation/graphic/standard/frameworks/vsync/include",
]
}

View File

@ -20,6 +20,7 @@
#include <refbase.h>
#include <mutex>
#include "vsync_generator.h"
#include "graphic_common.h"
namespace OHOS {
namespace Rosen {
@ -28,6 +29,7 @@ public:
class Callback {
public:
virtual void OnVSyncEvent(int64_t now) = 0;
virtual ~Callback() = default;
};
VSyncController(const sptr<VSyncGenerator> &geng, int64_t offset);
@ -37,9 +39,9 @@ public:
VSyncController(const VSyncController &) = delete;
VSyncController &operator=(const VSyncController &) = delete;
void SetEnable(bool enable = false);
void SetCallback(Callback* cb);
void SetPhaseOffset(int64_t offset);
VsyncError SetEnable(bool enable = false);
VsyncError SetCallback(Callback* cb);
VsyncError SetPhaseOffset(int64_t offset);
private:

View File

@ -64,9 +64,9 @@ public:
VSyncDistributor(const VSyncDistributor &) = delete;
VSyncDistributor &operator=(const VSyncDistributor &) = delete;
void AddConnection(const sptr<VSyncConnection>& connection);
void RemoveConnection(const sptr<VSyncConnection> &connection);
void RequestNextVSync(const sptr<VSyncConnection>& connection);
VsyncError AddConnection(const sptr<VSyncConnection>& connection);
VsyncError RemoveConnection(const sptr<VSyncConnection> &connection);
VsyncError RequestNextVSync(const sptr<VSyncConnection>& connection);
VsyncError SetVSyncRate(int32_t rate, const sptr<VSyncConnection>& connection);
private:

View File

@ -18,6 +18,7 @@
#define VSYNC_VSYNC_GENERATOR_H
#include <refbase.h>
#include "graphic_common.h"
#include <mutex>
#include <vector>
@ -34,26 +35,28 @@ public:
};
VSyncGenerator() = default;
virtual ~VSyncGenerator() noexcept = default;
virtual void UpdateMode(int64_t period, int64_t phase, int64_t refrenceTime) = 0;
virtual void AddListener(int64_t phase, const sptr<Callback>& cb) = 0;
virtual void RemoveListener(const sptr<Callback>& cb) = 0;
virtual void ChangePhaseOffset(const sptr<Callback>& cb, int64_t offset) = 0;
virtual VsyncError UpdateMode(int64_t period, int64_t phase, int64_t refrenceTime) = 0;
virtual VsyncError AddListener(int64_t phase, const sptr<Callback>& cb) = 0;
virtual VsyncError RemoveListener(const sptr<Callback>& cb) = 0;
virtual VsyncError ChangePhaseOffset(const sptr<Callback>& cb, int64_t offset) = 0;
};
sptr<VSyncGenerator> CreateVSyncGenerator();
void DestroyVSyncGenerator();
namespace impl {
class VSyncGenerator : public OHOS::Rosen::VSyncGenerator {
public:
static sptr<OHOS::Rosen::VSyncGenerator> GetInstance() noexcept;
static void DeleteInstance() noexcept;
// nocopyable
VSyncGenerator(const VSyncGenerator &) = delete;
VSyncGenerator &operator=(const VSyncGenerator &) = delete;
void UpdateMode(int64_t period, int64_t phase, int64_t refrenceTime) override;
void AddListener(int64_t phase, const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override;
void RemoveListener(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override;
void ChangePhaseOffset(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb, int64_t offset) override;
VsyncError UpdateMode(int64_t period, int64_t phase, int64_t refrenceTime) override;
VsyncError AddListener(int64_t phase, const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override;
VsyncError RemoveListener(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override;
VsyncError ChangePhaseOffset(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb, int64_t offset) override;
private:
friend class OHOS::Rosen::VSyncGenerator;

View File

@ -45,6 +45,7 @@ public:
}
void SetCallback(FrameCallback cb)
{
std::lock_guard<std::mutex> locker(mtx_);
vsyncCallbacks_ = cb.callback_;
userData_ = cb.userData_;
}
@ -53,6 +54,7 @@ private:
void OnReadable(int32_t fileDescriptor) override;
VSyncCallback vsyncCallbacks_;
void *userData_;
std::mutex mtx_;
};
class VSyncReceiver : public RefBase {
@ -68,8 +70,8 @@ public:
VSyncReceiver &operator=(const VSyncReceiver &) = delete;
VsyncError Init();
void RequestNextVSync(FrameCallback callback);
void SetVSyncRate(FrameCallback callback, int32_t rate);
VsyncError RequestNextVSync(FrameCallback callback);
VsyncError SetVSyncRate(FrameCallback callback, int32_t rate);
private:
sptr<IVSyncConnection> connection_;

View File

@ -27,37 +27,59 @@ VSyncController::~VSyncController()
{
}
void VSyncController::SetEnable(bool enbale)
VsyncError VSyncController::SetEnable(bool enbale)
{
if (generator_ == nullptr) {
return VSYNC_ERROR_NULLPTR;
}
const sptr<VSyncGenerator> generator = generator_.promote();
if (generator == nullptr) {
return;
return VSYNC_ERROR_NULLPTR;
}
std::lock_guard<std::mutex> locker(offsetMutex_);
VsyncError ret = VSYNC_ERROR_OK;
if (enbale) {
generator_->AddListener(phaseOffset_, this);
ret = generator->AddListener(phaseOffset_, this);
} else {
generator_->RemoveListener(this);
ret = generator->RemoveListener(this);
}
enabled_ = enbale;
return ret;
}
void VSyncController::SetCallback(Callback *cb)
VsyncError VSyncController::SetCallback(Callback *cb)
{
if (cb == nullptr) {
return VSYNC_ERROR_NULLPTR;
}
std::lock_guard<std::mutex> locker(callbackMutex_);
callback_ = cb;
return VSYNC_ERROR_OK;
}
void VSyncController::SetPhaseOffset(int64_t offset)
VsyncError VSyncController::SetPhaseOffset(int64_t offset)
{
if (generator_ == nullptr) {
return VSYNC_ERROR_NULLPTR;
}
const sptr<VSyncGenerator> generator = generator_.promote();
if (generator == nullptr) {
return VSYNC_ERROR_NULLPTR;
}
std::lock_guard<std::mutex> locker(offsetMutex_);
phaseOffset_ = offset;
generator_->ChangePhaseOffset(this, phaseOffset_);
return generator->ChangePhaseOffset(this, phaseOffset_);
}
void VSyncController::OnVSyncEvent(int64_t now)
{
if (callback_ != nullptr) {
callback_->OnVSyncEvent(now);
Callback *cb = nullptr;
{
std::lock_guard<std::mutex> locker(callbackMutex_);
cb = callback_;
}
if (cb != nullptr) {
cb->OnVSyncEvent(now);
}
}
}

View File

@ -17,11 +17,15 @@
#include <chrono>
#include <condition_variable>
#include <algorithm>
#include "vsync_log.h"
namespace OHOS {
namespace Rosen {
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0, "VsyncDistributor" };
constexpr int32_t SOFT_VSYNC_PERIOD = 16;
constexpr int32_t ERRNO_EAGAIN = -1;
constexpr int32_t ERRNO_OTHER = -2;
}
VSyncConnection::VSyncConnection(const sptr<VSyncDistributor>& distributor, std::string name)
: rate_(-1), distributor_(distributor), name_(name)
@ -36,12 +40,14 @@ VSyncConnection::~VSyncConnection()
VsyncError VSyncConnection::RequestNextVSync()
{
if (distributor_ == nullptr) {
return VSYNC_ERROR_NULLPTR;
}
const sptr<VSyncDistributor> distributor = distributor_.promote();
if (distributor == nullptr) {
return VSYNC_ERROR_NULLPTR;
}
distributor->RequestNextVSync(this);
return VSYNC_ERROR_OK;
return distributor->RequestNextVSync(this);
}
VsyncError VSyncConnection::GetReceiveFd(int32_t &fd)
@ -57,6 +63,9 @@ int32_t VSyncConnection::PostEvent(int64_t now)
VsyncError VSyncConnection::SetVSyncRate(int32_t rate)
{
if (distributor_ == nullptr) {
return VSYNC_ERROR_NULLPTR;
}
const sptr<VSyncDistributor> distributor = distributor_.promote();
if (distributor == nullptr) {
return VSYNC_ERROR_NULLPTR;
@ -77,25 +86,33 @@ VSyncDistributor::~VSyncDistributor()
{
vsyncThreadRunning_ = false;
if (threadLoop_.joinable()) {
con_.notify_all();
threadLoop_.join();
}
}
void VSyncDistributor::AddConnection(const sptr<VSyncConnection>& connection)
VsyncError VSyncDistributor::AddConnection(const sptr<VSyncConnection>& connection)
{
if (connection != nullptr) {
std::lock_guard<std::mutex> locker(mutex_);
connections_.push_back(connection);
if (connection == nullptr) {
return VSYNC_ERROR_NULLPTR;
}
std::lock_guard<std::mutex> locker(mutex_);
connections_.push_back(connection);
return VSYNC_ERROR_OK;
}
void VSyncDistributor::RemoveConnection(const sptr<VSyncConnection>& connection)
VsyncError VSyncDistributor::RemoveConnection(const sptr<VSyncConnection>& connection)
{
if (connection == nullptr) {
return VSYNC_ERROR_NULLPTR;
}
std::lock_guard<std::mutex> locker(mutex_);
auto it = find(connections_.begin(), connections_.end(), connection);
if (it != connections_.end()) {
connections_.erase(it);
if (it == connections_.end()) {
return VSYNC_ERROR_INVALID_ARGUMENTS;
}
connections_.erase(it);
return VSYNC_ERROR_OK;
}
void VSyncDistributor::ThreadMain()
@ -112,6 +129,8 @@ void VSyncDistributor::ThreadMain()
event_.timestamp = 0;
vsyncCount = event_.vsyncCount;
for (uint32_t i = 0; i <connections_.size(); i++) {
VLOGI("conn name:%{public}s, rate:%{public}d",
connections_[i]->GetName().c_str(), connections_[i]->rate_);
if (connections_[i]->rate_ == 0) {
waitForVSync = true;
if (timestamp > 0) {
@ -123,6 +142,9 @@ void VSyncDistributor::ThreadMain()
waitForVSync = true;
}
}
VLOGI("Distributor name:%{public}s, WaitforVSync:%{public}d, timestamp:" VPUBI64 "",
name_.c_str(), waitForVSync, timestamp);
// no vsync signal
if (timestamp == 0) {
// there is some connections request next vsync, enable vsync if vsync disable and
@ -149,8 +171,14 @@ void VSyncDistributor::ThreadMain()
}
for (uint32_t i = 0; i < conns.size(); i++) {
if (conns[i]->PostEvent(timestamp) < 0) {
int32_t ret = conns[i]->PostEvent(timestamp);
VLOGI("Distributor name:%{public}s, connection name:%{public}s, ret:%{public}d",
name_.c_str(), conns[i]->GetName().c_str(), ret);
if (ret == 0 || ret == ERRNO_OTHER) {
RemoveConnection(conns[i]);
} else if (ret == ERRNO_EAGAIN) {
std::unique_lock<std::mutex> locker(mutex_);
conns[i]->rate_ = 0;
}
}
}
@ -181,21 +209,36 @@ void VSyncDistributor::OnVSyncEvent(int64_t now)
con_.notify_all();
}
void VSyncDistributor::RequestNextVSync(const sptr<VSyncConnection>& connection)
VsyncError VSyncDistributor::RequestNextVSync(const sptr<VSyncConnection>& connection)
{
if (connection == nullptr) {
VLOGE("connection is nullptr");
return VSYNC_ERROR_NULLPTR;
}
std::lock_guard<std::mutex> locker(mutex_);
auto it = find(connections_.begin(), connections_.end(), connection);
if (it == connections_.end()) {
VLOGE("connection is invalid arguments");
return VSYNC_ERROR_INVALID_ARGUMENTS;
}
if (connection->rate_ < 0) {
connection->rate_ = 0;
con_.notify_all();
}
VLOGI("conn name:%{public}s, rate:%{public}d", connection->GetName().c_str(), connection->rate_);
return VSYNC_ERROR_OK;
}
VsyncError VSyncDistributor::SetVSyncRate(int32_t rate, const sptr<VSyncConnection>& connection)
{
if (rate <= 0) {
if (rate <= 0 || connection == nullptr) {
return VSYNC_ERROR_INVALID_ARGUMENTS;
}
std::lock_guard<std::mutex> locker(mutex_);
auto it = find(connections_.begin(), connections_.end(), connection);
if (it == connections_.end()) {
return VSYNC_ERROR_INVALID_ARGUMENTS;
}
if (connection->rate_ == rate) {
return VSYNC_ERROR_INVALID_ARGUMENTS;
}

View File

@ -39,6 +39,11 @@ sptr<OHOS::Rosen::VSyncGenerator> VSyncGenerator::GetInstance() noexcept
return instance_;
}
void VSyncGenerator::DeleteInstance() noexcept
{
instance_ = nullptr;
}
VSyncGenerator::VSyncGenerator()
: period_(0), phase_(0), refrenceTime_(0), wakeupDelay_(0), vsyncThreadRunning_(false)
{
@ -49,6 +54,7 @@ VSyncGenerator::~VSyncGenerator()
{
vsyncThreadRunning_ = false;
if (thread_.joinable()) {
con_.notify_all();
thread_.join();
}
}
@ -147,18 +153,25 @@ std::vector<VSyncGenerator::Listener> VSyncGenerator::GetListenerTimeouted(int64
return ret;
}
void VSyncGenerator::UpdateMode(int64_t period, int64_t phase, int64_t refrenceTime)
VsyncError VSyncGenerator::UpdateMode(int64_t period, int64_t phase, int64_t refrenceTime)
{
std::lock_guard<std::mutex> locker(mutex_);
if (period < 0 || refrenceTime < 0) {
return VSYNC_ERROR_INVALID_ARGUMENTS;
}
period_ = period;
phase_ = phase;
refrenceTime_ = refrenceTime;
con_.notify_all();
return VSYNC_ERROR_OK;
}
void VSyncGenerator::AddListener(int64_t phase, const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb)
VsyncError VSyncGenerator::AddListener(int64_t phase, const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb)
{
std::lock_guard<std::mutex> locker(mutex_);
if (cb == nullptr) {
return VSYNC_ERROR_INVALID_ARGUMENTS;
}
Listener listener;
listener.phase_ = phase;
listener.callback_ = cb;
@ -166,35 +179,59 @@ void VSyncGenerator::AddListener(int64_t phase, const sptr<OHOS::Rosen::VSyncGen
listeners_.push_back(listener);
con_.notify_all();
return VSYNC_ERROR_OK;
}
void VSyncGenerator::RemoveListener(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb)
VsyncError VSyncGenerator::RemoveListener(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb)
{
std::lock_guard<std::mutex> locker(mutex_);
for (auto it = listeners_.begin(); it < listeners_.end(); it++) {
if (cb == nullptr) {
return VSYNC_ERROR_INVALID_ARGUMENTS;
}
bool removeFlag = false;
auto it = listeners_.begin();
for (; it < listeners_.end(); it++) {
if (it->callback_ == cb) {
listeners_.erase(it);
removeFlag = true;
break;
}
}
if (!removeFlag) {
return VSYNC_ERROR_INVALID_ARGUMENTS;
}
con_.notify_all();
return VSYNC_ERROR_OK;
}
void VSyncGenerator::ChangePhaseOffset(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb, int64_t offset)
VsyncError VSyncGenerator::ChangePhaseOffset(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb, int64_t offset)
{
std::lock_guard<std::mutex> locker(mutex_);
if (cb == nullptr) {
return VSYNC_ERROR_INVALID_ARGUMENTS;
}
auto it = listeners_.begin();
for (; it < listeners_.end(); it++) {
if (it->callback_ == cb) {
break;
}
}
it->phase_ = offset;
if (it != listeners_.end()) {
it->phase_ = offset;
} else {
return VSYNC_ERROR_INVALID_OPERATING;
}
return VSYNC_ERROR_OK;
}
} // namespace impl
sptr<VSyncGenerator> CreateVSyncGenerator()
{
return impl::VSyncGenerator::GetInstance();
}
void DestroyVSyncGenerator()
{
impl::VSyncGenerator::DeleteInstance();
}
}
}

View File

@ -30,8 +30,13 @@ void VSyncCallBackListener::OnReadable(int32_t fileDescriptor)
}
int64_t now;
ssize_t retVal = read(fileDescriptor, &now, sizeof(int64_t));
if (retVal > 0 && vsyncCallbacks_ != nullptr) {
vsyncCallbacks_(now, userData_);
VSyncCallback cb = nullptr;
{
std::lock_guard<std::mutex> locker(mtx_);
cb = vsyncCallbacks_;
}
if (retVal > 0 && cb != nullptr) {
cb(now, userData_);
}
}
@ -50,6 +55,9 @@ VsyncError VSyncReceiver::Init()
if (init_) {
return VSYNC_ERROR_OK;
}
if (connection_ == nullptr) {
return VSYNC_ERROR_NULLPTR;
}
VsyncError ret = connection_->GetReceiveFd(fd_);
if (ret != VSYNC_ERROR_OK) {
@ -76,16 +84,24 @@ VSyncReceiver::~VSyncReceiver()
}
}
void VSyncReceiver::RequestNextVSync(FrameCallback callback)
VsyncError VSyncReceiver::RequestNextVSync(FrameCallback callback)
{
std::lock_guard<std::mutex> locker(initMutex_);
if (!init_) {
return VSYNC_ERROR_API_FAILED;
}
listener_->SetCallback(callback);
connection_->RequestNextVSync();
return connection_->RequestNextVSync();
}
void VSyncReceiver::SetVSyncRate(FrameCallback callback, int32_t rate)
VsyncError VSyncReceiver::SetVSyncRate(FrameCallback callback, int32_t rate)
{
std::lock_guard<std::mutex> locker(initMutex_);
if (!init_) {
return VSYNC_ERROR_API_FAILED;
}
listener_->SetCallback(callback);
connection_->SetVSyncRate(rate);
return connection_->SetVSyncRate(rate);
}
} // namespace Rosen
} // namespace OHOS

View File

@ -30,6 +30,8 @@ constexpr HiLogLabel LABEL = { LOG_CORE, 0xD001400, "LocalSocketPair" };
constexpr int32_t DEFAULT_CHANNEL_SIZE = 2 * 1024;
constexpr int32_t SOCKET_PAIR_SIZE = 2;
constexpr int32_t INVALID_FD = -1;
constexpr int32_t ERRNO_EAGAIN = -1;
constexpr int32_t ERRNO_OTHER = -2;
} // namespace
LocalSocketPair::LocalSocketPair()
@ -102,7 +104,11 @@ int32_t LocalSocketPair::SendData(const void *vaddr, size_t size)
} while (errno == EINTR);
if (length < 0) {
HiLog::Error(LABEL, "%{public}s send fail : %{public}d, length = %{public}d", __func__, errno, (int32_t)length);
return -1;
if (errno == EAGAIN) {
return ERRNO_EAGAIN;
} else {
return ERRNO_OTHER;
}
}
return length;
}