1 /*
2  * Copyright (c) 2022-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 #ifndef RENDER_SERVICE_CLIENT_CORE_UI_RS_MASK_H
16 #define RENDER_SERVICE_CLIENT_CORE_UI_RS_MASK_H
17 
18 #include <memory>
19 #include "common/rs_macros.h"
20 #include "draw/pen.h"
21 #include "draw/brush.h"
22 #include "draw/path.h"
23 #include "image/picture.h"
24 #if defined(NEW_SKIA)
25 #include "modules/svg/include/SkSVGDOM.h"
26 #endif
27 #include "transaction/rs_marshalling_helper.h"
28 
29 namespace OHOS {
30 namespace Rosen {
31 enum class MaskType {
32     NONE = 0,
33     SVG,
34     GRADIENT,
35     PATH,
36     PIXEL_MAP
37 };
38 
39 class RSB_EXPORT RSMask : public std::enable_shared_from_this<RSMask> {
40 public:
41     RSMask();
42     virtual ~RSMask();
43     static std::shared_ptr<RSMask> CreateGradientMask(const Drawing::Brush& maskBrush);
44     static std::shared_ptr<RSMask> CreatePathMask(const Drawing::Path& maskPath, const Drawing::Brush& maskBrush);
45     static std::shared_ptr<RSMask> CreatePathMask(
46         const Drawing::Path& maskPath, const Drawing::Pen&  maskPen, const Drawing::Brush& maskBrush);
47     static std::shared_ptr<RSMask> CreateSVGMask(double x, double y, double scaleX, double scaleY,
48         const sk_sp<SkSVGDOM>& svgDom);
49     static std::shared_ptr<RSMask> CreatePixelMapMask(const std::shared_ptr<Media::PixelMap> pixelMap);
50 
51     void SetSvgX(double x);
52     double GetSvgX() const;
53     void SetSvgY(double y);
54     double GetSvgY() const;
55     void SetScaleX(double scaleX);
56     double GetScaleX() const;
57     void SetScaleY(double scaleY);
58     double GetScaleY() const;
59     void SetMaskPath(const Drawing::Path& path);
60     std::shared_ptr<Drawing::Path> GetMaskPath() const;
61     void SetMaskPen(const Drawing::Pen& pen);
62     Drawing::Pen GetMaskPen() const;
63     void SetMaskBrush(const Drawing::Brush& brush);
64     Drawing::Brush GetMaskBrush() const;
65     bool MarshallingPathAndBrush(Parcel& parcel) const;
66     void SetSvgDom(const sk_sp<SkSVGDOM>& svgDom);
67     sk_sp<SkSVGDOM> GetSvgDom() const;
68     std::shared_ptr<Drawing::Picture> GetSvgPicture() const;
69     void SetPixelMap(const std::shared_ptr<Media::PixelMap> pixelMap);
70     std::shared_ptr<Media::PixelMap> GetPixelMap() const;
71     std::shared_ptr<Drawing::Image> GetImage() const;
72     void SetMaskType(MaskType type);
73     bool IsSvgMask() const;
74     bool IsGradientMask() const;
75     bool IsPathMask() const;
76     bool IsPixelMapMask() const;
77     void Dump(std::string& out) const;
78 
79 #ifdef ROSEN_OHOS
80     bool Marshalling(Parcel& parcel) const;
81     [[nodiscard]] static RSMask* Unmarshalling(Parcel& parcel);
82 #endif
83 
84 protected:
85     RSMask(const RSMask&) = delete;
86     RSMask(const RSMask&&) = delete;
87     RSMask& operator=(const RSMask&) = delete;
88     RSMask& operator=(const RSMask&&) = delete;
89 
90 private:
91     MaskType type_ = MaskType::NONE;
92     double svgX_ = 0.0f;
93     double svgY_ = 0.0f;
94     double scaleX_ = 1.0f;
95     double scaleY_ = 1.0f;
96     sk_sp<SkSVGDOM> svgDom_;
97     std::shared_ptr<Drawing::Picture> svgPicture_;
98     Drawing::Pen maskPen_;
99     Drawing::Brush maskBrush_;
100     std::shared_ptr<Drawing::Path> maskPath_;
101     std::shared_ptr<Media::PixelMap> pixelMap_;
102     std::shared_ptr<Drawing::Image> image_;
103 
104 };
105 } // namespace Rosen
106 } // namespace OHOS
107 
108 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_MASK_H