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