xref: /aosp_15_r20/frameworks/av/camera/CaptureResult.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "Camera-CaptureResult"
18 #include <utils/Log.h>
19 
20 #include <camera/CaptureResult.h>
21 #include <camera/StringUtils.h>
22 #include <binder/Parcel.h>
23 
24 namespace android {
25 
isValid()26 bool CaptureResultExtras::isValid() {
27     return requestId >= 0;
28 }
29 
readFromParcel(const android::Parcel * parcel)30 status_t CaptureResultExtras::readFromParcel(const android::Parcel *parcel) {
31     if (parcel == NULL) {
32         ALOGE("%s: Null parcel", __FUNCTION__);
33         return BAD_VALUE;
34     }
35 
36     parcel->readInt32(&requestId);
37     parcel->readInt32(&burstId);
38     parcel->readInt32(&afTriggerId);
39     parcel->readInt32(&precaptureTriggerId);
40     parcel->readInt64(&frameNumber);
41     parcel->readInt32(&partialResultCount);
42     parcel->readInt32(&errorStreamId);
43     auto physicalCameraIdPresent = parcel->readBool();
44     if (physicalCameraIdPresent) {
45         String16 cameraId;
46         status_t res = OK;
47         if ((res = parcel->readString16(&cameraId)) != OK) {
48             ALOGE("%s: Failed to read camera id: %d", __FUNCTION__, res);
49             return res;
50         }
51         errorPhysicalCameraId = toStdString(cameraId);
52     }
53     parcel->readInt64(&lastCompletedRegularFrameNumber);
54     parcel->readInt64(&lastCompletedReprocessFrameNumber);
55     parcel->readInt64(&lastCompletedZslFrameNumber);
56     parcel->readBool(&hasReadoutTimestamp);
57     if (hasReadoutTimestamp) {
58         parcel->readInt64(&readoutTimestamp);
59     }
60     return OK;
61 }
62 
writeToParcel(android::Parcel * parcel) const63 status_t CaptureResultExtras::writeToParcel(android::Parcel *parcel) const {
64     if (parcel == NULL) {
65         ALOGE("%s: Null parcel", __FUNCTION__);
66         return BAD_VALUE;
67     }
68 
69     parcel->writeInt32(requestId);
70     parcel->writeInt32(burstId);
71     parcel->writeInt32(afTriggerId);
72     parcel->writeInt32(precaptureTriggerId);
73     parcel->writeInt64(frameNumber);
74     parcel->writeInt32(partialResultCount);
75     parcel->writeInt32(errorStreamId);
76     if (errorPhysicalCameraId.size() > 0) {
77         parcel->writeBool(true);
78         status_t res = OK;
79         if ((res = parcel->writeString16(toString16(errorPhysicalCameraId))) != OK) {
80             ALOGE("%s: Failed to write physical camera ID to parcel: %d", __FUNCTION__, res);
81             return res;
82         }
83     } else {
84         parcel->writeBool(false);
85     }
86     parcel->writeInt64(lastCompletedRegularFrameNumber);
87     parcel->writeInt64(lastCompletedReprocessFrameNumber);
88     parcel->writeInt64(lastCompletedZslFrameNumber);
89     parcel->writeBool(hasReadoutTimestamp);
90     if (hasReadoutTimestamp) {
91         parcel->writeInt64(readoutTimestamp);
92     }
93 
94     return OK;
95 }
96 
readFromParcel(const android::Parcel * parcel)97 status_t PhysicalCaptureResultInfo::readFromParcel(const android::Parcel* parcel) {
98     status_t res;
99 
100     mPhysicalCameraId = "";
101 
102     String16 physicalCameraId;
103     if ((res = parcel->readString16(&physicalCameraId)) != OK) {
104         ALOGE("%s: Failed to read camera id: %d", __FUNCTION__, res);
105         return res;
106     }
107     mPhysicalCameraId = toStdString(physicalCameraId);
108 
109     if ((res = mCameraMetadataInfo.readFromParcel(parcel)) != OK) {
110         ALOGE("%s: Failed to read metadata from parcel: %d", __FUNCTION__, res);
111         return res;
112     }
113 
114     return OK;
115 }
116 
writeToParcel(android::Parcel * parcel) const117 status_t PhysicalCaptureResultInfo::writeToParcel(android::Parcel* parcel) const {
118     status_t res;
119     if ((res = parcel->writeString16(toString16(mPhysicalCameraId))) != OK) {
120         ALOGE("%s: Failed to write physical camera ID to parcel: %d",
121                 __FUNCTION__, res);
122         return res;
123     }
124 
125     if ((res = mCameraMetadataInfo.writeToParcel(parcel)) != OK) {
126         ALOGE("%s: Failed to write physical camera metadata to parcel: %d",
127                 __FUNCTION__, res);
128         return res;
129     }
130 
131     return OK;
132 }
133 
CaptureResult()134 CaptureResult::CaptureResult() :
135         mMetadata(), mResultExtras() {
136 }
137 
CaptureResult(CaptureResult && otherResult)138 CaptureResult::CaptureResult(CaptureResult &&otherResult) {
139     mMetadata = std::move(otherResult.mMetadata);
140     mResultExtras = otherResult.mResultExtras;
141     mPhysicalMetadatas = std::move(otherResult.mPhysicalMetadatas);
142 }
143 
CaptureResult(const CaptureResult & otherResult)144 CaptureResult::CaptureResult(const CaptureResult &otherResult) {
145     mResultExtras = otherResult.mResultExtras;
146     mMetadata = otherResult.mMetadata;
147     mPhysicalMetadatas = otherResult.mPhysicalMetadatas;
148 }
149 
readFromParcel(android::Parcel * parcel)150 status_t CaptureResult::readFromParcel(android::Parcel *parcel) {
151 
152     ALOGV("%s: parcel = %p", __FUNCTION__, parcel);
153 
154     if (parcel == NULL) {
155         ALOGE("%s: parcel is null", __FUNCTION__);
156         return BAD_VALUE;
157     }
158 
159     mMetadata.clear();
160     mPhysicalMetadatas.clear();
161 
162     status_t res = OK;
163     res = mMetadata.readFromParcel(parcel);
164     if (res != OK) {
165         ALOGE("%s: Failed to read metadata from parcel.",
166               __FUNCTION__);
167         return res;
168     }
169     ALOGV("%s: Read metadata from parcel", __FUNCTION__);
170 
171     int32_t physicalMetadataCount;
172     if ((res = parcel->readInt32(&physicalMetadataCount)) != OK) {
173         ALOGE("%s: Failed to read the physical metadata count from parcel: %d", __FUNCTION__, res);
174         return res;
175     }
176     if (physicalMetadataCount < 0) {
177         ALOGE("%s: Invalid physical metadata count from parcel: %d",
178                 __FUNCTION__, physicalMetadataCount);
179         return BAD_VALUE;
180     }
181 
182     for (int32_t i = 0; i < physicalMetadataCount; i++) {
183         PhysicalCaptureResultInfo result;
184         if ((res = result.readFromParcel(parcel)) != OK) {
185             ALOGE("%s: Failed to read physical result from parcel: %d", __FUNCTION__, res);
186             return res;
187         }
188         mPhysicalMetadatas.emplace(mPhysicalMetadatas.end(), result);
189     }
190     ALOGV("%s: Read physical metadata from parcel", __FUNCTION__);
191 
192     res = mResultExtras.readFromParcel(parcel);
193     if (res != OK) {
194         ALOGE("%s: Failed to read result extras from parcel.",
195                 __FUNCTION__);
196         return res;
197     }
198     ALOGV("%s: Read result extras from parcel", __FUNCTION__);
199 
200     return OK;
201 }
202 
writeToParcel(android::Parcel * parcel) const203 status_t CaptureResult::writeToParcel(android::Parcel *parcel) const {
204 
205     ALOGV("%s: parcel = %p", __FUNCTION__, parcel);
206 
207     if (parcel == NULL) {
208         ALOGE("%s: parcel is null", __FUNCTION__);
209         return BAD_VALUE;
210     }
211 
212     status_t res;
213 
214     res = mMetadata.writeToParcel(parcel);
215     if (res != OK) {
216         ALOGE("%s: Failed to write metadata to parcel", __FUNCTION__);
217         return res;
218     }
219     ALOGV("%s: Wrote metadata to parcel", __FUNCTION__);
220 
221     int32_t physicalMetadataCount = static_cast<int32_t>(mPhysicalMetadatas.size());
222     res = parcel->writeInt32(physicalMetadataCount);
223     if (res != OK) {
224         ALOGE("%s: Failed to write physical metadata count to parcel: %d",
225                 __FUNCTION__, res);
226         return BAD_VALUE;
227     }
228     for (const auto& physicalMetadata : mPhysicalMetadatas) {
229         if ((res = physicalMetadata.writeToParcel(parcel)) != OK) {
230             ALOGE("%s: Failed to write physicalMetadata to parcel: %d",
231                     __FUNCTION__, res);
232             return res;
233         }
234     }
235     ALOGV("%s: Wrote physical camera metadata to parcel", __FUNCTION__);
236 
237     res = mResultExtras.writeToParcel(parcel);
238     if (res != OK) {
239         ALOGE("%s: Failed to write result extras to parcel", __FUNCTION__);
240         return res;
241     }
242     ALOGV("%s: Wrote result extras to parcel", __FUNCTION__);
243 
244     return OK;
245 }
246 
247 }
248