xref: /aosp_15_r20/frameworks/av/camera/camera2/CaptureRequest.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2 **
3 ** Copyright 2013, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 // #define LOG_NDEBUG 0
19 #define LOG_TAG "CameraRequest"
20 #include <utils/Log.h>
21 #include <utils/String16.h>
22 
23 #include <camera/camera2/CaptureRequest.h>
24 #include <camera/StringUtils.h>
25 
26 #include <binder/Parcel.h>
27 #include <gui/Flags.h>  // remove with WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
28 #include <gui/Surface.h>
29 #include <gui/view/Surface.h>
30 
31 namespace android {
32 namespace hardware {
33 namespace camera2 {
34 
35 // These must be in the .cpp (to avoid inlining)
36 CaptureRequest::CaptureRequest() = default;
37 CaptureRequest::~CaptureRequest() = default;
38 CaptureRequest::CaptureRequest(const CaptureRequest& rhs) = default;
39 CaptureRequest::CaptureRequest(CaptureRequest&& rhs) noexcept = default;
40 
41 
readFromParcel(const android::Parcel * parcel)42 status_t CaptureRequest::readFromParcel(const android::Parcel* parcel) {
43     if (parcel == NULL) {
44         ALOGE("%s: Null parcel", __FUNCTION__);
45         return BAD_VALUE;
46     }
47 
48     mSurfaceList.clear();
49     mStreamIdxList.clear();
50     mSurfaceIdxList.clear();
51     mPhysicalCameraSettings.clear();
52 
53     status_t err = OK;
54 
55     int32_t settingsCount;
56     if ((err = parcel->readInt32(&settingsCount)) != OK) {
57         ALOGE("%s: Failed to read the settings count from parcel: %d", __FUNCTION__, err);
58         return err;
59     }
60 
61     if (settingsCount <= 0) {
62         ALOGE("%s: Settings count %d should always be positive!", __FUNCTION__, settingsCount);
63         return BAD_VALUE;
64     }
65 
66     for (int32_t i = 0; i < settingsCount; i++) {
67         String16 id;
68         if ((err = parcel->readString16(&id)) != OK) {
69             ALOGE("%s: Failed to read camera id!", __FUNCTION__);
70             return BAD_VALUE;
71         }
72 
73         CameraMetadata settings;
74         if ((err = settings.readFromParcel(parcel)) != OK) {
75             ALOGE("%s: Failed to read metadata from parcel", __FUNCTION__);
76             return err;
77         }
78         ALOGV("%s: Read metadata from parcel", __FUNCTION__);
79         mPhysicalCameraSettings.push_back({toStdString(id), settings});
80     }
81 
82     int isReprocess = 0;
83     if ((err = parcel->readInt32(&isReprocess)) != OK) {
84         ALOGE("%s: Failed to read reprocessing from parcel", __FUNCTION__);
85         return err;
86     }
87     mIsReprocess = (isReprocess != 0);
88 
89     int32_t size;
90     if ((err = parcel->readInt32(&size)) != OK) {
91         ALOGE("%s: Failed to read surface list size from parcel", __FUNCTION__);
92         return err;
93     }
94     ALOGV("%s: Read surface list size = %d", __FUNCTION__, size);
95 
96     // Do not distinguish null arrays from 0-sized arrays.
97     for (int32_t i = 0; i < size; ++i) {
98         // Parcel.writeParcelableArray
99         std::optional<std::string> className;
100         parcel->readUtf8FromUtf16(&className);
101         ALOGV("%s: Read surface class = %s", __FUNCTION__,
102               className.value_or("<null>").c_str());
103 
104         if (className == std::nullopt) {
105             continue;
106         }
107 
108         // Surface.writeToParcel
109         view::Surface surfaceShim;
110         if ((err = surfaceShim.readFromParcel(parcel)) != OK) {
111             ALOGE("%s: Failed to read output target Surface %d from parcel: %s (%d)",
112                     __FUNCTION__, i, strerror(-err), err);
113             return err;
114         }
115 
116 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
117         sp<Surface> surface = surfaceShim.toSurface();
118 #else
119         sp<Surface> surface;
120         if (surfaceShim.graphicBufferProducer != NULL) {
121             surface = new Surface(surfaceShim.graphicBufferProducer);
122         }
123 #endif
124         mSurfaceList.push_back(surface);
125     }
126 
127     int32_t streamSurfaceSize;
128     if ((err = parcel->readInt32(&streamSurfaceSize)) != OK) {
129         ALOGE("%s: Failed to read streamSurfaceSize from parcel", __FUNCTION__);
130         return err;
131     }
132 
133     if (streamSurfaceSize < 0) {
134         ALOGE("%s: Bad streamSurfaceSize %d from parcel", __FUNCTION__, streamSurfaceSize);
135         return BAD_VALUE;
136     }
137 
138     for (int32_t i = 0; i < streamSurfaceSize; ++i) {
139         int streamIdx;
140         if ((err = parcel->readInt32(&streamIdx)) != OK) {
141             ALOGE("%s: Failed to read stream index from parcel", __FUNCTION__);
142             return err;
143         }
144         mStreamIdxList.push_back(streamIdx);
145 
146         int surfaceIdx;
147         if ((err = parcel->readInt32(&surfaceIdx)) != OK) {
148             ALOGE("%s: Failed to read surface index from parcel", __FUNCTION__);
149             return err;
150         }
151         mSurfaceIdxList.push_back(surfaceIdx);
152     }
153 
154     int32_t hasUserTag;
155     if ((err = parcel->readInt32(&hasUserTag)) != OK) {
156         ALOGE("%s: Failed to read user tag availability flag", __FUNCTION__);
157         return BAD_VALUE;
158     }
159     if (hasUserTag) {
160         String16 userTag;
161         if ((err = parcel->readString16(&userTag)) != OK) {
162             ALOGE("%s: Failed to read user tag!", __FUNCTION__);
163             return BAD_VALUE;
164         }
165         mUserTag = toStdString(userTag);
166     }
167 
168     return OK;
169 }
170 
writeToParcel(android::Parcel * parcel) const171 status_t CaptureRequest::writeToParcel(android::Parcel* parcel) const {
172     if (parcel == NULL) {
173         ALOGE("%s: Null parcel", __FUNCTION__);
174         return BAD_VALUE;
175     }
176 
177     status_t err = OK;
178 
179     int32_t settingsCount = static_cast<int32_t>(mPhysicalCameraSettings.size());
180 
181     if ((err = parcel->writeInt32(settingsCount)) != OK) {
182         ALOGE("%s: Failed to write settings count!", __FUNCTION__);
183         return err;
184     }
185 
186     for (const auto &it : mPhysicalCameraSettings) {
187         if ((err = parcel->writeString16(toString16(it.id))) != OK) {
188             ALOGE("%s: Failed to camera id!", __FUNCTION__);
189             return err;
190         }
191 
192         if ((err = it.settings.writeToParcel(parcel)) != OK) {
193             ALOGE("%s: Failed to write settings!", __FUNCTION__);
194             return err;
195         }
196     }
197 
198     parcel->writeInt32(mIsReprocess ? 1 : 0);
199 
200     if (mSurfaceConverted) {
201         parcel->writeInt32(0); // 0-sized array
202     } else {
203         int32_t size = static_cast<int32_t>(mSurfaceList.size());
204 
205         // Send 0-sized arrays when it's empty. Do not send null arrays.
206         parcel->writeInt32(size);
207 
208         for (int32_t i = 0; i < size; ++i) {
209             // not sure if readParcelableArray does this, hard to tell from source
210             parcel->writeString16(String16("android.view.Surface"));
211 
212             // Surface.writeToParcel
213 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
214             view::Surface surfaceShim = view::Surface::fromSurface(mSurfaceList[i]);
215 #else
216             view::Surface surfaceShim;
217             surfaceShim.name = String16("unknown_name");
218             surfaceShim.graphicBufferProducer = mSurfaceList[i]->getIGraphicBufferProducer();
219 #endif
220             if ((err = surfaceShim.writeToParcel(parcel)) != OK) {
221                 ALOGE("%s: Failed to write output target Surface %d to parcel: %s (%d)",
222                         __FUNCTION__, i, strerror(-err), err);
223                 return err;
224             }
225         }
226     }
227 
228     parcel->writeInt32(mStreamIdxList.size());
229     for (size_t i = 0; i < mStreamIdxList.size(); ++i) {
230         if ((err = parcel->writeInt32(mStreamIdxList[i])) != OK) {
231             ALOGE("%s: Failed to write stream index to parcel", __FUNCTION__);
232             return err;
233         }
234         if ((err = parcel->writeInt32(mSurfaceIdxList[i])) != OK) {
235             ALOGE("%s: Failed to write surface index to parcel", __FUNCTION__);
236             return err;
237         }
238     }
239 
240     if (mUserTag.empty()) {
241         parcel->writeInt32(0);
242     } else {
243         parcel->writeInt32(1);
244         parcel->writeString16(toString16(mUserTag));
245     }
246 
247     return OK;
248 }
249 
250 } // namespace camera2
251 } // namespace hardware
252 } // namespace android
253