1 /*
2 * Copyright (C) 2021 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 "pixel_map_parcel.h"
17 #include <unistd.h>
18 #include "image_log.h"
19 #include "media_errors.h"
20 #ifndef _WIN32
21 #include "securec.h"
22 #else
23 #include "memory.h"
24 #endif
25
26 #if !defined(_WIN32) && !defined(_APPLE)
27 #include <sys/mman.h>
28 #include "ashmem.h"
29 #endif
30
31 #undef LOG_DOMAIN
32 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
33
34 #undef LOG_TAG
35 #define LOG_TAG "PixelMapParcel"
36
37 namespace OHOS {
38 namespace Media {
39 using namespace std;
40
41 constexpr int32_t PIXEL_MAP_INFO_MAX_LENGTH = 128;
42
ReleaseMemory(AllocatorType allocType,void * addr,void * context,uint32_t size)43 void PixelMapParcel::ReleaseMemory(AllocatorType allocType, void *addr, void *context, uint32_t size)
44 {
45 if (allocType == AllocatorType::SHARE_MEM_ALLOC) {
46 #if !defined(_WIN32) && !defined(_APPLE)
47 int *fd = static_cast<int *>(context);
48 if (addr != nullptr) {
49 ::munmap(addr, size);
50 }
51 if (fd != nullptr) {
52 ::close(*fd);
53 delete fd;
54 }
55 #endif
56 } else if (allocType == AllocatorType::HEAP_ALLOC) {
57 if (addr != nullptr) {
58 free(addr);
59 addr = nullptr;
60 }
61 }
62 }
63
ReadAshmemDataFromParcel(OHOS::MessageParcel & data,int32_t bufferSize,int32_t * & context)64 uint8_t *PixelMapParcel::ReadAshmemDataFromParcel(OHOS::MessageParcel& data, int32_t bufferSize, int32_t*& context)
65 {
66 uint8_t *base = nullptr;
67 int fd = data.ReadFileDescriptor();
68 if (fd < 0) {
69 IMAGE_LOGE("read fileDescriptor failed, fd < 0");
70 return nullptr;
71 }
72 void* ptr = ::mmap(nullptr, bufferSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
73 if (ptr == MAP_FAILED) {
74 ::close(fd);
75 IMAGE_LOGE("mmap shared memory failed");
76 return nullptr;
77 }
78 context = new(std::nothrow) int32_t();
79 if (context == nullptr) {
80 IMAGE_LOGE("alloc context failed.");
81 ::munmap(ptr, bufferSize);
82 ::close(fd);
83 return nullptr;
84 }
85 *static_cast<int32_t *>(context) = fd;
86 base = static_cast<uint8_t *>(ptr);
87 return base;
88 }
89
ReadHeapDataFromParcel(OHOS::MessageParcel & data,int32_t bufferSize)90 uint8_t *PixelMapParcel::ReadHeapDataFromParcel(OHOS::MessageParcel& data, int32_t bufferSize)
91 {
92 const uint8_t *addr = data.ReadBuffer(bufferSize);
93 uint8_t *base = nullptr;
94 if (addr == nullptr) {
95 IMAGE_LOGE("read buffer from parcel failed, read buffer addr is null");
96 return nullptr;
97 }
98 if (bufferSize <= 0 || bufferSize > PIXEL_MAP_MAX_RAM_SIZE) {
99 IMAGE_LOGE("read bufferSize failed, invalid bufferSize.");
100 return nullptr;
101 }
102 base = static_cast<uint8_t *>(malloc(bufferSize));
103 if (base == nullptr) {
104 IMAGE_LOGE("alloc new pixel memory size:[%{public}d] failed.", bufferSize);
105 return nullptr;
106 }
107 if (memcpy_s(base, bufferSize, addr, bufferSize) != 0) {
108 free(base);
109 base = nullptr;
110 IMAGE_LOGE("memcpy pixel data size:[%{public}d] error.", bufferSize);
111 return nullptr;
112 }
113 return base;
114 }
115
CreateFromParcel(OHOS::MessageParcel & data)116 std::unique_ptr<PixelMap> PixelMapParcel::CreateFromParcel(OHOS::MessageParcel& data)
117 {
118 unique_ptr<PixelMap> pixelMap = make_unique<PixelMap>();
119 if (pixelMap == nullptr) {
120 IMAGE_LOGE("create pixelmap pointer fail");
121 return nullptr;
122 }
123
124 ImageInfo imgInfo;
125 imgInfo.size.width = data.ReadInt32();
126 imgInfo.size.height = data.ReadInt32();
127 imgInfo.pixelFormat = static_cast<PixelFormat>(data.ReadInt32());
128 imgInfo.colorSpace = static_cast<ColorSpace>(data.ReadInt32());
129 imgInfo.alphaType = static_cast<AlphaType>(data.ReadInt32());
130 imgInfo.baseDensity = data.ReadInt32();
131 int32_t bufferSize = data.ReadInt32();
132 AllocatorType allocType = static_cast<AllocatorType>(data.ReadInt32());
133 uint8_t *base = nullptr;
134 int32_t *context = nullptr;
135 if (allocType == AllocatorType::SHARE_MEM_ALLOC) {
136 #if !defined(_WIN32) && !defined(_APPLE)
137 base = ReadAshmemDataFromParcel(data, bufferSize, context);
138 #endif
139 } else {
140 base = ReadHeapDataFromParcel(data, bufferSize);
141 }
142
143 uint32_t ret = pixelMap->SetImageInfo(imgInfo);
144 if (ret != SUCCESS) {
145 ReleaseMemory(allocType, base, reinterpret_cast<void *>(context), bufferSize);
146 IMAGE_LOGE("create pixel map from parcel failed, set image info error.");
147 return nullptr;
148 }
149 pixelMap->SetPixelsAddr(base, reinterpret_cast<void *>(context), bufferSize, allocType, nullptr);
150 return pixelMap;
151 }
152
WriteImageInfo(PixelMap * pixelMap,OHOS::MessageParcel & data)153 bool PixelMapParcel::WriteImageInfo(PixelMap* pixelMap, OHOS::MessageParcel& data)
154 {
155 int32_t bufferSize = pixelMap->GetByteCount();
156 if (static_cast<size_t>(bufferSize + PIXEL_MAP_INFO_MAX_LENGTH) > data.GetDataCapacity() &&
157 !data.SetDataCapacity(bufferSize + PIXEL_MAP_INFO_MAX_LENGTH)) {
158 IMAGE_LOGE("set parcel max capacity:[%{public}d] failed.", bufferSize + PIXEL_MAP_INFO_MAX_LENGTH);
159 return false;
160 }
161 if (!data.WriteInt32(pixelMap->GetWidth())) {
162 IMAGE_LOGE("write pixel map width:[%{public}d] to parcel failed.", pixelMap->GetWidth());
163 return false;
164 }
165 if (!data.WriteInt32(pixelMap->GetHeight())) {
166 IMAGE_LOGE("write pixel map height:[%{public}d] to parcel failed.", pixelMap->GetHeight());
167 return false;
168 }
169 if (!data.WriteInt32(static_cast<int32_t>(pixelMap->GetPixelFormat()))) {
170 IMAGE_LOGE("write pixel map pixel format:[%{public}d] to parcel failed.", pixelMap->GetPixelFormat());
171 return false;
172 }
173 if (!data.WriteInt32(static_cast<int32_t>(pixelMap->GetColorSpace()))) {
174 IMAGE_LOGE("write pixel map color space:[%{public}d] to parcel failed.", pixelMap->GetColorSpace());
175 return false;
176 }
177 if (!data.WriteInt32(static_cast<int32_t>(pixelMap->GetAlphaType()))) {
178 IMAGE_LOGE("write pixel map alpha type:[%{public}d] to parcel failed.", pixelMap->GetAlphaType());
179 return false;
180 }
181 if (!data.WriteInt32(pixelMap->GetBaseDensity())) {
182 IMAGE_LOGE("write pixel map base density:[%{public}d] to parcel failed.", pixelMap->GetBaseDensity());
183 return false;
184 }
185 if (!data.WriteInt32(bufferSize)) {
186 IMAGE_LOGE("write pixel map buffer size:[%{public}d] to parcel failed.", bufferSize);
187 return false;
188 }
189 if (!data.WriteInt32(static_cast<int32_t>(pixelMap->GetAllocatorType()))) {
190 IMAGE_LOGE("write pixel map allocator type:[%{public}d] to parcel failed.",
191 pixelMap->GetAllocatorType());
192 return false;
193 }
194 return true;
195 }
196
WriteToParcel(PixelMap * pixelMap,OHOS::MessageParcel & data)197 bool PixelMapParcel::WriteToParcel(PixelMap* pixelMap, OHOS::MessageParcel& data)
198 {
199 if (pixelMap == nullptr) {
200 return false;
201 }
202 int32_t bufferSize = pixelMap->GetByteCount();
203 if (!WriteImageInfo(pixelMap, data)) {
204 IMAGE_LOGE("write pixel map info failed.");
205 return false;
206 }
207 if (pixelMap->GetAllocatorType() == AllocatorType::SHARE_MEM_ALLOC) {
208 #if !defined(_WIN32) && !defined(_APPLE)
209 int *fd = static_cast<int *>(pixelMap->GetFd());
210 if (*fd < 0) {
211 IMAGE_LOGE("write pixel map failed, fd < 0.");
212 return false;
213 }
214 if (!data.WriteFileDescriptor(*fd)) {
215 IMAGE_LOGE("write pixel map fd:[%{public}d] to parcel failed.", *fd);
216 return false;
217 }
218 #endif
219 } else {
220 const uint8_t *addr = pixelMap->GetPixels();
221 if (addr == nullptr) {
222 IMAGE_LOGE("write to parcel failed, pixel memory is null.");
223 return false;
224 }
225 if (!data.WriteBuffer(addr, bufferSize)) {
226 IMAGE_LOGE("write pixel map buffer to parcel failed.");
227 return false;
228 }
229 }
230 return true;
231 }
232 } // namespace Media
233 } // namespace OHOS
234