1 /*
2  * Copyright (c) 2022 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 /**
17 * @file render_buffer.h
18 * @brief Defines Renderer buffer
19 * @since 1.0
20 * @version 1.0.
21 */
22 
23 #ifndef GRAPHIC_LITE_RENDER_BUFFER_H
24 #define GRAPHIC_LITE_RENDER_BUFFER_H
25 
26 #include <cstring>
27 
28 #include "gfx_utils/diagram/vertexprimitive/geometry_plaindata_array.h"
29 
30 namespace OHOS {
31 /**
32  * Row accessor.
33  */
34 class RenderBuffer {
35 public:
RenderBuffer()36     RenderBuffer() : renBuf_(0), start_(0), width_(0), height_(0), bufStride_(0) {}
37 
38     /**
39      * @brief RowAccessor Row accessor.
40      * @param renBuf buffer.
41      * @param areaWidth The width of the pixel area in the buffer.
42      * @param areaHeight The height of the pixel area in the buffer.
43      * @param stride Buffer stride.
44      */
RenderBuffer(uint8_t * renBuf,uint32_t areaWidth,uint32_t areaHeight,int32_t stride)45     RenderBuffer(uint8_t* renBuf, uint32_t areaWidth, uint32_t areaHeight, int32_t stride)
46         : renBuf_(0),
47           start_(0),
48           width_(0),
49           height_(0),
50           bufStride_(0)
51     {
52         Attach(renBuf, areaWidth, areaHeight, stride);
53     }
54 
55     /**
56      * @brief attach Incoming parameters.
57      * @param renBuf buffer
58      * @param areaWidth The width of the pixel area in the buffer.
59      * @param areaHeightThe height of the pixel area in the buffer.
60      * @param stride Buffer stride.
61      */
Attach(uint8_t * renBuf,uint32_t areaWidth,uint32_t areaHeight,int32_t stride)62     void Attach(uint8_t* renBuf, uint32_t areaWidth, uint32_t areaHeight, int32_t stride)
63     {
64         renBuf_ = renBuf;
65         start_ = renBuf;
66         width_ = areaWidth;
67         height_ = areaHeight;
68         bufStride_ = stride;
69         if (stride < 0) {
70             start_ = renBuf - static_cast<int32_t>(areaHeight - 1) * stride;
71         }
72     }
73 
74     /**
75      * @brief GetBuf Gets a pointer to the render buffer.
76      */
GetBuf()77     const uint8_t* GetBuf() const
78     {
79         return renBuf_;
80     }
81 
82     /**
83      * @brief GetBuf Get area width.
84      */
GetWidth()85     uint32_t GetWidth() const
86     {
87         return width_;
88     }
89 
90     /**
91      * @brief GetBuf GetBuf Get area height.
92      */
GetHeight()93     uint32_t GetHeight() const
94     {
95         return height_;
96     }
97 
GetStride()98     int32_t GetStride() const
99     {
100         return bufStride_;
101     }
102 
103     /**
104      * @brief GetRowPtr Returns a pointer to the beginning of line y.
105      */
GetRowPtr(int32_t y)106     uint8_t* GetRowPtr(int32_t y)
107     {
108         return start_ + y * bufStride_;
109     }
110 
111     /**
112      * @brief GetRowPtr Returns a pointer to the beginning of line y。
113      */
GetRowPtr(int32_t y)114     const uint8_t* GetRowPtr(int32_t y) const
115     {
116         return start_ + y * bufStride_;
117     }
118 
119     /**
120      * @brief GetRow Get row data
121      */
GetRow(int32_t y)122     RowData GetRow(int32_t y) const
123     {
124         return RowData(0, width_ - 1, GetRowPtr(y));
125     }
126 
127 private:
128     uint8_t* renBuf_;       // Pointer to render buffer.
129     uint8_t* start_;        // Point to the first pixel according to the stride.
130     uint32_t width_;  // Area width.
131     uint32_t height_; // Area height.
132     int32_t bufStride_;   // Number of bytes per line.
133 };
134 } // namespace OHOS
135 #endif
136