• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 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 #ifndef POINTER_RENDERER_H
17 #define POINTER_RENDERER_H
18 
19 #include "struct_multimodal.h"
20 #include "ui/rs_canvas_node.h"
21 #include "ui/rs_surface_node.h"
22 
23 namespace OHOS::MMI {
24 using image_ptr_t = std::shared_ptr<Rosen::Drawing::Image>;
25 using pixelmap_ptr_t = std::shared_ptr<OHOS::Media::PixelMap>;
26 constexpr int32_t DEFAULT_IMG_SIZE{ 10 };
27 
28 class RenderConfig {
29 public:
30     MOUSE_ICON style_;
31     ICON_TYPE align_;
32     std::string path_;
33     uint32_t color { 0 };
34     uint32_t size { 0 };
35     uint32_t direction { 0 };
36     float dpi { 0 };
37     bool isHard { false };
38     int32_t rotationAngle { 0 };
39     uint32_t rotationFocusX { 0 };
40     uint32_t rotationFocusY { 0 };
41     pixelmap_ptr_t userIconPixelMap { nullptr };
42     int32_t userIconHotSpotX { 0 };
43     int32_t userIconHotSpotY { 0 };
44     bool userIconFollowSystem { false };
45     int32_t GetImageSize() const;
46     float AdjustIncreaseRatio(float dpi) const;
47     std::string ToString() const;
48     int32_t GetOffsetX() const;
49     int32_t GetOffsetY() const;
50 
51     RenderConfig() = default;
52     ~RenderConfig() = default;
53 
54     bool operator == (const RenderConfig& rhs) const
55     {
56         return style_ == rhs.style_ && GetImageSize() == rhs.GetImageSize() && color == rhs.color;
57     }
58 
59     bool operator != (const RenderConfig& rhs) const
60     {
61         return style_ != rhs.style_ || GetImageSize() != rhs.GetImageSize() || color != rhs.color;
62     }
63 };
64 
65 class PointerRenderer {
66 public:
67     PointerRenderer() = default;
68     ~PointerRenderer() = default;
69 
70     int32_t Render(uint8_t *addr, uint32_t width, uint32_t height, const RenderConfig &cfg);
71     int32_t DynamicRender(uint8_t *addr, uint32_t width, uint32_t height, const RenderConfig &cfg);
72     image_ptr_t UserIconScale(uint32_t width, uint32_t height, const RenderConfig &cfg);
73 private:
74     image_ptr_t LoadPointerImage(const RenderConfig &cfg);
75     pixelmap_ptr_t LoadCursorSvgWithColor(const RenderConfig &cfg);
76     image_ptr_t ExtractDrawingImage(pixelmap_ptr_t pixelMap);
77     int32_t DrawImage(OHOS::Rosen::Drawing::Canvas &canvas, const RenderConfig &cfg);
78     std::vector<std::tuple<RenderConfig, image_ptr_t>> imgMaps_;
FindImg(const RenderConfig & cfg)79     image_ptr_t FindImg(const RenderConfig &cfg)
80     {
81         for (auto& data : imgMaps_) {
82             if (std::get<0>(data) == cfg) {
83                 return std::get<1>(data);
84             }
85         }
86         return nullptr;
87     }
PushImg(const RenderConfig & cfg,image_ptr_t img)88     void PushImg(const RenderConfig &cfg, image_ptr_t img)
89     {
90         if (imgMaps_.size() >= DEFAULT_IMG_SIZE) {
91             imgMaps_.erase(imgMaps_.begin());
92         }
93         imgMaps_.push_back({cfg, img});
94     }
95 };
96 } // namespace OHOS::MMI
97 
98 #endif // POINTER_RENDERER_H