1 /*
2  * Copyright (c) 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 #include "fusion_image_framework_internal.h"
17 
18 #include "c_parcel_internal.h"
19 
20 #include "devicestatus_define.h"
21 #include "fi_log.h"
22 
23 #undef LOG_TAG
24 #define LOG_TAG "fusion_image_framework"
25 
26 namespace {
27 static const int32_t MAX_PIXEL_MAP_WIDTH { 600 };
28 static const int32_t MAX_PIXEL_MAP_HEIGHT { 600 };
29 
CPixelMapFrom(std::shared_ptr<::OHOS::Media::PixelMap> pixelMap)30 CPixelMap* CPixelMapFrom(std::shared_ptr<::OHOS::Media::PixelMap> pixelMap)
31 {
32     auto cImg = new (std::nothrow) CPixelMap;
33     CHKPP(cImg);
34     cImg->refCnt = 1;
35     cImg->pixelMap = pixelMap;
36     return cImg;
37 }
38 
CPixelMapRef(struct CPixelMap * pixelMap)39 struct CPixelMap* CPixelMapRef(struct CPixelMap *pixelMap)
40 {
41     CHKPP(pixelMap);
42     pixelMap->refCnt++;
43     return pixelMap;
44 }
45 
CPixelMapUnref(struct CPixelMap * pixelMap)46 struct CPixelMap* CPixelMapUnref(struct CPixelMap *pixelMap)
47 {
48     CHKPP(pixelMap);
49     if (pixelMap->refCnt > 0) {
50         pixelMap->refCnt--;
51     }
52     if (pixelMap->refCnt > 0) {
53         return pixelMap;
54     }
55     delete pixelMap;
56     return nullptr;
57 }
58 
CPixelMapSerialize(const struct CPixelMap * pixelMap,CParcel * parcel)59 int32_t CPixelMapSerialize(const struct CPixelMap *pixelMap, CParcel *parcel)
60 {
61     CALL_DEBUG_ENTER;
62     CHKPR(parcel, RET_ERR);
63     CHKPR(parcel->parcel_, RET_ERR);
64     CHKPR(pixelMap, RET_ERR);
65     CHKPR(pixelMap->pixelMap, RET_ERR);
66     if (!pixelMap->pixelMap->Marshalling(*parcel->parcel_)) {
67         return RET_ERR;
68     }
69     return RET_OK;
70 }
71 
CPixelMapDeserialize(const CParcel * parcel)72 struct CPixelMap* CPixelMapDeserialize(const CParcel *parcel)
73 {
74     CALL_DEBUG_ENTER;
75     CHKPP(parcel);
76     CHKPP(parcel->parcel_);
77 
78     auto pixelMap = ::OHOS::Media::PixelMap::Unmarshalling(*parcel->parcel_);
79     CHKPP(pixelMap);
80     std::shared_ptr<::OHOS::Media::PixelMap> sPixelMap(pixelMap);
81 
82     if (sPixelMap->GetWidth() > MAX_PIXEL_MAP_WIDTH || sPixelMap->GetHeight() > MAX_PIXEL_MAP_HEIGHT) {
83         FI_HILOGE("Pixelmap is oversize, width:%{public}d, height:%{public}d",
84             sPixelMap->GetWidth(), sPixelMap->GetHeight());
85         return nullptr;
86     }
87     return CPixelMapFrom(sPixelMap);
88 }
89 } // namespace