1 /*
2 * Copyright (c) 2024-2024 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 <cstdint>
17 #include <unistd.h>
18 #include <sys/mman.h>
19 #include <iomanip>
20
21 #include <buffer_handle_parcel.h>
22 #include "camera_log.h"
23 #include "camera_photo_proxy.h"
24
25 namespace OHOS {
26 namespace CameraStandard {
27
CameraPhotoProxy()28 CameraPhotoProxy::CameraPhotoProxy()
29 {
30 MEDIA_INFO_LOG("CameraPhotoProxy no args");
31 format_ = 0;
32 photoId_ = "";
33 deferredProcType_ = 0;
34 photoWidth_ = 0;
35 photoHeight_ = 0;
36 isHighQuality_ = false;
37 bufferHandle_ = nullptr;
38 fileSize_ = 0;
39 isDeferredPhoto_ = 0;
40 longitude_ = -1.0;
41 latitude_ = -1.0;
42 captureId_ = 0;
43 burstSeqId_ = -1;
44 imageFormat_ = 0;
45 cloudImageEnhanceFlag_ = 0;
46 }
47
CameraPhotoProxy(BufferHandle * bufferHandle,int32_t format,int32_t photoWidth,int32_t photoHeight,bool isHighQuality,int32_t captureId)48 CameraPhotoProxy::CameraPhotoProxy(BufferHandle* bufferHandle, int32_t format,
49 int32_t photoWidth, int32_t photoHeight, bool isHighQuality, int32_t captureId)
50 {
51 MEDIA_INFO_LOG("CameraPhotoProxy");
52 bufferHandle_ = bufferHandle;
53 format_ = format;
54 photoWidth_ = photoWidth;
55 photoHeight_ = photoHeight;
56 fileSize_ = 0;
57 isHighQuality_ = isHighQuality;
58 deferredProcType_ = 0;
59 isDeferredPhoto_ = 0;
60 longitude_ = -1.0;
61 latitude_ = -1.0;
62 imageFormat_ = 0;
63 captureId_ = captureId;
64 burstSeqId_ = -1;
65 cloudImageEnhanceFlag_ = 0;
66 MEDIA_INFO_LOG("format = %{public}d, width = %{public}d, height = %{public}d",
67 format_, photoWidth, photoHeight);
68 }
69
CameraPhotoProxy(BufferHandle * bufferHandle,int32_t format,int32_t photoWidth,int32_t photoHeight,bool isHighQuality,int32_t captureId,int32_t burstSeqId)70 CameraPhotoProxy::CameraPhotoProxy(BufferHandle* bufferHandle, int32_t format,
71 int32_t photoWidth, int32_t photoHeight, bool isHighQuality, int32_t captureId, int32_t burstSeqId)
72 {
73 MEDIA_INFO_LOG("CameraPhotoProxy");
74 bufferHandle_ = bufferHandle;
75 format_ = format;
76 photoWidth_ = photoWidth;
77 photoHeight_ = photoHeight;
78 fileSize_ = 0;
79 isHighQuality_ = isHighQuality;
80 deferredProcType_ = 0;
81 isDeferredPhoto_ = 0;
82 longitude_ = -1.0;
83 latitude_ = -1.0;
84 captureId_ = captureId;
85 burstSeqId_ = burstSeqId;
86 imageFormat_ = 0;
87 cloudImageEnhanceFlag_ = 0;
88 MEDIA_INFO_LOG("format = %{public}d, width = %{public}d, height = %{public}d",
89 format_, photoWidth, photoHeight);
90 }
91
~CameraPhotoProxy()92 CameraPhotoProxy::~CameraPhotoProxy()
93 {
94 std::lock_guard<std::mutex> lock(mutex_);
95 MEDIA_INFO_LOG("~CameraPhotoProxy");
96 fileSize_ = 0;
97 }
98
ReadFromParcel(MessageParcel & parcel)99 void CameraPhotoProxy::ReadFromParcel(MessageParcel &parcel)
100 {
101 std::lock_guard<std::mutex> lock(mutex_);
102 photoId_ = parcel.ReadString();
103 deferredProcType_ = parcel.ReadInt32();
104 isDeferredPhoto_ = parcel.ReadInt32();
105 format_ = parcel.ReadInt32();
106 photoWidth_ = parcel.ReadInt32();
107 photoHeight_ = parcel.ReadInt32();
108 isHighQuality_ = parcel.ReadBool();
109 fileSize_ = parcel.ReadUint64();
110 latitude_ = parcel.ReadDouble();
111 longitude_ = parcel.ReadDouble();
112 captureId_ = parcel.ReadInt32();
113 burstSeqId_ = parcel.ReadInt32();
114 imageFormat_ = parcel.ReadInt32();
115 cloudImageEnhanceFlag_ = parcel.ReadUint32();
116 bufferHandle_ = ReadBufferHandle(parcel);
117 MEDIA_INFO_LOG("PhotoProxy::ReadFromParcel");
118 }
119
CameraFreeBufferHandle()120 int32_t CameraPhotoProxy::CameraFreeBufferHandle()
121 {
122 MEDIA_ERR_LOG("CameraFreeBufferHandle start");
123 std::lock_guard<std::mutex> lock(mutex_);
124 CHECK_ERROR_RETURN_RET_LOG(bufferHandle_ == nullptr, 0, "CameraFreeBufferHandle with nullptr handle");
125 if (bufferHandle_->fd >= 0) {
126 close(bufferHandle_->fd);
127 bufferHandle_->fd = -1;
128 }
129 const uint32_t reserveFds = bufferHandle_->reserveFds;
130 for (uint32_t i = 0; i < reserveFds; i++) {
131 if (bufferHandle_->reserve[i] >= 0) {
132 close(bufferHandle_->reserve[i]);
133 bufferHandle_->reserve[i] = -1;
134 }
135 }
136 free(bufferHandle_);
137 return 0;
138 }
139
WriteToParcel(MessageParcel & parcel)140 void CameraPhotoProxy::WriteToParcel(MessageParcel &parcel)
141 {
142 std::lock_guard<std::mutex> lock(mutex_);
143 parcel.WriteString(photoId_);
144 parcel.WriteInt32(deferredProcType_);
145 parcel.WriteInt32(isDeferredPhoto_);
146 parcel.WriteInt32(format_);
147 parcel.WriteInt32(photoWidth_);
148 parcel.WriteInt32(photoHeight_);
149 parcel.WriteBool(isHighQuality_);
150 parcel.WriteUint64(fileSize_);
151 parcel.WriteDouble(latitude_);
152 parcel.WriteDouble(longitude_);
153 parcel.WriteInt32(captureId_);
154 parcel.WriteInt32(burstSeqId_);
155 parcel.WriteInt32(imageFormat_);
156 parcel.WriteUint32(cloudImageEnhanceFlag_);
157 if (bufferHandle_) {
158 MEDIA_DEBUG_LOG("PhotoProxy::WriteToParcel %{public}d", bufferHandle_->fd);
159 bool ret = WriteBufferHandle(parcel, *bufferHandle_);
160 if (ret == false) {
161 MEDIA_ERR_LOG("Failure, Reason: WriteBufferHandle return false");
162 }
163 } else {
164 MEDIA_ERR_LOG("PhotoProxy::WriteToParcel without bufferHandle_");
165 }
166 MEDIA_INFO_LOG("PhotoProxy::WriteToParcel");
167 }
168
SetDeferredAttrs(std::string photoId,int32_t deferredProcType,uint64_t fileSize,int32_t imageFormat)169 void CameraPhotoProxy::SetDeferredAttrs(std::string photoId, int32_t deferredProcType,
170 uint64_t fileSize, int32_t imageFormat)
171 {
172 std::lock_guard<std::mutex> lock(mutex_);
173 isDeferredPhoto_ = 1;
174 photoId_ = photoId;
175 deferredProcType_ = deferredProcType;
176 fileSize_ = fileSize;
177 imageFormat_ = imageFormat;
178 }
179
SetLocation(double latitude,double longitude)180 void CameraPhotoProxy::SetLocation(double latitude, double longitude)
181 {
182 std::lock_guard<std::mutex> lock(mutex_);
183 latitude_ = latitude;
184 longitude_ = longitude;
185 }
186
SetCloudImageEnhanceFlag(uint32_t cloudImageEnhanceFlag)187 void CameraPhotoProxy::SetCloudImageEnhanceFlag(uint32_t cloudImageEnhanceFlag)
188 {
189 std::lock_guard<std::mutex> lock(mutex_);
190 MEDIA_DEBUG_LOG("%{public}s set value: %{public}u", __FUNCTION__, cloudImageEnhanceFlag);
191 cloudImageEnhanceFlag_ = cloudImageEnhanceFlag;
192 }
193
194 } // namespace CameraStandard
195 } // namespace OHOS
196