1 /*
2 * Copyright (c) 2025 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 <memory>
17
18 #include "common/rs_color.h"
19 #include "common/rs_common_def.h"
20 #include "common/rs_obj_abs_geometry.h"
21 #include "common/rs_optional_trace.h"
22 #include "draw/brush.h"
23 #include "drawable/rs_surface_render_node_drawable.h"
24 #include "feature/hetero_hdr/rs_hdr_manager.h"
25 #include "feature/uifirst/rs_sub_thread_manager.h"
26 #include "feature/uifirst/rs_uifirst_manager.h"
27 #include "hetero_hdr/rs_hdr_pattern_manager.h"
28 #include "impl_interface/region_impl.h"
29 #include "memory/rs_tag_tracker.h"
30 #include "metadata_helper.h"
31 #include "params/rs_surface_render_params.h"
32 #include "pipeline/main_thread/rs_main_thread.h"
33 #include "pipeline/render_thread/rs_uni_render_thread.h"
34 #include "pipeline/render_thread/rs_uni_render_util.h"
35 #include "pipeline/rs_paint_filter_canvas.h"
36 #include "pipeline/rs_surface_render_node.h"
37 #include "pipeline/sk_resource_manager.h"
38 #include "platform/common/rs_log.h"
39 #include "rs_frame_report.h"
40 #include "rs_profiler.h"
41 #include "rs_trace.h"
42 #include "utils/rect.h"
43 #include "utils/region.h"
44
45 #ifdef RS_ENABLE_VK
46 #include "include/gpu/GrBackendSurface.h"
47 #include "platform/ohos/backend/native_buffer_utils.h"
48 #include "platform/ohos/backend/rs_vulkan_context.h"
49 #endif
50
51 constexpr float GAMMA2_2 = 2.2f;
52 const int RS_ROTATION_90 = -90;
53 const int RS_ROTATION_180 = -180;
54 const int RS_ROTATION_270 = -270;
55
56 namespace OHOS::Rosen::DrawableV2 {
57
GetCurHeterogComputingHdr() const58 bool RSSurfaceRenderNodeDrawable::GetCurHeterogComputingHdr() const
59 {
60 return g_HDRHeterRenderContext.curHeterogComputingHdr_;
61 }
62
SetCurHeterogComputingHdr(bool curCondition)63 void RSSurfaceRenderNodeDrawable::SetCurHeterogComputingHdr(bool curCondition)
64 {
65 g_HDRHeterRenderContext.curHeterogComputingHdr_ = curCondition;
66 }
67
DrawHDRCacheWithDmaFFRT(RSPaintFilterCanvas & canvas,RSSurfaceRenderParams & surfaceParams)68 bool RSSurfaceRenderNodeDrawable::DrawHDRCacheWithDmaFFRT(
69 RSPaintFilterCanvas &canvas, RSSurfaceRenderParams &surfaceParams)
70 {
71 g_HDRHeterRenderContext.curHeterogComputingHdr_ = false;
72 if (surfaceParams.GetHardwareEnabled() || !(RSHdrManager::Instance().GetCurFrameHeterogHandleCanBeUsed())) {
73 return false;
74 }
75 if (RSHdrManager::Instance().IsHeterogComputingHdrOn() && GetVideoHdrStatus() == HdrStatus::HDR_VIDEO) {
76 std::shared_ptr<RSSurfaceHandler> hdrSurfaceHandler =
77 g_HDRHeterRenderContext.rsHdrBufferLayer_->GetMutableRSSurfaceHandler();
78
79 if (hdrSurfaceHandler == nullptr) {
80 RS_LOGE("hdrSurfaceHandler_ is nullptr!!");
81 return false;
82 }
83 if (!RSBaseRenderUtil::ConsumeAndUpdateBuffer(*hdrSurfaceHandler, CONSUME_DIRECTLY,
84 false, 0, true) || !hdrSurfaceHandler->GetBuffer()) {
85 RS_LOGE("DrawHDRCacheWithDmaFFRT ConsumeAndUpdateBuffer or GetBuffer return false");
86 return false;
87 }
88 RSHDRPatternManager::Instance().SetThreadId();
89
90 bool isSelfDrawingSurface = (surfaceParams.GetSurfaceNodeType() == RSSurfaceNodeType::SELF_DRAWING_NODE) &&
91 !surfaceParams.IsSpherizeValid() && !surfaceParams.IsAttractionValid();
92 if (isSelfDrawingSurface) {
93 SetSkip(surfaceParams.GetBuffer() != nullptr ? SkipType::SKIP_BACKGROUND_COLOR : SkipType::NONE);
94 canvas.Save();
95 DrawHDRBufferWithGPU(canvas);
96 canvas.Restore();
97 } else {
98 DrawHDRBufferWithGPU(canvas);
99 }
100
101 g_HDRHeterRenderContext.curHeterogComputingHdr_ = true;
102 return true;
103 }
104 return false;
105 }
106
SetHpaeDstRect(RectI boundSize)107 void RSSurfaceRenderNodeDrawable::SetHpaeDstRect(RectI boundSize)
108 {
109 g_HDRHeterRenderContext.aaeDstRect_ = {boundSize.GetLeft(), boundSize.GetTop(),
110 boundSize.GetRight(), boundSize.GetBottom()};
111 }
112
SetNodeDrawableNodeDstRect(bool isFixedDstBuffer,RectI boundSize)113 void RSSurfaceRenderNodeDrawable::SetNodeDrawableNodeDstRect(bool isFixedDstBuffer, RectI boundSize)
114 {
115 g_HDRHeterRenderContext.isFixedDstBuffer_ = isFixedDstBuffer;
116 g_HDRHeterRenderContext.boundSize_ = boundSize;
117 }
118
InitBufferDrawParam(RSSurfaceRenderParams * surfaceParams)119 BufferDrawParam RSSurfaceRenderNodeDrawable::InitBufferDrawParam(RSSurfaceRenderParams* surfaceParams)
120 {
121 BufferDrawParam param;
122 param.threadIndex = gettid();
123
124 if (surfaceParams == nullptr) {
125 RS_LOGE("surfaceParams is nullptr!");
126 return param;
127 }
128 auto dstRect = surfaceParams->GetLayerInfo().dstRect;
129 GraphicTransformType bufferTransform = surfaceParams->GetBuffer()->GetSurfaceBufferTransform();
130
131 if (g_HDRHeterRenderContext.isFixedDstBuffer_) {
132 param.srcRect = {0, 0, g_HDRHeterRenderContext.aaeDstRect_.right, g_HDRHeterRenderContext.aaeDstRect_.bottom};
133 param.dstRect = {0, 0, surfaceParams->GetCacheSize().x_, surfaceParams->GetCacheSize().y_};
134 if (bufferTransform == GraphicTransformType::GRAPHIC_ROTATE_90 ||
135 bufferTransform == GraphicTransformType::GRAPHIC_ROTATE_270) {
136 param.srcRect = {0, 0, g_HDRHeterRenderContext.aaeDstRect_.bottom,
137 g_HDRHeterRenderContext.aaeDstRect_.right};
138 }
139 } else {
140 param.srcRect = {0, 0, g_HDRHeterRenderContext.aaeDstRect_.right, g_HDRHeterRenderContext.aaeDstRect_.bottom};
141 param.dstRect = {dstRect.x, dstRect.y, dstRect.x + dstRect.w, dstRect.y + dstRect.h};
142 }
143
144 if ((param.srcRect.GetWidth() == 0.0f) || (param.srcRect.GetHeight() == 0.0f)) {
145 RS_LOGE("Skipping SetScaleTranslate due to zero division");
146 } else {
147 auto sx = param.dstRect.GetWidth() / param.srcRect.GetWidth();
148 auto sy = param.dstRect.GetHeight() / param.srcRect.GetHeight();
149 auto tx = param.dstRect.GetLeft() - param.srcRect.GetLeft() * sx;
150 auto ty = param.dstRect.GetTop() - param.srcRect.GetTop() * sy;
151 param.matrix.SetScaleTranslate(sx, sy, tx, ty);
152 }
153
154 switch (bufferTransform) {
155 case GraphicTransformType::GRAPHIC_ROTATE_90: {
156 param.matrix.PreTranslate(0, param.srcRect.GetHeight());
157 param.matrix.PreRotate(RS_ROTATION_90); // rotate 90 degrees anti-clockwise at last.
158 break;
159 }
160 case GraphicTransformType::GRAPHIC_ROTATE_180: {
161 param.matrix.PreTranslate(param.srcRect.GetWidth(), param.srcRect.GetHeight());
162 param.matrix.PreRotate(RS_ROTATION_180); // rotate 180 degrees anti-clockwise at last.
163 break;
164 }
165 case GraphicTransformType::GRAPHIC_ROTATE_270: {
166 param.matrix.PreTranslate(param.srcRect.GetWidth(), 0);
167 param.matrix.PreRotate(RS_ROTATION_270); // rotate 270 degrees anti-clockwise at last.
168 break;
169 }
170 default: break;
171 }
172
173 return param;
174 }
175
DrawHDRBufferWithGPU(RSPaintFilterCanvas & canvas)176 void RSSurfaceRenderNodeDrawable::DrawHDRBufferWithGPU(RSPaintFilterCanvas &canvas)
177 {
178 if (g_HDRHeterRenderContext.rsHdrBufferLayer_ == nullptr) {
179 RS_LOGE("g_HDRHeterRenderContext.rsHdrBufferLayer_ is nullptr!");
180 return;
181 }
182 std::shared_ptr<RSSurfaceHandler> hdrSurfaceHandler =
183 g_HDRHeterRenderContext.rsHdrBufferLayer_->GetMutableRSSurfaceHandler();
184
185 if (hdrSurfaceHandler == nullptr) {
186 RS_LOGE("hdrSurfaceHandler is nullptr!");
187 return;
188 }
189 auto buffer = hdrSurfaceHandler->GetBuffer();
190
191 auto surfaceParams = static_cast<RSSurfaceRenderParams *>(GetRenderParams().get());
192 BufferDrawParam param = InitBufferDrawParam(surfaceParams);
193 Drawing::Matrix matrix = surfaceParams->GetLayerInfo().matrix;
194
195 auto bgColor = surfaceParams->GetBackgroundColor();
196 if (surfaceParams->GetHardwareEnabled() && surfaceParams->GetIsHwcEnabledBySolidLayer()) {
197 bgColor = surfaceParams->GetSolidLayerColor();
198 RS_LOGD("solidLayer enabled, %{public}s, brush set color: %{public}08x", __func__, bgColor.AsArgbInt());
199 }
200 Drawing::Brush brush;
201 brush.SetColor(Drawing::Color(bgColor.AsArgbInt()));
202
203 canvas.AttachBrush(brush);
204 canvas.DrawRect(surfaceParams->GetBounds());
205 canvas.DetachBrush();
206
207 canvas.Save();
208 if (!g_HDRHeterRenderContext.isFixedDstBuffer_) {
209 Drawing::Matrix matrixInv;
210 matrix.Invert(matrixInv);
211 canvas.ConcatMatrix(matrixInv);
212 }
213
214 canvas.ConcatMatrix(param.matrix);
215 matrix = canvas.GetTotalMatrix();
216
217 param.dstRect = {0, 0, g_HDRHeterRenderContext.aaeDstRect_.right, g_HDRHeterRenderContext.aaeDstRect_.bottom};
218 param.buffer = buffer;
219 #ifdef USE_VIDEO_PROCESSING_ENGINE
220 param.sdrNits = surfaceParams->GetSdrNit();
221 param.tmoNits = surfaceParams->GetDisplayNit();
222 param.displayNits = param.tmoNits / std::pow(surfaceParams->GetBrightnessRatio(), GAMMA2_2);
223 #endif
224
225 param.acquireFence = hdrSurfaceHandler->GetAcquireFence();
226 param.isHeterog = true;
227
228 auto renderEngine = RSUniRenderThread::Instance().GetRenderEngine();
229 renderEngine->RegisterDeleteBufferListener(hdrSurfaceHandler->GetConsumer());
230 renderEngine->DrawHDRCacheWithParams(canvas, param);
231
232 RSBaseRenderUtil::ReleaseBuffer(*hdrSurfaceHandler);
233 canvas.Restore();
234 }
235
GetRsHdrBUfferLayer()236 std::shared_ptr<RSHDRBUfferLayer> RSSurfaceRenderNodeDrawable::GetRsHdrBUfferLayer()
237 {
238 return g_HDRHeterRenderContext.rsHdrBufferLayer_;
239 }
240
SetVideoHdrStatus(HdrStatus hasHdrVideoSurface)241 void RSSurfaceRenderNodeDrawable::SetVideoHdrStatus(HdrStatus hasHdrVideoSurface)
242 {
243 g_HDRHeterRenderContext.hdrSatus_ = hasHdrVideoSurface;
244 }
245
GetVideoHdrStatus()246 HdrStatus RSSurfaceRenderNodeDrawable::GetVideoHdrStatus()
247 {
248 return g_HDRHeterRenderContext.hdrSatus_;
249 }
250 } // namespace OHOS::Rosen::DrawableV2