• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_uni_render_composer_adapter.h"
17 #include <memory>
18 
19 #include "common/rs_common_def.h"
20 #include "common/rs_obj_abs_geometry.h"
21 #include "common/rs_optional_trace.h"
22 #include "drawable/rs_display_render_node_drawable.h"
23 #include "drawable/rs_render_node_drawable_adapter.h"
24 #include "drawable/rs_surface_render_node_drawable.h"
25 #include "params/rs_render_params.h"
26 #include "pipeline/render_thread/rs_divided_render_util.h"
27 #include "pipeline/main_thread/rs_uni_render_listener.h"
28 #include "params/rs_rcd_render_params.h"
29 #include "platform/common/rs_log.h"
30 #include "rs_trace.h"
31 #include "rs_uni_render_util.h"
32 #include "string_utils.h"
33 #include "metadata_helper.h"
34 #include "surface_type.h"
35 #include "third_party/libdrm/include/drm/drm.h"
36 
37 #include "feature/round_corner_display/rs_rcd_surface_render_node.h"
38 #include "feature/round_corner_display/rs_rcd_surface_render_node_drawable.h"
39 
40 namespace OHOS {
41 namespace Rosen {
42 namespace {
43 constexpr uint32_t FLAT_ANGLE = 180;
44 constexpr int32_t DEFAULT_BRIGHTNESS = 500;
45 constexpr float NO_RATIO = 1.0f;
46 static const int GLOBAL_ALPHA_MAX = 255;
47 
RectVectorToString(const std::vector<GraphicIRect> & dirtyRects)48 std::string RectVectorToString(const std::vector<GraphicIRect>& dirtyRects)
49 {
50     std::stringstream ss;
51     for (auto& rect : dirtyRects) {
52         ss << "[" << rect.x << "," << rect.y << "," << rect.w << "," << rect.h << "]";
53     }
54     return ss.str();
55 }
56 }
57 
Init(const ScreenInfo & screenInfo,int32_t offsetX,int32_t offsetY,float mirrorAdaptiveCoefficient)58 bool RSUniRenderComposerAdapter::Init(const ScreenInfo& screenInfo, int32_t offsetX, int32_t offsetY,
59     float mirrorAdaptiveCoefficient)
60 {
61     RS_LOGI_IF(DEBUG_COMPOSER,
62         "RSUniRenderComposerAdapter::initialize id:%{public}" PRIu64 " offsetX:%{public}d offsetY:%{public}d"
63         " mirrorAdaptiveCoefficient:%{public}f", screenInfo.id, offsetX, offsetY, mirrorAdaptiveCoefficient);
64     hdiBackend_ = HdiBackend::GetInstance();
65     if (hdiBackend_ == nullptr) {
66         RS_LOGE("RSUniRenderComposerAdapter::Init: hdiBackend is nullptr");
67         return false;
68     }
69     auto screenManager = CreateOrGetScreenManager();
70     if (screenManager == nullptr) {
71         RS_LOGE("RSUniRenderComposerAdapter::Init: ScreenManager is nullptr");
72         return false;
73     }
74     output_ = screenManager->GetOutput(ToScreenPhysicalId(screenInfo.id));
75     if (output_ == nullptr) {
76         RS_LOGE("RSUniRenderComposerAdapter::Init: output_ is nullptr");
77         return false;
78     }
79 
80     offsetX_ = offsetX;
81     offsetY_ = offsetY;
82     mirrorAdaptiveCoefficient_ = mirrorAdaptiveCoefficient;
83     screenInfo_ = screenInfo;
84 
85     GraphicIRect damageRect {0, 0, static_cast<int32_t>(screenInfo_.width), static_cast<int32_t>(screenInfo_.height)};
86     std::vector<GraphicIRect> damageRects;
87     damageRects.emplace_back(damageRect);
88     output_->SetOutputDamages(damageRects);
89 
90     return true;
91 }
92 
CommitLayers(const std::vector<LayerInfoPtr> & layers)93 void RSUniRenderComposerAdapter::CommitLayers(const std::vector<LayerInfoPtr>& layers)
94 {
95     if (hdiBackend_ == nullptr) {
96         RS_LOGE("RSUniRenderComposerAdapter::CommitLayers: backend is nullptr");
97         return;
98     }
99 
100     if (output_ == nullptr) {
101         RS_LOGE("RSUniRenderComposerAdapter::CommitLayers: output is nullptr");
102         return;
103     }
104     RSHardwareThread::Instance().CommitAndReleaseLayers(output_, layers);
105 }
106 
SetPreBufferInfo(RSSurfaceHandler & surfaceHandler,ComposeInfo & info) const107 void RSUniRenderComposerAdapter::SetPreBufferInfo(RSSurfaceHandler& surfaceHandler, ComposeInfo& info) const
108 {
109     info.preBuffer = surfaceHandler.GetPreBuffer();
110     surfaceHandler.ResetPreBuffer();
111 }
112 
113 // private func, for RSDisplayRenderNode
BuildComposeInfo(DrawableV2::RSDisplayRenderNodeDrawable & displayDrawable,const std::vector<RectI> & dirtyRegion)114 ComposeInfo RSUniRenderComposerAdapter::BuildComposeInfo(DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable,
115     const std::vector<RectI>& dirtyRegion)
116 {
117     ComposeInfo info {};
118     SetBufferColorSpace(displayDrawable);
119     auto surfaceHandler = displayDrawable.GetMutableRSSurfaceHandlerOnDraw();
120     auto& params = displayDrawable.GetRenderParams();
121     if (!surfaceHandler || !params) {
122         RS_LOGD("RSUniRenderComposerAdapter::BuildCInfo surfaceHandler or params is nullptr");
123         return info;
124     }
125     const auto& buffer = surfaceHandler->GetBuffer(); // we guarantee the buffer is valid.
126     const auto& activeRect = params->GetScreenInfo().activeRect;
127     info.srcRect = GraphicIRect {0, 0, buffer->GetSurfaceBufferWidth(), buffer->GetSurfaceBufferHeight()};
128     info.dstRect = GraphicIRect {0, 0, static_cast<int32_t>(screenInfo_.GetRotatedPhyWidth()),
129         static_cast<int32_t>(screenInfo_.GetRotatedPhyHeight())};
130     if (activeRect.width_ > 0 && activeRect.height_ > 0) {
131         info.srcRect = GraphicIRect {activeRect.left_, activeRect.top_, activeRect.width_, activeRect.height_};
132         info.dstRect = GraphicIRect {activeRect.left_, activeRect.top_, activeRect.width_, activeRect.height_};
133     }
134     auto bound = params->GetBounds();
135     info.boundRect = {0, 0,
136         static_cast<int32_t>(bound.GetWidth()), static_cast<int32_t>(bound.GetHeight())};
137     info.visibleRect = GraphicIRect {info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h};
138     std::vector<GraphicIRect> dirtyRects;
139     // layer damage always relative to the top-left, no matter gl or vk
140     std::vector<RectI> flipDirtyRects =
141         RSUniRenderUtil::GetFilpDirtyRects(dirtyRegion, screenInfo_);
142     for (const auto& rect : flipDirtyRects) {
143         dirtyRects.emplace_back(GraphicIRect {rect.left_, rect.top_, rect.width_, rect.height_});
144     }
145     if (RSSystemProperties::GetUniPartialRenderEnabled() == PartialRenderType::DISABLED && dirtyRects.empty()) {
146         dirtyRects.emplace_back(info.srcRect);
147     }
148     info.dirtyRects = dirtyRects;
149     auto displayParams = static_cast<RSDisplayRenderParams*>(params.get());
150     info.zOrder = static_cast<int32_t>(displayParams->GetGlobalZOrder());
151     info.alpha.enGlobalAlpha = true;
152     info.alpha.gAlpha = GLOBAL_ALPHA_MAX;
153     SetPreBufferInfo(*surfaceHandler, info);
154     info.buffer = buffer;
155     info.fence = surfaceHandler->GetAcquireFence();
156     info.blendType = GRAPHIC_BLEND_SRCOVER;
157     info.needClient = RSSystemProperties::IsForceClient();
158     auto matrix = params->GetMatrix();
159     info.matrix = GraphicMatrix {matrix.Get(Drawing::Matrix::Index::SCALE_X),
160         matrix.Get(Drawing::Matrix::Index::SKEW_X), matrix.Get(Drawing::Matrix::Index::TRANS_X),
161         matrix.Get(Drawing::Matrix::Index::SKEW_Y), matrix.Get(Drawing::Matrix::Index::SCALE_Y),
162         matrix.Get(Drawing::Matrix::Index::TRANS_Y), matrix.Get(Drawing::Matrix::Index::PERSP_0),
163         matrix.Get(Drawing::Matrix::Index::PERSP_1), matrix.Get(Drawing::Matrix::Index::PERSP_2)};
164     info.gravity = static_cast<int32_t>(Gravity::RESIZE);
165 
166     const auto curDisplayParam = static_cast<RSDisplayRenderParams*>(displayDrawable.GetRenderParams().get());
167     if (curDisplayParam) {
168         info.brightnessRatio = curDisplayParam->GetBrightnessRatio();
169     }
170     RS_LOGD_IF(DEBUG_COMPOSER,
171         "RSUniRenderComposerAdapter::BuildCInfo id:%{public}" PRIu64
172         " zOrder:%{public}d blendType:%{public}d needClient:%{public}d alpha[%{public}d %{public}d]"
173         " boundRect[%{public}d %{public}d %{public}d %{public}d]"
174         " srcRect[%{public}d %{public}d %{public}d %{public}d] dstRect[%{public}d %{public}d %{public}d %{public}d]"
175         " matrix[%{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f]",
176         displayDrawable.GetId(), info.zOrder, info.blendType, info.needClient, info.alpha.enGlobalAlpha,
177         info.alpha.gAlpha, info.boundRect.x, info.boundRect.y, info.boundRect.w, info.boundRect.h, info.srcRect.x,
178         info.srcRect.y, info.srcRect.w, info.srcRect.y, info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.y,
179         info.matrix.scaleX, info.matrix.scaleY, info.matrix.skewX, info.matrix.skewY, info.matrix.transX,
180         info.matrix.transY, info.matrix.pers0, info.matrix.pers1, info.matrix.pers2);
181     return info;
182 }
183 
BuildComposeInfo(RSRcdSurfaceRenderNode & node) const184 ComposeInfo RSUniRenderComposerAdapter::BuildComposeInfo(RSRcdSurfaceRenderNode& node) const
185 {
186     auto surfaceHandler = node.GetRSSurfaceHandler();
187     if (!surfaceHandler) {
188         return ComposeInfo {};
189     }
190     const auto& buffer = surfaceHandler->GetBuffer(); // we guarantee the buffer is valid.
191     if (buffer == nullptr) {
192         RS_LOGW("RSUniRenderComposerAdapter::BuildComposeInfo RSRcdSurfaceRenderNode buffer is nullptr");
193     }
194     const RectI& dstRect = node.GetDstRect();
195     const auto& srcRect = node.GetSrcRect();
196     ComposeInfo info {};
197     info.srcRect = GraphicIRect {srcRect.left_, srcRect.top_, srcRect.width_, srcRect.height_};
198     info.dstRect = GraphicIRect {static_cast<int32_t>(dstRect.left_ * screenInfo_.GetRogWidthRatio()),
199         static_cast<int32_t>(dstRect.top_ * screenInfo_.GetRogHeightRatio()),
200         static_cast<int32_t>(dstRect.width_ * screenInfo_.GetRogWidthRatio()),
201         static_cast<int32_t>(dstRect.height_ * screenInfo_.GetRogHeightRatio())};
202     info.boundRect = info.dstRect;
203     info.visibleRect = info.dstRect;
204     std::vector<GraphicIRect> dirtyRects;
205     dirtyRects.emplace_back(GraphicIRect {0, 0, 0, 0});
206     info.dirtyRects = dirtyRects;
207     info.zOrder = static_cast<int32_t>(surfaceHandler->GetGlobalZOrder());
208     info.alpha.enGlobalAlpha = true;
209     info.alpha.gAlpha = 255; // 255 means not transparent
210     SetPreBufferInfo(*surfaceHandler, info);
211     info.buffer = buffer;
212     info.fence = surfaceHandler->GetAcquireFence();
213     info.blendType = GRAPHIC_BLEND_SRCOVER;
214     info.needClient = false;
215     info.matrix = GraphicMatrix {1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 1.f};
216     info.gravity = static_cast<int32_t>(Gravity::RESIZE);
217 
218     info.displayNit = DEFAULT_BRIGHTNESS;
219     info.brightnessRatio = NO_RATIO;
220     RS_LOGD_IF(DEBUG_COMPOSER,
221         "RSUniRenderComposerAdapter::BuildCInfo id:%{public}" PRIu64
222         " zOrder:%{public}d blendType:%{public}d needClient:%{public}d displayNit:%{public}d brightnessRatio:%{public}f"
223         " alpha[%{public}d %{public}d] boundRect[%{public}d %{public}d %{public}d %{public}d]"
224         " srcRect[%{public}d %{public}d %{public}d %{public}d] dstRect[%{public}d %{public}d %{public}d %{public}d]"
225         " matrix[%{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f]",
226         node.GetId(), info.zOrder, info.blendType, info.needClient, info.displayNit, info.brightnessRatio,
227         info.alpha.enGlobalAlpha, info.alpha.gAlpha, info.boundRect.x, info.boundRect.y, info.boundRect.w,
228         info.boundRect.h, info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.y, info.dstRect.x,
229         info.dstRect.y, info.dstRect.w, info.dstRect.y, info.matrix.scaleX, info.matrix.scaleY, info.matrix.skewX,
230         info.matrix.skewY, info.matrix.transX, info.matrix.transY, info.matrix.pers0, info.matrix.pers1,
231         info.matrix.pers2);
232     return info;
233 }
234 
BuildComposeInfo(DrawableV2::RSRcdSurfaceRenderNodeDrawable & rcdDrawable) const235 ComposeInfo RSUniRenderComposerAdapter::BuildComposeInfo(DrawableV2::RSRcdSurfaceRenderNodeDrawable& rcdDrawable) const
236 {
237     auto surfaceHandler = rcdDrawable.GetSurfaceHandler();
238     if (!surfaceHandler) {
239         return ComposeInfo {};
240     }
241     const auto& buffer = surfaceHandler->GetBuffer(); // we guarantee the buffer is valid.
242     if (buffer == nullptr) {
243         RS_LOGW("RSUniRenderComposerAdapter::BuildComposeInfo RSRcdSurfaceRenderNodeDrawable buffer is nullptr");
244     }
245     auto params = static_cast<RSRcdRenderParams*>(rcdDrawable.GetRenderParams().get());
246     const RectI& dstRect = params->GetDstRect();
247     const auto& srcRect = params->GetSrcRect();
248     ComposeInfo info {};
249     info.srcRect = GraphicIRect {srcRect.left_, srcRect.top_, srcRect.width_, srcRect.height_};
250     info.dstRect = GraphicIRect {static_cast<int32_t>(dstRect.left_ * screenInfo_.GetRogWidthRatio()),
251         static_cast<int32_t>(dstRect.top_ * screenInfo_.GetRogHeightRatio()),
252         static_cast<int32_t>(dstRect.width_ * screenInfo_.GetRogWidthRatio()),
253         static_cast<int32_t>(dstRect.height_ * screenInfo_.GetRogHeightRatio())};
254     info.boundRect = info.dstRect;
255     info.visibleRect = info.dstRect;
256     std::vector<GraphicIRect> dirtyRects;
257     dirtyRects.emplace_back(GraphicIRect {0, 0, 0, 0});
258     info.dirtyRects = dirtyRects;
259     info.zOrder = static_cast<int32_t>(surfaceHandler->GetGlobalZOrder());
260     info.alpha.enGlobalAlpha = true;
261     info.alpha.gAlpha = 255; // 255 means not transparent
262     SetPreBufferInfo(*surfaceHandler, info);
263     info.buffer = buffer;
264     info.fence = surfaceHandler->GetAcquireFence();
265     info.blendType = GRAPHIC_BLEND_SRCOVER;
266     info.needClient = false;
267     info.matrix = GraphicMatrix {1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 1.f};
268     info.gravity = static_cast<int32_t>(Gravity::RESIZE);
269 
270     info.displayNit = DEFAULT_BRIGHTNESS;
271     info.brightnessRatio = NO_RATIO;
272     RS_LOGD_IF(DEBUG_COMPOSER,
273         "RSUniRenderComposerAdapter::BuildCInfo id:%{public}" PRIu64
274         " zOrder:%{public}d blendType:%{public}d needClient:%{public}d displayNit:%{public}d brightnessRatio:%{public}f"
275         " alpha[%{public}d %{public}d] boundRect[%{public}d %{public}d %{public}d %{public}d]"
276         " srcRect[%{public}d %{public}d %{public}d %{public}d] dstRect[%{public}d %{public}d %{public}d %{public}d]"
277         " matrix[%{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f]",
278         rcdDrawable.GetId(), info.zOrder, info.blendType, info.needClient, info.displayNit, info.brightnessRatio,
279         info.alpha.enGlobalAlpha, info.alpha.gAlpha,
280         info.boundRect.x, info.boundRect.y, info.boundRect.w, info.boundRect.h,
281         info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.y,
282         info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.y,
283         info.matrix.scaleX, info.matrix.scaleY, info.matrix.skewX, info.matrix.skewY, info.matrix.transX,
284         info.matrix.transY, info.matrix.pers0, info.matrix.pers1, info.matrix.pers2);
285     return info;
286 }
287 
SetComposeInfoToLayer(const LayerInfoPtr & layer,const ComposeInfo & info,const sptr<IConsumerSurface> & surface) const288 void RSUniRenderComposerAdapter::SetComposeInfoToLayer(
289     const LayerInfoPtr& layer,
290     const ComposeInfo& info,
291     const sptr<IConsumerSurface>& surface) const
292 {
293     if (layer == nullptr) {
294         RS_LOGE("RSUniRenderComposerAdapter::SetCInfoToLayer layer is nullptr");
295         return;
296     }
297     layer->SetSurface(surface);
298     layer->SetBuffer(info.buffer, info.fence);
299     layer->SetPreBuffer(info.preBuffer);
300     layer->SetZorder(info.zOrder);
301     layer->SetAlpha(info.alpha);
302     layer->SetLayerSize(info.dstRect);
303     layer->SetBoundSize(info.boundRect);
304     layer->SetCompositionType(info.needClient ?
305         GraphicCompositionType::GRAPHIC_COMPOSITION_CLIENT : GraphicCompositionType::GRAPHIC_COMPOSITION_DEVICE);
306     std::vector<GraphicIRect> visibleRegions;
307     visibleRegions.emplace_back(info.visibleRect);
308     layer->SetVisibleRegions(visibleRegions);
309     std::vector<GraphicIRect> dirtyRegions;
310     if (RSSystemProperties::GetHwcDirtyRegionEnabled()) {
311         // Make sure the dirty region does not exceed layer src range.
312         for (const auto& rect : info.dirtyRects) {
313             dirtyRegions.emplace_back(RSUniRenderUtil::IntersectRect(info.srcRect, rect));
314         }
315     } else {
316         dirtyRegions.emplace_back(info.srcRect);
317     }
318     layer->SetDirtyRegions(dirtyRegions);
319     layer->SetBlendType(info.blendType);
320     layer->SetCropRect(info.srcRect);
321     layer->SetMatrix(info.matrix);
322     layer->SetGravity(info.gravity);
323     SetMetaDataInfoToLayer(layer, info.buffer, surface);
324     layer->SetSdrNit(info.sdrNit);
325     layer->SetDisplayNit(info.displayNit);
326     layer->SetBrightnessRatio(info.brightnessRatio);
327     layer->SetLayerLinearMatrix(info.layerLinearMatrix);
328 }
329 
SetBufferColorSpace(DrawableV2::RSDisplayRenderNodeDrawable & displayDrawable)330 void RSUniRenderComposerAdapter::SetBufferColorSpace(DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable)
331 {
332     sptr<SurfaceBuffer> buffer = displayDrawable.GetRSSurfaceHandlerOnDraw()->GetBuffer();
333     if (buffer == nullptr) {
334         RS_LOGE("RSUniRenderComposerAdapter::SetBufferColorSpace SurfaceBuffer is null");
335         return;
336     }
337 
338     auto rsSurface = displayDrawable.GetRSSurface();
339     if (rsSurface == nullptr) {
340         RS_LOGE("RSUniRenderComposerAdapter::SetBufferColorSpace RSSurface is null");
341         return;
342     }
343 
344     using namespace HDI::Display::Graphic::Common::V1_0;
345     static const std::map<GraphicColorGamut, CM_ColorSpaceType> RS_TO_COMMON_COLOR_SPACE_TYPE_MAP {
346         {GRAPHIC_COLOR_GAMUT_STANDARD_BT601, CM_BT601_EBU_FULL},
347         {GRAPHIC_COLOR_GAMUT_STANDARD_BT709, CM_BT709_FULL},
348         {GRAPHIC_COLOR_GAMUT_SRGB, CM_SRGB_FULL},
349         {GRAPHIC_COLOR_GAMUT_ADOBE_RGB, CM_ADOBERGB_FULL},
350         {GRAPHIC_COLOR_GAMUT_DISPLAY_P3, CM_P3_FULL},
351         {GRAPHIC_COLOR_GAMUT_BT2020, CM_DISPLAY_BT2020_SRGB},
352         {GRAPHIC_COLOR_GAMUT_BT2100_PQ, CM_BT2020_PQ_FULL},
353         {GRAPHIC_COLOR_GAMUT_BT2100_HLG, CM_BT2020_HLG_FULL},
354         {GRAPHIC_COLOR_GAMUT_DISPLAY_BT2020, CM_DISPLAY_BT2020_SRGB},
355     };
356 
357     GraphicColorGamut rsColorSpace = rsSurface->GetColorSpace();
358     CM_ColorSpaceType colorSpace;
359     auto it = RS_TO_COMMON_COLOR_SPACE_TYPE_MAP.find(rsColorSpace);
360     if (it != RS_TO_COMMON_COLOR_SPACE_TYPE_MAP.end()) {
361         colorSpace = it->second;
362     } else {
363         RS_LOGW("RSUniRenderComposerAdapter::SetBufferColorSpace unknown color space");
364         colorSpace = CM_COLORSPACE_NONE;
365     }
366 
367     if (MetadataHelper::SetColorSpaceType(buffer, colorSpace) != GSERROR_OK) {
368         RS_LOGE("RSUniRenderComposerAdapter::SetBufferColorSpace set color space fail");
369     }
370 }
371 
SetMetaDataInfoToLayer(const LayerInfoPtr & layer,const sptr<SurfaceBuffer> & buffer,const sptr<IConsumerSurface> & surface) const372 void RSUniRenderComposerAdapter::SetMetaDataInfoToLayer(const LayerInfoPtr& layer, const sptr<SurfaceBuffer>& buffer,
373                                                         const sptr<IConsumerSurface>& surface) const
374 {
375     HDRMetaDataType type;
376     if (!layer || !surface || !buffer) {
377         RS_LOGE("RSUniRenderComposerAdapter::SetMDataInfoToLayer fail, layer or surface or buffer is nullptr");
378         return;
379     }
380     if (surface->QueryMetaDataType(buffer->GetSeqNum(), type) != GSERROR_OK) {
381         RS_LOGD("RSUniRenderComposerAdapter::SetComposeInfoToLayer: QueryMetaDataType failed");
382         return;
383     }
384     RS_LOGI_IF(DEBUG_COMPOSER, "RSUniRenderComposerAdapter::SetMDataInfoToLayer type is %{public}d", type);
385     switch (type) {
386         case HDRMetaDataType::HDR_META_DATA: {
387             std::vector<GraphicHDRMetaData> metaData;
388             if (surface->GetMetaData(buffer->GetSeqNum(), metaData) != GSERROR_OK) {
389                 RS_LOGE("RSUniRenderComposerAdapter::SetComposeInfoToLayer: GetMetaData failed");
390                 return;
391             }
392             layer->SetMetaData(metaData);
393             break;
394         }
395         case HDRMetaDataType::HDR_META_DATA_SET: {
396             GraphicHDRMetadataKey key;
397             std::vector<uint8_t> metaData;
398             if (surface->GetMetaDataSet(buffer->GetSeqNum(), key, metaData) != GSERROR_OK) {
399                 RS_LOGE("RSUniRenderComposerAdapter::SetComposeInfoToLayer: GetMetaDataSet failed");
400                 return;
401             }
402             GraphicHDRMetaDataSet metaDataSet;
403             metaDataSet.key = key;
404             metaDataSet.metaData = metaData;
405             layer->SetMetaDataSet(metaDataSet);
406             break;
407         }
408         case HDRMetaDataType::HDR_NOT_USED: {
409             break;
410         }
411         default:  {
412             break;
413         }
414     }
415 }
416 
GetComposerInfoSrcRect(ComposeInfo & info,const RSSurfaceRenderNode & node)417 void RSUniRenderComposerAdapter::GetComposerInfoSrcRect(ComposeInfo &info, const RSSurfaceRenderNode& node)
418 {
419     auto consumer = node.GetRSSurfaceHandler()->GetConsumer();
420     if (!consumer || !info.buffer) {
421         RS_LOGE("RSUniRenderComposerAdapter::GetCInfoSrcRect fail, consumer or buffer is nullptr");
422         return;
423     }
424     const auto& property = node.GetRenderProperties();
425     const auto bufferWidth = info.buffer->GetSurfaceBufferWidth();
426     const auto bufferHeight = info.buffer->GetSurfaceBufferHeight();
427     auto boundsWidth = property.GetBoundsWidth();
428     auto boundsHeight = property.GetBoundsHeight();
429     GraphicTransformType transformType = RSBaseRenderUtil::GetRotateTransform(
430         RSBaseRenderUtil::GetSurfaceBufferTransformType(consumer, node.GetRSSurfaceHandler()->GetBuffer()));
431     if (transformType == GraphicTransformType::GRAPHIC_ROTATE_270 ||
432         transformType == GraphicTransformType::GRAPHIC_ROTATE_90) {
433         std::swap(boundsWidth, boundsHeight);
434     }
435     RS_LOGI_IF(DEBUG_COMPOSER, "RSUniRenderComposerAdapter::GetCInfoSrcRect bufferWidth:%{public}d"
436         " boundsWidth:%{public}f bufferHeight:%{public}d boundsHeight:%{public}f frameGravity:%{public}d"
437         " transformType:%{public}d srcRect[%{public}d %{public}d %{public}d %{public}d]", bufferWidth,
438         boundsWidth, bufferHeight, boundsHeight, node.GetRenderProperties().GetFrameGravity(),
439         transformType, info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.h);
440     if ((bufferWidth != boundsWidth || bufferHeight != boundsHeight) &&
441         node.GetRenderProperties().GetFrameGravity() != Gravity::TOP_LEFT) {
442         float xScale = (ROSEN_EQ(boundsWidth, 0.0f) ? 1.0f : bufferWidth / boundsWidth);
443         float yScale = (ROSEN_EQ(boundsHeight, 0.0f) ? 1.0f : bufferHeight / boundsHeight);
444 
445         // If the scaling mode is SCALING_MODE_SCALE_TO_WINDOW, the scale should use smaller one.
446         if (info.buffer->GetSurfaceBufferScalingMode() == ScalingMode::SCALING_MODE_SCALE_CROP) {
447             float scale = std::min(xScale, yScale);
448             info.srcRect.x = info.srcRect.x * scale;
449             info.srcRect.y = info.srcRect.y * scale;
450             if (ROSEN_EQ(scale, 0.f)) {
451                 RS_LOGE("RSUniRenderComposerAdapter::GetCInfoSrcRect fail, ROSEN_EQ is true");
452                 return;
453             }
454             info.srcRect.w = (bufferWidth / scale - (boundsWidth - info.srcRect.w)) * scale;
455             info.srcRect.h = (bufferHeight / scale - (boundsHeight - info.srcRect.h)) * scale;
456         } else {
457             auto geo = property.GetBoundsGeometry();
458             if (geo && geo->GetAbsRect() == node.GetDstRect()) {
459                 // If the SurfaceRenderNode is completely in the DisplayRenderNode,
460                 // we do not need to crop the buffer.
461                 info.srcRect.w = bufferWidth;
462                 info.srcRect.h = bufferHeight;
463             } else {
464                 info.srcRect.x = info.srcRect.x * xScale;
465                 info.srcRect.y = info.srcRect.y * yScale;
466                 info.srcRect.w = std::min(static_cast<int32_t>(std::ceil(info.srcRect.w * xScale)), bufferWidth);
467                 info.srcRect.h = std::min(static_cast<int32_t>(std::ceil(info.srcRect.h * yScale)), bufferHeight);
468             }
469         }
470     }
471     Drawing::RectI srcRect(
472         info.srcRect.x, info.srcRect.y, info.srcRect.w + info.srcRect.x, info.srcRect.h + info.srcRect.y);
473     Drawing::RectI bufferRect(0, 0, bufferWidth, bufferHeight);
474     if (srcRect.Intersect(bufferRect)) {
475         info.srcRect.x = srcRect.GetLeft();
476         info.srcRect.y = srcRect.GetTop();
477         info.srcRect.w = srcRect.GetWidth();
478         info.srcRect.h = srcRect.GetHeight();
479     } else {
480         info.srcRect = { 0, 0, 0, 0 };
481     }
482 
483     RS_LOGD("RsDebug RSUniRenderComposerAdapter::GetComposerInfoSrcRect surfaceNode id:%{public}" PRIu64 ","\
484             "srcRect [%{public}d %{public}d %{public}d %{public}d]",
485             node.GetId(), info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.h);
486 }
487 
GetComposerInfoSrcRect(ComposeInfo & info,const DrawableV2::RSSurfaceRenderNodeDrawable & surfaceDrawable)488 void RSUniRenderComposerAdapter::GetComposerInfoSrcRect(
489     ComposeInfo& info, const DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable)
490 {
491     auto& params = surfaceDrawable.GetRenderParams();
492     if (!params || !info.buffer || !surfaceDrawable.GetConsumerOnDraw()) {
493         RS_LOGE("RSUniRenderComposerAdapter::GetCInfoSrcRect fail, params or buffer or surfaceDrawable's"
494             " consumerOnDraw is nullptr");
495         return;
496     }
497     const auto bufferWidth = info.buffer->GetSurfaceBufferWidth();
498     const auto bufferHeight = info.buffer->GetSurfaceBufferHeight();
499     auto boundsWidth = params->GetBounds().GetWidth();
500     auto boundsHeight = params->GetBounds().GetHeight();
501     GraphicTransformType transformType = RSBaseRenderUtil::GetRotateTransform(
502         RSBaseRenderUtil::GetSurfaceBufferTransformType(surfaceDrawable.GetConsumerOnDraw(), info.buffer));
503     if (transformType == GraphicTransformType::GRAPHIC_ROTATE_270 ||
504         transformType == GraphicTransformType::GRAPHIC_ROTATE_90) {
505         std::swap(boundsWidth, boundsHeight);
506     }
507     RS_LOGI_IF(DEBUG_COMPOSER, "RSUniRenderComposerAdapter::GetCInfoSrcRect bufferWidth:%{public}d"
508         " boundsWidth:%{public}f bufferHeight:%{public}d boundsHeight:%{public}f frameGravity:%{public}d"
509         " transformType:%{public}d srcRect[%{public}d %{public}d %{public}d %{public}d]", bufferWidth,
510         boundsWidth, bufferHeight, boundsHeight, params->GetFrameGravity(),
511         transformType, info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.h);
512     if ((bufferWidth != boundsWidth || bufferHeight != boundsHeight) &&
513         params->GetFrameGravity() != Gravity::TOP_LEFT) {
514         float xScale = (ROSEN_EQ(boundsWidth, 0.0f) ? 1.0f : bufferWidth / boundsWidth);
515         float yScale = (ROSEN_EQ(boundsHeight, 0.0f) ? 1.0f : bufferHeight / boundsHeight);
516 
517         // If the scaling mode is SCALING_MODE_SCALE_TO_WINDOW, the scale should use smaller one.
518         if (info.buffer->GetSurfaceBufferScalingMode() == ScalingMode::SCALING_MODE_SCALE_CROP) {
519             float scale = std::min(xScale, yScale);
520             info.srcRect.x = info.srcRect.x * scale;
521             info.srcRect.y = info.srcRect.y * scale;
522             if (ROSEN_EQ(scale, 0.f)) {
523                 RS_LOGE("RSUniRenderComposerAdapter::GetCInfoSrcRect fail, ROSEN_EQ is true");
524                 return;
525             }
526             info.srcRect.w = (bufferWidth / scale - (boundsWidth - info.srcRect.w)) * scale;
527             info.srcRect.h = (bufferHeight / scale - (boundsHeight - info.srcRect.h)) * scale;
528         } else {
529             RectI layerInfoSrcRect = { params->GetLayerInfo().srcRect.x, params->GetLayerInfo().srcRect.y,
530                 params->GetLayerInfo().srcRect.w, params->GetLayerInfo().srcRect.h };
531             if (params->GetAbsDrawRect() == layerInfoSrcRect) {
532                 // If the SurfaceRenderNode is completely in the DisplayRenderNode,
533                 // we do not need to crop the buffer.
534                 info.srcRect.w = bufferWidth;
535                 info.srcRect.h = bufferHeight;
536             } else {
537                 info.srcRect.x = info.srcRect.x * xScale;
538                 info.srcRect.y = info.srcRect.y * yScale;
539                 info.srcRect.w = std::min(static_cast<int32_t>(std::ceil(info.srcRect.w * xScale)), bufferWidth);
540                 info.srcRect.h = std::min(static_cast<int32_t>(std::ceil(info.srcRect.h * yScale)), bufferHeight);
541             }
542         }
543     }
544     Drawing::RectI srcRect(
545         info.srcRect.x, info.srcRect.y, info.srcRect.w + info.srcRect.x, info.srcRect.h + info.srcRect.y);
546     Drawing::RectI bufferRect(0, 0, bufferWidth, bufferHeight);
547     if (srcRect.Intersect(bufferRect)) {
548         info.srcRect.x = srcRect.GetLeft();
549         info.srcRect.y = srcRect.GetTop();
550         info.srcRect.w = srcRect.GetWidth();
551         info.srcRect.h = srcRect.GetHeight();
552     } else {
553         info.srcRect = { 0, 0, 0, 0 };
554     }
555 
556     RS_LOGD("RsDebug RSUniRenderComposerAdapter::GetComposerInfoSrcRect surfaceNode id:%{public}" PRIu64 ","\
557             "srcRect [%{public}d %{public}d %{public}d %{public}d]",
558             surfaceDrawable.GetId(), info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.h);
559 }
560 
GetComposerInfoNeedClient(const ComposeInfo & info,RSRenderParams & params) const561 bool RSUniRenderComposerAdapter::GetComposerInfoNeedClient(const ComposeInfo& info, RSRenderParams& params) const
562 {
563     bool needClient = params.GetNeedClient();
564     if (info.buffer &&
565         info.buffer->GetSurfaceBufferColorGamut() != static_cast<GraphicColorGamut>(screenInfo_.colorGamut)) {
566         needClient = true;
567     }
568     return needClient;
569 }
570 
DealWithNodeGravity(const RSSurfaceRenderNode & node,ComposeInfo & info) const571 void RSUniRenderComposerAdapter::DealWithNodeGravity(const RSSurfaceRenderNode& node, ComposeInfo& info) const
572 {
573     const auto& property = node.GetRenderProperties();
574     const float frameWidth = info.buffer->GetSurfaceBufferWidth();
575     const float frameHeight = info.buffer->GetSurfaceBufferHeight();
576     const float boundsWidth = property.GetBoundsWidth();
577     const float boundsHeight = property.GetBoundsHeight();
578     const Gravity frameGravity = property.GetFrameGravity();
579     info.gravity = static_cast<int32_t>(frameGravity);
580     // we do not need to do additional works for Gravity::RESIZE and if frameSize == boundsSize.
581     RS_LOGI_IF(DEBUG_COMPOSER, "RSUniRenderComposerAdapter::DealDataGravity frameGravity:%{public}d"
582         " frameWidth:%{public}f boundsWidth:%{public}f frameHeight:%{public}f boundsHeight:%{public}f",
583         frameGravity, frameWidth, boundsWidth, frameHeight, boundsHeight);
584     if (frameGravity == Gravity::RESIZE || frameGravity == Gravity::TOP_LEFT ||
585         (frameWidth == boundsWidth && frameHeight == boundsHeight)) {
586         RS_LOGE("RSUniRenderComposerAdapter::DealDataGravity fail, data check fail");
587         return;
588     }
589     auto traceInfo = node.GetName() + " DealWithNodeGravity " + std::to_string(static_cast<int>(frameGravity));
590     RS_TRACE_NAME(traceInfo.c_str());
591     // get current node's translate matrix and calculate gravity matrix.
592     auto translateMatrix = Drawing::Matrix();
593     translateMatrix.Translate(node.GetTotalMatrix().Get(Drawing::Matrix::Index::TRANS_X),
594         std::ceil(node.GetTotalMatrix().Get(Drawing::Matrix::Index::TRANS_Y)));
595     Drawing::Matrix gravityMatrix;
596     (void)RSPropertiesPainter::GetGravityMatrix(frameGravity,
597         RectF {0.0f, 0.0f, boundsWidth, boundsHeight}, frameWidth, frameHeight, gravityMatrix);
598     // create a canvas to calculate new dstRect and new srcRect
599     int32_t screenWidth = screenInfo_.phyWidth;
600     int32_t screenHeight = screenInfo_.phyHeight;
601     const auto screenRotation = screenInfo_.rotation;
602     if (screenRotation == ScreenRotation::ROTATION_90 || screenRotation == ScreenRotation::ROTATION_270) {
603         std::swap(screenWidth, screenHeight);
604     }
605     auto canvas = std::make_unique<Drawing::Canvas>(screenWidth, screenHeight);
606     canvas->ConcatMatrix(translateMatrix);
607     canvas->ConcatMatrix(gravityMatrix);
608     Drawing::Rect clipRect;
609     gravityMatrix.MapRect(clipRect, Drawing::Rect(0, 0, frameWidth, frameHeight));
610     canvas->ClipRect(Drawing::Rect(0, 0, clipRect.GetWidth(), clipRect.GetHeight()), Drawing::ClipOp::INTERSECT);
611     Drawing::RectI newDstRect = canvas->GetDeviceClipBounds();
612     // we make the newDstRect as the intersection of new and old dstRect,
613     // to deal with the situation that frameSize > boundsSize.
614     newDstRect.Intersect(Drawing::RectI(
615         info.dstRect.x, info.dstRect.y, info.dstRect.w + info.dstRect.x, info.dstRect.h + info.dstRect.y));
616     auto localRect = canvas->GetLocalClipBounds();
617     int left = std::clamp<int>(localRect.GetLeft(), 0, frameWidth);
618     int top = std::clamp<int>(localRect.GetTop(), 0, frameHeight);
619     int width = std::clamp<int>(localRect.GetWidth(), 0, frameWidth - left);
620     int height = std::clamp<int>(localRect.GetHeight(), 0, frameHeight - top);
621     GraphicIRect newSrcRect = {left, top, width, height};
622 
623     // log and apply new dstRect and srcRect
624     RS_LOGD("RsDebug DealWithNodeGravity: name[%{public}s], gravity[%{public}d], oldDstRect[%{public}d %{public}d"
625         " %{public}d %{public}d], newDstRect[%{public}d %{public}d %{public}d %{public}d], oldSrcRect[%{public}d"
626         " %{public}d %{public}d %{public}d], newSrcRect[%{public}d %{public}d %{public}d %{public}d].",
627         node.GetName().c_str(), static_cast<int>(frameGravity),
628         info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h,
629         newDstRect.GetLeft(), newDstRect.GetTop(), newDstRect.GetWidth(), newDstRect.GetHeight(),
630         info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.h,
631         newSrcRect.x, newSrcRect.y, newSrcRect.w, newSrcRect.h);
632     info.dstRect = {newDstRect.GetLeft(), newDstRect.GetTop(), newDstRect.GetWidth(), newDstRect.GetHeight()};
633     info.srcRect = newSrcRect;
634 }
635 
DealWithNodeGravity(const DrawableV2::RSSurfaceRenderNodeDrawable & surfaceDrawable,ComposeInfo & info) const636 void RSUniRenderComposerAdapter::DealWithNodeGravity(
637     const DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable, ComposeInfo& info) const
638 {
639     auto& params = surfaceDrawable.GetRenderParams();
640     if (!params) {
641         RS_LOGE("RSUniRenderComposerAdapter::DealDataGravity fail, params is nullptr");
642         return;
643     }
644     const float frameWidth = info.buffer->GetSurfaceBufferWidth();
645     const float frameHeight = info.buffer->GetSurfaceBufferHeight();
646     const float boundsWidth = params->GetBounds().GetWidth();
647     const float boundsHeight = params->GetBounds().GetHeight();
648     const Gravity frameGravity = params->GetFrameGravity();
649     info.gravity = static_cast<int32_t>(frameGravity);
650     // we do not need to do additional works for Gravity::RESIZE and if frameSize == boundsSize.
651     RS_LOGI_IF(DEBUG_COMPOSER, "RSUniRenderComposerAdapter::DealDataGravity frameGravity:%{public}d"
652         " frameWidth:%{public}f boundsWidth:%{public}f frameHeight:%{public}f boundsHeight:%{public}f",
653         frameGravity, frameWidth, boundsWidth, frameHeight, boundsHeight);
654     if (frameGravity == Gravity::RESIZE || frameGravity == Gravity::TOP_LEFT ||
655         (frameWidth == boundsWidth && frameHeight == boundsHeight)) {
656         return;
657     }
658 
659     auto traceInfo =
660         surfaceDrawable.GetName() + " DealWithNodeGravity " + std::to_string(static_cast<int>(frameGravity));
661     RS_TRACE_NAME(traceInfo.c_str());
662 
663     // get current node's translate matrix and calculate gravity matrix.
664     auto translateMatrix = Drawing::Matrix();
665     translateMatrix.Translate(params->GetTotalMatrix().Get(Drawing::Matrix::Index::TRANS_X),
666         std::ceil(params->GetTotalMatrix().Get(Drawing::Matrix::Index::TRANS_Y)));
667     Drawing::Matrix gravityMatrix;
668     (void)RSPropertiesPainter::GetGravityMatrix(frameGravity,
669         RectF {0.0f, 0.0f, boundsWidth, boundsHeight}, frameWidth, frameHeight, gravityMatrix);
670     // create a canvas to calculate new dstRect and new srcRect
671     int32_t screenWidth = screenInfo_.phyWidth;
672     int32_t screenHeight = screenInfo_.phyHeight;
673     const auto screenRotation = screenInfo_.rotation;
674     if (screenRotation == ScreenRotation::ROTATION_90 || screenRotation == ScreenRotation::ROTATION_270) {
675         std::swap(screenWidth, screenHeight);
676     }
677     auto canvas = std::make_unique<Drawing::Canvas>(screenWidth, screenHeight);
678     canvas->ConcatMatrix(translateMatrix);
679     canvas->ConcatMatrix(gravityMatrix);
680     Drawing::Rect clipRect;
681     gravityMatrix.MapRect(clipRect, Drawing::Rect(0, 0, frameWidth, frameHeight));
682     canvas->ClipRect(Drawing::Rect(0, 0, clipRect.GetWidth(), clipRect.GetHeight()), Drawing::ClipOp::INTERSECT);
683     Drawing::RectI newDstRect = canvas->GetDeviceClipBounds();
684     // we make the newDstRect as the intersection of new and old dstRect,
685     // to deal with the situation that frameSize > boundsSize.
686     newDstRect.Intersect(Drawing::RectI(
687         info.dstRect.x, info.dstRect.y, info.dstRect.w + info.dstRect.x, info.dstRect.h + info.dstRect.y));
688     auto localRect = canvas->GetLocalClipBounds();
689     int left = std::clamp<int>(localRect.GetLeft(), 0, frameWidth);
690     int top = std::clamp<int>(localRect.GetTop(), 0, frameHeight);
691     int width = std::clamp<int>(localRect.GetWidth(), 0, frameWidth - left);
692     int height = std::clamp<int>(localRect.GetHeight(), 0, frameHeight - top);
693     GraphicIRect newSrcRect = {left, top, width, height};
694 
695     // log and apply new dstRect and srcRect
696     RS_LOGD("RsDebug DealWithNodeGravity: name[%{public}s], gravity[%{public}d], oldDstRect[%{public}d %{public}d"
697         " %{public}d %{public}d], newDstRect[%{public}d %{public}d %{public}d %{public}d], oldSrcRect[%{public}d"
698         " %{public}d %{public}d %{public}d], newSrcRect[%{public}d %{public}d %{public}d %{public}d].",
699         surfaceDrawable.GetName().c_str(), static_cast<int>(frameGravity),
700         info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h,
701         newDstRect.GetLeft(), newDstRect.GetTop(), newDstRect.GetWidth(), newDstRect.GetHeight(),
702         info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.h,
703         newSrcRect.x, newSrcRect.y, newSrcRect.w, newSrcRect.h);
704     info.dstRect = {newDstRect.GetLeft(), newDstRect.GetTop(), newDstRect.GetWidth(), newDstRect.GetHeight()};
705     info.srcRect = newSrcRect;
706 }
707 
SrcRectRotateTransform(RSSurfaceRenderNode & node)708 RectI RSUniRenderComposerAdapter::SrcRectRotateTransform(RSSurfaceRenderNode& node)
709 {
710     auto consumer = node.GetRSSurfaceHandler()->GetConsumer();
711     if (!consumer) {
712         RS_LOGE("RSUniRenderComposerAdapter::SrcRectDataransform fail, consumer is nullptr");
713         return node.GetSrcRect();
714     }
715     RectI srcRect = node.GetSrcRect();
716     int left = srcRect.GetLeft();
717     int top = srcRect.GetTop();
718     int width = srcRect.GetWidth();
719     int height = srcRect.GetHeight();
720     GraphicTransformType transformType = RSBaseRenderUtil::GetRotateTransform(
721         RSBaseRenderUtil::GetSurfaceBufferTransformType(consumer, node.GetRSSurfaceHandler()->GetBuffer()));
722     int boundsWidth = static_cast<int>(node.GetRenderProperties().GetBoundsWidth());
723     int boundsHeight = static_cast<int>(node.GetRenderProperties().GetBoundsHeight());
724     // Left > 0 means move xComponent to the left outside of the screen
725     // Top > 0 means move xComponent to the top outside of the screen
726     // The left and top should recalculate when transformType is not GRAPHIC_ROTATE_NONE
727     // The width and height should exchange when transformType is GRAPHIC_ROTATE_270 and GRAPHIC_ROTATE_90
728     RS_LOGI_IF(DEBUG_COMPOSER,
729         "RSUniRenderComposerAdapter::DealDataGravity transformType is %{public}d", transformType);
730     switch (transformType) {
731         case GraphicTransformType::GRAPHIC_ROTATE_270: {
732             left = std::max(top, 0);
733             top = std::max(boundsWidth - width - srcRect.GetLeft(), 0);
734             srcRect = RectI {left, top, height, width};
735             break;
736         }
737         case GraphicTransformType::GRAPHIC_ROTATE_180: {
738             left = std::max(boundsWidth - width - left, 0);
739             top = std::max(boundsHeight - height - top, 0);
740             srcRect = RectI {left, top, width, height};
741             break;
742         }
743         case GraphicTransformType::GRAPHIC_ROTATE_90: {
744             left = std::max(boundsHeight - height - top, 0);
745             top = std::max(srcRect.GetLeft(), 0);
746             srcRect = RectI {left, top, height, width};
747             break;
748         }
749         default: {
750             break;
751         }
752     }
753     RS_LOGD("BuildComposeInfo: srcRect transformed info NodeId:%{public}" PRIu64 ", XYWH:%{public}u,"
754         "%{public}u,%{public}u,%{public}u", node.GetId(),
755         srcRect.GetLeft(), srcRect.GetTop(), srcRect.GetWidth(), srcRect.GetHeight());
756     return srcRect;
757 }
758 
SrcRectRotateTransform(DrawableV2::RSSurfaceRenderNodeDrawable & surfaceDrawable)759 RectI RSUniRenderComposerAdapter::SrcRectRotateTransform(DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable)
760 {
761     auto& params = surfaceDrawable.GetRenderParams();
762     auto consumer = surfaceDrawable.GetConsumerOnDraw();
763     if (!params || !consumer) {
764         RS_LOGE("RSUniRenderComposerAdapter::SrcRectDataransform fail, params or consumer is nullptr");
765         return {};
766     }
767     const auto& srcGraphicRect = params->GetLayerInfo().srcRect;
768     RectI srcRect = {srcGraphicRect.x, srcGraphicRect.y, srcGraphicRect.w, srcGraphicRect.h};
769     int left = srcRect.GetLeft();
770     int top = srcRect.GetTop();
771     int width = srcRect.GetWidth();
772     int height = srcRect.GetHeight();
773     GraphicTransformType transformType = RSBaseRenderUtil::GetRotateTransform(
774         RSBaseRenderUtil::GetSurfaceBufferTransformType(consumer, params->GetBuffer()));
775     const auto& bounds = params->GetBounds();
776     int boundsWidth = static_cast<int>(bounds.GetWidth());
777     int boundsHeight = static_cast<int>(bounds.GetHeight());
778     // Left > 0 means move xComponent to the left outside of the screen
779     // Top > 0 means move xComponent to the top outside of the screen
780     // The left and top should recalculate when transformType is not GRAPHIC_ROTATE_NONE
781     // The width and height should exchange when transformType is GRAPHIC_ROTATE_270 and GRAPHIC_ROTATE_90
782     RS_LOGI_IF(DEBUG_COMPOSER,
783         "RSUniRenderComposerAdapter::DealDataGravity transformType is %{public}d", transformType);
784     switch (transformType) {
785         case GraphicTransformType::GRAPHIC_ROTATE_270: {
786             left = std::max(top, 0);
787             top = std::max(boundsWidth - width - srcRect.GetLeft(), 0);
788             srcRect = RectI {left, top, height, width};
789             break;
790         }
791         case GraphicTransformType::GRAPHIC_ROTATE_180: {
792             left = std::max(boundsWidth - width - left, 0);
793             top = std::max(boundsHeight - height - top, 0);
794             srcRect = RectI {left, top, width, height};
795             break;
796         }
797         case GraphicTransformType::GRAPHIC_ROTATE_90: {
798             left = std::max(boundsHeight - height - top, 0);
799             top = std::max(srcRect.GetLeft(), 0);
800             srcRect = RectI {left, top, height, width};
801             break;
802         }
803         default: {
804             break;
805         }
806     }
807     RS_LOGD("BuildComposeInfo: srcRect transformed info NodeId:%{public}" PRIu64 ", XYWH:%{public}u,"
808         "%{public}u,%{public}u,%{public}u", surfaceDrawable.GetId(),
809         srcRect.GetLeft(), srcRect.GetTop(), srcRect.GetWidth(), srcRect.GetHeight());
810     return srcRect;
811 }
812 
813 // private func, for RSSurfaceRenderNode.
BuildComposeInfo(RSSurfaceRenderNode & node) const814 ComposeInfo RSUniRenderComposerAdapter::BuildComposeInfo(RSSurfaceRenderNode& node) const
815 {
816     ComposeInfo info {};
817     auto& params = node.GetStagingRenderParams();
818     if (!params) {
819         RS_LOGE("RSUniRenderComposerAdapter::BuildComposeInfo fail, node params is nullptr");
820         return info;
821     }
822 
823     auto surfaceHandler = node.GetRSSurfaceHandler();
824     const auto& dstRect = node.GetDstRect();
825     const auto srcRect = SrcRectRotateTransform(node);
826     info.srcRect = GraphicIRect {srcRect.left_, srcRect.top_, srcRect.width_, srcRect.height_};
827     info.dstRect = GraphicIRect {
828         static_cast<int32_t>(static_cast<float>(dstRect.left_) * screenInfo_.GetRogWidthRatio()),
829         static_cast<int32_t>(static_cast<float>(dstRect.top_) * screenInfo_.GetRogHeightRatio()),
830         static_cast<int32_t>(static_cast<float>(dstRect.width_) * screenInfo_.GetRogWidthRatio()),
831         static_cast<int32_t>(static_cast<float>(dstRect.height_) * screenInfo_.GetRogHeightRatio())
832     };
833     info.zOrder = surfaceHandler->GetGlobalZOrder();
834     info.alpha.enGlobalAlpha = true;
835     info.alpha.gAlpha = node.GetGlobalAlpha() * 255; // map gAlpha from float(0, 1) to uint8_t(0, 255).
836     info.fence = surfaceHandler->GetAcquireFence();
837     info.blendType = node.GetBlendType();
838     const auto& buffer = surfaceHandler->GetBuffer();
839     info.buffer = buffer;
840     SetPreBufferInfo(*surfaceHandler, info);
841     GetComposerInfoSrcRect(info, node);
842     info.needClient = GetComposerInfoNeedClient(info, *params);
843     DealWithNodeGravity(node, info);
844 
845     info.dstRect.x -= offsetX_;
846     info.dstRect.y -= offsetY_;
847     info.visibleRect = info.dstRect;
848     std::vector<GraphicIRect> dirtyRects;
849     const Rect& dirtyRect = surfaceHandler->GetDamageRegion();
850     dirtyRects.emplace_back(GraphicIRect {dirtyRect.x, dirtyRect.y, dirtyRect.w, dirtyRect.h});
851     info.dirtyRects = dirtyRects;
852     auto totalMatrix = node.GetTotalMatrix();
853     info.matrix = GraphicMatrix {totalMatrix.Get(Drawing::Matrix::Index::SCALE_X),
854         totalMatrix.Get(Drawing::Matrix::Index::SKEW_X), totalMatrix.Get(Drawing::Matrix::Index::TRANS_X),
855         totalMatrix.Get(Drawing::Matrix::Index::SKEW_Y), totalMatrix.Get(Drawing::Matrix::Index::SCALE_Y),
856         totalMatrix.Get(Drawing::Matrix::Index::TRANS_Y), totalMatrix.Get(Drawing::Matrix::Index::PERSP_0),
857         totalMatrix.Get(Drawing::Matrix::Index::PERSP_1), totalMatrix.Get(Drawing::Matrix::Index::PERSP_2)};
858 
859     const auto& property = node.GetRenderProperties();
860     info.boundRect = { 0, 0,
861         static_cast<int32_t>(property.GetBoundsWidth()), static_cast<int32_t>(property.GetBoundsHeight())};
862 
863     const auto& renderParam = static_cast<RSSurfaceRenderParams*>(params.get());
864     if (renderParam == nullptr) {
865         RS_LOGE("RSUniRenderComposerAdapter::BuildComposeInfo fail, node params is nullptr");
866         return info;
867     }
868     info.sdrNit = renderParam->GetSdrNit();
869     info.displayNit = renderParam->GetDisplayNit();
870     info.brightnessRatio = renderParam->GetBrightnessRatio();
871     info.layerLinearMatrix = renderParam->GetLayerLinearMatrix();
872     RS_LOGD("RSURCA::BuildCInfo sdrNit: %{public}d, displayNit: %{public}d, brightnessRatio: %{public}f",
873         info.sdrNit, info.displayNit, info.brightnessRatio);
874     RS_LOGD("RSUniRenderComposerAdapter::BuildCInfo id:%{public}" PRIu64
875         " zOrder:%{public}d blendType:%{public}d needClient:%{public}d"
876         " alpha[%{public}d %{public}d] boundRect[%{public}d %{public}d %{public}d %{public}d]"
877         " srcRect[%{public}d %{public}d %{public}d %{public}d] dstRect[%{public}d %{public}d %{public}d %{public}d]"
878         " matrix[%{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f]",
879         node.GetId(), info.zOrder, info.blendType, info.needClient,
880         info.alpha.enGlobalAlpha, info.alpha.gAlpha,
881         info.boundRect.x, info.boundRect.y, info.boundRect.w, info.boundRect.h,
882         info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.y,
883         info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.y,
884         info.matrix.scaleX, info.matrix.scaleY, info.matrix.skewX, info.matrix.skewY, info.matrix.transX,
885         info.matrix.transY, info.matrix.pers0, info.matrix.pers1, info.matrix.pers2);
886     return info;
887 }
888 
BuildComposeInfo(DrawableV2::RSSurfaceRenderNodeDrawable & surfaceDrawable) const889 ComposeInfo RSUniRenderComposerAdapter::BuildComposeInfo(DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable) const
890 {
891     ComposeInfo info {};
892     auto& params = surfaceDrawable.GetRenderParams();
893     if (!params) {
894         RS_LOGD("RSUniRenderComposerAdapter::BuildCInfo params is nullptr");
895         return info;
896     }
897     const auto& dstRect = params->GetLayerInfo().dstRect;
898     const auto srcRect = SrcRectRotateTransform(surfaceDrawable);
899     info.srcRect = GraphicIRect {srcRect.left_, srcRect.top_, srcRect.width_, srcRect.height_};
900     info.dstRect = GraphicIRect { static_cast<int32_t>(static_cast<float>(dstRect.x) * screenInfo_.GetRogWidthRatio()),
901         static_cast<int32_t>(static_cast<float>(dstRect.y) * screenInfo_.GetRogHeightRatio()),
902         static_cast<int32_t>(static_cast<float>(dstRect.w) * screenInfo_.GetRogWidthRatio()),
903         static_cast<int32_t>(static_cast<float>(dstRect.h) * screenInfo_.GetRogHeightRatio()) };
904     info.zOrder = params->GetLayerInfo().zOrder;
905     info.alpha.enGlobalAlpha = true;
906     info.alpha.gAlpha = params->GetGlobalAlpha() * 255; // map gAlpha from float(0, 1) to uint8_t(0, 255).
907     info.fence = params->GetAcquireFence();
908     info.blendType = params->GetLayerInfo().blendType;
909     const auto& buffer = params->GetBuffer();
910     info.buffer = buffer;
911     info.preBuffer = params->GetPreBuffer();
912     GetComposerInfoSrcRect(info, surfaceDrawable);
913     info.needClient = GetComposerInfoNeedClient(info, *params);
914     DealWithNodeGravity(surfaceDrawable, info);
915 
916     info.dstRect.x -= offsetX_;
917     info.dstRect.y -= offsetY_;
918     info.visibleRect = info.dstRect;
919     std::vector<GraphicIRect> dirtyRects;
920     const Rect& dirtyRect = params->GetBufferDamage();
921     dirtyRects.emplace_back(GraphicIRect {dirtyRect.x, dirtyRect.y, dirtyRect.w, dirtyRect.h});
922     info.dirtyRects = dirtyRects;
923     auto totalMatrix = params->GetTotalMatrix();
924     info.matrix = GraphicMatrix {totalMatrix.Get(Drawing::Matrix::Index::SCALE_X),
925         totalMatrix.Get(Drawing::Matrix::Index::SKEW_X), totalMatrix.Get(Drawing::Matrix::Index::TRANS_X),
926         totalMatrix.Get(Drawing::Matrix::Index::SKEW_Y), totalMatrix.Get(Drawing::Matrix::Index::SCALE_Y),
927         totalMatrix.Get(Drawing::Matrix::Index::TRANS_Y), totalMatrix.Get(Drawing::Matrix::Index::PERSP_0),
928         totalMatrix.Get(Drawing::Matrix::Index::PERSP_1), totalMatrix.Get(Drawing::Matrix::Index::PERSP_2)};
929 
930     info.boundRect = { 0, 0,
931         static_cast<int32_t>(params->GetBounds().GetWidth()), static_cast<int32_t>(params->GetBounds().GetHeight())};
932 
933     const auto& curRenderParam = static_cast<RSSurfaceRenderParams*>(params.get());
934     if (curRenderParam == nullptr) {
935         RS_LOGE("RSUniRenderComposerAdapter::curRenderParam is nullptr");
936         return info;
937     }
938     info.sdrNit = curRenderParam->GetSdrNit();
939     info.displayNit = curRenderParam->GetDisplayNit();
940     info.brightnessRatio = curRenderParam->GetBrightnessRatio();
941     info.layerLinearMatrix = curRenderParam->GetLayerLinearMatrix();
942     RS_LOGD("RSURCA::BuildCInfo sdrNit: %{public}d, displayNit: %{public}d, brightnessRatio: %{public}f",
943         info.sdrNit, info.displayNit, info.brightnessRatio);
944     RS_LOGD("RSUniRenderComposerAdapter::BuildCInfo id:%{public}" PRIu64
945         " zOrder:%{public}d blendType:%{public}d needClient:%{public}d"
946         " alpha[%{public}d %{public}d] boundRect[%{public}d %{public}d %{public}d %{public}d]"
947         " srcRect[%{public}d %{public}d %{public}d %{public}d] dstRect[%{public}d %{public}d %{public}d %{public}d]"
948         " matrix[%{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f %{public}f]",
949         surfaceDrawable.GetId(), info.zOrder, info.blendType, info.needClient,
950         info.alpha.enGlobalAlpha, info.alpha.gAlpha,
951         info.boundRect.x, info.boundRect.y, info.boundRect.w, info.boundRect.h,
952         info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.y,
953         info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.y,
954         info.matrix.scaleX, info.matrix.scaleY, info.matrix.skewX, info.matrix.skewY, info.matrix.transX,
955         info.matrix.transY, info.matrix.pers0, info.matrix.pers1, info.matrix.pers2);
956     return info;
957 }
958 
CheckStatusBeforeCreateLayer(RSSurfaceRenderNode & node) const959 bool RSUniRenderComposerAdapter::CheckStatusBeforeCreateLayer(RSSurfaceRenderNode& node) const
960 {
961     if (output_ == nullptr) {
962         RS_LOGD("RSUniRenderComposerAdapter::CheckStatusBeforeCreateLayer: output is nullptr");
963         return false;
964     }
965     auto surfaceHandler = node.GetRSSurfaceHandler();
966     if (!surfaceHandler) {
967         RS_LOGD("RSUniRenderComposerAdapter::CheckBeforeCreateLayer false, surfaceHandler is nullptr");
968         return false;
969     }
970     const auto& buffer = surfaceHandler->GetBuffer();
971     if (buffer == nullptr) {
972         RS_LOGD("RsDebug RSUniRenderComposerAdapter::CheckStatusBeforeCreateLayer:"\
973             " node(%{public}" PRIu64 ") has no available buffer.", node.GetId());
974         return false;
975     }
976     const auto& dstRect = node.GetDstRect();
977     const auto& srcRect = node.GetSrcRect();
978 
979     // check if the node's srcRect and dstRect are valid.
980     if (srcRect.width_ <= 0 || srcRect.height_ <= 0 || dstRect.width_ <= 0 || dstRect.height_ <= 0) {
981         RS_LOGD("RSUniRenderComposerAdapter::CheckBeforeCreateLayer false, data check lt 0");
982         return false;
983     }
984 
985     auto& geoPtr = (node.GetRenderProperties().GetBoundsGeometry());
986     if (geoPtr == nullptr) {
987         RS_LOGW("RsDebug RSUniRenderComposerAdapter::CheckStatusBeforeCreateLayer:"\
988             " node(%{public}" PRIu64 ")'s geoPtr is nullptr!", node.GetId());
989         return false;
990     }
991     return true;
992 }
993 
CheckStatusBeforeCreateLayer(DrawableV2::RSSurfaceRenderNodeDrawable & surfaceDrawable) const994 bool RSUniRenderComposerAdapter::CheckStatusBeforeCreateLayer(
995     DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable) const
996 {
997     if (output_ == nullptr) {
998         RS_LOGE("RSUniRenderComposerAdapter::CheckStatusBeforeCreateLayer: output is nullptr");
999         return false;
1000     }
1001 
1002     auto& params = surfaceDrawable.GetRenderParams();
1003     if (!params) {
1004         RS_LOGE("RSUniRenderComposerAdapter::CheckBeforeCreateLayer false, params is nullptr");
1005         return false;
1006     }
1007     const auto& buffer = params->GetBuffer();
1008     if (buffer == nullptr) {
1009         RS_LOGD("RsDebug RSUniRenderComposerAdapter::CheckStatusBeforeCreateLayer:"\
1010             " node(%{public}" PRIu64 ") has no available buffer.", surfaceDrawable.GetId());
1011         return false;
1012     }
1013     const auto& dstRect = params->GetLayerInfo().dstRect;
1014     const auto& srcRect = params->GetLayerInfo().srcRect;
1015     // check if the node's srcRect and dstRect are valid.
1016     if (srcRect.w <= 0 || srcRect.h <= 0 || dstRect.w <= 0 || dstRect.h <= 0) {
1017         RS_LOGE("RSUniRenderComposerAdapter::CheckBeforeCreateLayer false, data check lt 0");
1018         return false;
1019     }
1020 
1021     return true;
1022 }
1023 
1024 // private func, guarantee the layer is valid
LayerCrop(const LayerInfoPtr & layer) const1025 void RSUniRenderComposerAdapter::LayerCrop(const LayerInfoPtr& layer) const
1026 {
1027     GraphicIRect dstRect = layer->GetLayerSize();
1028     GraphicIRect srcRect = layer->GetCropRect();
1029     GraphicIRect originSrcRect = srcRect;
1030 
1031     RectI dstRectI(dstRect.x, dstRect.y, dstRect.w, dstRect.h);
1032     RectI screenRectI(0, 0, static_cast<int32_t>(screenInfo_.phyWidth),
1033         static_cast<int32_t>(screenInfo_.phyHeight));
1034     RectI resDstRect = dstRectI.IntersectRect(screenRectI);
1035     if (resDstRect == dstRectI) {
1036         RS_LOGD("RSUniRenderComposerAdapter::LayerCP fail, resDstRect eq dstRectI");
1037         return;
1038     }
1039     dstRect = {resDstRect.left_, resDstRect.top_, resDstRect.width_, resDstRect.height_};
1040     srcRect.x = (resDstRect.IsEmpty() || dstRectI.IsEmpty()) ? 0 : std::ceil((resDstRect.left_ - dstRectI.left_) *
1041         originSrcRect.w / dstRectI.width_);
1042     srcRect.y = (resDstRect.IsEmpty() || dstRectI.IsEmpty()) ? 0 : std::ceil((resDstRect.top_ - dstRectI.top_) *
1043         originSrcRect.h / dstRectI.height_);
1044     srcRect.w = dstRectI.IsEmpty() ? 0 : originSrcRect.w * resDstRect.width_ / dstRectI.width_;
1045     srcRect.h = dstRectI.IsEmpty() ? 0 : originSrcRect.h * resDstRect.height_ / dstRectI.height_;
1046     layer->SetLayerSize(dstRect);
1047     std::vector<GraphicIRect> dirtyRegions;
1048     dirtyRegions.emplace_back(srcRect);
1049     layer->SetDirtyRegions(dirtyRegions);
1050     layer->SetCropRect(srcRect);
1051     RS_LOGD("RsDebug RSUniRenderComposerAdapter::LayerCrop layer has been cropped dst[%{public}d %{public}d %{public}d"
1052         " %{public}d] src[%{public}d %{public}d %{public}d %{public}d]",
1053         dstRect.x, dstRect.y, dstRect.w, dstRect.h, srcRect.x, srcRect.y, srcRect.w, srcRect.h);
1054 }
1055 
1056 // private func, guarantee the layer is valid
LayerScaleDown(const LayerInfoPtr & layer,RSSurfaceRenderNode & node)1057 void RSUniRenderComposerAdapter::LayerScaleDown(const LayerInfoPtr& layer, RSSurfaceRenderNode& node)
1058 {
1059     const auto& buffer = layer->GetBuffer();
1060     const auto& surface = layer->GetSurface();
1061     if (buffer == nullptr || surface == nullptr) {
1062         RS_LOGE("RSUniRenderComposerAdapter::LayerSD fail, buffer or surface is nullptr");
1063         return;
1064     }
1065 
1066     GraphicIRect dstRect = layer->GetLayerSize();
1067     GraphicIRect srcRect = layer->GetCropRect();
1068 
1069     uint32_t newWidth = static_cast<uint32_t>(srcRect.w);
1070     uint32_t newHeight = static_cast<uint32_t>(srcRect.h);
1071     uint32_t dstWidth = static_cast<uint32_t>(dstRect.w);
1072     uint32_t dstHeight = static_cast<uint32_t>(dstRect.h);
1073 
1074     // If surfaceRotation is not a multiple of 180, need to change the correspondence between width & height.
1075     // ScreenRotation has been processed in SetLayerSize, and do not change the width & height correspondence.
1076     int surfaceRotation = RSUniRenderUtil::GetRotationFromMatrix(node.GetTotalMatrix()) +
1077                           RSBaseRenderUtil::RotateEnumToInt(RSBaseRenderUtil::GetRotateTransform(
1078                               RSBaseRenderUtil::GetSurfaceBufferTransformType(surface, buffer)));
1079     if (surfaceRotation % FLAT_ANGLE != 0) {
1080         std::swap(dstWidth, dstHeight);
1081     }
1082 
1083     uint32_t newWidthDstHeight = newWidth * dstHeight;
1084     uint32_t newHeightDstWidth = newHeight * dstWidth;
1085     RS_LOGI_IF(DEBUG_COMPOSER, "RSUniRenderComposerAdapter::LayerSD newWidthDstHeight:%{public}d"
1086         " newHeightDstWidth:%{public}d newWidth:%{public}d newHeight:%{public}d dstWidth:%{public}d"
1087         " dstHeight:%{public}d", newWidthDstHeight, newHeightDstWidth, newWidth, newHeight, dstWidth, dstHeight);
1088     if (newWidthDstHeight > newHeightDstWidth) {
1089         // too wide
1090         newWidth = dstWidth * newHeight / dstHeight;
1091     } else if (newWidthDstHeight < newHeightDstWidth) {
1092         // too tall
1093         newHeight = dstHeight * newWidth / dstWidth;
1094     } else {
1095         return;
1096     }
1097 
1098     uint32_t currentWidth = static_cast<uint32_t>(srcRect.w);
1099     uint32_t currentHeight = static_cast<uint32_t>(srcRect.h);
1100 
1101     if (newWidth < currentWidth) {
1102         // the crop is too wide
1103         uint32_t dw = currentWidth - newWidth;
1104         auto halfdw = dw / 2;
1105         srcRect.x += static_cast<int32_t>(halfdw);
1106         srcRect.w = static_cast<int32_t>(newWidth);
1107     } else {
1108         // thr crop is too tall
1109         uint32_t dh = currentHeight - newHeight;
1110         auto halfdh = dh / 2;
1111         srcRect.y += static_cast<int32_t>(halfdh);
1112         srcRect.h = static_cast<int32_t>(newHeight);
1113     }
1114     std::vector<GraphicIRect> dirtyRegions;
1115     dirtyRegions.emplace_back(srcRect);
1116     layer->SetDirtyRegions(dirtyRegions);
1117     layer->SetCropRect(srcRect);
1118     RS_LOGD("RsDebug RSUniRenderComposerAdapter::LayerScaleDown layer has been scaledown dst[%{public}d %{public}d"
1119         " %{public}d %{public}d] src[%{public}d %{public}d %{public}d %{public}d]",
1120         dstRect.x, dstRect.y, dstRect.w, dstRect.h, srcRect.x, srcRect.y, srcRect.w, srcRect.h);
1121 }
1122 
LayerScaleDown(const LayerInfoPtr & layer,DrawableV2::RSSurfaceRenderNodeDrawable & surfaceDrawable)1123 void RSUniRenderComposerAdapter::LayerScaleDown(
1124     const LayerInfoPtr& layer, DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable)
1125 {
1126     auto& params = surfaceDrawable.GetRenderParams();
1127     const auto& buffer = layer->GetBuffer();
1128     const auto& surface = layer->GetSurface();
1129     if (!params || !buffer || !surface) {
1130         RS_LOGE("RSUniRenderComposerAdapter::LayerSD fail, params or buffer or surface is nullptr");
1131         return;
1132     }
1133 
1134     GraphicIRect dstRect = layer->GetLayerSize();
1135     GraphicIRect srcRect = layer->GetCropRect();
1136 
1137     uint32_t newWidth = static_cast<uint32_t>(srcRect.w);
1138     uint32_t newHeight = static_cast<uint32_t>(srcRect.h);
1139     uint32_t dstWidth = static_cast<uint32_t>(dstRect.w);
1140     uint32_t dstHeight = static_cast<uint32_t>(dstRect.h);
1141 
1142     // If surfaceRotation is not a multiple of 180, need to change the correspondence between width & height.
1143     // ScreenRotation has been processed in SetLayerSize, and do not change the width & height correspondence.
1144     int surfaceRotation = RSUniRenderUtil::GetRotationFromMatrix(params->GetTotalMatrix()) +
1145                           RSBaseRenderUtil::RotateEnumToInt(RSBaseRenderUtil::GetRotateTransform(
1146                               RSBaseRenderUtil::GetSurfaceBufferTransformType(surface, buffer)));
1147     if (surfaceRotation % FLAT_ANGLE != 0) {
1148         std::swap(dstWidth, dstHeight);
1149     }
1150 
1151     uint32_t newWidthDstHeight = newWidth * dstHeight;
1152     uint32_t newHeightDstWidth = newHeight * dstWidth;
1153 
1154     RS_LOGI_IF(DEBUG_COMPOSER, "RSUniRenderComposerAdapter::LayerSD newWidthDstHeight:%{public}d"
1155         " newHeightDstWidth:%{public}d newWidth:%{public}d newHeight:%{public}d dstWidth:%{public}d"
1156         " dstHeight:%{public}d", newWidthDstHeight, newHeightDstWidth, newWidth, newHeight, dstWidth, dstHeight);
1157     if (newWidthDstHeight > newHeightDstWidth) {
1158         // too wide
1159         newWidth = dstWidth * newHeight / dstHeight;
1160     } else if (newWidthDstHeight < newHeightDstWidth) {
1161         // too tall
1162         newHeight = dstHeight * newWidth / dstWidth;
1163     } else {
1164         return;
1165     }
1166 
1167     uint32_t currentWidth = static_cast<uint32_t>(srcRect.w);
1168     uint32_t currentHeight = static_cast<uint32_t>(srcRect.h);
1169 
1170     if (newWidth < currentWidth) {
1171         // the crop is too wide
1172         uint32_t dw = currentWidth - newWidth;
1173         auto halfdw = dw / 2;
1174         srcRect.x += static_cast<int32_t>(halfdw);
1175         srcRect.w = static_cast<int32_t>(newWidth);
1176     } else {
1177         // thr crop is too tall
1178         uint32_t dh = currentHeight - newHeight;
1179         auto halfdh = dh / 2;
1180         srcRect.y += static_cast<int32_t>(halfdh);
1181         srcRect.h = static_cast<int32_t>(newHeight);
1182     }
1183     std::vector<GraphicIRect> dirtyRegions;
1184     dirtyRegions.emplace_back(srcRect);
1185     layer->SetDirtyRegions(dirtyRegions);
1186     layer->SetCropRect(srcRect);
1187     RS_LOGD("RsDebug RSUniRenderComposerAdapter::LayerScaleDown layer has been scaledown dst[%{public}d %{public}d"
1188         " %{public}d %{public}d] src[%{public}d %{public}d %{public}d %{public}d]",
1189         dstRect.x, dstRect.y, dstRect.w, dstRect.h, srcRect.x, srcRect.y, srcRect.w, srcRect.h);
1190 }
1191 
1192 // private func, guarantee the layer is valid
LayerScaleFit(const LayerInfoPtr & layer) const1193 void RSUniRenderComposerAdapter::LayerScaleFit(const LayerInfoPtr& layer) const
1194 {
1195     GraphicIRect srcRect = layer->GetCropRect();
1196     GraphicIRect dstRect = layer->GetLayerSize();
1197 
1198     ScreenRotation rotation = screenInfo_.rotation;
1199     if (rotation == ScreenRotation::ROTATION_90 || rotation == ScreenRotation::ROTATION_270) {
1200         std::swap(srcRect.w, srcRect.h);
1201     }
1202 
1203     uint32_t newWidth = static_cast<uint32_t>(srcRect.w);
1204     uint32_t newHeight = static_cast<uint32_t>(srcRect.h);
1205     uint32_t dstWidth = static_cast<uint32_t>(dstRect.w);
1206     uint32_t dstHeight = static_cast<uint32_t>(dstRect.h);
1207 
1208     uint32_t newWidthDstHeight = newWidth * dstHeight;
1209     uint32_t newHeightDstWidth = newHeight * dstWidth;
1210 
1211     RS_LOGI_IF(DEBUG_COMPOSER, "RSUniRenderComposerAdapter::LayerSF newWidthDstHeight:%{public}d"
1212         " newHeightDstWidth:%{public}d newWidth:%{public}d newHeight:%{public}d dstWidth:%{public}d"
1213         " dstHeight:%{public}d", newWidthDstHeight, newHeightDstWidth, newWidth, newHeight, dstWidth, dstHeight);
1214     if (newWidthDstHeight > newHeightDstWidth) {
1215         newHeight = newHeight * dstWidth / newWidth;
1216         newWidth = dstWidth;
1217     } else if (newWidthDstHeight < newHeightDstWidth) {
1218         newWidth = newWidth * dstHeight / newHeight;
1219         newHeight = dstHeight;
1220     } else {
1221         newHeight = dstHeight;
1222         newWidth = dstWidth;
1223     }
1224 
1225     if (newWidth < dstWidth) {
1226         uint32_t dw = dstWidth - newWidth;
1227         auto halfdw = dw / 2;
1228         dstRect.x += static_cast<int32_t>(halfdw);
1229     } else if (newHeight < dstHeight) {
1230         uint32_t dh = dstHeight - newHeight;
1231         auto halfdh = dh / 2;
1232         dstRect.y += static_cast<int32_t>(halfdh);
1233     }
1234     dstRect.h = static_cast<int32_t>(newHeight);
1235     dstRect.w = static_cast<int32_t>(newWidth);
1236     layer->SetLayerSize(dstRect);
1237     RS_LOGD("RsDebug RSUniRenderComposerAdapter::LayerScaleFit layer has been scalefit dst[%{public}d %{public}d"
1238         " %{public}d %{public}d] src[%{public}d %{public}d %{public}d %{public}d]",
1239         dstRect.x, dstRect.y, dstRect.w, dstRect.h, srcRect.x, srcRect.y, srcRect.w, srcRect.h);
1240 }
1241 
1242 // private func
IsOutOfScreenRegion(const ComposeInfo & info) const1243 bool RSUniRenderComposerAdapter::IsOutOfScreenRegion(const ComposeInfo& info) const
1244 {
1245     int32_t boundWidth = static_cast<int32_t>(screenInfo_.phyWidth);
1246     int32_t boundHeight = static_cast<int32_t>(screenInfo_.phyHeight);
1247     ScreenRotation rotation = screenInfo_.rotation;
1248     if (rotation == ScreenRotation::ROTATION_90 || rotation == ScreenRotation::ROTATION_270) {
1249         std::swap(boundWidth, boundHeight);
1250     }
1251 
1252     const auto& dstRect = info.dstRect;
1253     RS_LOGI_IF(DEBUG_COMPOSER, "RSUniRenderComposerAdapter::IsOutOfScreenData rotation:%{public}d boundWidth:%{public}d"
1254         " boundHeight:%{public}d dstRect[%{public}d %{public}d %{public}d %{public}d]", rotation, boundWidth,
1255         boundHeight, dstRect.x, dstRect.y, dstRect.w, dstRect.h);
1256     if (dstRect.x + dstRect.w <= 0 ||
1257         dstRect.x >= boundWidth ||
1258         dstRect.y + dstRect.h <= 0 ||
1259         dstRect.y >= boundHeight) {
1260         return true;
1261     }
1262 
1263     return false;
1264 }
1265 
CreateBufferLayer(DrawableV2::RSSurfaceRenderNodeDrawable & surfaceDrawable) const1266 LayerInfoPtr RSUniRenderComposerAdapter::CreateBufferLayer(
1267     DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable) const
1268 {
1269     auto& params = surfaceDrawable.GetRenderParams();
1270     if (!params) {
1271         RS_LOGE("RSUniRenderComposerAdapter::CreateBLayer fail, params is nullptr");
1272         return nullptr;
1273     }
1274 
1275     if (!CheckStatusBeforeCreateLayer(surfaceDrawable)) {
1276         RS_LOGE("RSUniRenderComposerAdapter::CreateBLayer fail, check surfaceDrawable fail");
1277         return nullptr;
1278     }
1279     ComposeInfo info = BuildComposeInfo(surfaceDrawable);
1280     if (IsOutOfScreenRegion(info)) {
1281         RS_LOGD("RsDebug RSUniRenderComposerAdapter::CreateBufferLayer: node(%{public}" PRIu64
1282                 ") out of screen region, no need to composite.",
1283             surfaceDrawable.GetId());
1284         return nullptr;
1285     }
1286     RS_TRACE_NAME_FMT("CreateLayer:%s XYWH[%d %d %d %d]", surfaceDrawable.GetName().c_str(),
1287         info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h);
1288     if (info.buffer) {
1289         RS_LOGD("RsDebug RSUniRenderComposerAdapter::CreateBufferLayer surfaceNode id:%{public}" PRIu64 " name:"
1290             "[%{public}s] dst [%{public}d %{public}d %{public}d %{public}d] SrcRect [%{public}d %{public}d]"
1291             " rawbuffer [%{public}d %{public}d] surfaceBuffer [%{public}d %{public}d], z:%{public}d,"
1292             " globalZOrder:%{public}d, blendType = %{public}d",
1293         surfaceDrawable.GetId(), surfaceDrawable.GetName().c_str(), info.dstRect.x, info.dstRect.y, info.dstRect.w,
1294         info.dstRect.h, info.srcRect.w, info.srcRect.h, info.buffer->GetWidth(), info.buffer->GetHeight(),
1295         info.buffer->GetSurfaceBufferWidth(), info.buffer->GetSurfaceBufferHeight(), params->GetLayerInfo().zOrder,
1296         info.zOrder, info.blendType);
1297     }
1298     LayerInfoPtr layer = HdiLayerInfo::CreateHdiLayerInfo();
1299     // planning surfaceNode prebuffer is set to hdilayerInfo, enable release prebuffer when HWC composition is ready
1300     SetComposeInfoToLayer(layer, info, surfaceDrawable.GetConsumerOnDraw());
1301     LayerRotate(layer, surfaceDrawable);
1302     LayerCrop(layer);
1303     const auto& buffer = layer->GetBuffer();
1304     if (buffer == nullptr) {
1305         RS_LOGE("buffer is nullptr");
1306         return layer;
1307     }
1308 
1309     ScalingMode scalingMode = buffer->GetSurfaceBufferScalingMode();
1310     RS_LOGI_IF(DEBUG_COMPOSER, "RSUniRenderComposerAdapter::CreateBLayer scalingMode is %{public}d", scalingMode);
1311     if (scalingMode == ScalingMode::SCALING_MODE_SCALE_CROP) {
1312         LayerScaleDown(layer, surfaceDrawable);
1313     } else if (scalingMode == ScalingMode::SCALING_MODE_SCALE_FIT) {
1314         LayerScaleFit(layer);
1315     }
1316     return layer;
1317 }
1318 
CreateBufferLayer(RSSurfaceRenderNode & node) const1319 LayerInfoPtr RSUniRenderComposerAdapter::CreateBufferLayer(RSSurfaceRenderNode& node) const
1320 {
1321     if (!CheckStatusBeforeCreateLayer(node)) {
1322         RS_LOGE("RSUniRenderComposerAdapter::CreateBLayer fail, check node fail");
1323         return nullptr;
1324     }
1325     auto surfaceHandler = node.GetRSSurfaceHandler();
1326     if (!surfaceHandler) {
1327         RS_LOGE("RSUniRenderComposerAdapter::CreateBLayer fail, surfaceHandler is nullptr");
1328         return nullptr;
1329     }
1330     ComposeInfo info = BuildComposeInfo(node);
1331     if (IsOutOfScreenRegion(info)) {
1332         RS_LOGD("RsDebug RSUniRenderComposerAdapter::CreateBufferLayer: node(%{public}" PRIu64
1333                 ") out of screen region, no need to composite.",
1334             node.GetId());
1335         return nullptr;
1336     }
1337     RS_TRACE_NAME_FMT("CreateLayer:%s XYWH[%d %d %d %d]", node.GetName().c_str(),
1338         info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h);
1339     if (info.buffer) {
1340         RS_LOGD(
1341             "RsDebug RSUniRenderComposerAdapter::CreateBufferLayer surfaceNode id:%{public}" PRIu64 " name:"
1342             "[%{public}s] dst [%{public}d %{public}d %{public}d %{public}d] SrcRect [%{public}d %{public}d]"
1343             " rawbuffer [%{public}d %{public}d] surfaceBuffer [%{public}d %{public}d], z:%{public}f,"
1344             " globalZOrder:%{public}d, blendType = %{public}d",
1345             node.GetId(), node.GetName().c_str(), info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h,
1346             info.srcRect.w, info.srcRect.h, info.buffer->GetWidth(), info.buffer->GetHeight(),
1347             info.buffer->GetSurfaceBufferWidth(), info.buffer->GetSurfaceBufferHeight(),
1348             surfaceHandler->GetGlobalZOrder(), info.zOrder, info.blendType);
1349     }
1350     LayerInfoPtr layer = HdiLayerInfo::CreateHdiLayerInfo();
1351     // planning surfaceNode prebuffer is set to hdilayerInfo, enable release prebuffer when HWC composition is ready
1352     SetComposeInfoToLayer(layer, info, surfaceHandler->GetConsumer());
1353     LayerRotate(layer, node);
1354     LayerCrop(layer);
1355     layer->SetNodeId(node.GetId());
1356     const auto& buffer = layer->GetBuffer();
1357     if (buffer == nullptr) {
1358         RS_LOGE("buffer or surface is nullptr");
1359         return layer;
1360     }
1361     ScalingMode scalingMode = buffer->GetSurfaceBufferScalingMode();
1362     RS_LOGI_IF(DEBUG_COMPOSER, "RSUniRenderComposerAdapter::CreateBLayer scalingMode is %{public}d", scalingMode);
1363     if (scalingMode == ScalingMode::SCALING_MODE_SCALE_CROP) {
1364         LayerScaleDown(layer, node);
1365     } else if (scalingMode == ScalingMode::SCALING_MODE_SCALE_FIT) {
1366         LayerScaleFit(layer);
1367     }
1368     return layer;
1369 }
1370 
CreateLayer(DrawableV2::RSDisplayRenderNodeDrawable & displayDrawable)1371 LayerInfoPtr RSUniRenderComposerAdapter::CreateLayer(DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable)
1372 {
1373     if (output_ == nullptr) {
1374         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer: output is nullptr");
1375         return nullptr;
1376     }
1377     auto surfaceHandler = displayDrawable.GetMutableRSSurfaceHandlerOnDraw();
1378     if (!surfaceHandler) {
1379         RS_LOGE("RSUniRenderComposerAdapter::CreateLY fail, surfaceHandler is nullptr");
1380         return nullptr;
1381     }
1382     RS_LOGD("RSUniRenderComposerAdapter::CreateLayer displayNode id:%{public}" PRIu64 " available buffer:%{public}d",
1383         displayDrawable.GetId(), surfaceHandler->GetAvailableBufferCount());
1384     if (!displayDrawable.IsSurfaceCreated()) {
1385         sptr<IBufferConsumerListener> listener = new RSUniRenderListener(surfaceHandler);
1386         if (!displayDrawable.CreateSurface(listener)) {
1387             RS_LOGE("RSUniRenderComposerAdapter::CreateLayer CreateSurface failed");
1388             return nullptr;
1389         }
1390     }
1391     if (!RSBaseRenderUtil::ConsumeAndUpdateBuffer(*surfaceHandler) ||
1392         !surfaceHandler->GetBuffer()) {
1393         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer RSDisplayRenderNodeDrawable consume buffer failed. %{public}d",
1394             !surfaceHandler->GetBuffer());
1395         return nullptr;
1396     }
1397     ComposeInfo info = BuildComposeInfo(displayDrawable, displayDrawable.GetDirtyRects());
1398     RS_OPTIONAL_TRACE_NAME_FMT("CreateLayer displayDrawable dirty:%s zorder:%d bufferFormat:%d",
1399         RectVectorToString(info.dirtyRects).c_str(), info.zOrder, surfaceHandler->GetBuffer()->GetFormat());
1400     if (info.buffer) {
1401         RS_LOGD_IF(DEBUG_COMPOSER,
1402             "RSUniRenderComposerAdapter::CreateLayer displayDrawable id:%{public}" PRIu64 " dst [%{public}d"
1403             " %{public}d %{public}d %{public}d] SrcRect [%{public}d %{public}d] rawbuffer [%{public}d %{public}d]"
1404             " surfaceBuffer [%{public}d %{public}d], globalZOrder:%{public}d, blendType = %{public}d, bufferFormat:%d",
1405             displayDrawable.GetId(), info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h, info.srcRect.w,
1406             info.srcRect.h, info.buffer->GetWidth(), info.buffer->GetHeight(), info.buffer->GetSurfaceBufferWidth(),
1407             info.buffer->GetSurfaceBufferHeight(), info.zOrder, info.blendType,
1408             surfaceHandler->GetBuffer()->GetFormat());
1409     }
1410     LayerInfoPtr layer = HdiLayerInfo::CreateHdiLayerInfo();
1411     layer->SetUniRenderFlag(true);
1412     SetComposeInfoToLayer(layer, info, surfaceHandler->GetConsumer());
1413     LayerRotate(layer, displayDrawable);
1414     // do not crop or scale down for displayNode's layer.
1415     return layer;
1416 }
1417 
CreateLayer(RSDisplayRenderNode & node)1418 LayerInfoPtr RSUniRenderComposerAdapter::CreateLayer(RSDisplayRenderNode& node)
1419 {
1420     if (output_ == nullptr) {
1421         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer: output is nullptr");
1422         return nullptr;
1423     }
1424     auto drawable = node.GetRenderDrawable();
1425     if (!drawable) {
1426         RS_LOGE("RSUniRenderComposerAdapter::CreateLY fail, drawable is nullptr");
1427         return nullptr;
1428     }
1429     auto displayDrawable = std::static_pointer_cast<DrawableV2::RSDisplayRenderNodeDrawable>(drawable);
1430     auto surfaceHandler = displayDrawable->GetMutableRSSurfaceHandlerOnDraw();
1431     RS_OPTIONAL_TRACE_NAME("RSUniRenderComposerAdapter::CreateLayer DisplayNode");
1432     if (!displayDrawable->IsSurfaceCreated()) {
1433         RS_LOGE("RSUniRenderComposerAdapter::CreateLY fail, displayDrawable's surfaceCreated is nullptr");
1434         return nullptr;
1435     }
1436     RS_LOGD("RSUniRenderComposerAdapter::CreateLayer displayNode id:%{public}" PRIu64 " available buffer:%{public}d",
1437         node.GetId(), surfaceHandler->GetAvailableBufferCount());
1438     if (!RSBaseRenderUtil::ConsumeAndUpdateBuffer(*surfaceHandler) ||
1439         !surfaceHandler->GetBuffer()) {
1440         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer consume buffer failed.");
1441         return nullptr;
1442     }
1443     std::vector<RectI> dirtyRegions;
1444     if (auto dirtyManager = node.GetDirtyManager()) {
1445         dirtyRegions.emplace_back(dirtyManager->GetCurrentFrameDirtyRegion());
1446     }
1447     ComposeInfo info = BuildComposeInfo(*displayDrawable, dirtyRegions);
1448     RS_OPTIONAL_TRACE_NAME_FMT("CreateLayer displayNode zorder:%d bufferFormat:%d", info.zOrder,
1449         surfaceHandler->GetBuffer()->GetFormat());
1450     if (info.buffer) {
1451         RS_LOGD("RSUniRenderComposerAdapter::CreateLayer displayNode id:%{public}" PRIu64 " dst [%{public}d %{public}d"
1452             " %{public}d %{public}d] SrcRect [%{public}d %{public}d] rawbuffer [%{public}d %{public}d] surfaceBuffer"
1453             " [%{public}d %{public}d], globalZOrder:%{public}d, blendType = %{public}d, bufferFormat:%d",
1454             node.GetId(), info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h, info.srcRect.w,
1455             info.srcRect.h, info.buffer->GetWidth(), info.buffer->GetHeight(), info.buffer->GetSurfaceBufferWidth(),
1456             info.buffer->GetSurfaceBufferHeight(), info.zOrder, info.blendType,
1457             surfaceHandler->GetBuffer()->GetFormat());
1458     }
1459     LayerInfoPtr layer = HdiLayerInfo::CreateHdiLayerInfo();
1460     layer->SetNodeId(node.GetId());
1461     layer->SetUniRenderFlag(true);
1462     SetComposeInfoToLayer(layer, info, surfaceHandler->GetConsumer());
1463     LayerRotate(layer, *displayDrawable);
1464     // do not crop or scale down for displayNode's layer.
1465     return layer;
1466 }
1467 
CreateLayer(RSSurfaceRenderNode & node) const1468 LayerInfoPtr RSUniRenderComposerAdapter::CreateLayer(RSSurfaceRenderNode& node) const
1469 {
1470     const auto& consumer = node.GetRSSurfaceHandler()->GetConsumer();
1471     if (consumer == nullptr) {
1472         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer get consumer fail");
1473         return nullptr;
1474     }
1475     return CreateBufferLayer(node);
1476 }
1477 
CreateLayer(DrawableV2::RSSurfaceRenderNodeDrawable & surfaceDrawable) const1478 LayerInfoPtr RSUniRenderComposerAdapter::CreateLayer(DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable) const
1479 {
1480     const auto consumer = surfaceDrawable.GetConsumerOnDraw();
1481     if (consumer == nullptr) {
1482         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer get consumer fail");
1483         return nullptr;
1484     }
1485     return CreateBufferLayer(surfaceDrawable);
1486 }
1487 
CreateLayer(RSRcdSurfaceRenderNode & node)1488 LayerInfoPtr RSUniRenderComposerAdapter::CreateLayer(RSRcdSurfaceRenderNode& node)
1489 {
1490     if (output_ == nullptr) {
1491         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer: output is nullptr");
1492         return nullptr;
1493     }
1494     auto surfaceHandler = node.GetRSSurfaceHandler();
1495     if (!surfaceHandler) {
1496         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer surfaceHandler is nullptr!");
1497         return nullptr;
1498     }
1499     if (surfaceHandler->GetBuffer() == nullptr) {
1500         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer buffer is nullptr!");
1501         return nullptr;
1502     }
1503 
1504     ComposeInfo info = BuildComposeInfo(node);
1505     if (info.buffer) {
1506         RS_LOGD("RSUniRenderComposerAdapter::ProcessRcdSurface rcdSurfaceNode id:%{public}" PRIu64 " DstRect"
1507             " [%{public}d %{public}d %{public}d %{public}d] SrcRect [%{public}d %{public}d %{public}d %{public}d]"
1508             " rawbuffer [%{public}d %{public}d] surfaceBuffer [%{public}d %{public}d], z-Order:%{public}d,"
1509             " blendType = %{public}d",
1510             node.GetId(), info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h,
1511             info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.h,
1512             info.buffer->GetWidth(), info.buffer->GetHeight(), info.buffer->GetSurfaceBufferWidth(),
1513             info.buffer->GetSurfaceBufferHeight(), info.zOrder, info.blendType);
1514     }
1515     LayerInfoPtr layer = HdiLayerInfo::CreateHdiLayerInfo();
1516     SetComposeInfoToLayer(layer, info, surfaceHandler->GetConsumer());
1517     auto drawable = node.GetRenderDrawable();
1518     if (drawable) {
1519         LayerRotate(layer, *drawable);
1520     }
1521     layer->SetNodeId(node.GetId());
1522     return layer;
1523 }
1524 
CreateLayer(DrawableV2::RSRcdSurfaceRenderNodeDrawable & rcdDrawable)1525 LayerInfoPtr RSUniRenderComposerAdapter::CreateLayer(DrawableV2::RSRcdSurfaceRenderNodeDrawable& rcdDrawable)
1526 {
1527     if (output_ == nullptr) {
1528         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer: output is nullptr");
1529         return nullptr;
1530     }
1531     auto surfaceHandler = rcdDrawable.GetSurfaceHandler();
1532     if (!surfaceHandler) {
1533         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer surfaceHandler is nullptr!");
1534         return nullptr;
1535     }
1536     if (surfaceHandler->GetBuffer() == nullptr) {
1537         RS_LOGE("RSUniRenderComposerAdapter::CreateLayer buffer is nullptr!");
1538         return nullptr;
1539     }
1540 
1541     ComposeInfo info = BuildComposeInfo(rcdDrawable);
1542     if (info.buffer) {
1543         RS_LOGD("RSUniRenderComposerAdapter::ProcessRcdSurface rcdSurfaceNodeDrawable id:%{public}" PRIu64 " DstRect"
1544             " [%{public}d %{public}d %{public}d %{public}d] SrcRect [%{public}d %{public}d %{public}d %{public}d]"
1545             " rawbuffer [%{public}d %{public}d] surfaceBuffer [%{public}d %{public}d], z-Order:%{public}d,"
1546             " blendType = %{public}d",
1547             rcdDrawable.GetId(), info.dstRect.x, info.dstRect.y, info.dstRect.w, info.dstRect.h,
1548             info.srcRect.x, info.srcRect.y, info.srcRect.w, info.srcRect.h,
1549             info.buffer->GetWidth(), info.buffer->GetHeight(), info.buffer->GetSurfaceBufferWidth(),
1550             info.buffer->GetSurfaceBufferHeight(), info.zOrder, info.blendType);
1551     }
1552     LayerInfoPtr layer = HdiLayerInfo::CreateHdiLayerInfo();
1553     layer->SetUniRenderFlag(true);
1554     SetComposeInfoToLayer(layer, info, surfaceHandler->GetConsumer());
1555     LayerRotate(layer, rcdDrawable);
1556     layer->SetNodeId(rcdDrawable.GetId());
1557     return layer;
1558 }
1559 
GetSurfaceNodeRotation(RSBaseRenderNode & node)1560 static int GetSurfaceNodeRotation(RSBaseRenderNode& node)
1561 {
1562     // only surface render node has the ability to rotate
1563     // the rotation of display render node is calculated as screen rotation
1564     if (node.GetType() != RSRenderNodeType::SURFACE_NODE) {
1565         return 0;
1566     }
1567     auto& surfaceNode = static_cast<RSSurfaceRenderNode&>(node);
1568     return RSUniRenderUtil::GetRotationFromMatrix(surfaceNode.GetTotalMatrix());
1569 }
1570 
GetSurfaceNodeRotation(DrawableV2::RSRenderNodeDrawableAdapter & drawable)1571 static int GetSurfaceNodeRotation(DrawableV2::RSRenderNodeDrawableAdapter& drawable)
1572 {
1573     // only surface render node has the ability to rotate
1574     // the rotation of display render node is calculated as screen rotation
1575     if (drawable.GetNodeType() != RSRenderNodeType::SURFACE_NODE) {
1576         return 0;
1577     }
1578 
1579     auto& params = drawable.GetRenderParams();
1580     return params ? RSUniRenderUtil::GetRotationFromMatrix(params->GetTotalMatrix()) : 0;
1581 }
1582 
SetLayerTransform(const LayerInfoPtr & layer,RSSurfaceRenderNode & node,const sptr<IConsumerSurface> & surface,ScreenRotation screenRotation)1583 static void SetLayerTransform(const LayerInfoPtr& layer, RSSurfaceRenderNode& node,
1584     const sptr<IConsumerSurface>& surface, ScreenRotation screenRotation)
1585 {
1586     // screenRotation: anti-clockwise, surfaceNodeRotation: anti-clockwise, surfaceTransform: anti-clockwise
1587     // layerTransform: clockwise
1588     int surfaceNodeRotation = GetSurfaceNodeRotation(node);
1589     auto transform = RSBaseRenderUtil::GetSurfaceBufferTransformType(layer->GetSurface(), layer->GetBuffer());
1590     int totalRotation = (RSBaseRenderUtil::RotateEnumToInt(screenRotation) + surfaceNodeRotation +
1591         RSBaseRenderUtil::RotateEnumToInt(RSBaseRenderUtil::GetRotateTransform(transform))) % 360;
1592     GraphicTransformType rotateEnum =
1593         RSBaseRenderUtil::RotateEnumToInt(totalRotation, RSBaseRenderUtil::GetFlipTransform(transform));
1594     layer->SetTransform(rotateEnum);
1595 }
1596 
SetLayerTransform(const LayerInfoPtr & layer,DrawableV2::RSRenderNodeDrawableAdapter & drawable,const sptr<IConsumerSurface> & surface,ScreenRotation screenRotation)1597 static void SetLayerTransform(const LayerInfoPtr& layer, DrawableV2::RSRenderNodeDrawableAdapter& drawable,
1598     const sptr<IConsumerSurface>& surface, ScreenRotation screenRotation)
1599 {
1600     // screenRotation: anti-clockwise, surfaceNodeRotation: anti-clockwise, surfaceTransform: anti-clockwise
1601     // layerTransform: clockwise
1602     int surfaceNodeRotation = GetSurfaceNodeRotation(drawable);
1603     auto transform = RSBaseRenderUtil::GetSurfaceBufferTransformType(layer->GetSurface(), layer->GetBuffer());
1604     int totalRotation = (RSBaseRenderUtil::RotateEnumToInt(screenRotation) + surfaceNodeRotation +
1605         RSBaseRenderUtil::RotateEnumToInt(RSBaseRenderUtil::GetRotateTransform(transform))) % 360;
1606     GraphicTransformType rotateEnum =
1607         RSBaseRenderUtil::RotateEnumToInt(totalRotation, RSBaseRenderUtil::GetFlipTransform(transform));
1608     layer->SetTransform(rotateEnum);
1609 }
1610 
SetLayerSize(const LayerInfoPtr & layer,const ScreenInfo & screenInfo)1611 static void SetLayerSize(const LayerInfoPtr& layer, const ScreenInfo& screenInfo)
1612 {
1613     const auto screenWidth = static_cast<int32_t>(screenInfo.width);
1614     const auto screenHeight = static_cast<int32_t>(screenInfo.height);
1615     const auto screenRotation = screenInfo.rotation;
1616     const auto rect = layer->GetLayerSize();
1617     // screenRotation: anti-clockwise, surfaceTransform: anti-clockwise, layerTransform: clockwise
1618     switch (screenRotation) {
1619         case ScreenRotation::ROTATION_90: {
1620             RS_LOGD("RsDebug ScreenRotation 90,Before Rotate layer size [%{public}d %{public}d %{public}d %{public}d]",
1621                 rect.x, rect.y, rect.w, rect.h);
1622             layer->SetLayerSize(GraphicIRect {rect.y, screenHeight - rect.x - rect.w, rect.h, rect.w});
1623             RS_LOGD("RsDebug ScreenRotation 90, After Rotate layer size [%{public}d %{public}d %{public}d %{public}d]",
1624                 layer->GetLayerSize().x, layer->GetLayerSize().y, layer->GetLayerSize().w, layer->GetLayerSize().h);
1625             break;
1626         }
1627         case ScreenRotation::ROTATION_180: {
1628             RS_LOGD("RsDebug ScreenRotation 180, Before Rotate layer size [%{public}d %{public}d %{public}d"
1629                 " %{public}d]", rect.x, rect.y, rect.w, rect.h);
1630             layer->SetLayerSize(
1631                 GraphicIRect {screenWidth - rect.x - rect.w, screenHeight - rect.y - rect.h, rect.w, rect.h});
1632             RS_LOGD("RsDebug ScreenRotation 180,After Rotate layer size [%{public}d %{public}d %{public}d %{public}d]",
1633                 layer->GetLayerSize().x, layer->GetLayerSize().y, layer->GetLayerSize().w, layer->GetLayerSize().h);
1634             break;
1635         }
1636         case ScreenRotation::ROTATION_270: {
1637             RS_LOGD("RsDebug ScreenRotation 270, Before Rotate layer size [%{public}d %{public}d %{public}d"
1638                 " %{public}d]", rect.x, rect.y, rect.w, rect.h);
1639             layer->SetLayerSize(GraphicIRect {screenWidth - rect.y - rect.h, rect.x, rect.h, rect.w});
1640             RS_LOGD("RsDebug ScreenRotation 270,After Rotate layer size [%{public}d %{public}d %{public}d %{public}d]",
1641                 layer->GetLayerSize().x, layer->GetLayerSize().y, layer->GetLayerSize().w, layer->GetLayerSize().h);
1642             break;
1643         }
1644         default:  {
1645             RS_LOGD("RsDebug ScreenRotation is default");
1646             break;
1647         }
1648     }
1649 }
1650 
1651 // private func, guarantee the layer is valid
LayerRotate(const LayerInfoPtr & layer,RSSurfaceRenderNode & node) const1652 void RSUniRenderComposerAdapter::LayerRotate(
1653     const LayerInfoPtr& layer, RSSurfaceRenderNode& node) const
1654 {
1655     auto surface = layer->GetSurface();
1656     if (surface == nullptr) {
1657         return;
1658     }
1659     SetLayerSize(layer, screenInfo_);
1660     SetLayerTransform(layer, node, surface, screenInfo_.rotation);
1661 }
1662 
1663 
LayerRotate(const LayerInfoPtr & layer,DrawableV2::RSRenderNodeDrawableAdapter & drawable) const1664 void RSUniRenderComposerAdapter::LayerRotate(
1665     const LayerInfoPtr& layer, DrawableV2::RSRenderNodeDrawableAdapter& drawable) const
1666 {
1667     auto surface = layer->GetSurface();
1668     if (surface == nullptr) {
1669         return;
1670     }
1671     SetLayerSize(layer, screenInfo_);
1672     SetLayerTransform(layer, drawable, surface, screenInfo_.rotation);
1673 }
1674 
1675 } // namespace Rosen
1676 } // namespace OHOS
1677