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