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