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