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 23namespace OHOS { 24namespace DistributedHardware { 25DataBuffer::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 36DataBuffer::~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 48size_t DataBuffer::Capacity() const 49{ 50 return capacity_; 51} 52 53uint8_t *DataBuffer::Data() const 54{ 55 return data_; 56} 57 58void DataBuffer::SetSize(size_t size) 59{ 60 capacity_ = size; 61} 62 63void DataBuffer::SetDataType(uint8_t dataType) 64{ 65 dataType_ = dataType; 66} 67 68uint8_t DataBuffer::DataType() 69{ 70 return dataType_; 71} 72 73void DataBuffer::SetDataNumber(size_t number) 74{ 75 frameNumber_ = number; 76} 77 78size_t DataBuffer::DataNumber() 79{ 80 return frameNumber_; 81} 82 83void 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 98void 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 111void DataBuffer::AddDirtyRect(DirtyRect rect) 112{ 113 dirtyRectVec_.push_back(rect); 114} 115 116std::vector<DirtyRect> DataBuffer::GetDirtyRectVec() 117{ 118 return dirtyRectVec_; 119} 120 121int32_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