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 #ifdef RS_ENABLE_GPU
82 RSBaseRenderUtil::DealWithSurfaceRotationAndGravity(surface->GetTransform(), property.GetFrameGravity(),
83 localBounds, params);
84 #endif
85 RSBaseRenderUtil::FlipMatrix(surface->GetTransform(), params);
86 return params;
87 }
88
CalculateSurfaceNodeClipRects(const RSSurfaceRenderNode & node,const RectF & absBounds,const RectF & localBounds,bool inLocalCoordinate,BufferDrawParam & params)89 void RSDividedRenderUtil::CalculateSurfaceNodeClipRects(
90 const RSSurfaceRenderNode& node,
91 const RectF& absBounds,
92 const RectF& localBounds,
93 bool inLocalCoordinate,
94 BufferDrawParam& params)
95 {
96 const RSProperties& property = node.GetRenderProperties();
97 params.cornerRadius = property.GetCornerRadius();
98 params.isNeedClip = property.GetClipToFrame();
99 if (inLocalCoordinate) {
100 // in canvas's local coordinate system.
101 params.clipRect = Drawing::Rect(0, 0, localBounds.GetWidth(), localBounds.GetHeight());
102 params.clipRRect = RRect(localBounds, params.cornerRadius);
103 } else {
104 // in logical screen's coordinate system.
105 auto clipRect = node.GetDstRect();
106 clipRect = clipRect.Offset(node.GetOffSetX() * -1, node.GetOffSetY() * -1);
107 params.clipRect = Drawing::Rect(clipRect.GetLeft(), clipRect.GetTop(),
108 clipRect.GetWidth() + clipRect.GetLeft(), clipRect.GetHeight() + clipRect.GetTop());
109 params.clipRRect = RRect(absBounds, params.cornerRadius);
110 }
111 }
112 } // namespace Rosen
113 } // namespace OHOS
114