• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 namespace android {
29 
30 
31 const int OutputConfiguration::INVALID_ROTATION = -1;
32 const int OutputConfiguration::INVALID_SET_ID = -1;
33 
34 const std::vector<sp<IGraphicBufferProducer>>&
getGraphicBufferProducers() const35         OutputConfiguration::getGraphicBufferProducers() const {
36     return mGbps;
37 }
38 
getRotation() const39 int OutputConfiguration::getRotation() const {
40     return mRotation;
41 }
42 
getSurfaceSetID() const43 int OutputConfiguration::getSurfaceSetID() const {
44     return mSurfaceSetID;
45 }
46 
getSurfaceType() const47 int OutputConfiguration::getSurfaceType() const {
48     return mSurfaceType;
49 }
50 
getWidth() const51 int OutputConfiguration::getWidth() const {
52     return mWidth;
53 }
54 
getHeight() const55 int OutputConfiguration::getHeight() const {
56     return mHeight;
57 }
58 
isDeferred() const59 bool OutputConfiguration::isDeferred() const {
60     return mIsDeferred;
61 }
62 
isShared() const63 bool OutputConfiguration::isShared() const {
64     return mIsShared;
65 }
66 
getPhysicalCameraId() const67 String16 OutputConfiguration::getPhysicalCameraId() const {
68     return mPhysicalCameraId;
69 }
70 
isMultiResolution() const71 bool OutputConfiguration::isMultiResolution() const {
72     return mIsMultiResolution;
73 }
74 
getSensorPixelModesUsed() const75 const std::vector<int32_t> &OutputConfiguration::getSensorPixelModesUsed() const {
76     return mSensorPixelModesUsed;
77 }
78 
OutputConfiguration()79 OutputConfiguration::OutputConfiguration() :
80         mRotation(INVALID_ROTATION),
81         mSurfaceSetID(INVALID_SET_ID),
82         mSurfaceType(SURFACE_TYPE_UNKNOWN),
83         mWidth(0),
84         mHeight(0),
85         mIsDeferred(false),
86         mIsShared(false),
87         mIsMultiResolution(false) {
88 }
89 
OutputConfiguration(const android::Parcel & parcel)90 OutputConfiguration::OutputConfiguration(const android::Parcel& parcel) :
91         mRotation(INVALID_ROTATION),
92         mSurfaceSetID(INVALID_SET_ID) {
93     readFromParcel(&parcel);
94 }
95 
readFromParcel(const android::Parcel * parcel)96 status_t OutputConfiguration::readFromParcel(const android::Parcel* parcel) {
97     status_t err = OK;
98     int rotation = 0;
99 
100     if (parcel == nullptr) return BAD_VALUE;
101 
102     if ((err = parcel->readInt32(&rotation)) != OK) {
103         ALOGE("%s: Failed to read rotation from parcel", __FUNCTION__);
104         return err;
105     }
106 
107     int setID = INVALID_SET_ID;
108     if ((err = parcel->readInt32(&setID)) != OK) {
109         ALOGE("%s: Failed to read surface set ID from parcel", __FUNCTION__);
110         return err;
111     }
112 
113     int surfaceType = SURFACE_TYPE_UNKNOWN;
114     if ((err = parcel->readInt32(&surfaceType)) != OK) {
115         ALOGE("%s: Failed to read surface type from parcel", __FUNCTION__);
116         return err;
117     }
118 
119     int width = 0;
120     if ((err = parcel->readInt32(&width)) != OK) {
121         ALOGE("%s: Failed to read surface width from parcel", __FUNCTION__);
122         return err;
123     }
124 
125     int height = 0;
126     if ((err = parcel->readInt32(&height)) != OK) {
127         ALOGE("%s: Failed to read surface height from parcel", __FUNCTION__);
128         return err;
129     }
130 
131     int isDeferred = 0;
132     if ((err = parcel->readInt32(&isDeferred)) != OK) {
133         ALOGE("%s: Failed to read surface isDeferred flag from parcel", __FUNCTION__);
134         return err;
135     }
136 
137     int isShared = 0;
138     if ((err = parcel->readInt32(&isShared)) != OK) {
139         ALOGE("%s: Failed to read surface isShared flag from parcel", __FUNCTION__);
140         return err;
141     }
142 
143     if (isDeferred && surfaceType != SURFACE_TYPE_SURFACE_VIEW &&
144             surfaceType != SURFACE_TYPE_SURFACE_TEXTURE) {
145         ALOGE("%s: Invalid surface type for deferred configuration", __FUNCTION__);
146         return BAD_VALUE;
147     }
148 
149     std::vector<view::Surface> surfaceShims;
150     if ((err = parcel->readParcelableVector(&surfaceShims)) != OK) {
151         ALOGE("%s: Failed to read surface(s) from parcel", __FUNCTION__);
152         return err;
153     }
154 
155     parcel->readString16(&mPhysicalCameraId);
156 
157     int isMultiResolution = 0;
158     if ((err = parcel->readInt32(&isMultiResolution)) != OK) {
159         ALOGE("%s: Failed to read surface isMultiResolution flag from parcel", __FUNCTION__);
160         return err;
161     }
162 
163     std::vector<int32_t> sensorPixelModesUsed;
164     if ((err = parcel->readInt32Vector(&sensorPixelModesUsed)) != OK) {
165         ALOGE("%s: Failed to read sensor pixel mode(s) from parcel", __FUNCTION__);
166         return err;
167     }
168     mRotation = rotation;
169     mSurfaceSetID = setID;
170     mSurfaceType = surfaceType;
171     mWidth = width;
172     mHeight = height;
173     mIsDeferred = isDeferred != 0;
174     mIsShared = isShared != 0;
175     mIsMultiResolution = isMultiResolution != 0;
176     for (auto& surface : surfaceShims) {
177         ALOGV("%s: OutputConfiguration: %p, name %s", __FUNCTION__,
178                 surface.graphicBufferProducer.get(),
179                 String8(surface.name).string());
180         mGbps.push_back(surface.graphicBufferProducer);
181     }
182 
183     mSensorPixelModesUsed = std::move(sensorPixelModesUsed);
184 
185     ALOGV("%s: OutputConfiguration: rotation = %d, setId = %d, surfaceType = %d,"
186           " physicalCameraId = %s, isMultiResolution = %d", __FUNCTION__, mRotation,
187           mSurfaceSetID, mSurfaceType, String8(mPhysicalCameraId).string(), mIsMultiResolution);
188 
189     return err;
190 }
191 
OutputConfiguration(sp<IGraphicBufferProducer> & gbp,int rotation,const String16 & physicalId,int surfaceSetID,bool isShared)192 OutputConfiguration::OutputConfiguration(sp<IGraphicBufferProducer>& gbp, int rotation,
193         const String16& physicalId,
194         int surfaceSetID, bool isShared) {
195     mGbps.push_back(gbp);
196     mRotation = rotation;
197     mSurfaceSetID = surfaceSetID;
198     mIsDeferred = false;
199     mIsShared = isShared;
200     mPhysicalCameraId = physicalId;
201     mIsMultiResolution = false;
202 }
203 
OutputConfiguration(const std::vector<sp<IGraphicBufferProducer>> & gbps,int rotation,const String16 & physicalCameraId,int surfaceSetID,int surfaceType,int width,int height,bool isShared)204 OutputConfiguration::OutputConfiguration(
205         const std::vector<sp<IGraphicBufferProducer>>& gbps,
206     int rotation, const String16& physicalCameraId, int surfaceSetID,  int surfaceType,
207     int width, int height, bool isShared)
208   : mGbps(gbps), mRotation(rotation), mSurfaceSetID(surfaceSetID), mSurfaceType(surfaceType),
209     mWidth(width), mHeight(height), mIsDeferred(false), mIsShared(isShared),
210     mPhysicalCameraId(physicalCameraId), mIsMultiResolution(false) { }
211 
writeToParcel(android::Parcel * parcel) const212 status_t OutputConfiguration::writeToParcel(android::Parcel* parcel) const {
213 
214     if (parcel == nullptr) return BAD_VALUE;
215     status_t err = OK;
216 
217     err = parcel->writeInt32(mRotation);
218     if (err != OK) return err;
219 
220     err = parcel->writeInt32(mSurfaceSetID);
221     if (err != OK) return err;
222 
223     err = parcel->writeInt32(mSurfaceType);
224     if (err != OK) return err;
225 
226     err = parcel->writeInt32(mWidth);
227     if (err != OK) return err;
228 
229     err = parcel->writeInt32(mHeight);
230     if (err != OK) return err;
231 
232     err = parcel->writeInt32(mIsDeferred ? 1 : 0);
233     if (err != OK) return err;
234 
235     err = parcel->writeInt32(mIsShared ? 1 : 0);
236     if (err != OK) return err;
237 
238     std::vector<view::Surface> surfaceShims;
239     for (auto& gbp : mGbps) {
240         view::Surface surfaceShim;
241         surfaceShim.name = String16("unknown_name"); // name of surface
242         surfaceShim.graphicBufferProducer = gbp;
243         surfaceShims.push_back(surfaceShim);
244     }
245     err = parcel->writeParcelableVector(surfaceShims);
246     if (err != OK) return err;
247 
248     err = parcel->writeString16(mPhysicalCameraId);
249     if (err != OK) return err;
250 
251     err = parcel->writeInt32(mIsMultiResolution ? 1 : 0);
252     if (err != OK) return err;
253 
254     err = parcel->writeParcelableVector(mSensorPixelModesUsed);
255     if (err != OK) return err;
256 
257     return OK;
258 }
259 
260 template <typename T>
simpleVectorsEqual(T first,T second)261 static bool simpleVectorsEqual(T first, T second) {
262     if (first.size() != second.size()) {
263         return false;
264     }
265 
266     for (size_t i = 0; i < first.size(); i++) {
267         if (first[i] != second[i]) {
268             return false;
269         }
270     }
271     return true;
272 }
273 
gbpsEqual(const OutputConfiguration & other) const274 bool OutputConfiguration::gbpsEqual(const OutputConfiguration& other) const {
275     const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
276             other.getGraphicBufferProducers();
277     return simpleVectorsEqual(otherGbps, mGbps);
278 }
279 
sensorPixelModesUsedEqual(const OutputConfiguration & other) const280 bool OutputConfiguration::sensorPixelModesUsedEqual(const OutputConfiguration& other) const {
281     const std::vector<int32_t>& othersensorPixelModesUsed = other.getSensorPixelModesUsed();
282     return simpleVectorsEqual(othersensorPixelModesUsed, mSensorPixelModesUsed);
283 }
284 
sensorPixelModesUsedLessThan(const OutputConfiguration & other) const285 bool OutputConfiguration::sensorPixelModesUsedLessThan(const OutputConfiguration& other) const {
286     const std::vector<int32_t>& spms = other.getSensorPixelModesUsed();
287 
288     if (mSensorPixelModesUsed.size() !=  spms.size()) {
289         return mSensorPixelModesUsed.size() < spms.size();
290     }
291 
292     for (size_t i = 0; i < spms.size(); i++) {
293         if (mSensorPixelModesUsed[i] != spms[i]) {
294             return mSensorPixelModesUsed[i] < spms[i];
295         }
296     }
297 
298     return false;
299 }
300 
gbpsLessThan(const OutputConfiguration & other) const301 bool OutputConfiguration::gbpsLessThan(const OutputConfiguration& other) const {
302     const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
303             other.getGraphicBufferProducers();
304 
305     if (mGbps.size() !=  otherGbps.size()) {
306         return mGbps.size() < otherGbps.size();
307     }
308 
309     for (size_t i = 0; i < mGbps.size(); i++) {
310         if (mGbps[i] != otherGbps[i]) {
311             return mGbps[i] < otherGbps[i];
312         }
313     }
314 
315     return false;
316 }
317 }; // namespace android
318