1 /*
2  * Copyright (c) 2020-2021 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 GRAPHIC_LITE_SCREEN_DEVICE_PROXY_H
17 #define GRAPHIC_LITE_SCREEN_DEVICE_PROXY_H
18 
19 #include "gfx_utils/color.h"
20 #include "dock/screen_device.h"
21 #include "gfx_utils/graphic_assert.h"
22 #include "graphic_semaphore.h"
23 #include "gfx_utils/image_info.h"
24 #include "gfx_utils/rect.h"
25 #include "gfx_utils/transform.h"
26 #if ENABLE_WINDOW
27 #include "surface.h"
28 #endif
29 
30 namespace OHOS {
31 /** @brief A display device proxy */
32 class ScreenDeviceProxy : public HeapBase {
33 public:
34     static ScreenDeviceProxy* GetInstance();
35 
SetDevice(ScreenDevice * device)36     void SetDevice(ScreenDevice* device)
37     {
38         device_ = device;
39     }
40 
41     void Flush();
42 
43     void OnFlushReady();
44 
45     void OnRenderFinish(const Rect& mask);
46 
HardwareFill(const Rect & fillArea,uint32_t color,OpacityType opa,uint8_t * dst,uint32_t dstStride,ColorMode dstColorMode)47     bool HardwareFill(const Rect& fillArea,
48                       uint32_t color,
49                       OpacityType opa,
50                       uint8_t* dst,
51                       uint32_t dstStride,
52                       ColorMode dstColorMode)
53     {
54         if (device_ != nullptr) {
55             return device_->HardwareFill(fillArea, color, opa, dst, dstStride, dstColorMode);
56         }
57         return false;
58     }
59 
HardwareBlend(const uint8_t * src,const Rect & srcRect,uint32_t srcStride,uint32_t srcLineNumber,ColorMode srcColorMode,uint32_t color,OpacityType opa,uint8_t * dst,uint32_t dstStride,ColorMode dstColorMode,uint32_t x,uint32_t y)60     bool HardwareBlend(const uint8_t* src,
61                        const Rect& srcRect,
62                        uint32_t srcStride,
63                        uint32_t srcLineNumber,
64                        ColorMode srcColorMode,
65                        uint32_t color,
66                        OpacityType opa,
67                        uint8_t* dst,
68                        uint32_t dstStride,
69                        ColorMode dstColorMode,
70                        uint32_t x,
71                        uint32_t y)
72     {
73         if (device_ != nullptr) {
74             return device_->HardwareBlend(src, srcRect, srcStride, srcLineNumber, srcColorMode,
75                                           color, opa, dst, dstStride, dstColorMode, x, y);
76         }
77         return false;
78     }
79 
HardwareTransform(const uint8_t * src,ColorMode srcColorMode,const Rect & srcRect,const Matrix3<float> & transformMatrix,OpacityType opa,uint32_t color,const Rect & mask,uint8_t * dst,uint32_t dstStride,ColorMode dstColorMode,const TransformOption & option)80     bool HardwareTransform(const uint8_t* src,
81                            ColorMode srcColorMode,
82                            const Rect& srcRect,
83                            const Matrix3<float>& transformMatrix,
84                            OpacityType opa,
85                            uint32_t color,
86                            const Rect& mask,
87                            uint8_t* dst,
88                            uint32_t dstStride,
89                            ColorMode dstColorMode,
90                            const TransformOption& option)
91     {
92         if (device_ != nullptr) {
93             return device_->HardwareTransform(src, srcColorMode, srcRect, transformMatrix, opa, color, mask, dst,
94                                               dstStride, dstColorMode, option);
95         }
96         return false;
97     }
98 
SnapShot(uint32_t len,bool justCopy,uint8_t * dest,const Rect & rect,bool justRender)99     void SnapShot(uint32_t len, bool justCopy, uint8_t* dest, const Rect& rect, bool justRender)
100     {
101         if (device_ != nullptr) {
102             device_->SnapShot(len, justCopy, dest, rect, justRender);
103         }
104     }
105 
SetFramebuffer(uint8_t * addr,ColorMode mode,uint16_t width)106     void SetFramebuffer(uint8_t* addr, ColorMode mode, uint16_t width)
107     {
108         frameBufferAddr_ = addr;
109         frameBufferWidth_ = width;
110         frameBufferMode_ = mode;
111     }
112 
SetAnimatorbuffer(uint8_t * addr,ColorMode mode,uint16_t width)113     void SetAnimatorbuffer(uint8_t* addr, ColorMode mode, uint16_t width)
114     {
115         animatorBufferAddr_ = addr;
116         animatorBufferWidth_ = width;
117         animatorBufferMode_ = mode;
118     }
119 
SetAnimatorbufferWidth(uint16_t width)120     void SetAnimatorbufferWidth(uint16_t width)
121     {
122         animatorBufferWidth_ = width;
123     }
124 
EnableAnimatorBuffer(bool status)125     void EnableAnimatorBuffer(bool status)
126     {
127         useAnimatorBuff_ = status;
128     }
129 
130     void SetAnimatorRect(const Rect& rect);
131 
SetAnimatorTransMap(TransformMap & transMap)132     void SetAnimatorTransMap(TransformMap& transMap)
133     {
134         transMap_ = transMap;
135     }
136 
137     void DrawAnimatorBuffer(const Rect& invalidatedArea);
138 
GetAnimatorBufferStatus()139     bool GetAnimatorBufferStatus()
140     {
141         return useAnimatorBuff_;
142     }
143 
144 #if ENABLE_WINDOW
GetAllocationInfo()145     AllocationInfo& GetAllocationInfo()
146     {
147         return gfxAlloc_;
148     }
149 #endif
150 
GetBufferWidth()151     uint16_t GetBufferWidth() const
152     {
153         if (enableBitmapBuffer_) {
154             return bitmapBufferWidth_;
155         }
156         if (useAnimatorBuff_) {
157             return animatorBufferWidth_;
158         }
159         return frameBufferWidth_;
160     }
161 
162     void SetScreenSize(uint16_t width, uint16_t height);
163 
GetScreenWidth()164     uint16_t GetScreenWidth() const
165     {
166         return width_;
167     }
168 
GetScreenHeight()169     uint16_t GetScreenHeight() const
170     {
171         return height_;
172     }
173 
GetScreenArea()174     uint32_t GetScreenArea() const
175     {
176         return width_ * height_;
177     }
178 
179     uint8_t* GetBuffer();
180 
181     ColorMode GetBufferMode();
182 
SetViewBitmapBufferWidth(uint16_t width)183     void SetViewBitmapBufferWidth(uint16_t width)
184     {
185         bitmapBufferWidth_ = width;
186     }
187 
188     void EnableBitmapBuffer(uint8_t* viewBitmapBuffer);
189 
DisableBitmapBuffer()190     void DisableBitmapBuffer()
191     {
192         enableBitmapBuffer_ = false;
193     }
194 
195     bool GetScreenBitmapBuffer(uint8_t* dest, uint32_t size);
196 ;
197 
198 private:
ScreenDeviceProxy()199     ScreenDeviceProxy() {}
~ScreenDeviceProxy()200     virtual ~ScreenDeviceProxy() {}
201 
202     ScreenDeviceProxy(const ScreenDeviceProxy&) = delete;
203     ScreenDeviceProxy& operator=(const ScreenDeviceProxy&) = delete;
204     ScreenDeviceProxy(ScreenDeviceProxy&&) = delete;
205     ScreenDeviceProxy& operator=(ScreenDeviceProxy&&) = delete;
206 
207     ScreenDevice* device_ = nullptr;
208     FlushSem flush_ = FlushSem(false);
209     uint16_t width_ = HORIZONTAL_RESOLUTION;
210     uint16_t height_ = VERTICAL_RESOLUTION;
211 
212     uint8_t* frameBufferAddr_ = nullptr;
213     uint16_t frameBufferWidth_ = 0;
214     ColorMode frameBufferMode_ = ARGB8888;
215 
216     uint8_t* animatorBufferAddr_ = nullptr;
217     uint16_t animatorBufferWidth_ = 0;
218     ColorMode animatorBufferMode_ = ARGB8888;
219     Rect curViewRect_;
220     TransformMap transMap_;
221     bool useAnimatorBuff_ = false;
222     ImageInfo animatorImageInfo_ = {{0}};
223     // snapshot related
224     uint8_t* viewBitmapBuffer_ = nullptr;
225     uint16_t bitmapBufferWidth_ = 0;
226     bool enableBitmapBuffer_ = false;
227     // snapshot related
228 #if ENABLE_WINDOW
229     AllocationInfo gfxAlloc_ = {0};
230 #endif
231 };
232 } // namespace OHOS
233 #endif // GRAPHIC_LITE_SCREEN_DEVICE_PROXY_H
234