1 /*
2 * Copyright (c) 2024 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 "pipeline/rs_uni_hwc_prevalidate_util.h"
17
18 #include <dlfcn.h>
19 #include <functional>
20 #include <string>
21
22 #include "rs_base_render_util.h"
23 #include "rs_uni_render_util.h"
24
25 #include "common/rs_common_hook.h"
26 #include "common/rs_obj_abs_geometry.h"
27 #include "drawable/rs_display_render_node_drawable.h"
28 #include "pipeline/rs_surface_render_node.h"
29 #include "pipeline/rs_uifirst_manager.h"
30 #include "platform/common/rs_log.h"
31
32 namespace OHOS {
33 namespace Rosen {
34 constexpr uint32_t ROTATION_360 = 360;
GetInstance()35 RSUniHwcPrevalidateUtil& RSUniHwcPrevalidateUtil::GetInstance()
36 {
37 static RSUniHwcPrevalidateUtil instance;
38 return instance;
39 }
40
RSUniHwcPrevalidateUtil()41 RSUniHwcPrevalidateUtil::RSUniHwcPrevalidateUtil()
42 {
43 preValidateHandle_ = dlopen("libdss_enhance.z.so", RTLD_LAZY);
44 if (preValidateHandle_ == nullptr) {
45 RS_LOGW("[%{public}s_%{public}d]:load library failed, reason: %{public}s", __func__, __LINE__, dlerror());
46 return;
47 }
48 preValidateFunc_ = reinterpret_cast<PreValidateFunc>(dlsym(preValidateHandle_, "RequestLayerStrategy"));
49 if (preValidateFunc_ == nullptr) {
50 RS_LOGW("[%{public}s_%{public}d]:load func failed, reason: %{public}s", __func__, __LINE__, dlerror());
51 dlclose(preValidateHandle_);
52 preValidateHandle_ = nullptr;
53 return;
54 }
55 RS_LOGI("[%{public}s_%{public}d]:load success", __func__, __LINE__);
56 loadSuccess = true;
57 }
58
~RSUniHwcPrevalidateUtil()59 RSUniHwcPrevalidateUtil::~RSUniHwcPrevalidateUtil()
60 {
61 if (preValidateHandle_) {
62 dlclose(preValidateHandle_);
63 preValidateHandle_ = nullptr;
64 }
65 }
66
IsLoadSuccess() const67 bool RSUniHwcPrevalidateUtil::IsLoadSuccess() const
68 {
69 return loadSuccess;
70 }
71
PreValidate(ScreenId id,std::vector<RequestLayerInfo> infos,std::map<uint64_t,RequestCompositionType> & strategy)72 bool RSUniHwcPrevalidateUtil::PreValidate(
73 ScreenId id, std::vector<RequestLayerInfo> infos, std::map<uint64_t, RequestCompositionType> &strategy)
74 {
75 if (!preValidateFunc_) {
76 RS_LOGI_IF(DEBUG_PREVALIDATE, "RSUniHwcPrevalidateUtil::PreValidate preValidateFunc is null");
77 return false;
78 }
79 int32_t ret = preValidateFunc_(id, infos, strategy);
80 return ret == 0;
81 }
82
CreateSurfaceNodeLayerInfo(uint32_t zorder,RSSurfaceRenderNode::SharedPtr node,GraphicTransformType transform,uint32_t fps,RequestLayerInfo & info)83 bool RSUniHwcPrevalidateUtil::CreateSurfaceNodeLayerInfo(uint32_t zorder,
84 RSSurfaceRenderNode::SharedPtr node, GraphicTransformType transform, uint32_t fps, RequestLayerInfo &info)
85 {
86 if (!node || !node->GetRSSurfaceHandler()->GetConsumer() || !node->GetRSSurfaceHandler()->GetBuffer()) {
87 return false;
88 }
89 info.id = node->GetId();
90 auto src = node->GetSrcRect();
91 info.srcRect = {src.left_, src.top_, src.width_, src.height_};
92 auto dst = node->GetDstRect();
93 info.dstRect = {dst.left_, dst.top_, dst.width_, dst.height_};
94 info.zOrder = zorder;
95 info.usage = node->GetRSSurfaceHandler()->GetBuffer()->GetUsage();
96 info.format = node->GetRSSurfaceHandler()->GetBuffer()->GetFormat();
97 info.fps = fps;
98 info.transform = static_cast<int>(transform);
99
100 if (RsCommonHook::Instance().GetVideoSurfaceFlag() && IsYUVBufferFormat(node)) {
101 info.perFrameParameters["SourceCropTuning"] = std::vector<int8_t> {1};
102 } else {
103 info.perFrameParameters["SourceCropTuning"] = std::vector<int8_t> {0};
104 }
105 if (CheckIfDoArsrPre(node)) {
106 info.perFrameParameters["ArsrDoEnhance"] = std::vector<int8_t> {1};
107 node->SetArsrTag(true);
108 }
109 RS_LOGD_IF(DEBUG_PREVALIDATE, "RSUniHwcPrevalidateUtil::CreateSurfaceNodeLayerInfo %{public}s,"
110 " %{public}" PRIu64 ", src: %{public}s, dst: %{public}s, z: %{public}" PRIu32 ","
111 " usage: %{public}" PRIu64 ", format: %{public}d, transform: %{public}d, fps: %{public}d",
112 node->GetName().c_str(), node->GetId(),
113 node->GetSrcRect().ToString().c_str(), node->GetDstRect().ToString().c_str(),
114 zorder, info.usage, info.format, info.transform, fps);
115 return true;
116 }
117
IsYUVBufferFormat(RSSurfaceRenderNode::SharedPtr node) const118 bool RSUniHwcPrevalidateUtil::IsYUVBufferFormat(RSSurfaceRenderNode::SharedPtr node) const
119 {
120 if (node->GetRSSurfaceHandler()->GetBuffer() == nullptr) {
121 return false;
122 }
123 auto format = node->GetRSSurfaceHandler()->GetBuffer()->GetFormat();
124 if (format < GRAPHIC_PIXEL_FMT_YUV_422_I || format == GRAPHIC_PIXEL_FMT_RGBA_1010102 ||
125 format > GRAPHIC_PIXEL_FMT_YCRCB_P010) {
126 return false;
127 }
128 return true;
129 }
130
CreateDisplayNodeLayerInfo(uint32_t zorder,RSDisplayRenderNode::SharedPtr node,const ScreenInfo & screenInfo,uint32_t fps,RequestLayerInfo & info)131 bool RSUniHwcPrevalidateUtil::CreateDisplayNodeLayerInfo(uint32_t zorder,
132 RSDisplayRenderNode::SharedPtr node, const ScreenInfo &screenInfo, uint32_t fps, RequestLayerInfo &info)
133 {
134 if (!node) {
135 return false;
136 }
137 auto drawable = node->GetRenderDrawable();
138 if (!drawable) {
139 return false;
140 }
141 auto displayDrawable = std::static_pointer_cast<DrawableV2::RSDisplayRenderNodeDrawable>(drawable);
142 auto surfaceHandler = displayDrawable->GetRSSurfaceHandlerOnDraw();
143 if (!surfaceHandler->GetConsumer() || !surfaceHandler->GetBuffer()) {
144 return false;
145 }
146 auto buffer = surfaceHandler->GetBuffer();
147 info.id = node->GetId();
148 info.srcRect = {0, 0, buffer->GetSurfaceBufferWidth(), buffer->GetSurfaceBufferHeight()};
149 info.dstRect = {0, 0, screenInfo.GetRotatedPhyWidth(), screenInfo.GetRotatedPhyHeight()};
150 info.zOrder = zorder;
151 info.usage = buffer->GetUsage() | USAGE_UNI_LAYER;
152 info.format = buffer->GetFormat();
153 info.fps = fps;
154 LayerRotate(info, surfaceHandler->GetConsumer(), screenInfo);
155 RS_LOGD_IF(DEBUG_PREVALIDATE, "RSUniHwcPrevalidateUtil::CreateDisplayNodeLayerInfo %{public}" PRIu64 ","
156 " src: %{public}d,%{public}d,%{public}d,%{public}d"
157 " dst: %{public}d,%{public}d,%{public}d,%{public}d, z: %{public}" PRIu32 ","
158 " usage: %{public}" PRIu64 ", format: %{public}d, transform: %{public}d, fps: %{public}d",
159 node->GetId(), info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.h,
160 info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h,
161 zorder, info.usage, info.format, info.transform, fps);
162 return true;
163 }
164
CreateUIFirstLayerInfo(RSSurfaceRenderNode::SharedPtr node,GraphicTransformType transform,uint32_t fps,RequestLayerInfo & info)165 bool RSUniHwcPrevalidateUtil::CreateUIFirstLayerInfo(
166 RSSurfaceRenderNode::SharedPtr node, GraphicTransformType transform, uint32_t fps, RequestLayerInfo &info)
167 {
168 if (!node) {
169 return false;
170 }
171 info.id = node->GetId();
172 auto src = node->GetSrcRect();
173 info.srcRect = {src.left_, src.top_, src.width_, src.height_};
174 auto dst = node->GetDstRect();
175 info.dstRect = {dst.left_, dst.top_, dst.width_, dst.height_};
176 info.zOrder = static_cast<uint32_t>(node->GetRSSurfaceHandler()->GetGlobalZOrder());
177 info.format = GRAPHIC_PIXEL_FMT_RGBA_8888;
178 info.usage = BUFFER_USAGE_HW_RENDER | BUFFER_USAGE_HW_TEXTURE | BUFFER_USAGE_HW_COMPOSER | BUFFER_USAGE_MEM_DMA;
179 info.fps = fps;
180 info.transform = static_cast<int>(transform);
181 RS_LOGD_IF(DEBUG_PREVALIDATE, "RSUniHwcPrevalidateUtil::CreateUIFirstLayerInfo %{public}s, %{public}" PRIu64 ","
182 " src: %{public}s, dst: %{public}s, z: %{public}" PRIu32 ","
183 " usage: %{public}" PRIu64 ", format: %{public}d, transform: %{public}d, fps: %{public}d",
184 node->GetName().c_str(), node->GetId(),
185 node->GetSrcRect().ToString().c_str(), node->GetDstRect().ToString().c_str(),
186 info.zOrder, info.usage, info.format, info.transform, fps);
187 return true;
188 }
189
CreateRCDLayerInfo(RSRcdSurfaceRenderNode::SharedPtr node,const ScreenInfo & screenInfo,uint32_t fps,RequestLayerInfo & info)190 bool RSUniHwcPrevalidateUtil::CreateRCDLayerInfo(
191 RSRcdSurfaceRenderNode::SharedPtr node, const ScreenInfo &screenInfo, uint32_t fps, RequestLayerInfo &info)
192 {
193 if (!node || !node->GetConsumer() || !node->GetBuffer()) {
194 return false;
195 }
196
197 info.id = node->GetId();
198 auto src = node->GetSrcRect();
199 info.srcRect = {src.left_, src.top_, src.width_, src.height_};
200 auto dst = node->GetDstRect();
201 info.dstRect.x = static_cast<uint32_t>(static_cast<float>(dst.left_) * screenInfo.GetRogWidthRatio());
202 info.dstRect.y = static_cast<uint32_t>(static_cast<float>(dst.top_) * screenInfo.GetRogHeightRatio());
203 info.dstRect.w = static_cast<uint32_t>(static_cast<float>(dst.width_) * screenInfo.GetRogWidthRatio());
204 info.dstRect.h = static_cast<uint32_t>(static_cast<float>(dst.height_) * screenInfo.GetRogHeightRatio());
205 info.zOrder = static_cast<uint32_t>(node->GetGlobalZOrder());
206 info.usage = node->GetBuffer()->GetUsage();
207 info.format = node->GetBuffer()->GetFormat();
208 info.fps = fps;
209 CopyCldInfo(node->GetCldInfo(), info);
210 LayerRotate(info, node->GetConsumer(), screenInfo);
211 RS_LOGD_IF(DEBUG_PREVALIDATE, "RSUniHwcPrevalidateUtil::CreateRCDLayerInfo %{public}" PRIu64 ","
212 " src: %{public}d,%{public}d,%{public}d,%{public}d"
213 " dst: %{public}d,%{public}d,%{public}d,%{public}d, z: %{public}" PRIu32 ","
214 " usage: %{public}" PRIu64 ", format: %{public}d, transform: %{public}d, fps: %{public}d",
215 node->GetId(),
216 info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.h,
217 info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h,
218 info.zOrder, info.usage, info.format, info.transform, fps);
219 return true;
220 }
221
CollectSurfaceNodeLayerInfo(std::vector<RequestLayerInfo> & prevalidLayers,std::vector<RSBaseRenderNode::SharedPtr> & surfaceNodes,uint32_t curFps,uint32_t & zOrder,const ScreenInfo & screenInfo)222 void RSUniHwcPrevalidateUtil::CollectSurfaceNodeLayerInfo(
223 std::vector<RequestLayerInfo>& prevalidLayers, std::vector<RSBaseRenderNode::SharedPtr>& surfaceNodes,
224 uint32_t curFps, uint32_t &zOrder, const ScreenInfo& screenInfo)
225 {
226 for (auto it = surfaceNodes.rbegin(); it != surfaceNodes.rend(); it++) {
227 auto surfaceNode = RSBaseRenderNode::ReinterpretCast<RSSurfaceRenderNode>(*it);
228 if (!surfaceNode) {
229 continue;
230 }
231 const auto& hwcNodes = surfaceNode->GetChildHardwareEnabledNodes();
232 if (hwcNodes.empty()) {
233 continue;
234 }
235 for (auto& hwcNode : hwcNodes) {
236 auto hwcNodePtr = hwcNode.lock();
237 if (!hwcNodePtr || !hwcNodePtr->IsOnTheTree() || hwcNodePtr->IsHardwareForcedDisabled()
238 || hwcNodePtr->GetAncoForceDoDirect()) {
239 continue;
240 }
241 auto transform = RSUniRenderUtil::GetLayerTransform(*hwcNodePtr, screenInfo);
242 RequestLayerInfo surfaceLayer;
243 if (RSUniHwcPrevalidateUtil::GetInstance().CreateSurfaceNodeLayerInfo(
244 zOrder++, hwcNodePtr, transform, curFps, surfaceLayer)) {
245 prevalidLayers.emplace_back(surfaceLayer);
246 }
247 }
248 }
249 }
250
CollectUIFirstLayerInfo(std::vector<RequestLayerInfo> & uiFirstLayers,uint32_t curFps,float zOrder,const ScreenInfo & screenInfo)251 void RSUniHwcPrevalidateUtil::CollectUIFirstLayerInfo(std::vector<RequestLayerInfo>& uiFirstLayers,
252 uint32_t curFps, float zOrder, const ScreenInfo& screenInfo)
253 {
254 auto pendingNodes = RSUifirstManager::Instance().GetPendingPostNodes();
255 for (auto iter : pendingNodes) {
256 if (!iter.second || iter.second->IsHardwareForcedDisabled() ||
257 !RSUifirstManager::Instance().GetUseDmaBuffer(iter.second->GetName())) {
258 continue;
259 }
260 iter.second->GetMutableRSSurfaceHandler()->SetGlobalZOrder(zOrder++);
261 auto transform = RSUniRenderUtil::GetLayerTransform(*iter.second, screenInfo);
262 RequestLayerInfo uiFirstLayer;
263 if (RSUniHwcPrevalidateUtil::GetInstance().CreateUIFirstLayerInfo(
264 iter.second, transform, curFps, uiFirstLayer)) {
265 uiFirstLayers.emplace_back(uiFirstLayer);
266 }
267 }
268 }
269
LayerRotate(RequestLayerInfo & info,const sptr<IConsumerSurface> & surface,const ScreenInfo & screenInfo)270 void RSUniHwcPrevalidateUtil::LayerRotate(
271 RequestLayerInfo& info, const sptr<IConsumerSurface>& surface, const ScreenInfo &screenInfo)
272 {
273 if (!surface) {
274 return;
275 }
276 const auto screenWidth = static_cast<int32_t>(screenInfo.width);
277 const auto screenHeight = static_cast<int32_t>(screenInfo.height);
278 const auto screenRotation = screenInfo.rotation;
279 const auto rect = info.dstRect;
280 switch (screenRotation) {
281 case ScreenRotation::ROTATION_90: {
282 info.dstRect = {rect.y, screenHeight - rect.x - rect.w, rect.h, rect.w};
283 break;
284 }
285 case ScreenRotation::ROTATION_180: {
286 info.dstRect = {screenWidth - rect.x - rect.w, screenHeight - rect.y - rect.h, rect.w, rect.h};
287 break;
288 }
289 case ScreenRotation::ROTATION_270: {
290 info.dstRect = {screenWidth - rect.y - rect.h, rect.x, rect.h, rect.w};
291 break;
292 }
293 default: {
294 break;
295 }
296 }
297 int totalRotation = (RSBaseRenderUtil::RotateEnumToInt(screenRotation) + RSBaseRenderUtil::RotateEnumToInt(
298 RSBaseRenderUtil::GetRotateTransform(surface->GetTransform()))) % ROTATION_360;
299 GraphicTransformType rotateEnum = RSBaseRenderUtil::RotateEnumToInt(totalRotation,
300 RSBaseRenderUtil::GetFlipTransform(surface->GetTransform()));
301 info.transform = rotateEnum;
302 }
303
CopyCldInfo(CldInfo src,RequestLayerInfo & info)304 void RSUniHwcPrevalidateUtil::CopyCldInfo(CldInfo src, RequestLayerInfo& info)
305 {
306 info.cldInfo = new CldInfo();
307 info.cldInfo->cldDataOffset = src.cldDataOffset;
308 info.cldInfo->cldSize = src.cldSize;
309 info.cldInfo->cldWidth = src.cldWidth;
310 info.cldInfo->cldHeight = src.cldHeight;
311 info.cldInfo->cldStride = src.cldStride;
312 info.cldInfo->exWidth = src.exWidth;
313 info.cldInfo->exHeight = src.exHeight;
314 info.cldInfo->baseColor = src.baseColor;
315 }
316
CheckIfDoArsrPre(const RSSurfaceRenderNode::SharedPtr node)317 bool RSUniHwcPrevalidateUtil::CheckIfDoArsrPre(const RSSurfaceRenderNode::SharedPtr node)
318 {
319 if (node->GetRSSurfaceHandler()->GetBuffer() == nullptr) {
320 return false;
321 }
322 static const std::unordered_set<std::string> videoLayers {
323 "xcomponentIdSurface",
324 "componentIdSurface",
325 "SceneViewer Model totemweather0",
326 };
327 if (IsYUVBufferFormat(node) || (videoLayers.count(node->GetName()) > 0)) {
328 return true;
329 }
330 return false;
331 }
332 } //Rosen
333 } //OHOS