1/*
2**
3** Copyright 2015-2018, 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_TAG "OutputConfiguration"
19//#define LOG_NDEBUG 0
20
21#include <utils/Log.h>
22
23#include <camera/camera2/OutputConfiguration.h>
24#include <binder/Parcel.h>
25#include <gui/view/Surface.h>
26#include <utils/String8.h>
27
28namespace android {
29
30
31const int OutputConfiguration::INVALID_ROTATION = -1;
32const int OutputConfiguration::INVALID_SET_ID = -1;
33
34const std::vector<sp<IGraphicBufferProducer>>&
35        OutputConfiguration::getGraphicBufferProducers() const {
36    return mGbps;
37}
38
39int OutputConfiguration::getRotation() const {
40    return mRotation;
41}
42
43int OutputConfiguration::getSurfaceSetID() const {
44    return mSurfaceSetID;
45}
46
47int OutputConfiguration::getSurfaceType() const {
48    return mSurfaceType;
49}
50
51int OutputConfiguration::getWidth() const {
52    return mWidth;
53}
54
55int OutputConfiguration::getHeight() const {
56    return mHeight;
57}
58
59bool OutputConfiguration::isDeferred() const {
60    return mIsDeferred;
61}
62
63bool OutputConfiguration::isShared() const {
64    return mIsShared;
65}
66
67String16 OutputConfiguration::getPhysicalCameraId() const {
68    return mPhysicalCameraId;
69}
70
71OutputConfiguration::OutputConfiguration() :
72        mRotation(INVALID_ROTATION),
73        mSurfaceSetID(INVALID_SET_ID),
74        mSurfaceType(SURFACE_TYPE_UNKNOWN),
75        mWidth(0),
76        mHeight(0),
77        mIsDeferred(false),
78        mIsShared(false) {
79}
80
81OutputConfiguration::OutputConfiguration(const android::Parcel& parcel) :
82        mRotation(INVALID_ROTATION),
83        mSurfaceSetID(INVALID_SET_ID) {
84    readFromParcel(&parcel);
85}
86
87status_t OutputConfiguration::readFromParcel(const android::Parcel* parcel) {
88    status_t err = OK;
89    int rotation = 0;
90
91    if (parcel == nullptr) return BAD_VALUE;
92
93    if ((err = parcel->readInt32(&rotation)) != OK) {
94        ALOGE("%s: Failed to read rotation from parcel", __FUNCTION__);
95        return err;
96    }
97
98    int setID = INVALID_SET_ID;
99    if ((err = parcel->readInt32(&setID)) != OK) {
100        ALOGE("%s: Failed to read surface set ID from parcel", __FUNCTION__);
101        return err;
102    }
103
104    int surfaceType = SURFACE_TYPE_UNKNOWN;
105    if ((err = parcel->readInt32(&surfaceType)) != OK) {
106        ALOGE("%s: Failed to read surface type from parcel", __FUNCTION__);
107        return err;
108    }
109
110    int width = 0;
111    if ((err = parcel->readInt32(&width)) != OK) {
112        ALOGE("%s: Failed to read surface width from parcel", __FUNCTION__);
113        return err;
114    }
115
116    int height = 0;
117    if ((err = parcel->readInt32(&height)) != OK) {
118        ALOGE("%s: Failed to read surface height from parcel", __FUNCTION__);
119        return err;
120    }
121
122    int isDeferred = 0;
123    if ((err = parcel->readInt32(&isDeferred)) != OK) {
124        ALOGE("%s: Failed to read surface isDeferred flag from parcel", __FUNCTION__);
125        return err;
126    }
127
128    int isShared = 0;
129    if ((err = parcel->readInt32(&isShared)) != OK) {
130        ALOGE("%s: Failed to read surface isShared flag from parcel", __FUNCTION__);
131        return err;
132    }
133
134    if (isDeferred && surfaceType != SURFACE_TYPE_SURFACE_VIEW &&
135            surfaceType != SURFACE_TYPE_SURFACE_TEXTURE) {
136        ALOGE("%s: Invalid surface type for deferred configuration", __FUNCTION__);
137        return BAD_VALUE;
138    }
139
140    std::vector<view::Surface> surfaceShims;
141    if ((err = parcel->readParcelableVector(&surfaceShims)) != OK) {
142        ALOGE("%s: Failed to read surface(s) from parcel", __FUNCTION__);
143        return err;
144    }
145
146    parcel->readString16(&mPhysicalCameraId);
147
148    mRotation = rotation;
149    mSurfaceSetID = setID;
150    mSurfaceType = surfaceType;
151    mWidth = width;
152    mHeight = height;
153    mIsDeferred = isDeferred != 0;
154    mIsShared = isShared != 0;
155    for (auto& surface : surfaceShims) {
156        ALOGV("%s: OutputConfiguration: %p, name %s", __FUNCTION__,
157                surface.graphicBufferProducer.get(),
158                String8(surface.name).string());
159        mGbps.push_back(surface.graphicBufferProducer);
160    }
161
162    ALOGV("%s: OutputConfiguration: rotation = %d, setId = %d, surfaceType = %d,"
163          " physicalCameraId = %s", __FUNCTION__, mRotation, mSurfaceSetID,
164          mSurfaceType, String8(mPhysicalCameraId).string());
165
166    return err;
167}
168
169OutputConfiguration::OutputConfiguration(sp<IGraphicBufferProducer>& gbp, int rotation,
170        int surfaceSetID, bool isShared) {
171    mGbps.push_back(gbp);
172    mRotation = rotation;
173    mSurfaceSetID = surfaceSetID;
174    mIsDeferred = false;
175    mIsShared = isShared;
176}
177
178status_t OutputConfiguration::writeToParcel(android::Parcel* parcel) const {
179
180    if (parcel == nullptr) return BAD_VALUE;
181    status_t err = OK;
182
183    err = parcel->writeInt32(mRotation);
184    if (err != OK) return err;
185
186    err = parcel->writeInt32(mSurfaceSetID);
187    if (err != OK) return err;
188
189    err = parcel->writeInt32(mSurfaceType);
190    if (err != OK) return err;
191
192    err = parcel->writeInt32(mWidth);
193    if (err != OK) return err;
194
195    err = parcel->writeInt32(mHeight);
196    if (err != OK) return err;
197
198    err = parcel->writeInt32(mIsDeferred ? 1 : 0);
199    if (err != OK) return err;
200
201    err = parcel->writeInt32(mIsShared ? 1 : 0);
202    if (err != OK) return err;
203
204    std::vector<view::Surface> surfaceShims;
205    for (auto& gbp : mGbps) {
206        view::Surface surfaceShim;
207        surfaceShim.name = String16("unknown_name"); // name of surface
208        surfaceShim.graphicBufferProducer = gbp;
209        surfaceShims.push_back(surfaceShim);
210    }
211    err = parcel->writeParcelableVector(surfaceShims);
212    if (err != OK) return err;
213
214    err = parcel->writeString16(mPhysicalCameraId);
215    if (err != OK) return err;
216
217    return OK;
218}
219
220bool OutputConfiguration::gbpsEqual(const OutputConfiguration& other) const {
221    const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
222            other.getGraphicBufferProducers();
223
224    if (mGbps.size() != otherGbps.size()) {
225        return false;
226    }
227
228    for (size_t i = 0; i < mGbps.size(); i++) {
229        if (mGbps[i] != otherGbps[i]) {
230            return false;
231        }
232    }
233
234    return true;
235}
236
237bool OutputConfiguration::gbpsLessThan(const OutputConfiguration& other) const {
238    const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
239            other.getGraphicBufferProducers();
240
241    if (mGbps.size() !=  otherGbps.size()) {
242        return mGbps.size() < otherGbps.size();
243    }
244
245    for (size_t i = 0; i < mGbps.size(); i++) {
246        if (mGbps[i] != otherGbps[i]) {
247            return mGbps[i] < otherGbps[i];
248        }
249    }
250
251    return false;
252}
253}; // namespace android
254