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/Surface.h>
28 #include <gui/view/Surface.h>
29 
30 namespace android {
31 namespace hardware {
32 namespace camera2 {
33 
34 // These must be in the .cpp (to avoid inlining)
35 CaptureRequest::CaptureRequest() = default;
36 CaptureRequest::~CaptureRequest() = default;
37 CaptureRequest::CaptureRequest(const CaptureRequest& rhs) = default;
38 CaptureRequest::CaptureRequest(CaptureRequest&& rhs) noexcept = default;
39 
40 
readFromParcel(const android::Parcel * parcel)41 status_t CaptureRequest::readFromParcel(const android::Parcel* parcel) {
42     if (parcel == NULL) {
43         ALOGE("%s: Null parcel", __FUNCTION__);
44         return BAD_VALUE;
45     }
46 
47     mSurfaceList.clear();
48     mStreamIdxList.clear();
49     mSurfaceIdxList.clear();
50     mPhysicalCameraSettings.clear();
51 
52     status_t err = OK;
53 
54     int32_t settingsCount;
55     if ((err = parcel->readInt32(&settingsCount)) != OK) {
56         ALOGE("%s: Failed to read the settings count from parcel: %d", __FUNCTION__, err);
57         return err;
58     }
59 
60     if (settingsCount <= 0) {
61         ALOGE("%s: Settings count %d should always be positive!", __FUNCTION__, settingsCount);
62         return BAD_VALUE;
63     }
64 
65     for (int32_t i = 0; i < settingsCount; i++) {
66         String16 id;
67         if ((err = parcel->readString16(&id)) != OK) {
68             ALOGE("%s: Failed to read camera id!", __FUNCTION__);
69             return BAD_VALUE;
70         }
71 
72         CameraMetadata settings;
73         if ((err = settings.readFromParcel(parcel)) != OK) {
74             ALOGE("%s: Failed to read metadata from parcel", __FUNCTION__);
75             return err;
76         }
77         ALOGV("%s: Read metadata from parcel", __FUNCTION__);
78         mPhysicalCameraSettings.push_back({toStdString(id), settings});
79     }
80 
81     int isReprocess = 0;
82     if ((err = parcel->readInt32(&isReprocess)) != OK) {
83         ALOGE("%s: Failed to read reprocessing from parcel", __FUNCTION__);
84         return err;
85     }
86     mIsReprocess = (isReprocess != 0);
87 
88     int32_t size;
89     if ((err = parcel->readInt32(&size)) != OK) {
90         ALOGE("%s: Failed to read surface list size from parcel", __FUNCTION__);
91         return err;
92     }
93     ALOGV("%s: Read surface list size = %d", __FUNCTION__, size);
94 
95     // Do not distinguish null arrays from 0-sized arrays.
96     for (int32_t i = 0; i < size; ++i) {
97         // Parcel.writeParcelableArray
98         std::optional<std::string> className;
99         parcel->readUtf8FromUtf16(&className);
100         ALOGV("%s: Read surface class = %s", __FUNCTION__,
101               className.value_or("<null>").c_str());
102 
103         if (className == std::nullopt) {
104             continue;
105         }
106 
107         // Surface.writeToParcel
108         view::Surface surfaceShim;
109         if ((err = surfaceShim.readFromParcel(parcel)) != OK) {
110             ALOGE("%s: Failed to read output target Surface %d from parcel: %s (%d)",
111                     __FUNCTION__, i, strerror(-err), err);
112             return err;
113         }
114 
115         sp<Surface> surface;
116         if (surfaceShim.graphicBufferProducer != NULL) {
117             surface = new Surface(surfaceShim.graphicBufferProducer);
118         }
119 
120         mSurfaceList.push_back(surface);
121     }
122 
123     int32_t streamSurfaceSize;
124     if ((err = parcel->readInt32(&streamSurfaceSize)) != OK) {
125         ALOGE("%s: Failed to read streamSurfaceSize from parcel", __FUNCTION__);
126         return err;
127     }
128 
129     if (streamSurfaceSize < 0) {
130         ALOGE("%s: Bad streamSurfaceSize %d from parcel", __FUNCTION__, streamSurfaceSize);
131         return BAD_VALUE;
132     }
133 
134     for (int32_t i = 0; i < streamSurfaceSize; ++i) {
135         int streamIdx;
136         if ((err = parcel->readInt32(&streamIdx)) != OK) {
137             ALOGE("%s: Failed to read stream index from parcel", __FUNCTION__);
138             return err;
139         }
140         mStreamIdxList.push_back(streamIdx);
141 
142         int surfaceIdx;
143         if ((err = parcel->readInt32(&surfaceIdx)) != OK) {
144             ALOGE("%s: Failed to read surface index from parcel", __FUNCTION__);
145             return err;
146         }
147         mSurfaceIdxList.push_back(surfaceIdx);
148     }
149 
150     int32_t hasUserTag;
151     if ((err = parcel->readInt32(&hasUserTag)) != OK) {
152         ALOGE("%s: Failed to read user tag availability flag", __FUNCTION__);
153         return BAD_VALUE;
154     }
155     if (hasUserTag) {
156         String16 userTag;
157         if ((err = parcel->readString16(&userTag)) != OK) {
158             ALOGE("%s: Failed to read user tag!", __FUNCTION__);
159             return BAD_VALUE;
160         }
161         mUserTag = toStdString(userTag);
162     }
163 
164     return OK;
165 }
166 
writeToParcel(android::Parcel * parcel) const167 status_t CaptureRequest::writeToParcel(android::Parcel* parcel) const {
168     if (parcel == NULL) {
169         ALOGE("%s: Null parcel", __FUNCTION__);
170         return BAD_VALUE;
171     }
172 
173     status_t err = OK;
174 
175     int32_t settingsCount = static_cast<int32_t>(mPhysicalCameraSettings.size());
176 
177     if ((err = parcel->writeInt32(settingsCount)) != OK) {
178         ALOGE("%s: Failed to write settings count!", __FUNCTION__);
179         return err;
180     }
181 
182     for (const auto &it : mPhysicalCameraSettings) {
183         if ((err = parcel->writeString16(toString16(it.id))) != OK) {
184             ALOGE("%s: Failed to camera id!", __FUNCTION__);
185             return err;
186         }
187 
188         if ((err = it.settings.writeToParcel(parcel)) != OK) {
189             ALOGE("%s: Failed to write settings!", __FUNCTION__);
190             return err;
191         }
192     }
193 
194     parcel->writeInt32(mIsReprocess ? 1 : 0);
195 
196     if (mSurfaceConverted) {
197         parcel->writeInt32(0); // 0-sized array
198     } else {
199         int32_t size = static_cast<int32_t>(mSurfaceList.size());
200 
201         // Send 0-sized arrays when it's empty. Do not send null arrays.
202         parcel->writeInt32(size);
203 
204         for (int32_t i = 0; i < size; ++i) {
205             // not sure if readParcelableArray does this, hard to tell from source
206             parcel->writeString16(String16("android.view.Surface"));
207 
208             // Surface.writeToParcel
209             view::Surface surfaceShim;
210             surfaceShim.name = String16("unknown_name");
211             surfaceShim.graphicBufferProducer = mSurfaceList[i]->getIGraphicBufferProducer();
212             if ((err = surfaceShim.writeToParcel(parcel)) != OK) {
213                 ALOGE("%s: Failed to write output target Surface %d to parcel: %s (%d)",
214                         __FUNCTION__, i, strerror(-err), err);
215                 return err;
216             }
217         }
218     }
219 
220     parcel->writeInt32(mStreamIdxList.size());
221     for (size_t i = 0; i < mStreamIdxList.size(); ++i) {
222         if ((err = parcel->writeInt32(mStreamIdxList[i])) != OK) {
223             ALOGE("%s: Failed to write stream index to parcel", __FUNCTION__);
224             return err;
225         }
226         if ((err = parcel->writeInt32(mSurfaceIdxList[i])) != OK) {
227             ALOGE("%s: Failed to write surface index to parcel", __FUNCTION__);
228             return err;
229         }
230     }
231 
232     if (mUserTag.empty()) {
233         parcel->writeInt32(0);
234     } else {
235         parcel->writeInt32(1);
236         parcel->writeString16(toString16(mUserTag));
237     }
238 
239     return OK;
240 }
241 
242 } // namespace camera2
243 } // namespace hardware
244 } // namespace android
245