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_H
17#define GRAPHIC_LITE_SCREEN_DEVICE_H
18
19#include "gfx_utils/color.h"
20#include "gfx_utils/graphic_assert.h"
21#include "gfx_utils/rect.h"
22#include "graphic_semaphore.h"
23#if ENABLE_WINDOW
24#include "gfx_utils/pixel_format_utils.h"
25#endif
26
27namespace OHOS {
28#if ENABLE_WINDOW
29struct AllocationInfo {
30    uint8_t* virAddr;
31    uint8_t* phyAddr;
32    uint16_t width;
33    uint16_t height;
34    uint32_t stride;
35    ImagePixelFormat pixelFormat;
36};
37#endif
38
39#ifndef TRANSFORMOPTION
40#define TRANSFORMOPTION
41struct TransformOption {
42    TransformAlgorithm algorithm;
43};
44#endif
45
46/** @brief A semaphore for display buffer flushing. */
47class FlushSem : public HeapBase {
48public:
49    /**
50     * Constructor
51     *
52     * @param [in] isFlushing (Optional) True if is flushing, false if not.
53     */
54    FlushSem(bool isFlushing = false) : sem_(1, 1), isFlushing_(isFlushing) {}
55
56    /**
57     * @brief Destructor
58     */
59    virtual ~FlushSem() {}
60
61    /** Notifies the buffer is flushing end */
62    void Notify()
63    {
64        isFlushing_ = false;
65        sem_.Notify();
66    }
67
68    /** Waits the buffer is flushing */
69    void Wait()
70    {
71        while (isFlushing_) {
72            sem_.Wait();
73        }
74    }
75
76    /** set the flag as flashing */
77    void Flushing()
78    {
79        isFlushing_ = true;
80    }
81
82private:
83    GraphicSemaphore sem_;
84    bool isFlushing_;
85};
86
87/** @brief A display device. */
88class ScreenDevice : public HeapBase {
89public:
90    /**
91     * @brief Constructor
92     */
93    ScreenDevice() {}
94
95    /**
96     * @brief Destructor
97     */
98    virtual ~ScreenDevice() {}
99
100    virtual void Flush(int16_t x1, int16_t y1, int16_t x2, int16_t y2, const uint8_t* buffer, ColorMode mode) {}
101
102    virtual void Fill(int16_t x1, int16_t y1, int16_t x2, int16_t y2, const ColorType& color) {}
103
104    /**
105     * @brief Hardware accelerated filling interface implemented by the product platform
106     * @param fillArea Indicates the area to be filled
107     * @param color Indicates the color to be filled
108     * @param opa Indicates the transparency
109     * @param dst Indicates the start address of destination memory
110     * @param dstStride Indicates the number of bytes in a single row of destination memory
111     * @param dstColorMode Indicates the color format of destination memory
112     * @return Return true on success, false on failure
113     * @since 5.0
114     * @version 3.0
115     */
116    virtual bool HardwareFill(const Rect& fillArea,
117                              uint32_t color,
118                              OpacityType opa,
119                              uint8_t* dst,
120                              uint32_t dstStride,
121                              ColorMode dstColorMode)
122    {
123        return false;
124    }
125
126    /**
127     * @brief Hardware accelerated blending interface implemented by the product platform
128     * @param src Indicates the start address of source memory
129     * @param srcRect Indicates the area of ​​the source memory for color blending
130     * @param srcStride Indicates the number of bytes in a single row of source memory
131     * @param srcLineNumber Indicates the number of source memory rows
132     * @param srcColorMode Indicates the source memory color format
133     * @param color 32-bit XRGB8888 value
134     *              (valid when the source memory is in a format with only alpha information such as A1)
135     * @param opa Indicates the transparency
136     * @param dst Indicates the start address of destination memory
137     * @param dstStride Indicates the number of bytes in a single row of destination memory
138     * @param dstColorMode Indicates the color format of destination memory
139     * @param x The x coordinate of the upper left vertex of the destination memory for color blending
140     * @param y The y coordinate of the upper left vertex of the destination memory for color blending
141     * @return Return true on success, false on failure
142     * @since 5.0
143     * @version 3.0
144     */
145    virtual bool HardwareBlend(const uint8_t* src,
146                               const Rect& srcRect,
147                               uint32_t srcStride,
148                               uint32_t srcLineNumber,
149                               ColorMode srcColorMode,
150                               uint32_t color,
151                               OpacityType opa,
152                               uint8_t* dst,
153                               uint32_t dstStride,
154                               ColorMode dstColorMode,
155                               uint32_t x,
156                               uint32_t y)
157    {
158        return false;
159    }
160
161    /**
162     * @brief Hardware accelerated transformation interface implemented by the product platform
163     * @param src Indicates the start address of the source image
164     * @param srcColorMode Indicates the color format of the source image
165     * @param srcRect Indicates the position of the source image in the destination memory and its width and height
166     * @param transformMatrix  Indicates the transformation matrix
167     * @param opa Indicates the transparency
168     * @param color 32-bit XRGB8888 value
169     *              (valid when the source memory is in a format with only alpha information such as A1)
170     * @param mask Indicates the masking rectangle, and the content beyond the rectangle is not drawn
171     * @param dst Indicates the start address of destination memory
172     * @param dstStride Indicates the number of bytes in a single row of destination memory
173     * @param dstColorMode Indicates the color format of destination memory
174     * @param option Indicates the optional setting items for transformation operation
175     * @return Return true on success, false on failure
176     * @since 5.0
177     * @version 3.0
178     */
179    virtual bool HardwareTransform(const uint8_t* src,
180                                   ColorMode srcColorMode,
181                                   const Rect& srcRect,
182                                   const Matrix3<float>& transformMatrix,
183                                   OpacityType opa,
184                                   uint32_t color,
185                                   const Rect& mask,
186                                   uint8_t* dst,
187                                   uint32_t dstStride,
188                                   ColorMode dstColorMode,
189                                   const TransformOption& option)
190    {
191        return false;
192    }
193
194    virtual void SnapShot(uint32_t len,
195                          bool justCopy,
196                          uint8_t* dest,
197                          const Rect& rect,
198                          bool justRender)
199    {
200    }
201
202    virtual void RenderFinish(const Rect& mask) {}
203};
204} // namespace OHOS
205#endif // GRAPHIC_LITE_SCREEN_DEVICE_H