1 /*
2  * Copyright (c) 2024-2024 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 RS_POINTER_RENDER_MANAGER_H
17 #define RS_POINTER_RENDER_MANAGER_H
18 
19 #include "ipc_callbacks/pointer_render/pointer_luminance_change_callback.h"
20 #include "pipeline/rs_display_render_node.h"
21 #include "pipeline/rs_processor.h"
22 #ifdef RS_ENABLE_VK
23 #include "pipeline/rs_vk_image_manager.h"
24 #endif
25 
26 namespace OHOS::Rosen {
27 
28 class RSPointerRenderManager {
29 public:
30     RSPointerRenderManager() = default;
31     ~RSPointerRenderManager() = default;
32 
33 #if defined (RS_ENABLE_VK)
34     static void InitInstance(const std::shared_ptr<RSVkImageManager>& vkImageManager);
35 #endif
36 
37 #if defined (RS_ENABLE_GL) && defined (RS_ENABLE_EGLIMAGE)
38     static void InitInstance(const std::shared_ptr<RSEglImageManager>& eglImageManager);
39 #endif
40 
41     static RSPointerRenderManager& GetInstance();
42     static int64_t GetCurrentTime();
43     void ProcessColorPicker(std::shared_ptr<RSProcessor> processor, std::shared_ptr<Drawing::GPUContext> gpuContext);
44     void SetPointerColorInversionConfig(float darkBuffer, float brightBuffer, int64_t interval, int32_t rangeSize);
45     void SetPointerColorInversionEnabled(bool enable);
GetPointerColorInversionEnabled()46     bool GetPointerColorInversionEnabled()
47     {
48         return isEnableCursorInversion_;
49     }
50     void RegisterPointerLuminanceChangeCallback(pid_t pid, sptr<RSIPointerLuminanceChangeCallback> callback);
51     void UnRegisterPointerLuminanceChangeCallback(pid_t pid);
SetCacheImgForPointer(std::shared_ptr<Drawing::Image> cacheImgForPointer)52     void SetCacheImgForPointer(std::shared_ptr<Drawing::Image> cacheImgForPointer)
53     {
54         cacheImgForPointer_ = cacheImgForPointer;
55     }
56 
57 private:
58     void ExecutePointerLuminanceChangeCallback(int32_t brightness);
59     void CallPointerLuminanceChange(int32_t brightness);
60     bool CheckColorPickerEnabled();
61     void CalculateColorRange(RectI& pRect);
62     bool CalculateTargetLayer(std::shared_ptr<RSProcessor> processor);
63     void RunColorPickerTask();
64     std::shared_ptr<Drawing::Image> GetIntersectImageByLayer(const BufferDrawParam& param);
65     bool GetIntersectImageBySubset(std::shared_ptr<Drawing::GPUContext> gpuContext);
66 #if defined (RS_ENABLE_VK)
67     std::shared_ptr<Drawing::Image> GetIntersectImageFromVK(Drawing::RectI& imgCutRect,
68     std::shared_ptr<Drawing::GPUContext>& context, const BufferDrawParam& param);
69 #endif
70 #if defined (RS_ENABLE_GL) && defined (RS_ENABLE_EGLIMAGE)
71     std::shared_ptr<Drawing::Image> GetIntersectImageFromGL(Drawing::RectI& imgCutRect,
72     std::shared_ptr<Drawing::GPUContext>& context, const BufferDrawParam& param);
73 #endif
74     std::shared_ptr<Drawing::Image> GetImageTexture(std::shared_ptr<Drawing::Image>& image);
75     void GetRectAndTargetLayer(std::vector<LayerInfoPtr>& layers, RectI& pRect, int displayNodeIndex);
76     void RunColorPickerTaskBackground(const BufferDrawParam& param);
77     int16_t CalcAverageLuminance(std::shared_ptr<Drawing::Image> image);
78 
79 private:
80     RectI rect_;
81     LayerInfoPtr target_;
82 #if defined (RS_ENABLE_VK)
83     std::shared_ptr<RSVkImageManager> vkImageManager_ = nullptr;
84 #endif
85 #if defined (RS_ENABLE_GL) && defined (RS_ENABLE_EGLIMAGE)
86     std::shared_ptr<RSEglImageManager> eglImageManager_ = nullptr;
87 #endif
88     int16_t luminance_ = 0;
89     bool forceCPU_ = false;
90     std::shared_ptr<Drawing::Image> image_ = nullptr;
91     Drawing::BackendTexture backendTexture_;
92     Drawing::BackendTexture backendTexturePre_;
93     Drawing::BitmapFormat bitmapFormat_;
94     std::atomic<bool> taskDoing_ = false;
95     bool isEnableCursorInversion_ = false;
96     std::shared_ptr<Drawing::Image> cacheImgForPointer_ = nullptr;
97     std::mutex mtx_;
98     std::mutex cursorInvertMutex_;
99     std::map<pid_t, sptr<RSIPointerLuminanceChangeCallback>> colorChangeListeners_;
100     enum class CursorBrightness {
101         NONE,
102         BRIGHT,
103         DARK
104     };
105     CursorBrightness brightnessMode_ = CursorBrightness::NONE;
106     int64_t lastColorPickerTime_ = 0;
107     int64_t colorSamplingInterval_ = 50;
108     int32_t rangeSize_ = 0;
109     float darkBuffer_ = 0.58f;
110     float brightBuffer_ = 0.40f;
111 };
112 }
113 #endif // RS_POINTER_RENDER_MANAGER_H