1 /*
2  * Copyright (c) 2022-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 RENDER_SERVICE_BASE_TRANSACTION_RS_ASHMEM_HELPER_H
17 #define RENDER_SERVICE_BASE_TRANSACTION_RS_ASHMEM_HELPER_H
18 
19 #include <message_parcel.h>
20 #include "common/rs_common_def.h"
21 #include "common/rs_macros.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 class AshmemAllocator : public Allocator {
26 public:
27     static std::unique_ptr<AshmemAllocator> CreateAshmemAllocator(size_t size, int mapType);
28     static std::unique_ptr<AshmemAllocator> CreateAshmemAllocatorWithFd(int fd, size_t size, int mapType);
29     bool MapAshmem(int mapType);
30     bool WriteToAshmem(const void *data, size_t size);
31     void* CopyFromAshmem(size_t size);
32 
33     int GetFd() const; // the fd is only valid during the object lifetime
34     size_t GetSize() const;
35     void* GetData() const;
36 
37     AshmemAllocator(int fd, size_t size);
38     ~AshmemAllocator() override;
39     void Dealloc(void* data) override;
40     void* Alloc(size_t size) override;
41     void* Realloc(void* data, size_t newSize) override;
42 
43 private:
44     int fd_;
45     size_t size_;
46     void* data_ = nullptr;
47 };
48 
49 class RSB_EXPORT RSAshmemHelper {
50 public:
51     static std::shared_ptr<MessageParcel> CreateAshmemParcel(std::shared_ptr<MessageParcel>& dataParcel);
52     static std::shared_ptr<MessageParcel> ParseFromAshmemParcel(MessageParcel* ashmemParcel);
53 
54     static void CopyFileDescriptor(
55         MessageParcel* ashmemParcel, std::shared_ptr<MessageParcel>& dataParcel);
56     static void InjectFileDescriptor(std::shared_ptr<MessageParcel>& dataParcel, MessageParcel* ashmemParcel);
57 };
58 } // namespace Rosen
59 } // namespace OHOS
60 
61 #endif // RENDER_SERVICE_BASE_TRANSACTION_RS_ASHMEM_HELPER_H
62