1 /*
2  * Copyright (c) 2021-2023 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_drawing_filter.h"
23 #include "render/rs_skia_filter.h"
24 
25 namespace OHOS {
26 namespace Rosen {
CreateBufferDrawParam(const RSSurfaceRenderNode & node,bool inLocalCoordinate,bool isClipHole,bool forceCPU,bool setColorFilter)27 BufferDrawParam RSDividedRenderUtil::CreateBufferDrawParam(
28     const RSSurfaceRenderNode& node, bool inLocalCoordinate, bool isClipHole, bool forceCPU, bool setColorFilter)
29 {
30     BufferDrawParam params;
31 #ifdef RS_ENABLE_EGLIMAGE
32     params.useCPU = forceCPU;
33 #else // RS_ENABLE_EGLIMAGE
34     (void)(forceCPU); // unused param.
35     params.useCPU = true;
36 #endif // RS_ENABLE_EGLIMAGE
37     params.paint.SetAlphaF(node.GetGlobalAlpha());
38     params.paint.SetAntiAlias(true);
39     Drawing::Filter filter;
40     filter.SetFilterQuality(Drawing::Filter::FilterQuality::LOW);
41     params.paint.SetFilter(filter);
42     params.setColorFilter = setColorFilter;
43     params.threadIndex = static_cast<uint32_t>(gettid());
44 
45     const RSProperties& property = node.GetRenderProperties();
46     auto backgroundColor = property.GetBackgroundColor();
47     auto backgroundAlpha = backgroundColor.GetAlpha();
48     int16_t finalBackgroundAlpha = static_cast<int16_t>(backgroundAlpha * node.GetGlobalAlpha());
49     backgroundColor.SetAlpha(finalBackgroundAlpha);
50     params.backgroundColor = static_cast<Drawing::ColorQuad>(backgroundColor.AsArgbInt());
51 
52     const RectF absBounds = {
53         node.GetTotalMatrix().Get(Drawing::Matrix::TRANS_X), node.GetTotalMatrix().Get(Drawing::Matrix::TRANS_Y),
54         property.GetBoundsWidth(), property.GetBoundsHeight()};
55     RectF localBounds = {0.0f, 0.0f, absBounds.GetWidth(), absBounds.GetHeight()};
56 
57     // calculate clipRect and clipRRect(if has cornerRadius) for canvas.
58     CalculateSurfaceNodeClipRects(node, absBounds, localBounds, inLocalCoordinate, params);
59 
60     // inLocalCoordinate: reset the translate to (0, 0).
61     // else: use node's total matrix.
62     if (inLocalCoordinate) {
63         params.matrix = Drawing::Matrix();
64     } else {
65         params.matrix = node.GetTotalMatrix();
66     }
67 
68     // we can use only the bound's size (ignore its offset) now,
69     // (the canvas was moved to the node's left-top point correctly).
70     params.dstRect = Drawing::Rect(0, 0, localBounds.GetWidth(), localBounds.GetHeight());
71     auto surfaceHandler = node.GetRSSurfaceHandler();
72     const sptr<IConsumerSurface>& surface = surfaceHandler->GetConsumer();
73     const sptr<SurfaceBuffer>& buffer = surfaceHandler->GetBuffer();
74     if (isClipHole || surface == nullptr || buffer == nullptr) {
75         return params;
76     }
77 
78     params.buffer = buffer;
79     params.acquireFence = surfaceHandler->GetAcquireFence();
80     params.srcRect = Drawing::Rect(0, 0, buffer->GetSurfaceBufferWidth(), buffer->GetSurfaceBufferHeight());
81     RSBaseRenderUtil::DealWithSurfaceRotationAndGravity(surface->GetTransform(), property.GetFrameGravity(),
82         localBounds, params);
83     RSBaseRenderUtil::FlipMatrix(surface->GetTransform(), params);
84     return params;
85 }
86 
CalculateSurfaceNodeClipRects(const RSSurfaceRenderNode & node,const RectF & absBounds,const RectF & localBounds,bool inLocalCoordinate,BufferDrawParam & params)87 void RSDividedRenderUtil::CalculateSurfaceNodeClipRects(
88     const RSSurfaceRenderNode& node,
89     const RectF& absBounds,
90     const RectF& localBounds,
91     bool inLocalCoordinate,
92     BufferDrawParam& params)
93 {
94     const RSProperties& property = node.GetRenderProperties();
95     params.cornerRadius = property.GetCornerRadius();
96     params.isNeedClip = property.GetClipToFrame();
97     if (inLocalCoordinate) {
98         // in canvas's local coordinate system.
99         params.clipRect = Drawing::Rect(0, 0, localBounds.GetWidth(), localBounds.GetHeight());
100         params.clipRRect = RRect(localBounds, params.cornerRadius);
101     } else {
102         // in logical screen's coordinate system.
103         const auto& clipRect = node.GetDstRect();
104         params.clipRect = Drawing::Rect(clipRect.GetLeft(), clipRect.GetTop(),
105             clipRect.GetWidth() + clipRect.GetLeft(), clipRect.GetHeight() + clipRect.GetTop());
106         params.clipRRect = RRect(absBounds, params.cornerRadius);
107     }
108 }
109 } // namespace Rosen
110 } // namespace OHOS
111