OutputConfiguration.cpp revision 758c215374dba397dabe17b8e96dd38593c09dd7
1/*
2**
3** Copyright 2015, 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/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
67OutputConfiguration::OutputConfiguration() :
68        mRotation(INVALID_ROTATION),
69        mSurfaceSetID(INVALID_SET_ID),
70        mSurfaceType(SURFACE_TYPE_UNKNOWN),
71        mWidth(0),
72        mHeight(0),
73        mIsDeferred(false),
74        mIsShared(false) {
75}
76
77OutputConfiguration::OutputConfiguration(const android::Parcel& parcel) :
78        mRotation(INVALID_ROTATION),
79        mSurfaceSetID(INVALID_SET_ID) {
80    readFromParcel(&parcel);
81}
82
83status_t OutputConfiguration::readFromParcel(const android::Parcel* parcel) {
84    status_t err = OK;
85    int rotation = 0;
86
87    if (parcel == nullptr) return BAD_VALUE;
88
89    if ((err = parcel->readInt32(&rotation)) != OK) {
90        ALOGE("%s: Failed to read rotation from parcel", __FUNCTION__);
91        return err;
92    }
93
94    int setID = INVALID_SET_ID;
95    if ((err = parcel->readInt32(&setID)) != OK) {
96        ALOGE("%s: Failed to read surface set ID from parcel", __FUNCTION__);
97        return err;
98    }
99
100    int surfaceType = SURFACE_TYPE_UNKNOWN;
101    if ((err = parcel->readInt32(&surfaceType)) != OK) {
102        ALOGE("%s: Failed to read surface type from parcel", __FUNCTION__);
103        return err;
104    }
105
106    int width = 0;
107    if ((err = parcel->readInt32(&width)) != OK) {
108        ALOGE("%s: Failed to read surface width from parcel", __FUNCTION__);
109        return err;
110    }
111
112    int height = 0;
113    if ((err = parcel->readInt32(&height)) != OK) {
114        ALOGE("%s: Failed to read surface height from parcel", __FUNCTION__);
115        return err;
116    }
117
118    int isDeferred = 0;
119    if ((err = parcel->readInt32(&isDeferred)) != OK) {
120        ALOGE("%s: Failed to read surface isDeferred flag from parcel", __FUNCTION__);
121        return err;
122    }
123
124    int isShared = 0;
125    if ((err = parcel->readInt32(&isShared)) != OK) {
126        ALOGE("%s: Failed to read surface isShared flag from parcel", __FUNCTION__);
127        return err;
128    }
129
130    if (isDeferred && surfaceType != SURFACE_TYPE_SURFACE_VIEW &&
131            surfaceType != SURFACE_TYPE_SURFACE_TEXTURE) {
132        ALOGE("%s: Invalid surface type for deferred configuration", __FUNCTION__);
133        return BAD_VALUE;
134    }
135
136    std::vector<view::Surface> surfaceShims;
137    if ((err = parcel->readParcelableVector(&surfaceShims)) != OK) {
138        ALOGE("%s: Failed to read surface(s) from parcel", __FUNCTION__);
139        return err;
140    }
141
142    mRotation = rotation;
143    mSurfaceSetID = setID;
144    mSurfaceType = surfaceType;
145    mWidth = width;
146    mHeight = height;
147    mIsDeferred = isDeferred != 0;
148    mIsShared = isShared != 0;
149    for (auto& surface : surfaceShims) {
150        ALOGV("%s: OutputConfiguration: %p, name %s", __FUNCTION__,
151                surface.graphicBufferProducer.get(),
152                String8(surface.name).string());
153        mGbps.push_back(surface.graphicBufferProducer);
154    }
155
156    ALOGV("%s: OutputConfiguration: rotation = %d, setId = %d, surfaceType = %d",
157            __FUNCTION__, mRotation, mSurfaceSetID, mSurfaceType);
158
159    return err;
160}
161
162OutputConfiguration::OutputConfiguration(sp<IGraphicBufferProducer>& gbp, int rotation,
163        int surfaceSetID) {
164    mGbps.push_back(gbp);
165    mRotation = rotation;
166    mSurfaceSetID = surfaceSetID;
167    mIsDeferred = false;
168    mIsShared = false;
169}
170
171status_t OutputConfiguration::writeToParcel(android::Parcel* parcel) const {
172
173    if (parcel == nullptr) return BAD_VALUE;
174    status_t err = OK;
175
176    err = parcel->writeInt32(mRotation);
177    if (err != OK) return err;
178
179    err = parcel->writeInt32(mSurfaceSetID);
180    if (err != OK) return err;
181
182    err = parcel->writeInt32(mSurfaceType);
183    if (err != OK) return err;
184
185    err = parcel->writeInt32(mWidth);
186    if (err != OK) return err;
187
188    err = parcel->writeInt32(mHeight);
189    if (err != OK) return err;
190
191    err = parcel->writeInt32(mIsDeferred ? 1 : 0);
192    if (err != OK) return err;
193
194    err = parcel->writeInt32(mIsShared ? 1 : 0);
195    if (err != OK) return err;
196
197    std::vector<view::Surface> surfaceShims;
198    for (auto& gbp : mGbps) {
199        view::Surface surfaceShim;
200        surfaceShim.name = String16("unknown_name"); // name of surface
201        surfaceShim.graphicBufferProducer = gbp;
202        surfaceShims.push_back(surfaceShim);
203    }
204    err = parcel->writeParcelableVector(surfaceShims);
205    if (err != OK) return err;
206
207    return OK;
208}
209
210bool OutputConfiguration::gbpsEqual(const OutputConfiguration& other) const {
211    const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
212            other.getGraphicBufferProducers();
213
214    if (mGbps.size() != otherGbps.size()) {
215        return false;
216    }
217
218    for (size_t i = 0; i < mGbps.size(); i++) {
219        if (mGbps[i] != otherGbps[i]) {
220            return false;
221        }
222    }
223
224    return true;
225}
226
227bool OutputConfiguration::gbpsLessThan(const OutputConfiguration& other) const {
228    const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
229            other.getGraphicBufferProducers();
230
231    if (mGbps.size() !=  otherGbps.size()) {
232        return mGbps.size() < otherGbps.size();
233    }
234
235    for (size_t i = 0; i < mGbps.size(); i++) {
236        if (mGbps[i] != otherGbps[i]) {
237            return mGbps[i] < otherGbps[i];
238        }
239    }
240
241    return false;
242}
243}; // namespace android
244