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 "common/rs_singleton.h"
17 #include "display_engine/rs_luminance_control.h"
18 #include "feature/hdr/rs_hdr_util.h"
19 #include "info_collection/rs_layer_compose_collection.h"
20 #include "rs_uni_render_engine.h"
21 #include "rs_uni_render_util.h"
22 #ifdef RS_ENABLE_GPU
23 #include "feature/round_corner_display/rs_round_corner_display_manager.h"
24 #endif
25 #ifdef USE_VIDEO_PROCESSING_ENGINE
26 #include "metadata_helper.h"
27 #endif
28
29 #include "drawable/rs_display_render_node_drawable.h"
30 #include "drawable/rs_surface_render_node_drawable.h"
31
32 namespace OHOS {
33 namespace Rosen {
34
35 using RSRcdManager = RSSingleton<RoundCornerDisplayManager>;
36 namespace {
37 const float REDRAW_DFX_ALPHA = 0.4f; // redraw dfx drawrect alpha
38 }
DrawSurfaceNodeWithParams(RSPaintFilterCanvas & canvas,RSSurfaceRenderNode & node,BufferDrawParam & params,PreProcessFunc preProcess,PostProcessFunc postProcess)39 void RSUniRenderEngine::DrawSurfaceNodeWithParams(RSPaintFilterCanvas& canvas, RSSurfaceRenderNode& node,
40 BufferDrawParam& params, PreProcessFunc preProcess, PostProcessFunc postProcess)
41 {
42 RS_LOGE("RSUniRenderEngine::DrawSurfaceNodeWithParams is not support");
43 }
44
DrawSurfaceNodeWithParams(RSPaintFilterCanvas & canvas,DrawableV2::RSSurfaceRenderNodeDrawable & surfaceDrawable,BufferDrawParam & params,PreProcessFunc preProcess,PostProcessFunc postProcess)45 void RSUniRenderEngine::DrawSurfaceNodeWithParams(RSPaintFilterCanvas& canvas,
46 DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable, BufferDrawParam& params, PreProcessFunc preProcess,
47 PostProcessFunc postProcess)
48 {
49 canvas.Save();
50 canvas.ConcatMatrix(params.matrix);
51 if (!params.useCPU) {
52 RegisterDeleteBufferListener(surfaceDrawable.GetConsumerOnDraw());
53 DrawImage(canvas, params);
54 } else {
55 DrawBuffer(canvas, params);
56 }
57 canvas.Restore();
58 }
59
60 #ifdef USE_VIDEO_PROCESSING_ENGINE
DrawLayers(RSPaintFilterCanvas & canvas,const std::vector<LayerInfoPtr> & layers,bool forceCPU,const ScreenInfo & screenInfo,GraphicColorGamut colorGamut)61 void RSUniRenderEngine::DrawLayers(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers, bool forceCPU,
62 const ScreenInfo& screenInfo, GraphicColorGamut colorGamut)
63 #else
64 void RSUniRenderEngine::DrawLayers(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers, bool forceCPU,
65 const ScreenInfo& screenInfo)
66 #endif
67 {
68 for (const auto& layer : layers) {
69 if (layer == nullptr) {
70 continue;
71 }
72 if (layer->GetCompositionType() == GraphicCompositionType::GRAPHIC_COMPOSITION_DEVICE ||
73 layer->GetCompositionType() == GraphicCompositionType::GRAPHIC_COMPOSITION_DEVICE_CLEAR ||
74 layer->GetCompositionType() == GraphicCompositionType::GRAPHIC_COMPOSITION_SOLID_COLOR) {
75 continue;
76 }
77 GraphicLayerColor layerBlackColor = {
78 .r = 0,
79 .g = 0,
80 .b = 0,
81 .a = 0
82 };
83 auto layerSurface = layer->GetSurface();
84 if (layerSurface == nullptr) {
85 const auto& layerColor = layer->GetLayerColor();
86 if (layerColor.a != layerBlackColor.a || layerColor.r != layerBlackColor.r ||
87 layerColor.g != layerBlackColor.g || layerColor.b != layerBlackColor.b) {
88 Drawing::AutoCanvasRestore acr(canvas, true);
89 const auto& dstRect = layer->GetLayerSize();
90 auto color = Drawing::Color::ColorQuadSetARGB(layerColor.a, layerColor.r, layerColor.g, layerColor.b);
91 Drawing::Rect clipRect = Drawing::Rect(static_cast<float>(dstRect.x), static_cast<float>(dstRect.y),
92 static_cast<float>(dstRect.w) + static_cast<float>(dstRect.x),
93 static_cast<float>(dstRect.h) + static_cast<float>(dstRect.y));
94 canvas.ClipRect(clipRect, Drawing::ClipOp::INTERSECT, false);
95 canvas.DrawColor(color);
96 }
97 continue;
98 } else if (RSRcdManager::GetInstance().CheckLayerIsRCD(layerSurface->GetName())) {
99 continue;
100 }
101 Drawing::AutoCanvasRestore acr(canvas, true);
102 DrawLayerPreProcess(canvas, layer);
103 // prepare BufferDrawParam
104 auto params = RSUniRenderUtil::CreateLayerBufferDrawParam(layer, forceCPU);
105 params.matrix.PostScale(screenInfo.GetRogWidthRatio(), screenInfo.GetRogHeightRatio());
106 params.screenId = screenInfo.id;
107 #ifdef USE_VIDEO_PROCESSING_ENGINE
108 params.targetColorGamut = colorGamut;
109 auto screenManager = CreateOrGetScreenManager();
110 if (screenManager != nullptr) {
111 params.sdrNits = layer->GetSdrNit();
112 params.tmoNits = layer->GetDisplayNit();
113 params.displayNits = params.tmoNits / std::pow(layer->GetBrightnessRatio(), 2.2f); // gamma 2.2
114 // color temperature
115 params.layerLinearMatrix = layer->GetLayerLinearMatrix();
116 }
117 if (RSHdrUtil::CheckIsHdrSurfaceBuffer(layer->GetBuffer()) == HdrStatus::NO_HDR) {
118 params.brightnessRatio = layer->GetBrightnessRatio();
119 if (RSHdrUtil::CheckIsSurfaceBufferWithMetadata(layer->GetBuffer())) {
120 params.hasMetadata = true;
121 }
122 } else {
123 params.isHdrRedraw = true;
124 }
125 #endif
126 DrawHdiLayerWithParams(canvas, layer, params);
127 // Dfx for redraw region
128 auto dstRect = layer->GetLayerSize();
129 if (RSSystemProperties::GetHwcRegionDfxEnabled()) {
130 RectI dst(dstRect.x, dstRect.y, dstRect.w, dstRect.h);
131 RSUniRenderUtil::DrawRectForDfx(canvas, dst, Drawing::Color::COLOR_YELLOW, REDRAW_DFX_ALPHA,
132 layerSurface->GetName());
133 }
134 }
135
136 LayerComposeCollection::GetInstance().UpdateRedrawFrameNumberForDFX();
137 }
138
DrawLayerPreProcess(RSPaintFilterCanvas & canvas,const LayerInfoPtr & layer)139 void RSUniRenderEngine::DrawLayerPreProcess(RSPaintFilterCanvas& canvas, const LayerInfoPtr& layer)
140 {
141 const auto& dstRect = layer->GetLayerSize();
142 const auto& drmCornerRadiusInfo = layer->GetCornerRadiusInfoForDRM();
143 const auto& layerBackgroundColor = layer->GetBackgroundColor();
144 Color backgroundColor = {
145 layerBackgroundColor.r,
146 layerBackgroundColor.g,
147 layerBackgroundColor.b,
148 layerBackgroundColor.a
149 };
150 // clip round rect when drm has radius info
151 if (!drmCornerRadiusInfo.empty()) {
152 auto rect = RectF();
153 rect = {drmCornerRadiusInfo[0], drmCornerRadiusInfo[1], // 0 and 1 represent rect left and top
154 drmCornerRadiusInfo[2], drmCornerRadiusInfo[3]}; // 2 and 3 represent rect width and height
155 Vector4f radiusVector {drmCornerRadiusInfo[4], drmCornerRadiusInfo[5], // 4 and 5 represent corner radius
156 drmCornerRadiusInfo[6], drmCornerRadiusInfo[7]}; // 6 and 7 represent corner radius
157 RRect rrect = RRect(rect, radiusVector);
158 canvas.ClipRoundRect(RSPropertiesPainter::RRect2DrawingRRect(rrect), Drawing::ClipOp::INTERSECT, true);
159 if (backgroundColor != RgbPalette::Transparent()) {
160 canvas.DrawColor(backgroundColor.AsArgbInt());
161 }
162 return;
163 }
164 // draw background color for DRM buffer and then draw layer size image when surface has scaling mode
165 if (layer->GetBuffer() && (layer->GetBuffer()->GetUsage() & BUFFER_USAGE_PROTECTED) &&
166 backgroundColor != RgbPalette::Transparent()) {
167 const auto& boundsRect = layer->GetBoundSize();
168 const auto& layerMatrix = layer->GetMatrix();
169 auto skMatrix = Drawing::Matrix();
170 skMatrix.SetMatrix(layerMatrix.scaleX, layerMatrix.skewX, layerMatrix.transX, layerMatrix.skewY,
171 layerMatrix.scaleY, layerMatrix.transY, layerMatrix.pers0, layerMatrix.pers1, layerMatrix.pers2);
172 Drawing::AutoCanvasRestore acr(canvas, true);
173 canvas.ConcatMatrix(skMatrix);
174 Drawing::Rect drawRect = Drawing::Rect(0.f, 0.f,
175 static_cast<float>(boundsRect.w), static_cast<float>(boundsRect.h));
176 Drawing::Brush rectBrush;
177 rectBrush.SetColor(backgroundColor.AsArgbInt());
178 canvas.AttachBrush(rectBrush);
179 canvas.DrawRect(drawRect);
180 canvas.DetachBrush();
181 }
182 Drawing::Rect clipRect = Drawing::Rect(static_cast<float>(dstRect.x), static_cast<float>(dstRect.y),
183 static_cast<float>(dstRect.w) + static_cast<float>(dstRect.x),
184 static_cast<float>(dstRect.h) + static_cast<float>(dstRect.y));
185 canvas.ClipRect(clipRect, Drawing::ClipOp::INTERSECT, false);
186 }
187
DrawHdiLayerWithParams(RSPaintFilterCanvas & canvas,const LayerInfoPtr & layer,BufferDrawParam & params)188 void RSUniRenderEngine::DrawHdiLayerWithParams(RSPaintFilterCanvas& canvas, const LayerInfoPtr& layer,
189 BufferDrawParam& params)
190 {
191 canvas.ConcatMatrix(params.matrix);
192 if (!params.useCPU) {
193 RegisterDeleteBufferListener(layer->GetSurface(), true);
194 DrawImage(canvas, params);
195 } else {
196 DrawBuffer(canvas, params);
197 }
198 }
199 } // namespace Rosen
200 } // namespace OHOS
201