From e62f8a6a3d11825617085665aa7ddef3e978af58 Mon Sep 17 00:00:00 2001 From: xxfeng_hw Date: Wed, 12 Jan 2022 17:44:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BARender=20Service=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=9A=84=E6=AD=BB=E4=BA=A1=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=8C=E7=94=A8=E4=BA=8EClient=E7=AB=AF?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E9=80=80=E5=87=BA=E6=97=B6=E7=9A=84=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xxfeng_hw Change-Id: Ia2ddf3239ce6b8d2362fa1c39564bd16794177be --- rosen/modules/render_service/BUILD.gn | 3 +- rosen/modules/render_service/core/main.cpp | 21 +- .../core/pipeline/rs_main_thread.cpp | 8 +- .../core/pipeline/rs_main_thread.h | 1 + .../core/pipeline/rs_render_service.cpp | 279 ++---------- .../core/pipeline/rs_render_service.h | 58 +-- .../pipeline/rs_render_service_connection.cpp | 291 +++++++++++++ .../pipeline/rs_render_service_connection.h | 113 +++++ ...screen_change_callback_death_recipient.cpp | 30 -- .../core/screen_manager/rs_screen_manager.cpp | 15 +- .../core/screen_manager/rs_screen_manager.h | 5 +- .../rs_render_service_connection_stub.cpp | 254 +++++++++++ .../rs_render_service_connection_stub.h} | 75 ++-- .../transaction/rs_render_service_stub.cpp | 235 ++-------- .../core/transaction/rs_render_service_stub.h | 5 +- .../platform/ohos/rs_irender_service.h | 85 +--- .../ohos/rs_irender_service_connection.h | 108 +++++ .../src/platform/ohos/BUILD.gn | 3 +- .../ohos/rs_render_service_client.cpp | 36 +- .../ohos/rs_render_service_connect.cpp | 98 ----- .../ohos/rs_render_service_connect_hub.cpp | 160 +++++++ ...nect.h => rs_render_service_connect_hub.h} | 40 +- .../rs_render_service_connection_proxy.cpp | 401 ++++++++++++++++++ .../ohos/rs_render_service_connection_proxy.h | 76 ++++ .../platform/ohos/rs_render_service_proxy.cpp | 376 +--------------- .../platform/ohos/rs_render_service_proxy.h | 51 +-- .../test/virtual_display_test.cpp | 12 +- 27 files changed, 1635 insertions(+), 1204 deletions(-) create mode 100644 rosen/modules/render_service/core/pipeline/rs_render_service_connection.cpp create mode 100644 rosen/modules/render_service/core/pipeline/rs_render_service_connection.h delete mode 100644 rosen/modules/render_service/core/screen_manager/rs_screen_change_callback_death_recipient.cpp create mode 100644 rosen/modules/render_service/core/transaction/rs_render_service_connection_stub.cpp rename rosen/modules/render_service/core/{screen_manager/rs_screen_change_callback_death_recipient.h => transaction/rs_render_service_connection_stub.h} (50%) create mode 100644 rosen/modules/render_service_base/include/platform/ohos/rs_irender_service_connection.h delete mode 100644 rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect.cpp create mode 100755 rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect_hub.cpp rename rosen/modules/render_service_base/src/platform/ohos/{rs_render_service_connect.h => rs_render_service_connect_hub.h} (58%) create mode 100644 rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connection_proxy.cpp create mode 100644 rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connection_proxy.h diff --git a/rosen/modules/render_service/BUILD.gn b/rosen/modules/render_service/BUILD.gn index d25dce950b..ea4c79f110 100644 --- a/rosen/modules/render_service/BUILD.gn +++ b/rosen/modules/render_service/BUILD.gn @@ -22,14 +22,15 @@ ohos_shared_library("librender_service") { "core/pipeline/rs_processor.cpp", "core/pipeline/rs_processor_factory.cpp", "core/pipeline/rs_render_service.cpp", + "core/pipeline/rs_render_service_connection.cpp", "core/pipeline/rs_render_service_listener.cpp", "core/pipeline/rs_render_service_util.cpp", "core/pipeline/rs_render_service_visitor.cpp", "core/pipeline/rs_software_processor.cpp", "core/pipeline/rs_surface_capture_task.cpp", "core/screen_manager/rs_screen.cpp", - "core/screen_manager/rs_screen_change_callback_death_recipient.cpp", "core/screen_manager/rs_screen_manager.cpp", + "core/transaction/rs_render_service_connection_stub.cpp", "core/transaction/rs_render_service_stub.cpp", ] diff --git a/rosen/modules/render_service/core/main.cpp b/rosen/modules/render_service/core/main.cpp index e9824ad973..4771fad7f4 100644 --- a/rosen/modules/render_service/core/main.cpp +++ b/rosen/modules/render_service/core/main.cpp @@ -19,8 +19,12 @@ #include #include +#include "platform/common/rs_log.h" #include "pipeline/rs_render_service.h" +using namespace OHOS; +using namespace OHOS::Rosen; + int main(int argc, const char *argv[]) { signal(SIGPIPE, SIG_IGN); @@ -30,19 +34,18 @@ int main(int argc, const char *argv[]) struct sched_param param = {0}; param.sched_priority = 2; if (sched_setscheduler(0, SCHED_FIFO, ¶m) != 0) { - OHOS::HiviewDFX::HiLog::Error(::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "renderserver"}, - "%{public}s: Couldn't set SCHED_FIFO.", __func__); + ROSEN_LOGE("RSRenderService Couldn't set SCHED_FIFO."); } else { - OHOS::HiviewDFX::HiLog::Error(::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "renderserver"}, - "%{public}s: set SCHED_FIFO succ.", __func__); + ROSEN_LOGE("RSRenderService set SCHED_FIFO succeed."); } - if (!OHOS::Rosen::RSRenderService::GetInstance().Init()) { - OHOS::HiviewDFX::HiLog::Error(::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "renderserver"}, - "%{public}s: render service init failed.", __func__); - } else { - OHOS::Rosen::RSRenderService::GetInstance().Run(); + sptr renderService(new RSRenderService()); + if (!renderService->Init()) { + ROSEN_LOGE("RSRenderService init failed."); + return -1; } + renderService->Run(); + return 0; } diff --git a/rosen/modules/render_service/core/pipeline/rs_main_thread.cpp b/rosen/modules/render_service/core/pipeline/rs_main_thread.cpp index 4d0128e86a..fdfbf08213 100644 --- a/rosen/modules/render_service/core/pipeline/rs_main_thread.cpp +++ b/rosen/modules/render_service/core/pipeline/rs_main_thread.cpp @@ -38,7 +38,7 @@ RSMainThread::~RSMainThread() noexcept { } -void RSMainThread::Start() +void RSMainThread::Init() { mainLoop_ = [&]() { ROSEN_LOGE("RsDebug mainLoop start"); @@ -55,7 +55,11 @@ void RSMainThread::Start() if (vsyncClient_) { vsyncClient_->SetVsyncCallback(std::bind(&RSMainThread::OnVsync, this, std::placeholders::_1)); } - while (1) { +} + +void RSMainThread::Start() +{ + while (true) { threadLooper_->ProcessAllMessages(-1); } } diff --git a/rosen/modules/render_service/core/pipeline/rs_main_thread.h b/rosen/modules/render_service/core/pipeline/rs_main_thread.h index ded8ba2a26..ce9abc6497 100644 --- a/rosen/modules/render_service/core/pipeline/rs_main_thread.h +++ b/rosen/modules/render_service/core/pipeline/rs_main_thread.h @@ -59,6 +59,7 @@ class RSMainThread { public: static RSMainThread* Instance(); + void Init(); void Start(); void RecvRSTransactionData(std::unique_ptr& rsTransactionData); void RequestNextVSync(); diff --git a/rosen/modules/render_service/core/pipeline/rs_render_service.cpp b/rosen/modules/render_service/core/pipeline/rs_render_service.cpp index 5e010b991e..5b2d8c1fe0 100644 --- a/rosen/modules/render_service/core/pipeline/rs_render_service.cpp +++ b/rosen/modules/render_service/core/pipeline/rs_render_service.cpp @@ -13,47 +13,44 @@ * limitations under the License. */ -#include "pipeline/rs_render_service.h" +#include "rs_render_service.h" +#include "rs_main_thread.h" +#include "rs_render_service_connection.h" #include #include #include +#include #include -#include "command/rs_command.h" -#include "pipeline/rs_main_thread.h" -#include "pipeline/rs_render_node_map.h" -#include "pipeline/rs_render_service_listener.h" -#include "pipeline/rs_surface_capture_task.h" -#include "pipeline/rs_surface_render_node.h" -#include "platform/common/rs_log.h" -#include "rs_trace.h" -#include "screen_manager/screen_types.h" namespace OHOS { namespace Rosen { -RSRenderService& RSRenderService::GetInstance() -{ - static RSRenderService instance; - return instance; -} - RSRenderService::RSRenderService() {} RSRenderService::~RSRenderService() noexcept {} bool RSRenderService::Init() { - mainThread_ = RSMainThread::Instance(); - screenManager_ = CreateOrGetScreenManager(); if (screenManager_ == nullptr || !screenManager_->Init()) { - ROSEN_LOGI("RSRenderService CreateOrGetScreenManager fail"); + ROSEN_LOGE("RSRenderService CreateOrGetScreenManager fail."); return false; } + mainThread_ = RSMainThread::Instance(); + if (mainThread_ == nullptr) { + return false; + } + mainThread_->Init(); + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - samgr->AddSystemAbility(RENDER_SERVICE, &GetInstance()); + if (samgr == nullptr) { + ROSEN_LOGE("RSRenderService GetSystemAbilityManager fail."); + return false; + } + samgr->AddSystemAbility(RENDER_SERVICE, this); + return true; } @@ -63,6 +60,36 @@ void RSRenderService::Run() mainThread_->Start(); } +sptr RSRenderService::CreateConnection(const sptr& token) +{ + auto tokenObj = token->AsObject(); + sptr newConn( + new RSRenderServiceConnection(this, mainThread_, screenManager_, tokenObj)); + + sptr tmp; + std::unique_lock lock(mutex_); + // if connections_ has the same token one, replace it. + if (connections_.count(tokenObj) > 0) { + tmp = connections_.at(tokenObj); + } + connections_[tokenObj] = newConn; + lock.unlock(); + + return newConn; +} + +void RSRenderService::RemoveConnection(sptr token) +{ + std::unique_lock lock(mutex_); + if (connections_.count(token) == 0) { + return; + } + + auto tmp = connections_.at(token); + connections_.erase(token); + lock.unlock(); +} + int RSRenderService::Dump(int fd, const std::vector& args) { std::unordered_set argSets; @@ -89,217 +116,5 @@ int RSRenderService::Dump(int fd, const std::vector& args) write(fd, dumpString.c_str(), dumpString.size()); return OHOS::NO_ERROR; } - -void RSRenderService::CommitTransaction(std::unique_ptr& transactionData) -{ - mainThread_->RecvRSTransactionData(transactionData); -} - -void RSRenderService::ExecuteSynchronousTask(const std::shared_ptr& task) -{ - std::mutex mutex; - std::unique_lock lock(mutex); - std::condition_variable cv; - auto& mainThread = mainThread_; - mainThread->PostTask([task, &cv, &mainThread] { - if (task == nullptr) { - return; - } - task->Process(mainThread->GetContext()); - cv.notify_all(); - }); - cv.wait_for(lock, std::chrono::nanoseconds(task->GetTimeout())); -} - -sptr RSRenderService::CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) -{ - std::shared_ptr node = std::make_shared(config); - if (node == nullptr) { - ROSEN_LOGE("RSRenderService::CreateNodeAndSurface CreateNode fail"); - return nullptr; - } - sptr surface = Surface::CreateSurfaceAsConsumer(); - if (surface == nullptr) { - ROSEN_LOGE("RSRenderService::CreateNodeAndSurface get consumer surface fail"); - return nullptr; - } - node->SetConsumer(surface); - std::function registerNode = [node, this]() -> void { - this->mainThread_->GetContext().GetNodeMap().RegisterRenderNode(node); - }; - mainThread_->PostTask(registerNode); - std::weak_ptr surfaceRenderNode(node); - sptr listener = new RSRenderServiceListener(surfaceRenderNode); - SurfaceError ret = surface->RegisterConsumerListener(listener); - if (ret != SURFACE_ERROR_OK) { - ROSEN_LOGE("RSRenderService::CreateNodeAndSurface Register Consumer Listener fail"); - return nullptr; - } - return surface; -} - -ScreenId RSRenderService::GetDefaultScreenId() -{ - if (screenManager_ == nullptr) { - return INVALID_SCREEN_ID; - } - - return mainThread_->ScheduleTask([this]() { - return screenManager_->GetDefaultScreenId(); - }).get(); -} - -ScreenId RSRenderService::CreateVirtualScreen( - const std::string &name, - uint32_t width, - uint32_t height, - sptr surface, - ScreenId mirrorId, - int32_t flags) -{ - if (screenManager_ == nullptr) { - return INVALID_SCREEN_ID; - } - - return mainThread_->ScheduleTask([=]() { - return screenManager_->CreateVirtualScreen(name, width, height, surface, mirrorId, flags); - }).get(); -} - -void RSRenderService::RemoveVirtualScreen(ScreenId id) -{ - if (screenManager_ == nullptr) { - return; - } - - mainThread_->ScheduleTask([=]() { - screenManager_->RemoveVirtualScreen(id); - }).wait(); -} - -void RSRenderService::SetScreenChangeCallback(sptr callback) -{ - if (screenManager_ == nullptr) { - return; - } - - mainThread_->ScheduleTask([=]() { - screenManager_->AddScreenChangeCallback(callback); - }).wait(); -} - -void RSRenderService::SetScreenActiveMode(ScreenId id, uint32_t modeId) -{ - if (screenManager_ == nullptr) { - return; - } - - mainThread_->ScheduleTask([=]() { - screenManager_->SetScreenActiveMode(id, modeId); - }).wait(); -} - -void RSRenderService::SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) -{ - if (screenManager_ == nullptr) { - return; - } - - mainThread_->ScheduleTask([=]() { - screenManager_->SetScreenPowerStatus(id, status); - }).wait(); -} - -void RSRenderService::TakeSurfaceCapture(NodeId id, sptr callback) -{ - std::function captureTask = [callback, id]() -> void { - ROSEN_LOGD("RSRenderService::TakeSurfaceCapture callback->OnSurfaceCapture nodeId:[%llu]", id); - ROSEN_TRACE_BEGIN(BYTRACE_TAG_GRAPHIC_AGP, "RSRenderService::TakeSurfaceCapture"); - RSSurfaceCaptureTask task(id); - std::unique_ptr pixelmap = task.Run(); - callback->OnSurfaceCapture(id, pixelmap.get()); - ROSEN_TRACE_END(BYTRACE_TAG_GRAPHIC_AGP); - }; - mainThread_->PostTask(captureTask); -} - -RSScreenModeInfo RSRenderService::GetScreenActiveMode(ScreenId id) -{ - RSScreenModeInfo screenModeInfo; - if (screenManager_ == nullptr) { - return screenModeInfo; - } - - mainThread_->ScheduleTask([=, &screenModeInfo]() { - return screenManager_->GetScreenActiveMode(id, screenModeInfo); - }).wait(); - return screenModeInfo; -} - -std::vector RSRenderService::GetScreenSupportedModes(ScreenId id) -{ - if (screenManager_ == nullptr) { - return {}; - } - - return mainThread_->ScheduleTask([=]() { - return screenManager_->GetScreenSupportedModes(id); - }).get(); -} - -RSScreenCapability RSRenderService::GetScreenCapability(ScreenId id) -{ - RSScreenCapability screenCapability; - if (screenManager_ == nullptr) { - return screenCapability; - } - - return mainThread_->ScheduleTask([=]() { - return screenManager_->GetScreenCapability(id); - }).get(); -} - -ScreenPowerStatus RSRenderService::GetScreenPowerStatus(ScreenId id) -{ - if (screenManager_ == nullptr) { - return INVALID_POWER_STATUS; - } - - return mainThread_->ScheduleTask([=]() { - return screenManager_->GetScreenPowerStatus(id); - }).get(); -} - -RSScreenData RSRenderService::GetScreenData(ScreenId id) -{ - RSScreenData screenData; - if (screenManager_ == nullptr) { - return screenData; - } - - return mainThread_->ScheduleTask([=]() { - return screenManager_->GetScreenData(id); - }).get(); -} - -int32_t RSRenderService::GetScreenBacklight(ScreenId id) -{ - if (screenManager_ == nullptr) { - return INVALID_BACKLIGHT_VALUE; - } - return mainThread_->ScheduleTask([=]() { - return screenManager_->GetScreenBacklight(id); - }).get(); -} - -void RSRenderService::SetScreenBacklight(ScreenId id, uint32_t level) -{ - if (screenManager_ == nullptr) { - return; - } - mainThread_->ScheduleTask([=]() { - screenManager_->SetScreenBacklight(id, level); - }).wait(); -} } // namespace Rosen } // namespace OHOS diff --git a/rosen/modules/render_service/core/pipeline/rs_render_service.h b/rosen/modules/render_service/core/pipeline/rs_render_service.h index afc26a504c..56df229821 100644 --- a/rosen/modules/render_service/core/pipeline/rs_render_service.h +++ b/rosen/modules/render_service/core/pipeline/rs_render_service.h @@ -15,73 +15,39 @@ #ifndef RENDER_SERVICE_PIPELINE_RS_RENDER_SERVICE_H #define RENDER_SERVICE_PIPELINE_RS_RENDER_SERVICE_H -#include +#include #include "screen_manager/rs_screen_manager.h" - #include "transaction/rs_render_service_stub.h" namespace OHOS { namespace Rosen { class RSMainThread; class RSSyncTask; +class RSRenderServiceConnection; class RSRenderService : public RSRenderServiceStub { public: - static RSRenderService& GetInstance(); + RSRenderService(); + ~RSRenderService() noexcept; + RSRenderService(const RSRenderService&) = delete; RSRenderService& operator=(const RSRenderService&) = delete; bool Init(); void Run(); - int Dump(int fd, const std::vector& args) override; private: - RSRenderService(); - ~RSRenderService() noexcept; - - void CommitTransaction(std::unique_ptr& transactionData) override; - void ExecuteSynchronousTask(const std::shared_ptr& task) override; - - sptr CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) override; - - ScreenId GetDefaultScreenId() override; - - ScreenId CreateVirtualScreen( - const std::string &name, - uint32_t width, - uint32_t height, - sptr surface, - ScreenId mirrorId, - int32_t flags) override; - - void RemoveVirtualScreen(ScreenId id) override; - - void SetScreenChangeCallback(sptr callback) override; - - void SetScreenActiveMode(ScreenId id, uint32_t modeId) override; - - void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) override; - - void TakeSurfaceCapture(NodeId id, sptr callback) override; - - RSScreenModeInfo GetScreenActiveMode(ScreenId id) override; - - std::vector GetScreenSupportedModes(ScreenId id) override; - - RSScreenCapability GetScreenCapability(ScreenId id) override; - - ScreenPowerStatus GetScreenPowerStatus(ScreenId id) override; - - RSScreenData GetScreenData(ScreenId id) override; - - int32_t GetScreenBacklight(ScreenId id) override; - - void SetScreenBacklight(ScreenId id, uint32_t level) override; + int Dump(int fd, const std::vector& args) override; + sptr CreateConnection(const sptr& token) override; + void RemoveConnection(sptr token); RSMainThread* mainThread_ = nullptr; - sptr screenManager_; + + friend class RSRenderServiceConnection; + mutable std::mutex mutex_; + std::map, sptr> connections_; }; } // Rosen } // OHOS diff --git a/rosen/modules/render_service/core/pipeline/rs_render_service_connection.cpp b/rosen/modules/render_service/core/pipeline/rs_render_service_connection.cpp new file mode 100644 index 0000000000..5ddd304382 --- /dev/null +++ b/rosen/modules/render_service/core/pipeline/rs_render_service_connection.cpp @@ -0,0 +1,291 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 "rs_render_service_connection.h" +#include "rs_main_thread.h" +#include "rs_trace.h" + +#include "pipeline/rs_render_node_map.h" +#include "pipeline/rs_render_service_listener.h" +#include "pipeline/rs_surface_render_node.h" +#include "pipeline/rs_render_node_map.h" +#include "pipeline/rs_surface_capture_task.h" +#include "platform/common/rs_log.h" + +namespace OHOS { +namespace Rosen { +// we guarantee that when constructing this object, +// all these pointers are valid, so will not check them. +RSRenderServiceConnection::RSRenderServiceConnection( + wptr renderService, + RSMainThread* mainThread, + sptr screenManager, + sptr token) + : renderService_(renderService), + mainThread_(mainThread), + screenManager_(screenManager), + token_(token), + connDeathRecipient_(new RSConnectionDeathRecipient(this)) +{ + if (!token_->AddDeathRecipient(connDeathRecipient_)) { + ROSEN_LOGW("RSRenderServiceConnection: Failed to set death recipient."); + } +} + +RSRenderServiceConnection::~RSRenderServiceConnection() noexcept +{ + CleanAll(); +} + +void RSRenderServiceConnection::CleanAll(bool toDelete) noexcept +{ + { + std::lock_guard lock(mutex_); + if (cleanDone_) { + return; + } + } + + ROSEN_LOGD("RSRenderServiceConnection::CleanAll() start."); + mainThread_->ScheduleTask([this]() { + std::lock_guard lock(mutex_); + for (auto &id : virtualScreenIds_) { + screenManager_->RemoveVirtualScreen(id); + } + virtualScreenIds_.clear(); + if (screenChangeCallback_ != nullptr) { + screenManager_->RemoveScreenChangeCallback(screenChangeCallback_); + screenChangeCallback_ = nullptr; + } + }).wait(); + + { + std::lock_guard lock(mutex_); + cleanDone_ = true; + } + + if (toDelete) { + auto renderService = renderService_.promote(); + if (renderService == nullptr) { + ROSEN_LOGW("RSRenderServiceConnection::CleanAll() RenderService is dead."); + } else { + renderService->RemoveConnection(GetToken()); + } + } + + ROSEN_LOGD("RSRenderServiceConnection::CleanAll() end."); +} + +RSRenderServiceConnection::RSConnectionDeathRecipient::RSConnectionDeathRecipient( + wptr conn) : conn_(conn) +{ +} + +void RSRenderServiceConnection::RSConnectionDeathRecipient::OnRemoteDied( + const wptr &token) +{ + auto tokenSptr = token.promote(); + if (tokenSptr == nullptr) { + ROSEN_LOGW("RSConnectionDeathRecipient::OnRemoteDied: can't promote remote object."); + return; + } + + auto rsConn = conn_.promote(); + if (rsConn == nullptr) { + ROSEN_LOGW("RSConnectionDeathRecipient::OnRemoteDied: RSRenderServiceConnection was dead, do nothing."); + return; + } + + if (rsConn->GetToken() != tokenSptr) { + ROSEN_LOGI("RSConnectionDeathRecipient::OnRemoteDied: token doesn't match, ignore it."); + return; + } + + ROSEN_LOGI("RSConnectionDeathRecipient::OnRemoteDied: do the clean work."); + rsConn->CleanAll(true); +} + +void RSRenderServiceConnection::CommitTransaction(std::unique_ptr& transactionData) +{ + mainThread_->RecvRSTransactionData(transactionData); +} + +void RSRenderServiceConnection::ExecuteSynchronousTask(const std::shared_ptr& task) +{ + if (task == nullptr) { + return; + } + + auto& context = mainThread_->GetContext(); + mainThread_->ScheduleTask([task, &context]() { + task->Process(context); + }).wait_for(std::chrono::nanoseconds(task->GetTimeout())); +} + +sptr RSRenderServiceConnection::CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) +{ + std::shared_ptr node = std::make_shared(config); + if (node == nullptr) { + ROSEN_LOGE("RSRenderService::CreateNodeAndSurface CreateNode fail"); + return nullptr; + } + sptr surface = Surface::CreateSurfaceAsConsumer(); + if (surface == nullptr) { + ROSEN_LOGE("RSRenderService::CreateNodeAndSurface get consumer surface fail"); + return nullptr; + } + node->SetConsumer(surface); + std::function registerNode = [node, this]() -> void { + this->mainThread_->GetContext().GetNodeMap().RegisterRenderNode(node); + }; + mainThread_->PostTask(registerNode); + std::weak_ptr surfaceRenderNode(node); + sptr listener = new RSRenderServiceListener(surfaceRenderNode); + SurfaceError ret = surface->RegisterConsumerListener(listener); + if (ret != SURFACE_ERROR_OK) { + ROSEN_LOGE("RSRenderService::CreateNodeAndSurface Register Consumer Listener fail"); + return nullptr; + } + return surface; +} + +ScreenId RSRenderServiceConnection::GetDefaultScreenId() +{ + return mainThread_->ScheduleTask([this]() { + return screenManager_->GetDefaultScreenId(); + }).get(); +} + +ScreenId RSRenderServiceConnection::CreateVirtualScreen( + const std::string &name, + uint32_t width, + uint32_t height, + sptr surface, + ScreenId mirrorId, + int32_t flags) +{ + std::lock_guard lock(mutex_); + auto newVirtualScreenId = screenManager_->CreateVirtualScreen(name, width, height, surface, mirrorId, flags); + virtualScreenIds_.insert(newVirtualScreenId); + return newVirtualScreenId; +} + +void RSRenderServiceConnection::RemoveVirtualScreen(ScreenId id) +{ + std::lock_guard lock(mutex_); + screenManager_->RemoveVirtualScreen(id); + virtualScreenIds_.erase(id); +} + +void RSRenderServiceConnection::SetScreenChangeCallback(sptr callback) +{ + std::unique_lock lock(mutex_); + if (screenChangeCallback_ == callback) { + return; + } + + if (screenChangeCallback_ != nullptr) { + // remove the old callback + screenManager_->RemoveScreenChangeCallback(screenChangeCallback_); + } + + // update + screenManager_->AddScreenChangeCallback(callback); + auto tmp = screenChangeCallback_; + screenChangeCallback_ = callback; + lock.unlock(); +} + +void RSRenderServiceConnection::SetScreenActiveMode(ScreenId id, uint32_t modeId) +{ + mainThread_->ScheduleTask([=]() { + screenManager_->SetScreenActiveMode(id, modeId); + }).wait(); +} + +void RSRenderServiceConnection::SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) +{ + mainThread_->ScheduleTask([=]() { + screenManager_->SetScreenPowerStatus(id, status); + }).wait(); +} + +void RSRenderServiceConnection::TakeSurfaceCapture(NodeId id, sptr callback) +{ + std::function captureTask = [callback, id]() -> void { + ROSEN_LOGD("RSRenderService::TakeSurfaceCapture callback->OnSurfaceCapture nodeId:[%llu]", id); + ROSEN_TRACE_BEGIN(BYTRACE_TAG_GRAPHIC_AGP, "RSRenderService::TakeSurfaceCapture"); + RSSurfaceCaptureTask task(id); + std::unique_ptr pixelmap = task.Run(); + callback->OnSurfaceCapture(id, pixelmap.get()); + ROSEN_TRACE_END(BYTRACE_TAG_GRAPHIC_AGP); + }; + mainThread_->PostTask(captureTask); +} + +RSScreenModeInfo RSRenderServiceConnection::GetScreenActiveMode(ScreenId id) +{ + RSScreenModeInfo screenModeInfo; + mainThread_->ScheduleTask([=, &screenModeInfo]() { + return screenManager_->GetScreenActiveMode(id, screenModeInfo); + }).wait(); + return screenModeInfo; +} + +std::vector RSRenderServiceConnection::GetScreenSupportedModes(ScreenId id) +{ + return mainThread_->ScheduleTask([=]() { + return screenManager_->GetScreenSupportedModes(id); + }).get(); +} + +RSScreenCapability RSRenderServiceConnection::GetScreenCapability(ScreenId id) +{ + RSScreenCapability screenCapability; + return mainThread_->ScheduleTask([=]() { + return screenManager_->GetScreenCapability(id); + }).get(); +} + +ScreenPowerStatus RSRenderServiceConnection::GetScreenPowerStatus(ScreenId id) +{ + return mainThread_->ScheduleTask([=]() { + return screenManager_->GetScreenPowerStatus(id); + }).get(); +} + +RSScreenData RSRenderServiceConnection::GetScreenData(ScreenId id) +{ + RSScreenData screenData; + return mainThread_->ScheduleTask([=]() { + return screenManager_->GetScreenData(id); + }).get(); +} + +int32_t RSRenderServiceConnection::GetScreenBacklight(ScreenId id) +{ + return mainThread_->ScheduleTask([=]() { + return screenManager_->GetScreenBacklight(id); + }).get(); +} + +void RSRenderServiceConnection::SetScreenBacklight(ScreenId id, uint32_t level) +{ + mainThread_->ScheduleTask([=]() { + screenManager_->SetScreenBacklight(id, level); + }).wait(); +} +} // namespace Rosen +} // namespace OHOS diff --git a/rosen/modules/render_service/core/pipeline/rs_render_service_connection.h b/rosen/modules/render_service/core/pipeline/rs_render_service_connection.h new file mode 100644 index 0000000000..8ed082af5a --- /dev/null +++ b/rosen/modules/render_service/core/pipeline/rs_render_service_connection.h @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef RENDER_SERVICE_PIPELINE_RS_RENDER_SERVICE_CONNECTION_H +#define RENDER_SERVICE_PIPELINE_RS_RENDER_SERVICE_CONNECTION_H + +#include +#include + +#include "pipeline/rs_render_service.h" +#include "screen_manager/rs_screen_manager.h" +#include "transaction/rs_render_service_connection_stub.h" + +namespace OHOS { +namespace Rosen { +class RSRenderServiceConnection : public RSRenderServiceConnectionStub { +public: + RSRenderServiceConnection( + wptr renderService, + RSMainThread* mainThread, + sptr screenManager, + sptr token); + ~RSRenderServiceConnection() noexcept; + RSRenderServiceConnection(const RSRenderServiceConnection&) = delete; + RSRenderServiceConnection& operator=(const RSRenderServiceConnection&) = delete; + + sptr GetToken() const + { + return token_; + } +private: + void CleanAll(bool toDelete = false) noexcept; + + // IPC RSIRenderServiceConnection Interfaces + void CommitTransaction(std::unique_ptr& transactionData) override; + void ExecuteSynchronousTask(const std::shared_ptr& task) override; + + sptr CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) override; + + ScreenId GetDefaultScreenId() override; + + ScreenId CreateVirtualScreen( + const std::string &name, + uint32_t width, + uint32_t height, + sptr surface, + ScreenId mirrorId, + int32_t flags) override; + + void RemoveVirtualScreen(ScreenId id) override; + + void SetScreenChangeCallback(sptr callback) override; + + void SetScreenActiveMode(ScreenId id, uint32_t modeId) override; + + void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) override; + + void TakeSurfaceCapture(NodeId id, sptr callback) override; + + RSScreenModeInfo GetScreenActiveMode(ScreenId id) override; + + std::vector GetScreenSupportedModes(ScreenId id) override; + + RSScreenCapability GetScreenCapability(ScreenId id) override; + + ScreenPowerStatus GetScreenPowerStatus(ScreenId id) override; + + RSScreenData GetScreenData(ScreenId id) override; + + int32_t GetScreenBacklight(ScreenId id) override; + + void SetScreenBacklight(ScreenId id, uint32_t level) override; + + wptr renderService_; + RSMainThread* mainThread_ = nullptr; + sptr screenManager_; + sptr token_; + + class RSConnectionDeathRecipient : public IRemoteObject::DeathRecipient { + public: + explicit RSConnectionDeathRecipient(wptr conn); + virtual ~RSConnectionDeathRecipient() = default; + + void OnRemoteDied(const wptr &remote) override; + + private: + wptr conn_; + }; + friend class RSConnectionDeathRecipient; + sptr connDeathRecipient_; + mutable std::mutex mutex_; + bool cleanDone_ = false; + + // save all virtual screenIds created by this connection. + std::unordered_set virtualScreenIds_; + sptr screenChangeCallback_; +}; +} // Rosen +} // OHOS + +#endif // RENDER_SERVICE_PIPELINE_RS_RENDER_SERVICE_CONNECTION_H diff --git a/rosen/modules/render_service/core/screen_manager/rs_screen_change_callback_death_recipient.cpp b/rosen/modules/render_service/core/screen_manager/rs_screen_change_callback_death_recipient.cpp deleted file mode 100644 index a38793b511..0000000000 --- a/rosen/modules/render_service/core/screen_manager/rs_screen_change_callback_death_recipient.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * 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 "rs_screen_change_callback_death_recipient.h" - -namespace OHOS { -namespace Rosen { -RSScreenChangeCallbackDeathRecipient::RSScreenChangeCallbackDeathRecipient(sptr screenManager) - : screenManager_(std::move(screenManager)) -{ -} - -void RSScreenChangeCallbackDeathRecipient::OnRemoteDied(const wptr &remote) -{ - screenManager_->OnRemoteScreenChangeCallbackDied(remote); -} -} // namespace Rosen -} // namespace OHOS diff --git a/rosen/modules/render_service/core/screen_manager/rs_screen_manager.cpp b/rosen/modules/render_service/core/screen_manager/rs_screen_manager.cpp index df363fd281..87e1832d16 100644 --- a/rosen/modules/render_service/core/screen_manager/rs_screen_manager.cpp +++ b/rosen/modules/render_service/core/screen_manager/rs_screen_manager.cpp @@ -15,7 +15,6 @@ #include "rs_screen_manager.h" #include "pipeline/rs_main_thread.h" -#include "rs_screen_change_callback_death_recipient.h" #include @@ -497,11 +496,6 @@ void RSScreenManager::AddScreenChangeCallback(const sptr deathRecipient(new RSScreenChangeCallbackDeathRecipient(instance_)); - if (callback->AsObject()->AddDeathRecipient(deathRecipient) == false) { - HiLog::Warn(LOG_LABEL, "%{public}s: Failed to add callback's death recipient.", __func__); - } - std::lock_guard lock(mutex_); // when the callback first registered, maybe there were some physical screens already connected, // so notify to remote immediately. @@ -514,16 +508,11 @@ void RSScreenManager::AddScreenChangeCallback(const sptr &remote) +void RSScreenManager::RemoveScreenChangeCallback(const sptr &callback) { - auto sptrRemote = remote.promote(); - if (sptrRemote == nullptr) { - return; - } - std::lock_guard lock(mutex_); for (auto it = screenChangeCallbacks_.begin(); it != screenChangeCallbacks_.end(); it++) { - if ((*it)->AsObject() == sptrRemote) { + if (*it == callback) { screenChangeCallbacks_.erase(it); HiLog::Debug(LOG_LABEL, "%{public}s: remove a remote callback succeed.", __func__); break; diff --git a/rosen/modules/render_service/core/screen_manager/rs_screen_manager.h b/rosen/modules/render_service/core/screen_manager/rs_screen_manager.h index a34bed2187..58562aa554 100644 --- a/rosen/modules/render_service/core/screen_manager/rs_screen_manager.h +++ b/rosen/modules/render_service/core/screen_manager/rs_screen_manager.h @@ -25,7 +25,6 @@ #include #include #include -#include #include #include #include @@ -100,7 +99,7 @@ public: virtual void AddScreenChangeCallback(const sptr &callback) = 0; - virtual void OnRemoteScreenChangeCallbackDied(const wptr &remote) = 0; + virtual void RemoveScreenChangeCallback(const sptr &callback) = 0; virtual void ProcessScreenHotPlugEvents() = 0; @@ -172,7 +171,7 @@ public: void AddScreenChangeCallback(const sptr &callback) override; - void OnRemoteScreenChangeCallbackDied(const wptr &remote) override; + void RemoveScreenChangeCallback(const sptr &callback) override; void ProcessScreenHotPlugEvents() override; diff --git a/rosen/modules/render_service/core/transaction/rs_render_service_connection_stub.cpp b/rosen/modules/render_service/core/transaction/rs_render_service_connection_stub.cpp new file mode 100644 index 0000000000..824cefcfd2 --- /dev/null +++ b/rosen/modules/render_service/core/transaction/rs_render_service_connection_stub.cpp @@ -0,0 +1,254 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 "rs_render_service_connection_stub.h" + +#include "command/rs_command_factory.h" + +namespace OHOS { +namespace Rosen { +int RSRenderServiceConnectionStub::OnRemoteRequest( + uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + int ret = ERR_NONE; + switch (code) { + case COMMIT_TRANSACTION: { + auto token = data.ReadInterfaceToken(); + auto transactionData = data.ReadParcelable(); + std::unique_ptr transData(transactionData); + CommitTransaction(transData); + break; + } + case CREATE_NODE_AND_SURFACE: { + auto nodeId = data.ReadUint64(); + RSSurfaceRenderNodeConfig config = {.id = nodeId}; + sptr surface = CreateNodeAndSurface(config); + auto producer = surface->GetProducer(); + reply.WriteRemoteObject(producer->AsObject()); + break; + } + case GET_DEFAULT_SCREEN_ID: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + ScreenId id = GetDefaultScreenId(); + reply.WriteUint64(id); + break; + } + case CREATE_VIRTUAL_SCREEN: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + + // read the parcel data. + std::string name = data.ReadString(); + uint32_t width = data.ReadUint32(); + uint32_t height = data.ReadUint32(); + auto remoteObject = data.ReadRemoteObject(); + if (remoteObject == nullptr) { + ret = ERR_NULL_OBJECT; + break; + } + auto bufferProducer = iface_cast(remoteObject); + sptr surface = Surface::CreateSurfaceAsProducer(bufferProducer); + ScreenId mirrorId = data.ReadUint64(); + int32_t flags = data.ReadInt32(); + + ScreenId id = CreateVirtualScreen(name, width, height, surface, mirrorId, flags); + reply.WriteUint64(id); + break; + } + case REMOVE_VIRTUAL_SCREEN: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + ScreenId id = data.ReadUint64(); + RemoveVirtualScreen(id); + break; + } + case SET_SCREEN_CHANGE_CALLBACK: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + + auto remoteObject = data.ReadRemoteObject(); + if (remoteObject == nullptr) { + ret = ERR_NULL_OBJECT; + break; + } + sptr cb = iface_cast(remoteObject); + SetScreenChangeCallback(cb); + break; + } + case SET_SCREEN_ACTIVE_MODE: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + ScreenId id = data.ReadUint64(); + uint32_t modeId = data.ReadUint32(); + SetScreenActiveMode(id, modeId); + break; + } + case SET_SCREEN_POWER_STATUS: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + ScreenId id = data.ReadUint64(); + uint32_t status = data.ReadUint32(); + SetScreenPowerStatus(id, static_cast(status)); + break; + } + case TAKE_SURFACE_CAPTURE: { + NodeId id = data.ReadUint64(); + auto remoteObject = data.ReadRemoteObject(); + if (remoteObject == nullptr) { + ret = ERR_NULL_OBJECT; + break; + } + sptr cb = iface_cast(remoteObject); + TakeSurfaceCapture(id, cb); + break; + } + case GET_SCREEN_ACTIVE_MODE: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + ScreenId id = data.ReadUint64(); + RSScreenModeInfo screenModeInfo = GetScreenActiveMode(id); + reply.WriteParcelable(&screenModeInfo); + break; + } + case GET_SCREEN_SUPPORTED_MODES: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + ScreenId id = data.ReadUint64(); + std::vector screenSupportedModes = GetScreenSupportedModes(id); + reply.WriteUint64(static_cast(screenSupportedModes.size())); + for (uint32_t modeIndex = 0; modeIndex < screenSupportedModes.size(); modeIndex++) { + reply.WriteParcelable(&screenSupportedModes[modeIndex]); + } + break; + } + case GET_SCREEN_CAPABILITY: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + ScreenId id = data.ReadUint64(); + RSScreenCapability screenCapability = GetScreenCapability(id); + reply.WriteParcelable(&screenCapability); + break; + } + case GET_SCREEN_POWER_STATUS: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + ScreenId id = data.ReadUint64(); + ScreenPowerStatus status = GetScreenPowerStatus(id); + reply.WriteUint32(static_cast(status)); + break; + } + case GET_SCREEN_DATA: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + ScreenId id = data.ReadUint64(); + RSScreenData screenData = GetScreenData(id); + reply.WriteParcelable(&screenData); + break; + } + case EXECUTE_SYNCHRONOUS_TASK: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + auto type = data.ReadUint16(); + auto subType = data.ReadUint16(); + if (type != RS_NODE_SYNCHRONOUS_READ_PROPERTY) { + ret = ERR_INVALID_STATE; + break; + } + auto func = RSCommandFactory::Instance().GetUnmarshallingFunc(type, subType); + if (func == nullptr) { + ret = ERR_INVALID_STATE; + break; + } + auto command = static_cast((*func)(data)); + if (command == nullptr) { + ret = ERR_INVALID_STATE; + break; + } + std::shared_ptr task(command); + ExecuteSynchronousTask(task); + if (!task->Marshalling(reply)) { + ret = ERR_INVALID_STATE; + } + break; + } + case GET_SCREEN_BACK_LIGHT: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + ScreenId id = data.ReadUint64(); + int32_t level = GetScreenBacklight(id); + reply.WriteInt32(level); + break; + } + case SET_SCREEN_BACK_LIGHT: { + auto token = data.ReadInterfaceToken(); + if (token != RSIRenderServiceConnection::GetDescriptor()) { + ret = ERR_INVALID_STATE; + break; + } + ScreenId id = data.ReadUint64(); + uint32_t level = data.ReadUint32(); + SetScreenBacklight(id, level); + break; + } + default: { + ret = ERR_UNKNOWN_TRANSACTION; + break; + } + } + + return ret; +} +} // namespace Rosen +} // namespace OHOS diff --git a/rosen/modules/render_service/core/screen_manager/rs_screen_change_callback_death_recipient.h b/rosen/modules/render_service/core/transaction/rs_render_service_connection_stub.h similarity index 50% rename from rosen/modules/render_service/core/screen_manager/rs_screen_change_callback_death_recipient.h rename to rosen/modules/render_service/core/transaction/rs_render_service_connection_stub.h index 635cb87408..9fb1208430 100644 --- a/rosen/modules/render_service/core/screen_manager/rs_screen_change_callback_death_recipient.h +++ b/rosen/modules/render_service/core/transaction/rs_render_service_connection_stub.h @@ -1,38 +1,37 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * 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. - */ - -#ifndef RS_SCREEN_CHANGE_CALLBACK_DEATH_RECIPIENT_H -#define RS_SCREEN_CHANGE_CALLBACK_DEATH_RECIPIENT_H - -#include - -#include "rs_screen_manager.h" - -namespace OHOS { -namespace Rosen { -class RSScreenChangeCallbackDeathRecipient : public IRemoteObject::DeathRecipient { -public: - RSScreenChangeCallbackDeathRecipient(sptr screenManager); - virtual ~RSScreenChangeCallbackDeathRecipient() = default; - - void OnRemoteDied(const wptr &remote) override; - -private: - sptr screenManager_ = nullptr; -}; -} // namespace Rosen -} // namespace OHOS - -#endif // RS_SCREEN_CHANGE_CALLBACK_DEATH_RECIPIENT_H +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECTION_STUB_H +#define ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECTION_STUB_H + +#include +#include +#include + +#include "platform/ohos/rs_irender_service_connection.h" + +namespace OHOS { +namespace Rosen { +class RSRenderServiceConnectionStub : public IRemoteStub { +public: + RSRenderServiceConnectionStub() = default; + ~RSRenderServiceConnectionStub() noexcept = default; + + int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; +}; +} // namespace Rosen +} // namespace OHOS + +#endif // ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECTION_STUB_H diff --git a/rosen/modules/render_service/core/transaction/rs_render_service_stub.cpp b/rosen/modules/render_service/core/transaction/rs_render_service_stub.cpp index 5047154da7..d3791d1082 100644 --- a/rosen/modules/render_service/core/transaction/rs_render_service_stub.cpp +++ b/rosen/modules/render_service/core/transaction/rs_render_service_stub.cpp @@ -15,241 +15,54 @@ #include "rs_render_service_stub.h" -#include "command/rs_command.h" -#include "command/rs_command_factory.h" -#include "transaction/rs_transaction_data.h" +#include namespace OHOS { namespace Rosen { +class RSConnectionTokenProxy : public IRemoteProxy { +public: + explicit RSConnectionTokenProxy(const sptr& impl) + : IRemoteProxy(impl) {} + virtual ~RSConnectionTokenProxy() noexcept = default; + +private: + static inline BrokerDelegator delegator_; +}; int RSRenderServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) { - int ret = 0; + int ret = ERR_NONE; switch (code) { - case COMMIT_TRANSACTION: { - auto token = data.ReadInterfaceToken(); - auto transactionData = data.ReadParcelable(); - std::unique_ptr transData(transactionData); - CommitTransaction(transData); - break; - } - case CREATE_SURFACE: { - auto nodeId = data.ReadUint64(); - RSSurfaceRenderNodeConfig config = {.id = nodeId}; - sptr surface = CreateNodeAndSurface(config); - auto producer = surface->GetProducer(); - reply.WriteRemoteObject(producer->AsObject()); - break; - } - case GET_DEFAULT_SCREEN_ID: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - ScreenId id = GetDefaultScreenId(); - reply.WriteUint64(id); - break; - } - case CREATE_VIRTUAL_DISPLAY: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { + case CREATE_CONNECTION: { + auto interfaceToken = data.ReadInterfaceToken(); + if (interfaceToken != RSIRenderService::GetDescriptor()) { ret = ERR_INVALID_STATE; break; } - // read the parcel data. - std::string name = data.ReadString(); - uint32_t width = data.ReadUint32(); - uint32_t height = data.ReadUint32(); - auto remoteObject = data.ReadRemoteObject(); - if (remoteObject == nullptr) { + auto remoteObj = data.ReadRemoteObject(); + if (remoteObj == nullptr) { ret = ERR_NULL_OBJECT; break; } - auto bufferProducer = iface_cast(remoteObject); - sptr surface = Surface::CreateSurfaceAsProducer(bufferProducer); - ScreenId mirrorId = data.ReadUint64(); - int32_t flags = data.ReadInt32(); - - ScreenId id = CreateVirtualScreen(name, width, height, surface, mirrorId, flags); - reply.WriteUint64(id); - break; - } - case REMOVE_VIRTUAL_DISPLAY: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - ScreenId id = data.ReadUint64(); - RemoveVirtualScreen(id); - break; - } - case SET_SCREEN_CHANGE_CALLBACK: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - auto remoteObject = data.ReadRemoteObject(); - if (remoteObject == nullptr) { - ret = ERR_NULL_OBJECT; + if (!remoteObj->IsProxyObject()) { + ret = ERR_UNKNOWN_OBJECT; break; } - sptr cb = iface_cast(remoteObject); - SetScreenChangeCallback(cb); + + auto token = iface_cast(remoteObj); + auto newConn = CreateConnection(token); + reply.WriteRemoteObject(newConn->AsObject()); break; } - case SET_SCREEN_ACTIVE_MODE: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - ScreenId id = data.ReadUint64(); - uint32_t modeId = data.ReadUint32(); - SetScreenActiveMode(id, modeId); + default: { + ret = ERR_UNKNOWN_TRANSACTION; break; } - case SET_SCREEN_POWER_STATUS: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - ScreenId id = data.ReadUint64(); - uint32_t status = data.ReadUint32(); - SetScreenPowerStatus(id, static_cast(status)); - break; - } - case TAKE_SURFACE_CAPTURE: { - NodeId id = data.ReadUint64(); - auto remoteObject = data.ReadRemoteObject(); - if (remoteObject == nullptr) { - ret = ERR_NULL_OBJECT; - break; - } - sptr cb = iface_cast(remoteObject); - TakeSurfaceCapture(id, cb); - break; - } - case GET_SCREEN_ACTIVE_MODE: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - ScreenId id = data.ReadUint64(); - RSScreenModeInfo screenModeInfo = GetScreenActiveMode(id); - reply.WriteParcelable(&screenModeInfo); - break; - } - case GET_SCREEN_SUPPORTED_MODES: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - ScreenId id = data.ReadUint64(); - std::vector screenSupportedModes = GetScreenSupportedModes(id); - reply.WriteUint64(static_cast(screenSupportedModes.size())); - for (uint32_t modeIndex = 0; modeIndex < screenSupportedModes.size(); modeIndex++) { - reply.WriteParcelable(&screenSupportedModes[modeIndex]); - } - break; - } - case GET_SCREEN_CAPABILITY: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - ScreenId id = data.ReadUint64(); - RSScreenCapability screenCapability = GetScreenCapability(id); - reply.WriteParcelable(&screenCapability); - break; - } - case GET_SCREEN_POWER_STATUS: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - ScreenId id = data.ReadUint64(); - ScreenPowerStatus status = GetScreenPowerStatus(id); - reply.WriteUint32(static_cast(status)); - break; - } - case GET_SCREEN_DATA: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - ScreenId id = data.ReadUint64(); - RSScreenData screenData = GetScreenData(id); - reply.WriteParcelable(&screenData); - break; - } - case EXECUTE_SYNCHRONOUS_TASK: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - auto type = data.ReadUint16(); - auto subType = data.ReadUint16(); - if (type != RS_NODE_SYNCHRONOUS_READ_PROPERTY) { - ret = ERR_INVALID_STATE; - break; - } - auto func = RSCommandFactory::Instance().GetUnmarshallingFunc(type, subType); - if (func == nullptr) { - ret = ERR_INVALID_STATE; - break; - } - auto command = static_cast((*func)(data)); - if (command == nullptr) { - ret = ERR_INVALID_STATE; - break; - } - std::shared_ptr task(command); - ExecuteSynchronousTask(task); - if (!task->Marshalling(reply)) { - ret = ERR_INVALID_STATE; - } - break; - } - case GET_SCREEN_BACK_LIGHT: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - ScreenId id = data.ReadUint64(); - int32_t level = GetScreenBacklight(id); - reply.WriteInt32(level); - break; - } - case SET_SCREEN_BACK_LIGHT: { - auto token = data.ReadInterfaceToken(); - if (token != RSIRenderService::GetDescriptor()) { - ret = ERR_INVALID_STATE; - break; - } - ScreenId id = data.ReadUint64(); - uint32_t level = data.ReadUint32(); - SetScreenBacklight(id, level); - break; - } - default: - break; } return ret; } - } // namespace Rosen } // namespace OHOS diff --git a/rosen/modules/render_service/core/transaction/rs_render_service_stub.h b/rosen/modules/render_service/core/transaction/rs_render_service_stub.h index 15cfd17576..dbc84064f2 100644 --- a/rosen/modules/render_service/core/transaction/rs_render_service_stub.h +++ b/rosen/modules/render_service/core/transaction/rs_render_service_stub.h @@ -21,19 +21,16 @@ #include #include "platform/ohos/rs_irender_service.h" -#include "screen_manager/screen_types.h" namespace OHOS { namespace Rosen { - class RSRenderServiceStub : public IRemoteStub { public: RSRenderServiceStub() = default; - ~RSRenderServiceStub() = default; + ~RSRenderServiceStub() noexcept = default; int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; }; - } // namespace Rosen } // namespace OHOS diff --git a/rosen/modules/render_service_base/include/platform/ohos/rs_irender_service.h b/rosen/modules/render_service_base/include/platform/ohos/rs_irender_service.h index 74c0df8e2c..0174b05897 100644 --- a/rosen/modules/render_service_base/include/platform/ohos/rs_irender_service.h +++ b/rosen/modules/render_service_base/include/platform/ohos/rs_irender_service.h @@ -16,93 +16,30 @@ #ifndef ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_IRENDER_SERVICE_H #define ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_IRENDER_SERVICE_H -#include -#include -#include - -#include -#include - -#include "command/rs_command.h" -#include "screen_manager/screen_types.h" -#include "screen_manager/rs_screen_mode_info.h" -#include "screen_manager/rs_screen_capability.h" -#include "screen_manager/rs_screen_data.h" -#include "ipc_callbacks/screen_change_callback.h" -#include "ipc_callbacks/surface_capture_callback.h" -#include "transaction/rs_transaction_data.h" +#include "rs_irender_service_connection.h" namespace OHOS { namespace Rosen { -class RSSyncTask; +class RSIConnectionToken : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.rosen.RSConnectionToken"); + + RSIConnectionToken() = default; + virtual ~RSIConnectionToken() noexcept = default; +}; class RSIRenderService : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"ohos.rosen.RenderService"); RSIRenderService() = default; - virtual ~RSIRenderService() = default; + virtual ~RSIRenderService() noexcept = default; enum { - COMMIT_TRANSACTION, - CREATE_SURFACE, - GET_DEFAULT_SCREEN_ID, - CREATE_VIRTUAL_DISPLAY, - REMOVE_VIRTUAL_DISPLAY, - SET_SCREEN_CHANGE_CALLBACK, - SET_SCREEN_ACTIVE_MODE, - SET_SCREEN_POWER_STATUS, - TAKE_SURFACE_CAPTURE, - SET_SCREEN_BACK_LIGHT, - GET_SCREEN_ACTIVE_MODE, - GET_SCREEN_SUPPORTED_MODES, - GET_SCREEN_CAPABILITY, - GET_SCREEN_POWER_STATUS, - GET_SCREEN_DATA, - EXECUTE_SYNCHRONOUS_TASK, - GET_SCREEN_BACK_LIGHT, + CREATE_CONNECTION, }; - virtual void CommitTransaction(std::unique_ptr& transactionData) = 0; - - virtual void ExecuteSynchronousTask(const std::shared_ptr& task) = 0; - - virtual sptr CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) = 0; - - virtual ScreenId GetDefaultScreenId() = 0; - - // mirrorId: decide which screen id to mirror, INVALID_SCREEN_ID means do not mirror any screen. - virtual ScreenId CreateVirtualScreen( - const std::string &name, - uint32_t width, - uint32_t height, - sptr surface, - ScreenId mirrorId, - int32_t flags) = 0; - - virtual void RemoveVirtualScreen(ScreenId id) = 0; - - virtual void SetScreenChangeCallback(sptr callback) = 0; - - virtual void SetScreenActiveMode(ScreenId id, uint32_t modeId) = 0; - - virtual void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) = 0; - - virtual void TakeSurfaceCapture(NodeId id, sptr callback) = 0; - - virtual RSScreenModeInfo GetScreenActiveMode(ScreenId id) = 0; - - virtual std::vector GetScreenSupportedModes(ScreenId id) = 0; - - virtual RSScreenCapability GetScreenCapability(ScreenId id) = 0; - - virtual ScreenPowerStatus GetScreenPowerStatus(ScreenId id) = 0; - - virtual RSScreenData GetScreenData(ScreenId id) = 0; - - virtual int32_t GetScreenBacklight(ScreenId id) = 0; - - virtual void SetScreenBacklight(ScreenId id, uint32_t level) = 0; + virtual sptr CreateConnection(const sptr& token) = 0; }; } // namespace Rosen } // namespace OHOS diff --git a/rosen/modules/render_service_base/include/platform/ohos/rs_irender_service_connection.h b/rosen/modules/render_service_base/include/platform/ohos/rs_irender_service_connection.h new file mode 100644 index 0000000000..6d0314e613 --- /dev/null +++ b/rosen/modules/render_service_base/include/platform/ohos/rs_irender_service_connection.h @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_IRENDER_SERVICE_CONNECTION_H +#define ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_IRENDER_SERVICE_CONNECTION_H + +#include + +#include +#include + +#include "command/rs_command.h" +#include "screen_manager/screen_types.h" +#include "screen_manager/rs_screen_mode_info.h" +#include "screen_manager/rs_screen_capability.h" +#include "screen_manager/rs_screen_data.h" +#include "ipc_callbacks/screen_change_callback.h" +#include "ipc_callbacks/surface_capture_callback.h" +#include "transaction/rs_transaction_data.h" + +namespace OHOS { +namespace Rosen { +class RSSyncTask; + +class RSIRenderServiceConnection : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.rosen.RenderServiceConnection"); + + RSIRenderServiceConnection() = default; + virtual ~RSIRenderServiceConnection() noexcept = default; + + enum { + COMMIT_TRANSACTION, + CREATE_NODE_AND_SURFACE, + GET_DEFAULT_SCREEN_ID, + CREATE_VIRTUAL_SCREEN, + REMOVE_VIRTUAL_SCREEN, + SET_SCREEN_CHANGE_CALLBACK, + SET_SCREEN_ACTIVE_MODE, + SET_SCREEN_POWER_STATUS, + SET_SCREEN_BACK_LIGHT, + TAKE_SURFACE_CAPTURE, + GET_SCREEN_ACTIVE_MODE, + GET_SCREEN_SUPPORTED_MODES, + GET_SCREEN_CAPABILITY, + GET_SCREEN_POWER_STATUS, + GET_SCREEN_BACK_LIGHT, + GET_SCREEN_DATA, + EXECUTE_SYNCHRONOUS_TASK, + }; + + virtual void CommitTransaction(std::unique_ptr& transactionData) = 0; + + virtual void ExecuteSynchronousTask(const std::shared_ptr& task) = 0; + + virtual sptr CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) = 0; + + virtual ScreenId GetDefaultScreenId() = 0; + + // mirrorId: decide which screen id to mirror, INVALID_SCREEN_ID means do not mirror any screen. + virtual ScreenId CreateVirtualScreen( + const std::string &name, + uint32_t width, + uint32_t height, + sptr surface, + ScreenId mirrorId, + int32_t flags) = 0; + + virtual void RemoveVirtualScreen(ScreenId id) = 0; + + virtual void SetScreenChangeCallback(sptr callback) = 0; + + virtual void SetScreenActiveMode(ScreenId id, uint32_t modeId) = 0; + + virtual void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) = 0; + + virtual void TakeSurfaceCapture(NodeId id, sptr callback) = 0; + + virtual RSScreenModeInfo GetScreenActiveMode(ScreenId id) = 0; + + virtual std::vector GetScreenSupportedModes(ScreenId id) = 0; + + virtual RSScreenCapability GetScreenCapability(ScreenId id) = 0; + + virtual ScreenPowerStatus GetScreenPowerStatus(ScreenId id) = 0; + + virtual RSScreenData GetScreenData(ScreenId id) = 0; + + virtual int32_t GetScreenBacklight(ScreenId id) = 0; + + virtual void SetScreenBacklight(ScreenId id, uint32_t level) = 0; +}; +} // namespace Rosen +} // namespace OHOS + +#endif // ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_IRENDER_SERVICE_CONNECTION_H diff --git a/rosen/modules/render_service_base/src/platform/ohos/BUILD.gn b/rosen/modules/render_service_base/src/platform/ohos/BUILD.gn index 0912a43812..dbc47708e2 100644 --- a/rosen/modules/render_service_base/src/platform/ohos/BUILD.gn +++ b/rosen/modules/render_service_base/src/platform/ohos/BUILD.gn @@ -40,7 +40,8 @@ ohos_source_set("rosen_ohos_sources") { "backend/rs_surface_ohos_raster.cpp", "rs_log.cpp", "rs_render_service_client.cpp", - "rs_render_service_connect.cpp", + "rs_render_service_connect_hub.cpp", + "rs_render_service_connection_proxy.cpp", "rs_render_service_proxy.cpp", "rs_surface_frame_ohos.cpp", "rs_surface_ohos.cpp", diff --git a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_client.cpp b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_client.cpp index ff91294e8c..0b57e6a47d 100644 --- a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_client.cpp +++ b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_client.cpp @@ -21,7 +21,7 @@ #include "ipc_callbacks/screen_change_callback_stub.h" #include "ipc_callbacks/surface_capture_callback_stub.h" #include "platform/common/rs_log.h" -#include "rs_render_service_connect.h" +#include "rs_render_service_connect_hub.h" #include "rs_surface_ohos.h" namespace OHOS { @@ -34,7 +34,7 @@ std::shared_ptr RSIRenderClient::CreateRenderServiceClient() void RSRenderServiceClient::CommitTransaction(std::unique_ptr& transactionData) { - auto renderService = RSRenderServiceConnect::GetRenderService(); + auto renderService = RSRenderServiceConnectHub::GetRenderService(); if (renderService != nullptr) { renderService->CommitTransaction(transactionData); } @@ -42,7 +42,7 @@ void RSRenderServiceClient::CommitTransaction(std::unique_ptr void RSRenderServiceClient::ExecuteSynchronousTask(const std::shared_ptr& task) { - auto renderService = RSRenderServiceConnect::GetRenderService(); + auto renderService = RSRenderServiceConnectHub::GetRenderService(); if (renderService != nullptr) { renderService->ExecuteSynchronousTask(task); } @@ -50,7 +50,7 @@ void RSRenderServiceClient::ExecuteSynchronousTask(const std::shared_ptr RSRenderServiceClient::CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) { - auto renderService = RSRenderServiceConnect::GetRenderService(); + auto renderService = RSRenderServiceConnectHub::GetRenderService(); if (renderService == nullptr) { return nullptr; } @@ -104,7 +104,7 @@ private: bool RSRenderServiceClient::TakeSurfaceCapture(NodeId id, std::shared_ptr callback) { - auto renderService = RSRenderServiceConnect::GetRenderService(); + auto renderService = RSRenderServiceConnectHub::GetRenderService(); if (renderService == nullptr) { ROSEN_LOGE("RSRenderServiceClient::TakeSurfaceCapture renderService == nullptr!"); return false; @@ -131,7 +131,7 @@ bool RSRenderServiceClient::TakeSurfaceCapture(NodeId id, std::shared_ptr RSRenderServiceClient::GetScreenSupportedModes(ScreenId id) { - auto renderService = RSRenderServiceConnect::GetRenderService(); + auto renderService = RSRenderServiceConnectHub::GetRenderService(); if (renderService == nullptr) { return {}; } @@ -235,7 +235,7 @@ std::vector RSRenderServiceClient::GetScreenSupportedModes(Scr RSScreenCapability RSRenderServiceClient::GetScreenCapability(ScreenId id) { - auto renderService = RSRenderServiceConnect::GetRenderService(); + auto renderService = RSRenderServiceConnectHub::GetRenderService(); if (renderService == nullptr) { return RSScreenCapability {}; } @@ -245,7 +245,7 @@ RSScreenCapability RSRenderServiceClient::GetScreenCapability(ScreenId id) ScreenPowerStatus RSRenderServiceClient::GetScreenPowerStatus(ScreenId id) { - auto renderService = RSRenderServiceConnect::GetRenderService(); + auto renderService = RSRenderServiceConnectHub::GetRenderService(); if (renderService == nullptr) { return ScreenPowerStatus::INVALID_POWER_STATUS; } @@ -255,7 +255,7 @@ ScreenPowerStatus RSRenderServiceClient::GetScreenPowerStatus(ScreenId id) RSScreenData RSRenderServiceClient::GetScreenData(ScreenId id) { - auto renderService = RSRenderServiceConnect::GetRenderService(); + auto renderService = RSRenderServiceConnectHub::GetRenderService(); if (renderService == nullptr) { return RSScreenData {}; } @@ -265,7 +265,7 @@ RSScreenData RSRenderServiceClient::GetScreenData(ScreenId id) int32_t RSRenderServiceClient::GetScreenBacklight(ScreenId id) { - auto renderService = RSRenderServiceConnect::GetRenderService(); + auto renderService = RSRenderServiceConnectHub::GetRenderService(); if (renderService == nullptr) { return INVALID_BACKLIGHT_VALUE; } @@ -275,7 +275,7 @@ int32_t RSRenderServiceClient::GetScreenBacklight(ScreenId id) void RSRenderServiceClient::SetScreenBacklight(ScreenId id, uint32_t level) { - auto renderService = RSRenderServiceConnect::GetRenderService(); + auto renderService = RSRenderServiceConnectHub::GetRenderService(); if (renderService == nullptr) { return; } diff --git a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect.cpp b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect.cpp deleted file mode 100644 index e710aeaec2..0000000000 --- a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * 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 "rs_render_service_connect.h" - -#include -#include -#include - -#include "rs_render_service_proxy.h" -#include "platform/common/rs_log.h" - -namespace OHOS { -namespace Rosen { - -RSRenderServiceConnect::RSRenderServiceConnect() -{ -} - -RSRenderServiceConnect::~RSRenderServiceConnect() -{ -} - -sptr RSRenderServiceConnect::GetRenderService() -{ - std::lock_guard lock(RSRenderServiceConnect::GetInstance().renderConnectlock_); - - if (RSRenderServiceConnect::GetInstance().renderService_ == nullptr) { - bool result = RSRenderServiceConnect::GetInstance().Connect(); - if (!result) { - ROSEN_LOGE("RenderService connect fail"); - return nullptr; - } - } - - return RSRenderServiceConnect::GetInstance().renderService_; -} - -bool RSRenderServiceConnect::Connect() -{ - if (renderService_ != nullptr) { - return true; - } - - sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgr == nullptr) { - ROSEN_LOGE("RSRenderServiceConnect::Connect, GetSystemAbilityManager failed"); - return false; - } - - sptr remoteObject = samgr->GetSystemAbility(RENDER_SERVICE); - if (remoteObject == nullptr) { - ROSEN_LOGE("RSRenderServiceConnect::Connect, GetSystemAbility(RENDER_SERVICE) failed"); - return false; - } - - if (deathRecipient_ == nullptr) { - deathRecipient_ = sptr(new RenderServiceDeathRecipient()); - } - - if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) { - } - - renderService_ = iface_cast(remoteObject); - if (renderService_ == nullptr) { - ROSEN_LOGE("RSRenderServiceConnect::Connect, renderService_ is nullptr"); - return false; - } - - return true; -} - -void RSRenderServiceConnect::ConnectDied() -{ - std::lock_guard lock(renderConnectlock_); - renderService_ = nullptr; - deathRecipient_ = nullptr; -} - -void RSRenderServiceConnect::RenderServiceDeathRecipient::OnRemoteDied(const wptr& remote) -{ - RSRenderServiceConnect::GetInstance().ConnectDied(); -} - -} // namespace Rosen -} // namespace OHOS diff --git a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect_hub.cpp b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect_hub.cpp new file mode 100755 index 0000000000..65c59c9704 --- /dev/null +++ b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect_hub.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 "rs_render_service_connect_hub.h" + +#include +#include +#include +#include +#include +#include + +#include "message_parcel.h" +#include "rs_render_service_proxy.h" +#include "rs_render_service_connection_proxy.h" +#include "platform/common/rs_log.h" + +namespace OHOS { +namespace Rosen { +std::once_flag RSRenderServiceConnectHub::flag_; +RSRenderServiceConnectHub* RSRenderServiceConnectHub::instance_ = nullptr; + +RSRenderServiceConnectHub* RSRenderServiceConnectHub::GetInstance() +{ + std::call_once(flag_, &RSRenderServiceConnectHub::Init); + return instance_; +} + +void RSRenderServiceConnectHub::Init() +{ + instance_ = new RSRenderServiceConnectHub(); + ::atexit(&RSRenderServiceConnectHub::Destory); +} + +void RSRenderServiceConnectHub::Destory() +{ + if (instance_ != nullptr) { + delete instance_; + instance_ = nullptr; + } +} + +RSRenderServiceConnectHub::RSRenderServiceConnectHub() +{ +} + +RSRenderServiceConnectHub::~RSRenderServiceConnectHub() +{ +} + +sptr RSRenderServiceConnectHub::GetRenderService() +{ + auto connHub = RSRenderServiceConnectHub::GetInstance(); + return connHub == nullptr ? nullptr : connHub->GetRenderServiceConnection(); +} + +sptr RSRenderServiceConnectHub::GetRenderServiceConnection() +{ + std::lock_guard lock(mutex_); + + if (conn_ != nullptr && renderService_ != nullptr) { + return conn_; + } + + if (!Connect()) { + ROSEN_LOGE("RenderService connect fail"); + return nullptr; + } + + return conn_; +} + +bool RSRenderServiceConnectHub::Connect() +{ + int tryCnt = 0; + do { + // sleep move time (1000us * tryCnt) wthen tryCnt++ + usleep(1000 * tryCnt); + ++tryCnt; + // try most 5 times to get render service. + if (tryCnt == 5) { + ROSEN_LOGE("RSRenderServiceConnect::Connect failed, tried %d times.", tryCnt); + break; + } + + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + continue; + } + auto remoteObject = samgr->GetSystemAbility(RENDER_SERVICE); + if (remoteObject == nullptr || !remoteObject->IsProxyObject()) { + continue; + } + renderService_ = iface_cast(remoteObject); + if (renderService_ != nullptr) { + break; + } + } while (true); + + if (renderService_ == nullptr) { + ROSEN_LOGE("RSRenderServiceConnect::Connect, failed to get render service proxy."); + return false; + } + + deathRecipient_ = new RenderServiceDeathRecipient(this); + if (!renderService_->AsObject()->AddDeathRecipient(deathRecipient_)) { + ROSEN_LOGW("RSRenderServiceConnect::Connect, failed to AddDeathRecipient of render service."); + } + + if (token_ == nullptr) { + token_ = new IRemoteStub(); + } + + conn_ = renderService_->CreateConnection(token_); + if (conn_ == nullptr) { + ROSEN_LOGE("RSRenderServiceConnect::Connect, failed to CreateConnection to render service."); + return false; + } + + return true; +} + +void RSRenderServiceConnectHub::ConnectDied() +{ + std::lock_guard lock(mutex_); + renderService_ = nullptr; + conn_ = nullptr; + deathRecipient_ = nullptr; +} + +void RSRenderServiceConnectHub::RenderServiceDeathRecipient::OnRemoteDied(const wptr& remote) +{ + auto remoteSptr = remote.promote(); + if (remoteSptr == nullptr) { + ROSEN_LOGW("RSRenderServiceConnectHub::RenderServiceDeathRecipient::OnRemoteDied: can't promote."); + return; + } + + auto rsConnHub = rsConnHub_.promote(); + if (rsConnHub == nullptr) { + ROSEN_LOGW("RSRenderServiceConnectHub::RenderServiceDeathRecipient::OnRemoteDied: connHub was dead."); + return; + } + + rsConnHub->ConnectDied(); +} +} // namespace Rosen +} // namespace OHOS diff --git a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect.h b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect_hub.h similarity index 58% rename from rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect.h rename to rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect_hub.h index e378492582..a44ab29240 100644 --- a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect.h +++ b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connect_hub.h @@ -13,42 +13,54 @@ * limitations under the License. */ -#ifndef ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECT_H -#define ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECT_H +#ifndef ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECT_HUB_H +#define ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECT_HUB_H -#include - -#include "platform/ohos/rs_irender_service.h" +#include +#include namespace OHOS { namespace Rosen { - -class RSRenderServiceConnect : public Singleton { +class RSRenderServiceConnectHub : public RefBase { public: - static sptr GetRenderService(); + static sptr GetRenderService(); private: + static RSRenderServiceConnectHub* GetInstance(); + static void Init(); + static void Destory(); + + RSRenderServiceConnectHub(); + ~RSRenderServiceConnectHub() noexcept; + class RenderServiceDeathRecipient final : public IRemoteObject::DeathRecipient { public: - RenderServiceDeathRecipient() = default; - ~RenderServiceDeathRecipient() final = default; + RenderServiceDeathRecipient(wptr rsConnHub) + : rsConnHub_(std::move(rsConnHub)) {} + ~RenderServiceDeathRecipient() noexcept final = default; DISALLOW_COPY_AND_MOVE(RenderServiceDeathRecipient); void OnRemoteDied(const wptr &remote) final override; + + private: + wptr rsConnHub_; }; + sptr GetRenderServiceConnection(); bool Connect(); void ConnectDied(); - std::mutex renderConnectlock_; + mutable std::mutex mutex_; sptr renderService_; + sptr token_; + sptr conn_; sptr deathRecipient_; - DECLARE_SINGLETON(RSRenderServiceConnect) + static std::once_flag flag_; + static RSRenderServiceConnectHub* instance_; }; - } // namespace Rosen } // namespace OHOS -#endif // ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECT_H +#endif // ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECT_HUB_H diff --git a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connection_proxy.cpp b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connection_proxy.cpp new file mode 100644 index 0000000000..dd742e6a88 --- /dev/null +++ b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connection_proxy.cpp @@ -0,0 +1,401 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 "rs_render_service_connection_proxy.h" + +#include +#include +#include "platform/common/rs_log.h" + +namespace OHOS { +namespace Rosen { +RSRenderServiceConnectionProxy::RSRenderServiceConnectionProxy(const sptr& impl) + : IRemoteProxy(impl) +{ +} + +void RSRenderServiceConnectionProxy::CommitTransaction(std::unique_ptr& transactionData) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return; + } + + if (!data.WriteParcelable(transactionData.get())) { + return; + } + + option.SetFlags(MessageOption::TF_ASYNC); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::COMMIT_TRANSACTION, data, reply, option); + if (err != NO_ERROR) { + return; + } +} + +void RSRenderServiceConnectionProxy::ExecuteSynchronousTask(const std::shared_ptr& task) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return; + } + + if (!task->Marshalling(data)) { + return; + } + + option.SetFlags(MessageOption::TF_SYNC); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::EXECUTE_SYNCHRONOUS_TASK, data, reply, option); + if (err != NO_ERROR) { + return; + } + + if (task->CheckHeader(reply)) { + task->ReadFromParcel(reply); + } +} + +sptr RSRenderServiceConnectionProxy::CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteUint64(config.id)) { + return nullptr; + } + + option.SetFlags(MessageOption::TF_SYNC); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::CREATE_NODE_AND_SURFACE, data, reply, option); + if (err != NO_ERROR) { + return nullptr; + } + sptr surfaceObject = reply.ReadRemoteObject(); + sptr bp = iface_cast(surfaceObject); + sptr surface = Surface::CreateSurfaceAsProducer(bp); + return surface; +} + +ScreenId RSRenderServiceConnectionProxy::GetDefaultScreenId() +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return INVALID_SCREEN_ID; + } + + option.SetFlags(MessageOption::TF_SYNC); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::GET_DEFAULT_SCREEN_ID, data, reply, option); + if (err != NO_ERROR) { + return INVALID_SCREEN_ID; + } + + ScreenId id = reply.ReadUint64(); + return id; +} + +ScreenId RSRenderServiceConnectionProxy::CreateVirtualScreen( + const std::string &name, + uint32_t width, + uint32_t height, + sptr surface, + ScreenId mirrorId, + int32_t flags) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return INVALID_SCREEN_ID; + } + + option.SetFlags(MessageOption::TF_SYNC); + data.WriteString(name); + data.WriteUint32(width); + data.WriteUint32(height); + auto producer = surface->GetProducer(); + data.WriteRemoteObject(producer->AsObject()); + data.WriteUint64(mirrorId); + data.WriteInt32(flags); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::CREATE_VIRTUAL_SCREEN, data, reply, option); + if (err != NO_ERROR) { + return INVALID_SCREEN_ID; + } + + ScreenId id = reply.ReadUint64(); + return id; +} + +void RSRenderServiceConnectionProxy::RemoveVirtualScreen(ScreenId id) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return; + } + + option.SetFlags(MessageOption::TF_ASYNC); + data.WriteUint64(id); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::REMOVE_VIRTUAL_SCREEN, data, reply, option); + if (err != NO_ERROR) { + ROSEN_LOGE("RSRenderServiceConnectionProxy::RemoveVirtualScreen: Send Request err."); + } +} + +void RSRenderServiceConnectionProxy::SetScreenChangeCallback(sptr callback) +{ + if (callback == nullptr) { + ROSEN_LOGE("RSRenderServiceConnectionProxy::SetScreenChangeCallback: callback is nullptr."); + return; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return; + } + + option.SetFlags(MessageOption::TF_ASYNC); + data.WriteRemoteObject(callback->AsObject()); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::SET_SCREEN_CHANGE_CALLBACK, data, reply, option); + if (err != NO_ERROR) { + ROSEN_LOGE("RSRenderServiceConnectionProxy::SetScreenChangeCallback: Send Request err."); + } +} + +void RSRenderServiceConnectionProxy::SetScreenActiveMode(ScreenId id, uint32_t modeId) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return; + } + option.SetFlags(MessageOption::TF_SYNC); + data.WriteUint64(id); + data.WriteUint32(modeId); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::SET_SCREEN_ACTIVE_MODE, data, reply, option); + if (err != NO_ERROR) { + return; + } +} + +void RSRenderServiceConnectionProxy::SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return; + } + option.SetFlags(MessageOption::TF_SYNC); + data.WriteUint64(id); + data.WriteUint32(static_cast(status)); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::SET_SCREEN_POWER_STATUS, data, reply, option); + if (err != NO_ERROR) { + return; + } +} + +void RSRenderServiceConnectionProxy::TakeSurfaceCapture(NodeId id, sptr callback) +{ + if (callback == nullptr) { + ROSEN_LOGE("RSRenderServiceProxy: callback == nullptr\n"); + return; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + option.SetFlags(MessageOption::TF_ASYNC); + data.WriteUint64(id); + data.WriteRemoteObject(callback->AsObject()); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::TAKE_SURFACE_CAPTURE, data, reply, option); + if (err != NO_ERROR) { + ROSEN_LOGE("RSRenderServiceProxy: Remote()->SendRequest() error.\n"); + return; + } +} + +RSScreenModeInfo RSRenderServiceConnectionProxy::GetScreenActiveMode(ScreenId id) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + RSScreenModeInfo screenModeInfo; + + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return screenModeInfo; + } + option.SetFlags(MessageOption::TF_SYNC); + data.WriteUint64(id); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::GET_SCREEN_ACTIVE_MODE, data, reply, option); + if (err != NO_ERROR) { + return screenModeInfo; + } + + sptr pScreenModeInfo(reply.ReadParcelable()); + if (pScreenModeInfo == nullptr) { + return screenModeInfo; + } + screenModeInfo = *pScreenModeInfo; + return screenModeInfo; +} + +std::vector RSRenderServiceConnectionProxy::GetScreenSupportedModes(ScreenId id) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + std::vector screenSupportedModes; + + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return screenSupportedModes; + } + + option.SetFlags(MessageOption::TF_SYNC); + data.WriteUint64(id); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::GET_SCREEN_SUPPORTED_MODES, data, reply, option); + if (err != NO_ERROR) { + return screenSupportedModes; + } + + uint64_t modeCount = reply.ReadUint64(); + screenSupportedModes.resize(modeCount); + for (uint64_t modeIndex = 0; modeIndex < modeCount; modeIndex++) { + sptr itemScreenMode = reply.ReadParcelable(); + if (itemScreenMode == nullptr) { + continue; + } else { + screenSupportedModes[modeIndex] = *itemScreenMode; + } + } + return screenSupportedModes; +} + +RSScreenCapability RSRenderServiceConnectionProxy::GetScreenCapability(ScreenId id) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + RSScreenCapability screenCapability; + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return screenCapability; + } + option.SetFlags(MessageOption::TF_SYNC); + data.WriteUint64(id); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::GET_SCREEN_CAPABILITY, data, reply, option); + if (err != NO_ERROR) { + return screenCapability; + } + + sptr pScreenCapability(reply.ReadParcelable()); + if (pScreenCapability == nullptr) { + return screenCapability; + } + screenCapability = *pScreenCapability; + return screenCapability; +} + +ScreenPowerStatus RSRenderServiceConnectionProxy::GetScreenPowerStatus(ScreenId id) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return INVALID_POWER_STATUS; + } + option.SetFlags(MessageOption::TF_SYNC); + data.WriteUint64(id); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::GET_SCREEN_POWER_STATUS, data, reply, option); + if (err != NO_ERROR) { + return INVALID_POWER_STATUS; + } + return static_cast(reply.ReadUint32()); +} + +RSScreenData RSRenderServiceConnectionProxy::GetScreenData(ScreenId id) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + RSScreenData screenData; + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return screenData; + } + option.SetFlags(MessageOption::TF_SYNC); + data.WriteUint64(id); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::GET_SCREEN_DATA, data, reply, option); + if (err != NO_ERROR) { + return screenData; + } + sptr pScreenData(reply.ReadParcelable()); + if (pScreenData == nullptr) { + return screenData; + } + screenData = *pScreenData; + return screenData; +} + +int32_t RSRenderServiceConnectionProxy::GetScreenBacklight(ScreenId id) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return INVALID_BACKLIGHT_VALUE; + } + option.SetFlags(MessageOption::TF_SYNC); + data.WriteUint64(id); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::GET_SCREEN_BACK_LIGHT, data, reply, option); + if (err != NO_ERROR) { + return INVALID_BACKLIGHT_VALUE; + } + int32_t level = reply.ReadInt32(); + return level; +} + +void RSRenderServiceConnectionProxy::SetScreenBacklight(ScreenId id, uint32_t level) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(RSIRenderServiceConnection::GetDescriptor())) { + return; + } + option.SetFlags(MessageOption::TF_SYNC); + data.WriteUint64(id); + data.WriteUint32(level); + int32_t err = Remote()->SendRequest(RSIRenderServiceConnection::SET_SCREEN_BACK_LIGHT, data, reply, option); + if (err != NO_ERROR) { + return; + } +} +} // namespace Rosen +} // namespace OHOS diff --git a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connection_proxy.h b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connection_proxy.h new file mode 100644 index 0000000000..5666a17b3a --- /dev/null +++ b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_connection_proxy.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECTION_PROXY_H +#define ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECTION_PROXY_H + +#include +#include + +namespace OHOS { +namespace Rosen { +class RSRenderServiceConnectionProxy : public IRemoteProxy { +public: + explicit RSRenderServiceConnectionProxy(const sptr& impl); + virtual ~RSRenderServiceConnectionProxy() noexcept = default; + + void CommitTransaction(std::unique_ptr& transactionData) override; + + void ExecuteSynchronousTask(const std::shared_ptr& task) override; + + sptr CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) override; + + ScreenId GetDefaultScreenId() override; + + // mirrorId: decide which screen id to mirror, INVALID_SCREEN_ID means do not mirror any screen. + ScreenId CreateVirtualScreen( + const std::string &name, + uint32_t width, + uint32_t height, + sptr surface, + ScreenId mirrorId, + int32_t flags) override; + + void RemoveVirtualScreen(ScreenId id) override; + + void SetScreenChangeCallback(sptr callback) override; + + void SetScreenActiveMode(ScreenId id, uint32_t modeId) override; + + void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) override; + + void TakeSurfaceCapture(NodeId id, sptr callback) override; + + RSScreenModeInfo GetScreenActiveMode(ScreenId id) override; + + std::vector GetScreenSupportedModes(ScreenId id) override; + + RSScreenCapability GetScreenCapability(ScreenId id) override; + + ScreenPowerStatus GetScreenPowerStatus(ScreenId id) override; + + RSScreenData GetScreenData(ScreenId id) override; + + int32_t GetScreenBacklight(ScreenId id) override; + + void SetScreenBacklight(ScreenId id, uint32_t level) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace Rosen +} // namespace OHOS + +#endif // ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECTION_PROXY_H diff --git a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_proxy.cpp b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_proxy.cpp index e4ee7ac74a..2269af98bd 100644 --- a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_proxy.cpp +++ b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_proxy.cpp @@ -17,383 +17,43 @@ #include #include - -#include "command/rs_command.h" -#include "platform/common/rs_log.h" +#include namespace OHOS { namespace Rosen { RSRenderServiceProxy::RSRenderServiceProxy(const sptr& impl) : IRemoteProxy(impl) {} -void RSRenderServiceProxy::CommitTransaction(std::unique_ptr& transactionData) +sptr RSRenderServiceProxy::CreateConnection(const sptr& token) { - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(RSRenderServiceProxy::GetDescriptor())) { - return; - } - - if (!data.WriteParcelable(transactionData.get())) { - return; - } - - option.SetFlags(MessageOption::TF_ASYNC); - int32_t err = Remote()->SendRequest(RSIRenderService::COMMIT_TRANSACTION, data, reply, option); - if (err != NO_ERROR) { - return; - } -} - -void RSRenderServiceProxy::ExecuteSynchronousTask(const std::shared_ptr& task) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(RSRenderServiceProxy::GetDescriptor())) { - return; - } - - if (!task->Marshalling(data)) { - return; - } - - option.SetFlags(MessageOption::TF_SYNC); - int32_t err = Remote()->SendRequest(RSIRenderService::EXECUTE_SYNCHRONOUS_TASK, data, reply, option); - if (err != NO_ERROR) { - return; - } - - if (task->CheckHeader(reply)) { - task->ReadFromParcel(reply); - } -} - -sptr RSRenderServiceProxy::CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteUint64(config.id)) { + if (token == nullptr) { + ROSEN_LOGE("RSRenderServiceProxy::CreateConnection(): token is null."); return nullptr; } + MessageParcel data; + MessageParcel reply; + MessageOption option; option.SetFlags(MessageOption::TF_SYNC); - int32_t err = Remote()->SendRequest(RSIRenderService::CREATE_SURFACE, data, reply, option); - if (err != NO_ERROR) { + if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { + ROSEN_LOGE("RSRenderServiceProxy::CreateConnection(): WriteInterfaceToken failed."); return nullptr; } - sptr surfaceObject = reply.ReadRemoteObject(); - sptr bp = iface_cast(surfaceObject); - sptr surface = Surface::CreateSurfaceAsProducer(bp); - return surface; -} -ScreenId RSRenderServiceProxy::GetDefaultScreenId() -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return INVALID_SCREEN_ID; - } - - option.SetFlags(MessageOption::TF_SYNC); - int32_t err = Remote()->SendRequest(RSIRenderService::GET_DEFAULT_SCREEN_ID, data, reply, option); + data.WriteRemoteObject(token->AsObject()); + int32_t err = Remote()->SendRequest(RSIRenderService::CREATE_CONNECTION, data, reply, option); if (err != NO_ERROR) { - return INVALID_SCREEN_ID; + ROSEN_LOGE("RSRenderServiceProxy::CreateConnection(): SendRequest failed, err is %d.", err); + return nullptr; } - ScreenId id = reply.ReadUint64(); - return id; -} - -ScreenId RSRenderServiceProxy::CreateVirtualScreen( - const std::string& name, uint32_t width, uint32_t height, sptr surface, ScreenId mirrorId, int32_t flags) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return INVALID_SCREEN_ID; + auto remoteObj = reply.ReadRemoteObject(); + if (remoteObj == nullptr || !remoteObj->IsProxyObject()) { + ROSEN_LOGE("RSRenderServiceProxy::CreateConnection(): Reply is not valid."); + return nullptr; } - option.SetFlags(MessageOption::TF_SYNC); - data.WriteString(name); - data.WriteUint32(width); - data.WriteUint32(height); - auto producer = surface->GetProducer(); - data.WriteRemoteObject(producer->AsObject()); - data.WriteUint64(mirrorId); - data.WriteInt32(flags); - int32_t err = Remote()->SendRequest(RSIRenderService::CREATE_VIRTUAL_DISPLAY, data, reply, option); - if (err != NO_ERROR) { - return INVALID_SCREEN_ID; - } - - ScreenId id = reply.ReadUint64(); - return id; -} - -void RSRenderServiceProxy::RemoveVirtualScreen(ScreenId id) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return; - } - - option.SetFlags(MessageOption::TF_ASYNC); - data.WriteUint64(id); - int32_t err = Remote()->SendRequest(RSIRenderService::REMOVE_VIRTUAL_DISPLAY, data, reply, option); - if (err != NO_ERROR) { - // TODO: Error log. - } -} - -void RSRenderServiceProxy::SetScreenChangeCallback(sptr callback) -{ - if (callback == nullptr) { - // TODO: Error Log - return; - } - - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return; - } - - option.SetFlags(MessageOption::TF_ASYNC); - data.WriteRemoteObject(callback->AsObject()); - int32_t err = Remote()->SendRequest(RSIRenderService::SET_SCREEN_CHANGE_CALLBACK, data, reply, option); - if (err != NO_ERROR) { - // TODO: Error log. - } -} - -void RSRenderServiceProxy::SetScreenActiveMode(ScreenId id, uint32_t modeId) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return; - } - option.SetFlags(MessageOption::TF_SYNC); - data.WriteUint64(id); - data.WriteUint32(modeId); - int32_t err = Remote()->SendRequest(RSIRenderService::SET_SCREEN_ACTIVE_MODE, data, reply, option); - if (err != NO_ERROR) { - return; - } -} - -void RSRenderServiceProxy::SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return; - } - option.SetFlags(MessageOption::TF_SYNC); - data.WriteUint64(id); - data.WriteUint32(static_cast(status)); - int32_t err = Remote()->SendRequest(RSIRenderService::SET_SCREEN_POWER_STATUS, data, reply, option); - if (err != NO_ERROR) { - return; - } -} - -void RSRenderServiceProxy::TakeSurfaceCapture(NodeId id, sptr callback) -{ - if (callback == nullptr) { - ROSEN_LOGE("RSRenderServiceProxy::TakeSurfaceCapture callback == nullptr"); - return; - } - MessageParcel data; - MessageParcel reply; - MessageOption option; - option.SetFlags(MessageOption::TF_ASYNC); - data.WriteUint64(id); - data.WriteRemoteObject(callback->AsObject()); - int32_t err = Remote()->SendRequest(RSIRenderService::TAKE_SURFACE_CAPTURE, data, reply, option); - if (err != NO_ERROR) { - ROSEN_LOGE("RSRenderServiceProxy::TakeSurfaceCapture Remote()->SendRequest() error."); - return; - } -} - -RSScreenModeInfo RSRenderServiceProxy::GetScreenActiveMode(ScreenId id) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - RSScreenModeInfo screenModeInfo; - - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return screenModeInfo; - } - option.SetFlags(MessageOption::TF_SYNC); - data.WriteUint64(id); - int32_t err = Remote()->SendRequest(RSIRenderService::GET_SCREEN_ACTIVE_MODE, data, reply, option); - if (err != NO_ERROR) { - return screenModeInfo; - } - - sptr pScreenModeInfo(reply.ReadParcelable()); - if (pScreenModeInfo == nullptr) { - return screenModeInfo; - } - screenModeInfo = *pScreenModeInfo; - return screenModeInfo; -} - -std::vector RSRenderServiceProxy::GetScreenSupportedModes(ScreenId id) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - std::vector screenSupportedModes; - - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return screenSupportedModes; - } - option.SetFlags(MessageOption::TF_SYNC); - data.WriteUint64(id); - int32_t err = Remote()->SendRequest(RSIRenderService::GET_SCREEN_SUPPORTED_MODES, data, reply, option); - if (err != NO_ERROR) { - return screenSupportedModes; - } - - uint64_t modeCount = reply.ReadUint64(); - screenSupportedModes.resize(modeCount); - for (uint64_t modeIndex = 0; modeIndex < modeCount; modeIndex++) { - sptr itemScreenMode = reply.ReadParcelable(); - if (itemScreenMode == nullptr) { - continue; - } else { - screenSupportedModes[modeIndex] = *itemScreenMode; - } - } - return screenSupportedModes; -} - -RSScreenCapability RSRenderServiceProxy::GetScreenCapability(ScreenId id) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - RSScreenCapability screenCapability; - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return screenCapability; - } - option.SetFlags(MessageOption::TF_SYNC); - data.WriteUint64(id); - int32_t err = Remote()->SendRequest(RSIRenderService::GET_SCREEN_CAPABILITY, data, reply, option); - if (err != NO_ERROR) { - return screenCapability; - } - - sptr pScreenCapability(reply.ReadParcelable()); - if (pScreenCapability == nullptr) { - return screenCapability; - } - screenCapability = *pScreenCapability; - return screenCapability; -} - -ScreenPowerStatus RSRenderServiceProxy::GetScreenPowerStatus(ScreenId id) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return INVALID_POWER_STATUS; - } - option.SetFlags(MessageOption::TF_SYNC); - data.WriteUint64(id); - int32_t err = Remote()->SendRequest(RSIRenderService::GET_SCREEN_POWER_STATUS, data, reply, option); - if (err != NO_ERROR) { - return INVALID_POWER_STATUS; - } - return static_cast(reply.ReadUint32()); -} - -RSScreenData RSRenderServiceProxy::GetScreenData(ScreenId id) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - RSScreenData screenData; - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return screenData; - } - option.SetFlags(MessageOption::TF_SYNC); - data.WriteUint64(id); - int32_t err = Remote()->SendRequest(RSIRenderService::GET_SCREEN_DATA, data, reply, option); - if (err != NO_ERROR) { - return screenData; - } - sptr pScreenData(reply.ReadParcelable()); - if (pScreenData == nullptr) { - return screenData; - } - screenData = *pScreenData; - return screenData; -} - -int RSRenderServiceProxy::Dump(int fd, const std::vector &args) -{ - return Remote()->Dump(fd, args); -} - -int32_t RSRenderServiceProxy::GetScreenBacklight(ScreenId id) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return INVALID_BACKLIGHT_VALUE; - } - option.SetFlags(MessageOption::TF_SYNC); - data.WriteUint64(id); - int32_t err = Remote()->SendRequest(RSIRenderService::GET_SCREEN_BACK_LIGHT, data, reply, option); - if (err != NO_ERROR) { - return INVALID_BACKLIGHT_VALUE; - } - int32_t level = reply.ReadInt32(); - return level; -} - -void RSRenderServiceProxy::SetScreenBacklight(ScreenId id, uint32_t level) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - if (!data.WriteInterfaceToken(RSIRenderService::GetDescriptor())) { - return; - } - option.SetFlags(MessageOption::TF_SYNC); - data.WriteUint64(id); - data.WriteUint32(level); - int32_t err = Remote()->SendRequest(RSIRenderService::SET_SCREEN_BACK_LIGHT, data, reply, option); - if (err != NO_ERROR) { - return; - } + return iface_cast(remoteObj); } } // namespace Rosen } // namespace OHOS diff --git a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_proxy.h b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_proxy.h index ec286fbf54..0dd6d643da 100644 --- a/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_proxy.h +++ b/rosen/modules/render_service_base/src/platform/ohos/rs_render_service_proxy.h @@ -16,13 +16,8 @@ #ifndef ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_PROXY_H #define ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_PROXY_H -#include - #include -#include - -#include "platform/ohos/rs_irender_service.h" -#include "screen_manager/screen_types.h" +#include namespace OHOS { namespace Rosen { @@ -30,49 +25,9 @@ namespace Rosen { class RSRenderServiceProxy : public IRemoteProxy { public: explicit RSRenderServiceProxy(const sptr& impl); - virtual ~RSRenderServiceProxy() = default; + virtual ~RSRenderServiceProxy() noexcept = default; - void CommitTransaction(std::unique_ptr& transactionData) override; - void ExecuteSynchronousTask(const std::shared_ptr& task) override; - - sptr CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) override; - - ScreenId GetDefaultScreenId() override; - - // mirrorId: decide which screen id to mirror, INVALID_SCREEN_ID means do not mirror any screen. - ScreenId CreateVirtualScreen( - const std::string &name, - uint32_t width, - uint32_t height, - sptr surface, - ScreenId mirrorId, - int32_t flags) override; - - void RemoveVirtualScreen(ScreenId id) override; - - void SetScreenChangeCallback(sptr callback) override; - - void SetScreenActiveMode(ScreenId id, uint32_t modeId) override; - - void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) override; - - void TakeSurfaceCapture(NodeId id, sptr callback) override; - - RSScreenModeInfo GetScreenActiveMode(ScreenId id) override; - - std::vector GetScreenSupportedModes(ScreenId id) override; - - RSScreenCapability GetScreenCapability(ScreenId id) override; - - ScreenPowerStatus GetScreenPowerStatus(ScreenId id) override; - - RSScreenData GetScreenData(ScreenId id) override; - - int Dump(int fd, const std::vector &args); - - int32_t GetScreenBacklight(ScreenId id) override; - - void SetScreenBacklight(ScreenId id, uint32_t level) override; + sptr CreateConnection(const sptr& token) override; private: static inline BrokerDelegator delegator_; diff --git a/rosen/modules/render_service_client/test/virtual_display_test.cpp b/rosen/modules/render_service_client/test/virtual_display_test.cpp index 838e5b4f99..64d6371536 100644 --- a/rosen/modules/render_service_client/test/virtual_display_test.cpp +++ b/rosen/modules/render_service_client/test/virtual_display_test.cpp @@ -461,6 +461,11 @@ private: int main() { + auto transactionProxy = RSTransactionProxy::GetInstance(); + if (transactionProxy == nullptr) { + return -1; + } + DisplayId id = g_dms.GetDefaultDisplayId(); std::cout << "level =" << g_dms.GetDisplayBacklight(id) << std::endl; int lightValue = 0; @@ -484,12 +489,11 @@ int main() auto surfaceNode = CreateSurface(); DrawSurface(SkRect::MakeXYWH(0, 0, 200, 200), 0xffa10f1b, SkRect::MakeXYWH(0, 0, 200, 200), surfaceNode); displayNode->AddChild(surfaceNode, -1); - RSTransactionProxy::GetInstance()->FlushImplicitTransaction(); - sleep(4); + transactionProxy->FlushImplicitTransaction(); while (1) { - DrawSurface(SkRect::MakeXYWH(0, 0, 200, 200), 0xffa10f1b, SkRect::MakeXYWH(0, 0, 200, 200), surfaceNode); - RSTransactionProxy::GetInstance()->FlushImplicitTransaction(); sleep(detail::SLEEP_TIME); + DrawSurface(SkRect::MakeXYWH(0, 0, 200, 200), 0xffa10f1b, SkRect::MakeXYWH(0, 0, 200, 200), surfaceNode); + transactionProxy->FlushImplicitTransaction(); } return 0;