• 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 "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_screen_render_node_drawable.h"
25 #include "drawable/rs_logical_display_render_node_drawable.h"
26 #include "params/rs_screen_render_params.h"
27 #include "params/rs_logical_display_render_params.h"
28 #include "pipeline/rs_screen_render_node.h"
29 #include "platform/common/rs_log.h"
30 
31 #ifdef SOC_PERF_ENABLE
32 #include "socperf_client.h"
33 #endif
34 #ifdef FRAME_AWARE_TRACE
35 #include "render_frame_trace.h"
36 
37 using namespace FRAME_TRACE;
38 #endif
39 
40 namespace OHOS {
41 namespace Rosen {
42 
InitForRenderThread(DrawableV2::RSScreenRenderNodeDrawable & screenDrawable,std::shared_ptr<RSBaseRenderEngine> renderEngine)43 bool RSProcessor::InitForRenderThread(DrawableV2::RSScreenRenderNodeDrawable& screenDrawable,
44     std::shared_ptr<RSBaseRenderEngine> renderEngine)
45 {
46 #ifdef RS_ENABLE_GPU
47     if (renderEngine == nullptr) {
48         RS_LOGE("renderEngine is nullptr");
49         return false;
50     }
51     auto& params = screenDrawable.GetRenderParams();
52     if (params == nullptr) {
53         RS_LOGE("RSProcessor::InitForRenderThread params is null!");
54         return false;
55     }
56     auto screenParams = static_cast<RSScreenRenderParams*>(params.get());
57     offsetX_ = screenParams->GetScreenOffsetX();
58     offsetY_ = screenParams->GetScreenOffsetY();
59     renderEngine_ = renderEngine;
60     screenInfo_ = screenParams->GetScreenInfo();
61 
62     // set default render frame config
63     renderFrameConfig_ = RSBaseRenderUtil::GetFrameBufferRequestConfig(screenInfo_);
64 #endif
65     return true;
66 }
67 
UpdateMirrorInfo(DrawableV2::RSLogicalDisplayRenderNodeDrawable & displayDrawable)68 bool RSProcessor::UpdateMirrorInfo(DrawableV2::RSLogicalDisplayRenderNodeDrawable& displayDrawable)
69 {
70     auto& params = displayDrawable.GetRenderParams();
71     if (params == nullptr) {
72         RS_LOGE("RSProcess::UpdateMirrorInfo params is null!");
73         return false;
74     }
75     auto displayParams = static_cast<RSLogicalDisplayRenderParams*>(params.get());
76     screenInfo_.rotation = displayParams->GetNodeRotation();
77     // CalculateScreenTransformMatrix
78     auto mirroredNodeDrawable = displayParams->GetMirrorSourceDrawable().lock();
79     isMirror_ = mirroredNodeDrawable != nullptr;
80     if (mirroredNodeDrawable && mirroredNodeDrawable->GetRenderParams()) {
81         auto& mirrorNodeParam = mirroredNodeDrawable->GetRenderParams();
82         CalculateMirrorAdaptiveCoefficient(static_cast<float>(params->GetBounds().GetWidth()),
83             static_cast<float>(params->GetBounds().GetHeight()),
84             static_cast<float>(mirrorNodeParam->GetBounds().GetWidth()),
85             static_cast<float>(mirrorNodeParam->GetBounds().GetHeight()));
86     }
87     return true;
88 }
89 
Init(RSScreenRenderNode & node,int32_t offsetX,int32_t offsetY,ScreenId mirroredId,std::shared_ptr<RSBaseRenderEngine> renderEngine)90 bool RSProcessor::Init(RSScreenRenderNode& node, int32_t offsetX, int32_t offsetY, ScreenId mirroredId,
91     std::shared_ptr<RSBaseRenderEngine> renderEngine)
92 {
93     if (renderEngine == nullptr) {
94         RS_LOGE("renderEngine is nullptr");
95         return false;
96     }
97     auto screenManager = CreateOrGetScreenManager();
98     if (screenManager == nullptr) {
99         RS_LOGE("RSPhysicalScreenProcessor::Init: ScreenManager is nullptr");
100         return false;
101     }
102     renderEngine_ = renderEngine;
103     offsetX_ = offsetX;
104     offsetY_ = offsetY;
105     mirroredId_ = mirroredId;
106     screenInfo_ = screenManager->QueryScreenInfo(node.GetScreenId());
107 
108     auto mirrorNode = node.GetMirrorSource().lock();
109     CalculateScreenTransformMatrix(mirrorNode ? *mirrorNode : node);
110 
111     if (mirroredId_ != INVALID_SCREEN_ID) {
112         mirroredScreenInfo_ = screenManager->QueryScreenInfo(mirroredId_);
113         CalculateMirrorAdaptiveMatrix();
114     }
115 
116     // set default render frame config
117     renderFrameConfig_ = RSBaseRenderUtil::GetFrameBufferRequestConfig(screenInfo_);
118     return true;
119 }
120 
SetMirrorScreenSwap(const RSScreenRenderNode & node)121 void RSProcessor::SetMirrorScreenSwap(const RSScreenRenderNode& node)
122 {
123     auto mirroredNode = node.GetMirrorSource().lock();
124     if (mirroredNode == nullptr) {
125         RS_LOGE("RSProcessor::Init: Get mirroredNode failed");
126         return;
127     }
128 }
129 
CalculateScreenTransformMatrix(const RSScreenRenderNode & node)130 void RSProcessor::CalculateScreenTransformMatrix(const RSScreenRenderNode& 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