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 "rs_processor.h"
17
18 #include <memory>
19
20 #include "render_thread/rs_base_render_util.h"
21 #include "main_thread/rs_main_thread.h"
22
23 #include "common/rs_obj_abs_geometry.h"
24 #include "drawable/rs_display_render_node_drawable.h"
25 #include "params/rs_display_render_params.h"
26 #include "pipeline/rs_display_render_node.h"
27 #include "platform/common/rs_log.h"
28
29 #ifdef SOC_PERF_ENABLE
30 #include "socperf_client.h"
31 #endif
32 #ifdef FRAME_AWARE_TRACE
33 #include "render_frame_trace.h"
34
35 using namespace FRAME_TRACE;
36 #endif
37
38 namespace OHOS {
39 namespace Rosen {
40
InitForRenderThread(DrawableV2::RSDisplayRenderNodeDrawable & displayDrawable,ScreenId mirroredId,std::shared_ptr<RSBaseRenderEngine> renderEngine)41 bool RSProcessor::InitForRenderThread(DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable, ScreenId mirroredId,
42 std::shared_ptr<RSBaseRenderEngine> renderEngine)
43 {
44 #ifdef RS_ENABLE_GPU
45 if (renderEngine == nullptr) {
46 RS_LOGE("renderEngine is nullptr");
47 return false;
48 }
49 auto& params = displayDrawable.GetRenderParams();
50 if (params == nullptr) {
51 RS_LOGE("RSProcessor::InitForRenderThread params is null!");
52 return false;
53 }
54 auto displayParams = static_cast<RSDisplayRenderParams*>(params.get());
55 offsetX_ = displayParams->GetDisplayOffsetX();
56 offsetY_ = displayParams->GetDisplayOffsetY();
57 mirroredId_ = mirroredId;
58 renderEngine_ = renderEngine;
59 screenInfo_ = displayParams->GetScreenInfo();
60 screenInfo_.rotation = displayParams->GetNodeRotation();
61
62 // CalculateScreenTransformMatrix
63 auto mirroredNodeDrawable = displayParams->GetMirrorSourceDrawable().lock();
64 if (!mirroredNodeDrawable) {
65 screenTransformMatrix_ = displayParams->GetMatrix();
66 } else if (mirroredNodeDrawable->GetRenderParams()) {
67 auto& mirrorNodeParam = mirroredNodeDrawable->GetRenderParams();
68 screenTransformMatrix_ = mirrorNodeParam->GetMatrix();
69 if (mirroredId_ != INVALID_SCREEN_ID) {
70 auto mirroredScreenInfo = mirrorNodeParam->GetScreenInfo();
71 CalculateMirrorAdaptiveCoefficient(
72 static_cast<float>(screenInfo_.width), static_cast<float>(screenInfo_.height),
73 static_cast<float>(mirroredScreenInfo.width), static_cast<float>(mirroredScreenInfo.height)
74 );
75 }
76 }
77
78 // set default render frame config
79 renderFrameConfig_ = RSBaseRenderUtil::GetFrameBufferRequestConfig(screenInfo_);
80 #endif
81 return true;
82 }
83
Init(RSDisplayRenderNode & node,int32_t offsetX,int32_t offsetY,ScreenId mirroredId,std::shared_ptr<RSBaseRenderEngine> renderEngine)84 bool RSProcessor::Init(RSDisplayRenderNode& node, int32_t offsetX, int32_t offsetY, ScreenId mirroredId,
85 std::shared_ptr<RSBaseRenderEngine> renderEngine)
86 {
87 if (renderEngine == nullptr) {
88 RS_LOGE("renderEngine is nullptr");
89 return false;
90 }
91 auto screenManager = CreateOrGetScreenManager();
92 if (screenManager == nullptr) {
93 RS_LOGE("RSPhysicalScreenProcessor::Init: ScreenManager is nullptr");
94 return false;
95 }
96 renderEngine_ = renderEngine;
97 offsetX_ = offsetX;
98 offsetY_ = offsetY;
99 mirroredId_ = mirroredId;
100 screenInfo_ = screenManager->QueryScreenInfo(node.GetScreenId());
101 screenInfo_.rotation = node.GetRotation();
102 auto mirrorNode = node.GetMirrorSource().lock();
103 CalculateScreenTransformMatrix(mirrorNode ? *mirrorNode : node);
104
105 if (mirroredId_ != INVALID_SCREEN_ID) {
106 mirroredScreenInfo_ = screenManager->QueryScreenInfo(mirroredId_);
107 mirroredScreenInfo_.rotation = mirrorNode->GetRotation();
108 CalculateMirrorAdaptiveMatrix();
109 }
110
111 // set default render frame config
112 renderFrameConfig_ = RSBaseRenderUtil::GetFrameBufferRequestConfig(screenInfo_);
113 return true;
114 }
115
SetMirrorScreenSwap(const RSDisplayRenderNode & node)116 void RSProcessor::SetMirrorScreenSwap(const RSDisplayRenderNode& node)
117 {
118 auto mirroredNode = node.GetMirrorSource().lock();
119 if (mirroredNode == nullptr) {
120 RS_LOGE("RSProcessor::Init: Get mirroredNode failed");
121 return;
122 }
123 if (mirroredNode->GetRotation() == ScreenRotation::ROTATION_90 ||
124 mirroredNode->GetRotation() == ScreenRotation::ROTATION_270) {
125 std::swap(screenInfo_.width, screenInfo_.height);
126 std::swap(renderFrameConfig_.width, renderFrameConfig_.height);
127 }
128 }
129
CalculateScreenTransformMatrix(const RSDisplayRenderNode & node)130 void RSProcessor::CalculateScreenTransformMatrix(const RSDisplayRenderNode& node)
131 {
132 auto& boundsGeoPtr = (node.GetRenderProperties().GetBoundsGeometry());
133 if (boundsGeoPtr != nullptr) {
134 boundsGeoPtr->UpdateByMatrixFromSelf();
135 screenTransformMatrix_ = boundsGeoPtr->GetMatrix();
136 }
137 }
138
CalculateMirrorAdaptiveCoefficient(float curWidth,float curHeight,float mirroredWidth,float mirroredHeight)139 void RSProcessor::CalculateMirrorAdaptiveCoefficient(float curWidth, float curHeight,
140 float mirroredWidth, float mirroredHeight)
141 {
142 if (std::fabs(mirroredWidth) < 1e-6 || std::fabs(mirroredHeight) < 1e-6) {
143 RS_LOGE("RSSoftwareProcessor::Init mirroredScreen width or height is zero");
144 return;
145 }
146 mirrorAdaptiveCoefficient_ = std::min(curWidth / mirroredWidth, curHeight / mirroredHeight);
147 }
148
SetSecurityDisplay(bool isSecurityDisplay)149 void RSProcessor::SetSecurityDisplay(bool isSecurityDisplay)
150 {
151 isSecurityDisplay_ = isSecurityDisplay;
152 }
153
SetDisplayHasSecSurface(bool displayHasSecSurface)154 void RSProcessor::SetDisplayHasSecSurface(bool displayHasSecSurface)
155 {
156 displayHasSecSurface_ = displayHasSecSurface;
157 }
158
CalculateMirrorAdaptiveMatrix()159 void RSProcessor::CalculateMirrorAdaptiveMatrix()
160 {
161 CalculateMirrorAdaptiveCoefficient(static_cast<float>(screenInfo_.GetRotatedWidth()),
162 static_cast<float>(screenInfo_.GetRotatedHeight()), static_cast<float>(mirroredScreenInfo_.GetRotatedWidth()),
163 static_cast<float>(mirroredScreenInfo_.GetRotatedHeight()));
164
165 float rotation = 0.0f;
166 float offsetX = 0.0f;
167 float offsetY = 0.0f;
168
169 switch (screenInfo_.rotation) {
170 case ScreenRotation::ROTATION_90:
171 rotation = -90.0f;
172 offsetX = screenInfo_.GetRotatedWidth() * -1.0f;
173 break;
174 case ScreenRotation::ROTATION_180:
175 rotation = -180.0f;
176 offsetX = screenInfo_.GetRotatedWidth() * -1.0f;
177 offsetY = screenInfo_.GetRotatedHeight() * -1.0f;
178 break;
179 case ScreenRotation::ROTATION_270:
180 rotation = -270.0f;
181 offsetY = screenInfo_.GetRotatedHeight() * -1.0f;
182 break;
183 default:
184 break;
185 }
186
187 // align center
188 offsetX +=
189 (screenInfo_.GetRotatedWidth() - mirroredScreenInfo_.GetRotatedWidth() * mirrorAdaptiveCoefficient_) / 2.0f;
190 offsetY +=
191 (screenInfo_.GetRotatedHeight() - mirroredScreenInfo_.GetRotatedHeight() * mirrorAdaptiveCoefficient_) /
192 2.0f;
193
194 mirrorAdaptiveMatrix_.PreRotate(rotation);
195 mirrorAdaptiveMatrix_.PreTranslate(offsetX, offsetY);
196 mirrorAdaptiveMatrix_.PreScale(mirrorAdaptiveCoefficient_, mirrorAdaptiveCoefficient_);
197 }
198
199 } // namespace Rosen
200 } // namespace OHOS
201