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 std::string ToString() const; 47 int32_t GetOffsetX() const; 48 int32_t GetOffsetY() const; 49 50 RenderConfig() = default; 51 ~RenderConfig() = default; 52 53 bool operator == (const RenderConfig& rhs) const 54 { 55 return style == rhs.style && GetImageSize() == rhs.GetImageSize() && color == rhs.color; 56 } 57 58 bool operator != (const RenderConfig& rhs) const 59 { 60 return style != rhs.style || GetImageSize() != rhs.GetImageSize() || color != rhs.color; 61 } 62 }; 63 64 class PointerRenderer { 65 public: 66 PointerRenderer() = default; 67 ~PointerRenderer() = default; 68 69 int32_t Render(uint8_t *addr, uint32_t width, uint32_t height, const RenderConfig &cfg); 70 int32_t DynamicRender(uint8_t *addr, uint32_t width, uint32_t height, const RenderConfig &cfg); 71 image_ptr_t UserIconScale(uint32_t width, uint32_t height, const RenderConfig &cfg); 72 private: 73 image_ptr_t LoadPointerImage(const RenderConfig &cfg); 74 pixelmap_ptr_t LoadCursorSvgWithColor(const RenderConfig &cfg); 75 image_ptr_t ExtractDrawingImage(pixelmap_ptr_t pixelMap); 76 int32_t DrawImage(OHOS::Rosen::Drawing::Canvas &canvas, const RenderConfig &cfg); 77 std::vector<std::tuple<RenderConfig, image_ptr_t>> imgMaps_; FindImg(const RenderConfig & cfg)78 image_ptr_t FindImg(const RenderConfig &cfg) 79 { 80 for (auto& data : imgMaps_) { 81 if (std::get<0>(data) == cfg) { 82 return std::get<1>(data); 83 } 84 } 85 return nullptr; 86 } PushImg(const RenderConfig & cfg,image_ptr_t img)87 void PushImg(const RenderConfig &cfg, image_ptr_t img) 88 { 89 if (imgMaps_.size() >= DEFAULT_IMG_SIZE) { 90 imgMaps_.erase(imgMaps_.begin()); 91 } 92 imgMaps_.push_back({cfg, img}); 93 } 94 }; 95 } // namespace OHOS::MMI 96 97 #endif // POINTER_RENDERER_H