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 #include "utils/drawing_macros.h" 21 22 namespace OHOS { 23 namespace Rosen { 24 namespace Drawing { 25 class DRAWING_API Data { 26 public: 27 Data() noexcept; ~Data()28 virtual ~Data() {}; 29 30 /* 31 * @brief Create a new Data from a pointer allocated by malloc. When Data is destroyed, data is released. 32 * @param data A pointer to data. 33 * @param length Length of data. 34 * @return If create Data successed, return true. 35 */ 36 bool BuildFromMalloc(const void* data, size_t length); 37 38 /* 39 * @brief Create a new Data by copying the specified data. 40 * @param data A pointer to data. It must not be nullptr. 41 * @param length Length of data. 42 * @return If create Data successed, return true. 43 */ 44 bool BuildWithCopy(const void* data, size_t length); 45 46 /* 47 * @brief Create a new dataref, taking the ptr as is, 48 and using the releaseproc to free it. The proc may be NULL. 49 * @param ptr A pointer to data. It must not be nullptr. 50 * @param length Length of data. 51 * @param proc release callback func. 52 * @param ctx context, usually nullptr. 53 * @return If create Data successed, return true. 54 */ 55 bool BuildWithProc(const void* ptr, size_t length, DataReleaseProc proc, void* ctx); 56 57 /* 58 * @brief Create a new Data. When Data is destroyed, data isn't released. 59 * @param data A pointer to data. 60 * @param length Length of data. 61 * @return If create Data successed, return true. 62 */ 63 bool BuildWithoutCopy(const void* data, size_t length); 64 65 /* 66 * @brief: Create a new Data with uninitialized contents. 67 * @param length Size of Data buffer. 68 * @return If create Data successed, return true. 69 */ 70 bool BuildUninitialized(size_t length); 71 72 bool BuildEmpty(); 73 74 /* 75 * @brief Gets a writable pointer to Data buffer. 76 * @return A writable pointer to Data buffer. 77 */ 78 void* WritableData(); 79 80 /* 81 * @brief Gets length of Data buffer. 82 * @return Length of Data buffer. 83 */ 84 size_t GetSize() const; 85 86 /* 87 * @brief Gets a const pointer to Data buffer. 88 * @return A const pointer to Data buffer 89 */ 90 const void* GetData() const; 91 92 /** 93 * @brief Create a new data with the specified path. 94 * @param length The specified path. 95 * @return A shared pointer to Data. 96 */ 97 static std::shared_ptr<Data> MakeFromFileName(const char path[]); 98 99 /* 100 * @brief Get the adaptation layer instance, called in the adaptation layer. 101 * @return Adaptation Layer instance. 102 */ 103 template<typename T> GetImpl()104 T* GetImpl() const 105 { 106 return impl_->DowncastingTo<T>(); 107 } 108 109 std::shared_ptr<Data> Serialize() const; 110 111 private: 112 std::shared_ptr<DataImpl> impl_; 113 }; 114 } // namespace Drawing 115 } // namespace Rosen 116 } // namespace OHOS 117 #endif 118