1 /* 2 * Copyright (c) 2023 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 DATA_H 17 #define DATA_H 18 19 #include "impl_interface/data_impl.h" 20 21 namespace OHOS { 22 namespace Rosen { 23 namespace Drawing { 24 class Data { 25 public: 26 Data() noexcept; ~Data()27 virtual ~Data() {}; 28 29 /* 30 * @brief Create a new Data from a pointer allocated by malloc. When Data is destroyed, data is released. 31 * @param data A pointer to data. 32 * @param length Length of data. 33 * @return If create Data successed, return true. 34 */ 35 bool BuildFromMalloc(const void* data, size_t length); 36 37 /* 38 * @brief Create a new Data by copying the specified data. 39 * @param data A pointer to data. It must not be nullptr. 40 * @param length Length of data. 41 * @return If create Data successed, return true. 42 */ 43 bool BuildWithCopy(const void* data, size_t length); 44 45 /* 46 * @brief Create a new Data. When Data is destroyed, data isn't released. 47 * @param data A pointer to data. 48 * @param length Length of data. 49 * @return If create Data successed, return true. 50 */ 51 bool BuildWithoutCopy(const void* data, size_t length); 52 53 /* 54 * @brief: Create a new Data with uninitialized contents. 55 * @param length Size of Data buffer. 56 * @return If create Data successed, return true. 57 */ 58 bool BuildUninitialized(size_t length); 59 60 /* 61 * @brief Gets a writable pointer to Data buffer. 62 * @return A writable pointer to Data buffer. 63 */ 64 void* WritableData(); 65 66 /* 67 * @brief Gets length of Data buffer. 68 * @return Length of Data buffer. 69 */ 70 size_t GetSize() const; 71 72 /* 73 * @brief Gets a const pointer to Data buffer. 74 * @return A const pointer to Data buffer 75 */ 76 const void* GetData() const; 77 78 /* 79 * @brief Get the adaptation layer instance, called in the adaptation layer. 80 * @return Adaptation Layer instance. 81 */ 82 template<typename T> GetImpl()83 const std::shared_ptr<T> GetImpl() const 84 { 85 return impl_->DowncastingTo<T>(); 86 } 87 88 private: 89 std::shared_ptr<DataImpl> impl_; 90 }; 91 } // namespace Drawing 92 } // namespace Rosen 93 } // namespace OHOS 94 #endif 95