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