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