• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "form_ashmem.h"
17 
18 #include "hilog_wrapper.h"
19 #include "message_parcel.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
~FormAshmem()23 FormAshmem::~FormAshmem()
24 {
25     if (ashmem_ != nullptr) {
26         ashmem_->CloseAshmem();
27     }
28 }
29 
Marshalling(Parcel & parcel) const30 bool FormAshmem::Marshalling(Parcel &parcel) const
31 {
32     MessageParcel* messageParcel = (MessageParcel*)&parcel;
33     if (!messageParcel->WriteAshmem(ashmem_)) {
34         return false;
35     }
36     return true;
37 }
38 
ReadFromParcel(Parcel & parcel)39 bool FormAshmem::ReadFromParcel(Parcel &parcel)
40 {
41     MessageParcel* messageParcel = (MessageParcel*)&parcel;
42     ashmem_ = messageParcel->ReadAshmem();
43     if (ashmem_ == nullptr) {
44         return false;
45     }
46     HILOG_INFO("fd: %{public}d, size: %{public}d", ashmem_->GetAshmemFd(), ashmem_->GetAshmemSize());
47     return true;
48 }
49 
Unmarshalling(Parcel & parcel)50 FormAshmem* FormAshmem::Unmarshalling(Parcel &parcel)
51 {
52     FormAshmem* formAshmem = new (std::nothrow) FormAshmem();
53     if (formAshmem != nullptr && !formAshmem->ReadFromParcel(parcel)) {
54         delete formAshmem;
55         formAshmem = nullptr;
56     }
57     return formAshmem;
58 }
59 
WriteToAshmem(std::string name,char * data,int32_t size)60 bool FormAshmem::WriteToAshmem(std::string name, char *data, int32_t size)
61 {
62     if (size <= 0) {
63         HILOG_ERROR("%{public}s: Parameter is invalid, size= %{public}d", __func__, size);
64     }
65 
66     ashmem_ = Ashmem::CreateAshmem(name.c_str(), size);
67     if (ashmem_ == nullptr) {
68         HILOG_ERROR("create shared memory fail");
69         return false;
70     }
71 
72     bool ret = ashmem_->MapReadAndWriteAshmem();
73     if (!ret) {
74         HILOG_ERROR("map shared memory fail");
75         return false;
76     }
77 
78     ret = ashmem_->WriteToAshmem(data, size, 0);
79     if (!ret) {
80         ashmem_->UnmapAshmem();
81         HILOG_ERROR("write image data to shared memory fail");
82         return false;
83     }
84 
85     ashmem_->UnmapAshmem();
86     return true;
87 }
88 
GetAshmemSize()89 int32_t FormAshmem::GetAshmemSize()
90 {
91     if (ashmem_ == nullptr) {
92         return 0;
93     }
94     return ashmem_->GetAshmemSize();
95 }
96 
GetAshmemFd()97 int32_t FormAshmem::GetAshmemFd()
98 {
99     if (ashmem_ == nullptr) {
100         return -1;
101     }
102     return ashmem_->GetAshmemFd();
103 }
104 }  // namespace AppExecFwk
105 }  // namespace OHOS
106