• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ui/rs_surface_node.h"
17 
18 #include <algorithm>
19 #include <string>
20 
21 #include "command/rs_surface_node_command.h"
22 #include "command/rs_base_node_command.h"
23 #include "pipeline/rs_node_map.h"
24 #include "platform/common/rs_log.h"
25 #include "platform/drawing/rs_surface_converter.h"
26 #include "transaction/rs_render_service_client.h"
27 #include "transaction/rs_transaction_proxy.h"
28 #include "render_context/render_context.h"
29 
30 namespace OHOS {
31 namespace Rosen {
32 
Create(const RSSurfaceNodeConfig & surfaceNodeConfig,bool isWindow)33 RSSurfaceNode::SharedPtr RSSurfaceNode::Create(const RSSurfaceNodeConfig& surfaceNodeConfig, bool isWindow)
34 {
35     SharedPtr node(new RSSurfaceNode(surfaceNodeConfig, isWindow));
36 
37     RSNodeMap::MutableInstance().RegisterNode(node);
38 
39     // create node in RS
40     RSSurfaceRenderNodeConfig config = { .id = node->GetId(), .name = node->name_ };
41     if (!node->CreateNodeAndSurface(config)) {
42         ROSEN_LOGE("RSSurfaceNode::Create, create node and surface failed");
43         return nullptr;
44     }
45 
46     // create node in RT
47     if (!isWindow) {
48         std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeCreate>(node->GetId());
49         auto transactionProxy = RSTransactionProxy::GetInstance();
50         if (transactionProxy != nullptr) {
51             transactionProxy->AddCommand(command, isWindow);
52         }
53         command = std::make_unique<RSSurfaceNodeConnectToNodeInRenderService>(node->GetId());
54         if (transactionProxy != nullptr) {
55             transactionProxy->AddCommand(command, isWindow);
56         }
57     }
58     ROSEN_LOGD("RsDebug RSSurfaceNode::Create id:%llu", node->GetId());
59     return node;
60 }
61 
SetBounds(const Vector4f & bounds)62 void RSSurfaceNode::SetBounds(const Vector4f& bounds)
63 {
64     RSNode::SetBounds(bounds);
65     UpdateSurfaceDefaultSize(bounds.z_, bounds.w_);
66 }
67 
SetBounds(float positionX,float positionY,float width,float height)68 void RSSurfaceNode::SetBounds(float positionX, float positionY, float width, float height)
69 {
70     RSNode::SetBounds(positionX, positionY, width, height);
71     UpdateSurfaceDefaultSize(width, height);
72 }
73 
SetBoundsSize(const Vector2f & size)74 void RSSurfaceNode::SetBoundsSize(const Vector2f& size)
75 {
76     RSNode::SetBoundsSize(size);
77     UpdateSurfaceDefaultSize(size.x_, size.y_);
78 }
79 
SetBoundsSize(float width,float height)80 void RSSurfaceNode::SetBoundsSize(float width, float height)
81 {
82     RSNode::SetBoundsSize(width, height);
83     UpdateSurfaceDefaultSize(width, height);
84 }
85 
SetBoundsWidth(float width)86 void RSSurfaceNode::SetBoundsWidth(float width)
87 {
88     RSNode::SetBoundsWidth(width);
89     UpdateSurfaceDefaultSize(width, GetStagingProperties().GetBoundsHeight());
90 }
91 
SetBoundsHeight(float height)92 void RSSurfaceNode::SetBoundsHeight(float height)
93 {
94     RSNode::SetBoundsHeight(height);
95     UpdateSurfaceDefaultSize(GetStagingProperties().GetBoundsWidth(), height);
96 }
97 
UpdateSurfaceDefaultSize(float width,float height)98 void RSSurfaceNode::UpdateSurfaceDefaultSize(float width, float height)
99 {
100     std::unique_ptr<RSCommand> command =
101         std::make_unique<RSSurfaceNodeUpdateSurfaceDefaultSize>(GetId(), width, height);
102     auto transactionProxy = RSTransactionProxy::GetInstance();
103     if (transactionProxy != nullptr) {
104         transactionProxy->AddCommand(command, true);
105     }
106 }
107 
SetSecurityLayer(bool isSecurityLayer)108 void RSSurfaceNode::SetSecurityLayer(bool isSecurityLayer)
109 {
110     isSecurityLayer_ = isSecurityLayer;
111     std::unique_ptr<RSCommand> command =
112         std::make_unique<RSSurfaceNodeSetSecurityLayer>(GetId(), isSecurityLayer);
113     auto transactionProxy = RSTransactionProxy::GetInstance();
114     if (transactionProxy != nullptr) {
115         transactionProxy->AddCommand(command, true);
116     }
117     ROSEN_LOGD("RSSurfaceNode::SetSecurityLayer, surfaceNodeId:[%llu] isSecurityLayer:%s", GetId(),
118         isSecurityLayer ? "true" : "false");
119 }
120 
GetSecurityLayer() const121 bool RSSurfaceNode::GetSecurityLayer() const
122 {
123     return isSecurityLayer_;
124 }
125 
SetColorSpace(SurfaceColorGamut colorSpace)126 void RSSurfaceNode::SetColorSpace(SurfaceColorGamut colorSpace)
127 {
128     colorSpace_ = colorSpace;
129 }
130 
Marshalling(Parcel & parcel) const131 bool RSSurfaceNode::Marshalling(Parcel& parcel) const
132 {
133     return parcel.WriteUint64(GetId()) && parcel.WriteString(name_) && parcel.WriteBool(IsRenderServiceNode());
134 }
135 
Unmarshalling(Parcel & parcel)136 RSSurfaceNode* RSSurfaceNode::Unmarshalling(Parcel& parcel)
137 {
138     uint64_t id = UINT64_MAX;
139     std::string name;
140     bool isRenderServiceNode = false;
141     if (!(parcel.ReadUint64(id) && parcel.ReadString(name) && parcel.ReadBool(isRenderServiceNode))) {
142         ROSEN_LOGE("RSSurfaceNode::Unmarshalling, read param failed");
143         return nullptr;
144     }
145     RSSurfaceNodeConfig config = { name };
146 
147     RSSurfaceNode* surfaceNode = new RSSurfaceNode(config, isRenderServiceNode);
148     surfaceNode->SetId(id);
149 
150     return surfaceNode;
151 }
152 
CreateNodeAndSurface(const RSSurfaceRenderNodeConfig & config)153 bool RSSurfaceNode::CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config)
154 {
155     surface_ = std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient())
156                    ->CreateNodeAndSurface(config);
157     return (surface_ != nullptr);
158 }
159 
160 #ifdef ROSEN_OHOS
GetSurface() const161 sptr<OHOS::Surface> RSSurfaceNode::GetSurface() const
162 {
163     if (surface_ == nullptr) {
164         ROSEN_LOGE("RSSurfaceNode::GetSurface, surface_ is nullptr");
165         return nullptr;
166     }
167     auto ohosSurface = RSSurfaceConverter::ConvertToOhosSurface(surface_);
168     return ohosSurface;
169 }
170 #endif
171 
NeedForcedSendToRemote() const172 bool RSSurfaceNode::NeedForcedSendToRemote() const
173 {
174     if (IsRenderServiceNode()) {
175         // RSRenderSurfaceNode in RS only need send property message to RenderService.
176         return false;
177     } else {
178         // RSRenderSurfaceNode in RT need send property message both to RenderService & RenderThread.
179         return true;
180     }
181 }
182 
RSSurfaceNode(const RSSurfaceNodeConfig & config,bool isRenderServiceNode)183 RSSurfaceNode::RSSurfaceNode(const RSSurfaceNodeConfig& config, bool isRenderServiceNode)
184     : RSNode(isRenderServiceNode), name_(config.SurfaceNodeName)
185 {}
186 
~RSSurfaceNode()187 RSSurfaceNode::~RSSurfaceNode() {
188     if (!IsRenderServiceNode()) {
189         std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(GetId());
190         auto transactionProxy = RSTransactionProxy::GetInstance();
191         if (transactionProxy != nullptr) {
192             transactionProxy->AddCommand(command, true);
193         }
194     }
195 }
196 
197 } // namespace Rosen
198 } // namespace OHOS
199