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 "fms_log_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 HILOG_INFO("formAshmem destroy");
29 }
30
Marshalling(Parcel & parcel) const31 bool FormAshmem::Marshalling(Parcel &parcel) const
32 {
33 MessageParcel* messageParcel = (MessageParcel*)&parcel;
34 if (!messageParcel->WriteAshmem(ashmem_)) {
35 HILOG_ERROR("WriteAshmem failed");
36 return false;
37 }
38 return true;
39 }
40
ReadFromParcel(Parcel & parcel)41 bool FormAshmem::ReadFromParcel(Parcel &parcel)
42 {
43 MessageParcel* messageParcel = (MessageParcel*)&parcel;
44 ashmem_ = messageParcel->ReadAshmem();
45 if (ashmem_ == nullptr) {
46 return false;
47 }
48 HILOG_INFO("fd:%{public}d,size:%{public}d", ashmem_->GetAshmemFd(), ashmem_->GetAshmemSize());
49 return true;
50 }
51
Unmarshalling(Parcel & parcel)52 FormAshmem* FormAshmem::Unmarshalling(Parcel &parcel)
53 {
54 FormAshmem* formAshmem = new (std::nothrow) FormAshmem();
55 if (formAshmem != nullptr && !formAshmem->ReadFromParcel(parcel)) {
56 delete formAshmem;
57 formAshmem = nullptr;
58 }
59 return formAshmem;
60 }
61
WriteToAshmem(std::string name,char * data,int32_t size)62 bool FormAshmem::WriteToAshmem(std::string name, char *data, int32_t size)
63 {
64 if (size <= 0) {
65 HILOG_ERROR("invalid param, size= %{public}d", size);
66 }
67
68 ashmem_ = Ashmem::CreateAshmem(name.c_str(), size);
69 if (ashmem_ == nullptr) {
70 HILOG_ERROR("create shared memory fail");
71 return false;
72 }
73
74 bool ret = ashmem_->MapReadAndWriteAshmem();
75 if (!ret) {
76 HILOG_ERROR("map shared memory fail");
77 return false;
78 }
79
80 ret = ashmem_->WriteToAshmem(data, size, 0);
81 if (!ret) {
82 ashmem_->UnmapAshmem();
83 HILOG_ERROR("write image data to shared memory fail");
84 return false;
85 }
86
87 ashmem_->UnmapAshmem();
88 return true;
89 }
90
GetAshmem() const91 sptr<Ashmem> FormAshmem::GetAshmem() const
92 {
93 return ashmem_;
94 }
95
GetAshmemSize()96 int32_t FormAshmem::GetAshmemSize()
97 {
98 if (ashmem_ == nullptr) {
99 return 0;
100 }
101 return ashmem_->GetAshmemSize();
102 }
103
GetAshmemFd()104 int32_t FormAshmem::GetAshmemFd()
105 {
106 if (ashmem_ == nullptr) {
107 return -1;
108 }
109 return ashmem_->GetAshmemFd();
110 }
111 } // namespace AppExecFwk
112 } // namespace OHOS
113