1 /*
2 * Copyright (c) 2022-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 #include "data_buffer.h"
17
18 #include "dscreen_errcode.h"
19 #include "dscreen_log.h"
20 #include <new>
21 #include <securec.h>
22
23 namespace OHOS {
24 namespace DistributedHardware {
DataBuffer(size_t capacity)25 DataBuffer::DataBuffer(size_t capacity)
26 {
27 if (capacity != 0) {
28 data_ = new (std::nothrow) uint8_t[capacity] {0};
29 if (data_ != nullptr) {
30 capacity_ = capacity;
31 }
32 }
33 dataType_ = 0;
34 }
35
~DataBuffer()36 DataBuffer::~DataBuffer()
37 {
38 if (data_ != nullptr) {
39 delete [] data_;
40 data_ = nullptr;
41 }
42 dirtyRectVec_.clear();
43 dataType_ = 0;
44 frameNumber_ = 0;
45 capacity_ = 0;
46 }
47
Capacity() const48 size_t DataBuffer::Capacity() const
49 {
50 return capacity_;
51 }
52
Data() const53 uint8_t *DataBuffer::Data() const
54 {
55 return data_;
56 }
57
SetSize(size_t size)58 void DataBuffer::SetSize(size_t size)
59 {
60 capacity_ = size;
61 }
62
SetDataType(uint8_t dataType)63 void DataBuffer::SetDataType(uint8_t dataType)
64 {
65 dataType_ = dataType;
66 }
67
DataType()68 uint8_t DataBuffer::DataType()
69 {
70 return dataType_;
71 }
72
SetDataNumber(size_t number)73 void DataBuffer::SetDataNumber(size_t number)
74 {
75 frameNumber_ = number;
76 }
77
DataNumber()78 size_t DataBuffer::DataNumber()
79 {
80 return frameNumber_;
81 }
82
ResetCapcity(size_t capacity)83 void DataBuffer::ResetCapcity(size_t capacity)
84 {
85 DHLOGI("%{public}s: ResetCapcity.", DSCREEN_LOG_TAG);
86 if (capacity < capacity_) {
87 return;
88 }
89 delete [] data_;
90 data_ = new (std::nothrow) uint8_t[capacity] {0};
91 if (data_ == nullptr) {
92 capacity_ = 0;
93 } else {
94 capacity_ = capacity;
95 }
96 }
97
AddData(size_t dataSize,unsigned char * & inputData)98 void DataBuffer::AddData(size_t dataSize, unsigned char* &inputData)
99 {
100 if (inputData == nullptr) {
101 return;
102 }
103 int32_t ret = memcpy_s(data_ + capacity_, dataSize, inputData, dataSize);
104 if (ret != EOK) {
105 DHLOGE("%{public}s: in AddData memcpy data failed, ret: %{public}" PRId32, DSCREEN_LOG_TAG, ret);
106 return;
107 }
108 capacity_ += dataSize;
109 }
110
AddDirtyRect(DirtyRect rect)111 void DataBuffer::AddDirtyRect(DirtyRect rect)
112 {
113 dirtyRectVec_.push_back(rect);
114 }
115
GetDirtyRectVec()116 std::vector<DirtyRect> DataBuffer::GetDirtyRectVec()
117 {
118 return dirtyRectVec_;
119 }
120
GetData(int32_t offset,int32_t datasize,uint8_t * & output)121 int32_t DataBuffer::GetData(int32_t offset, int32_t datasize, uint8_t* &output)
122 {
123 if (static_cast<unsigned long>(offset + datasize) > capacity_ || output == nullptr) {
124 DHLOGE("DataBuffer GetData parameter invalid.");
125 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
126 }
127 int32_t ret = memcpy_s(output, datasize, data_ + offset, datasize);
128 if (ret != EOK) {
129 DHLOGE("GetData memcpy data failed, ret: %{public}" PRId32, ret);
130 return ret;
131 }
132 return DH_SUCCESS;
133 }
134 } // namespace DistributedHardware
135 } // namespace OHOS