• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     format_ = format;
53     photoWidth_ = photoWidth;
54     photoHeight_ = photoHeight;
55     fileSize_ = 0;
56     bufferHandle_ = bufferHandle;
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 }
97 
ReadFromParcel(MessageParcel & parcel)98 void CameraPhotoProxy::ReadFromParcel(MessageParcel &parcel)
99 {
100     std::lock_guard<std::mutex> lock(mutex_);
101     photoId_ = parcel.ReadString();
102     deferredProcType_ = parcel.ReadInt32();
103     isDeferredPhoto_ = parcel.ReadInt32();
104     format_ = parcel.ReadInt32();
105     photoWidth_ = parcel.ReadInt32();
106     photoHeight_ = parcel.ReadInt32();
107     isHighQuality_ = parcel.ReadBool();
108     fileSize_ = parcel.ReadUint64();
109     latitude_ = parcel.ReadDouble();
110     longitude_ = parcel.ReadDouble();
111     captureId_ = parcel.ReadInt32();
112     burstSeqId_ = parcel.ReadInt32();
113     imageFormat_ = parcel.ReadInt32();
114     cloudImageEnhanceFlag_ = parcel.ReadUint32();
115     bufferHandle_ = ReadBufferHandle(parcel);
116     MEDIA_INFO_LOG("PhotoProxy::ReadFromParcel");
117 }
118 
CameraFreeBufferHandle()119 int32_t CameraPhotoProxy::CameraFreeBufferHandle()
120 {
121     MEDIA_ERR_LOG("CameraFreeBufferHandle start");
122     std::lock_guard<std::mutex> lock(mutex_);
123     CHECK_RETURN_RET_ELOG(bufferHandle_ == nullptr, 0, "CameraFreeBufferHandle with nullptr handle");
124     if (bufferHandle_->fd >= 0) {
125         close(bufferHandle_->fd);
126         bufferHandle_->fd = -1;
127     }
128     const uint32_t reserveFds = bufferHandle_->reserveFds;
129     for (uint32_t i = 0; i < reserveFds; i++) {
130         if (bufferHandle_->reserve[i] >= 0) {
131             close(bufferHandle_->reserve[i]);
132             bufferHandle_->reserve[i] = -1;
133         }
134     }
135     free(bufferHandle_);
136     return 0;
137 }
138 
WriteToParcel(MessageParcel & parcel) const139 void CameraPhotoProxy::WriteToParcel(MessageParcel &parcel) const
140 {
141     std::lock_guard<std::mutex> lock(mutex_);
142     parcel.WriteString(photoId_);
143     parcel.WriteInt32(deferredProcType_);
144     parcel.WriteInt32(isDeferredPhoto_);
145     parcel.WriteInt32(format_);
146     parcel.WriteInt32(photoWidth_);
147     parcel.WriteInt32(photoHeight_);
148     parcel.WriteBool(isHighQuality_);
149     parcel.WriteUint64(fileSize_);
150     parcel.WriteDouble(latitude_);
151     parcel.WriteDouble(longitude_);
152     parcel.WriteInt32(captureId_);
153     parcel.WriteInt32(burstSeqId_);
154     parcel.WriteInt32(imageFormat_);
155     parcel.WriteUint32(cloudImageEnhanceFlag_);
156     if (bufferHandle_) {
157         MEDIA_DEBUG_LOG("PhotoProxy::WriteToParcel %{public}d", bufferHandle_->fd);
158         bool ret = WriteBufferHandle(parcel, *bufferHandle_);
159         if (ret == false) {
160             MEDIA_ERR_LOG("Failure, Reason: WriteBufferHandle return false");
161         }
162     } else {
163         MEDIA_ERR_LOG("PhotoProxy::WriteToParcel without bufferHandle_");
164     }
165     MEDIA_INFO_LOG("PhotoProxy::WriteToParcel");
166 }
167 
Marshalling(Parcel & parcel) const168 bool CameraPhotoProxy::Marshalling(Parcel &parcel) const
169 {
170     WriteToParcel(static_cast<MessageParcel&>(parcel));
171     return true;
172 }
173 
Unmarshalling(Parcel & parcel)174 CameraPhotoProxy* CameraPhotoProxy::Unmarshalling(Parcel &parcel)
175 {
176     CameraPhotoProxy* cameraPhotoProxy = new (std::nothrow) CameraPhotoProxy();
177     CHECK_RETURN_RET_ELOG(cameraPhotoProxy == nullptr, nullptr, "create CameraPhotoProxy fail");
178     cameraPhotoProxy->ReadFromParcel(static_cast<MessageParcel&>(parcel));
179     return cameraPhotoProxy;
180 }
181 
SetDeferredAttrs(std::string photoId,int32_t deferredProcType,uint64_t fileSize,int32_t imageFormat)182 void CameraPhotoProxy::SetDeferredAttrs(std::string photoId, int32_t deferredProcType,
183     uint64_t fileSize, int32_t imageFormat)
184 {
185     std::lock_guard<std::mutex> lock(mutex_);
186     isDeferredPhoto_ = 1;
187     photoId_ = photoId;
188     deferredProcType_ = deferredProcType;
189     fileSize_ = fileSize;
190     imageFormat_ = imageFormat;
191 }
192 
SetLocation(double latitude,double longitude)193 void CameraPhotoProxy::SetLocation(double latitude, double longitude)
194 {
195     std::lock_guard<std::mutex> lock(mutex_);
196     latitude_ = latitude;
197     longitude_ = longitude;
198 }
199 
SetCloudImageEnhanceFlag(uint32_t cloudImageEnhanceFlag)200 void CameraPhotoProxy::SetCloudImageEnhanceFlag(uint32_t cloudImageEnhanceFlag)
201 {
202     std::lock_guard<std::mutex> lock(mutex_);
203     MEDIA_DEBUG_LOG("%{public}s set value: %{public}u", __FUNCTION__, cloudImageEnhanceFlag);
204     cloudImageEnhanceFlag_ = cloudImageEnhanceFlag;
205 }
206 
207 } // namespace CameraStandard
208 } // namespace OHOS
209