1 /*
2  * Copyright (c) 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 
16 #include "property/rs_property_drawable_frame_geometry.h"
17 
18 #include "pipeline/rs_paint_filter_canvas.h"
19 #include "pipeline/rs_render_node.h"
20 #include "platform/common/rs_log.h"
21 #include "property/rs_properties.h"
22 #include "property/rs_properties_painter.h"
23 
24 #include "src/image/SkImage_Base.h"
25 
26 namespace OHOS::Rosen {
Draw(const RSRenderContent & content,RSPaintFilterCanvas & canvas) const27 void RSFrameGeometryDrawable::Draw(const RSRenderContent& content, RSPaintFilterCanvas& canvas) const
28 {
29     canvas.Translate(content.GetRenderProperties().GetFrameOffsetX(), content.GetRenderProperties().GetFrameOffsetY());
30 }
Generate(const RSRenderContent & content)31 RSPropertyDrawable::DrawablePtr RSFrameGeometryDrawable::Generate(const RSRenderContent& content)
32 {
33     return std::make_unique<RSFrameGeometryDrawable>();
34 }
35 
Draw(const RSRenderContent & content,RSPaintFilterCanvas & canvas) const36 void RSColorFilterDrawable::Draw(const RSRenderContent& content, RSPaintFilterCanvas& canvas) const
37 {
38     auto drSurface = canvas.GetSurface();
39     if (drSurface == nullptr) {
40         ROSEN_LOGE("RSColorFilterDrawable::Draw drSurface is null");
41         return;
42     }
43     auto clipBounds = canvas.GetDeviceClipBounds();
44     auto imageSnapshot = drSurface->GetImageSnapshot(clipBounds);
45     if (imageSnapshot == nullptr) {
46         ROSEN_LOGD("RSColorFilterDrawable::Draw image is null");
47         return;
48     }
49     imageSnapshot->HintCacheGpuResource();
50     Drawing::AutoCanvasRestore acr(canvas, true);
51     canvas.ResetMatrix();
52     static Drawing::SamplingOptions options(Drawing::FilterMode::NEAREST, Drawing::MipmapMode::NONE);
53     canvas.AttachBrush(brush_);
54     Drawing::Rect clipBoundsRect = {clipBounds.GetLeft(), clipBounds.GetTop(),
55         clipBounds.GetRight(), clipBounds.GetBottom()};
56     canvas.DrawImageRect(*imageSnapshot, clipBoundsRect, options);
57     canvas.DetachBrush();
58 }
59 
Generate(const RSRenderContent & content)60 RSPropertyDrawable::DrawablePtr RSColorFilterDrawable::Generate(const RSRenderContent& content)
61 {
62     auto& colorFilter = content.GetRenderProperties().GetColorFilter();
63     if (colorFilter == nullptr) {
64         return nullptr;
65     }
66     Drawing::Brush brush;
67     brush.SetAntiAlias(true);
68     Drawing::Filter filter;
69     filter.SetColorFilter(colorFilter);
70     brush.SetFilter(filter);
71     return std::make_unique<RSColorFilterDrawable>(std::move(brush));
72 }
73 
Update(const RSRenderContent & content)74 bool RSColorFilterDrawable::Update(const RSRenderContent& content)
75 {
76     auto& colorFilter = content.GetRenderProperties().GetColorFilter();
77     if (colorFilter == nullptr) {
78         return false;
79     }
80     Drawing::Filter filter;
81     filter.SetColorFilter(colorFilter);
82     brush_.SetFilter(filter);
83     return true;
84 }
85 
Generate(const RSRenderContent & content)86 RSPropertyDrawable::DrawablePtr RSClipFrameDrawable::Generate(const RSRenderContent& content)
87 {
88     // PLANNING: cache frame rect, and update when frame rect changed
89     return content.GetRenderProperties().GetClipToFrame() ? std::make_unique<RSClipFrameDrawable>() : nullptr;
90 }
91 
Draw(const RSRenderContent & content,RSPaintFilterCanvas & canvas) const92 void RSClipFrameDrawable::Draw(const RSRenderContent& content, RSPaintFilterCanvas& canvas) const
93 {
94     canvas.ClipRect(RSPropertiesPainter::Rect2DrawingRect(content.GetRenderProperties().GetFrameRect()),
95         Drawing::ClipOp::INTERSECT, false);
96 }
97 } // namespace OHOS::Rosen
98