1 /*
2 * Copyright (c) 2021-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 #include "rs_divided_render_util.h"
16
17 #include <parameters.h>
18
19 #include "common/rs_obj_abs_geometry.h"
20 #include "platform/common/rs_log.h"
21 #include "property/rs_properties_painter.h"
22 #include "render/rs_skia_filter.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 bool RSDividedRenderUtil::enableClient = false;
27
SetNeedClient(bool flag)28 void RSDividedRenderUtil::SetNeedClient(bool flag)
29 {
30 enableClient = flag;
31 }
32
IsNeedClient(RSSurfaceRenderNode & node,const ComposeInfo & info)33 bool RSDividedRenderUtil::IsNeedClient(RSSurfaceRenderNode& node, const ComposeInfo& info)
34 {
35 if (IsForceClient()) {
36 RS_LOGD("RsDebug RSDividedRenderUtil::IsNeedClient: client composition is force enabled.");
37 return true;
38 }
39
40 if (enableClient) {
41 RS_LOGD("RsDebug RSDividedRenderUtil::IsNeedClient enable composition client");
42 return true;
43 }
44
45 const auto& property = node.GetRenderProperties();
46 auto backgroundColor = static_cast<SkColor>(property.GetBackgroundColor().AsArgbInt());
47 // If node's gravity is not RESIZE and backgroundColor is not transparent,
48 // we check the src and dst size to decide whether to use client composition or not.
49 if (property.GetFrameGravity() != Gravity::RESIZE && SkColorGetA(backgroundColor) != SK_AlphaTRANSPARENT &&
50 (info.srcRect.w != info.dstRect.w || info.srcRect.h != info.dstRect.h)) {
51 return true;
52 }
53
54 if (property.GetBackgroundFilter() || property.GetFilter()) {
55 RS_LOGD("RsDebug RSDividedRenderUtil::IsNeedClient enable composition client need filter");
56 return true;
57 }
58
59 if (!property.GetCornerRadius().IsZero()) {
60 RS_LOGD("RsDebug RSDividedRenderUtil::IsNeedClient enable composition client need round corner");
61 return true;
62 }
63 if (property.IsShadowValid()) {
64 RS_LOGD("RsDebug RSDividedRenderUtil::IsNeedClient enable composition client need shadow");
65 return true;
66 }
67 if (property.GetRotation() != 0 || property.GetRotationX() != 0 || property.GetRotationY() != 0 ||
68 property.GetQuaternion() != Quaternion()) {
69 RS_LOGD("RsDebug RSDividedRenderUtil::IsNeedClient enable composition client need rotation");
70 return true;
71 }
72 return false;
73 }
74
IsForceClient()75 bool RSDividedRenderUtil::IsForceClient()
76 {
77 return (std::atoi((system::GetParameter("rosen.client_composition.enabled", "0")).c_str()) != 0);
78 }
79
CreateBufferDrawParam(const RSSurfaceRenderNode & node,bool inLocalCoordinate,bool isClipHole,bool forceCPU,bool setColorFilter)80 BufferDrawParam RSDividedRenderUtil::CreateBufferDrawParam(
81 const RSSurfaceRenderNode& node, bool inLocalCoordinate, bool isClipHole, bool forceCPU, bool setColorFilter)
82 {
83 BufferDrawParam params;
84 #ifdef RS_ENABLE_EGLIMAGE
85 params.useCPU = forceCPU;
86 #else // RS_ENABLE_EGLIMAGE
87 (void)(forceCPU); // unused param.
88 params.useCPU = true;
89 #endif // RS_ENABLE_EGLIMAGE
90 params.paint.setAlphaf(node.GetGlobalAlpha());
91 params.paint.setAntiAlias(true);
92 params.paint.setFilterQuality(SkFilterQuality::kLow_SkFilterQuality);
93 params.setColorFilter = setColorFilter;
94
95 const RSProperties& property = node.GetRenderProperties();
96 auto backgroundColor = property.GetBackgroundColor();
97 auto backgroundAlpha = backgroundColor.GetAlpha();
98 int16_t finalBackgroundAlpha = static_cast<int16_t>(backgroundAlpha * node.GetGlobalAlpha());
99 backgroundColor.SetAlpha(finalBackgroundAlpha);
100 params.backgroundColor = static_cast<SkColor>(backgroundColor.AsArgbInt());
101
102 const RectF absBounds = {
103 node.GetTotalMatrix().getTranslateX(), node.GetTotalMatrix().getTranslateY(),
104 property.GetBoundsWidth(), property.GetBoundsHeight()};
105 RectF localBounds = {0.0f, 0.0f, absBounds.GetWidth(), absBounds.GetHeight()};
106
107 // calculate clipRect and clipRRect(if has cornerRadius) for canvas.
108 CalculateSurfaceNodeClipRects(node, absBounds, localBounds, inLocalCoordinate, params);
109
110 // inLocalCoordinate: reset the translate to (0, 0).
111 // else: use node's total matrix.
112 if (inLocalCoordinate) {
113 params.matrix = SkMatrix::I();
114 } else {
115 params.matrix = node.GetTotalMatrix();
116 }
117
118 // we can use only the bound's size (ignore its offset) now,
119 // (the canvas was moved to the node's left-top point correctly).
120 params.dstRect = SkRect::MakeWH(localBounds.GetWidth(), localBounds.GetHeight());
121
122 const sptr<Surface>& surface = node.GetConsumer();
123 const sptr<SurfaceBuffer>& buffer = node.GetBuffer();
124 if (isClipHole || surface == nullptr || buffer == nullptr) {
125 return params;
126 }
127
128 params.buffer = buffer;
129 params.acquireFence = node.GetAcquireFence();
130 params.srcRect = SkRect::MakeWH(buffer->GetSurfaceBufferWidth(), buffer->GetSurfaceBufferHeight());
131
132 RSBaseRenderUtil::FlipMatrix(node, params);
133 RSBaseRenderUtil::DealWithSurfaceRotationAndGravity(node, localBounds, params);
134 return params;
135 }
136
CalculateSurfaceNodeClipRects(const RSSurfaceRenderNode & node,const RectF & absBounds,const RectF & localBounds,bool inLocalCoordinate,BufferDrawParam & params)137 void RSDividedRenderUtil::CalculateSurfaceNodeClipRects(
138 const RSSurfaceRenderNode& node,
139 const RectF& absBounds,
140 const RectF& localBounds,
141 bool inLocalCoordinate,
142 BufferDrawParam& params)
143 {
144 const RSProperties& property = node.GetRenderProperties();
145 params.cornerRadius = property.GetCornerRadius();
146 params.isNeedClip = property.GetClipToFrame();
147 if (inLocalCoordinate) {
148 // in canvas's local coordinate system.
149 params.clipRect = SkRect::MakeWH(localBounds.GetWidth(), localBounds.GetHeight());
150 params.clipRRect = RRect(localBounds, params.cornerRadius);
151 } else {
152 // in logical screen's coordinate system.
153 const auto& clipRect = node.GetDstRect();
154 params.clipRect = SkRect::MakeXYWH(
155 clipRect.GetLeft(), clipRect.GetTop(), clipRect.GetWidth(), clipRect.GetHeight());
156 params.clipRRect = RRect(absBounds, params.cornerRadius);
157 }
158 }
159 } // namespace Rosen
160 } // namespace OHOS
161