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 <system/camera_metadata.h>
27 #include <utils/String8.h>
28
29 namespace android {
30
31
32 const int OutputConfiguration::INVALID_ROTATION = -1;
33 const int OutputConfiguration::INVALID_SET_ID = -1;
34
35 const std::vector<sp<IGraphicBufferProducer>>&
getGraphicBufferProducers() const36 OutputConfiguration::getGraphicBufferProducers() const {
37 return mGbps;
38 }
39
getRotation() const40 int OutputConfiguration::getRotation() const {
41 return mRotation;
42 }
43
getSurfaceSetID() const44 int OutputConfiguration::getSurfaceSetID() const {
45 return mSurfaceSetID;
46 }
47
getSurfaceType() const48 int OutputConfiguration::getSurfaceType() const {
49 return mSurfaceType;
50 }
51
getWidth() const52 int OutputConfiguration::getWidth() const {
53 return mWidth;
54 }
55
getHeight() const56 int OutputConfiguration::getHeight() const {
57 return mHeight;
58 }
59
isDeferred() const60 bool OutputConfiguration::isDeferred() const {
61 return mIsDeferred;
62 }
63
isShared() const64 bool OutputConfiguration::isShared() const {
65 return mIsShared;
66 }
67
getPhysicalCameraId() const68 String16 OutputConfiguration::getPhysicalCameraId() const {
69 return mPhysicalCameraId;
70 }
71
isMultiResolution() const72 bool OutputConfiguration::isMultiResolution() const {
73 return mIsMultiResolution;
74 }
75
getSensorPixelModesUsed() const76 const std::vector<int32_t> &OutputConfiguration::getSensorPixelModesUsed() const {
77 return mSensorPixelModesUsed;
78 }
79
getDynamicRangeProfile() const80 int64_t OutputConfiguration::getDynamicRangeProfile() const {
81 return mDynamicRangeProfile;
82 }
83
getStreamUseCase() const84 int64_t OutputConfiguration::getStreamUseCase() const {
85 return mStreamUseCase;
86 }
87
getTimestampBase() const88 int OutputConfiguration::getTimestampBase() const {
89 return mTimestampBase;
90 }
91
getMirrorMode() const92 int OutputConfiguration::getMirrorMode() const {
93 return mMirrorMode;
94 }
95
OutputConfiguration()96 OutputConfiguration::OutputConfiguration() :
97 mRotation(INVALID_ROTATION),
98 mSurfaceSetID(INVALID_SET_ID),
99 mSurfaceType(SURFACE_TYPE_UNKNOWN),
100 mWidth(0),
101 mHeight(0),
102 mIsDeferred(false),
103 mIsShared(false),
104 mIsMultiResolution(false),
105 mDynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
106 mStreamUseCase(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT),
107 mTimestampBase(TIMESTAMP_BASE_DEFAULT),
108 mMirrorMode(MIRROR_MODE_AUTO) {
109 }
110
OutputConfiguration(const android::Parcel & parcel)111 OutputConfiguration::OutputConfiguration(const android::Parcel& parcel) :
112 mRotation(INVALID_ROTATION),
113 mSurfaceSetID(INVALID_SET_ID) {
114 readFromParcel(&parcel);
115 }
116
readFromParcel(const android::Parcel * parcel)117 status_t OutputConfiguration::readFromParcel(const android::Parcel* parcel) {
118 status_t err = OK;
119 int rotation = 0;
120
121 if (parcel == nullptr) return BAD_VALUE;
122
123 if ((err = parcel->readInt32(&rotation)) != OK) {
124 ALOGE("%s: Failed to read rotation from parcel", __FUNCTION__);
125 return err;
126 }
127
128 int setID = INVALID_SET_ID;
129 if ((err = parcel->readInt32(&setID)) != OK) {
130 ALOGE("%s: Failed to read surface set ID from parcel", __FUNCTION__);
131 return err;
132 }
133
134 int surfaceType = SURFACE_TYPE_UNKNOWN;
135 if ((err = parcel->readInt32(&surfaceType)) != OK) {
136 ALOGE("%s: Failed to read surface type from parcel", __FUNCTION__);
137 return err;
138 }
139
140 int width = 0;
141 if ((err = parcel->readInt32(&width)) != OK) {
142 ALOGE("%s: Failed to read surface width from parcel", __FUNCTION__);
143 return err;
144 }
145
146 int height = 0;
147 if ((err = parcel->readInt32(&height)) != OK) {
148 ALOGE("%s: Failed to read surface height from parcel", __FUNCTION__);
149 return err;
150 }
151
152 int isDeferred = 0;
153 if ((err = parcel->readInt32(&isDeferred)) != OK) {
154 ALOGE("%s: Failed to read surface isDeferred flag from parcel", __FUNCTION__);
155 return err;
156 }
157
158 int isShared = 0;
159 if ((err = parcel->readInt32(&isShared)) != OK) {
160 ALOGE("%s: Failed to read surface isShared flag from parcel", __FUNCTION__);
161 return err;
162 }
163
164 if (isDeferred && surfaceType != SURFACE_TYPE_SURFACE_VIEW &&
165 surfaceType != SURFACE_TYPE_SURFACE_TEXTURE) {
166 ALOGE("%s: Invalid surface type for deferred configuration", __FUNCTION__);
167 return BAD_VALUE;
168 }
169
170 std::vector<view::Surface> surfaceShims;
171 if ((err = parcel->readParcelableVector(&surfaceShims)) != OK) {
172 ALOGE("%s: Failed to read surface(s) from parcel", __FUNCTION__);
173 return err;
174 }
175
176 parcel->readString16(&mPhysicalCameraId);
177
178 int isMultiResolution = 0;
179 if ((err = parcel->readInt32(&isMultiResolution)) != OK) {
180 ALOGE("%s: Failed to read surface isMultiResolution flag from parcel", __FUNCTION__);
181 return err;
182 }
183
184 std::vector<int32_t> sensorPixelModesUsed;
185 if ((err = parcel->readInt32Vector(&sensorPixelModesUsed)) != OK) {
186 ALOGE("%s: Failed to read sensor pixel mode(s) from parcel", __FUNCTION__);
187 return err;
188 }
189 int64_t dynamicProfile;
190 if ((err = parcel->readInt64(&dynamicProfile)) != OK) {
191 ALOGE("%s: Failed to read surface dynamic range profile flag from parcel", __FUNCTION__);
192 return err;
193 }
194
195 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
196 if ((err = parcel->readInt64(&streamUseCase)) != OK) {
197 ALOGE("%s: Failed to read stream use case from parcel", __FUNCTION__);
198 return err;
199 }
200
201 int timestampBase = TIMESTAMP_BASE_DEFAULT;
202 if ((err = parcel->readInt32(×tampBase)) != OK) {
203 ALOGE("%s: Failed to read timestamp base from parcel", __FUNCTION__);
204 return err;
205 }
206
207 int mirrorMode = MIRROR_MODE_AUTO;
208 if ((err = parcel->readInt32(&mirrorMode)) != OK) {
209 ALOGE("%s: Failed to read mirroring mode from parcel", __FUNCTION__);
210 return err;
211 }
212
213 mRotation = rotation;
214 mSurfaceSetID = setID;
215 mSurfaceType = surfaceType;
216 mWidth = width;
217 mHeight = height;
218 mIsDeferred = isDeferred != 0;
219 mIsShared = isShared != 0;
220 mIsMultiResolution = isMultiResolution != 0;
221 mStreamUseCase = streamUseCase;
222 mTimestampBase = timestampBase;
223 mMirrorMode = mirrorMode;
224 for (auto& surface : surfaceShims) {
225 ALOGV("%s: OutputConfiguration: %p, name %s", __FUNCTION__,
226 surface.graphicBufferProducer.get(),
227 String8(surface.name).string());
228 mGbps.push_back(surface.graphicBufferProducer);
229 }
230
231 mSensorPixelModesUsed = std::move(sensorPixelModesUsed);
232 mDynamicRangeProfile = dynamicProfile;
233
234 ALOGV("%s: OutputConfiguration: rotation = %d, setId = %d, surfaceType = %d,"
235 " physicalCameraId = %s, isMultiResolution = %d, streamUseCase = %" PRId64
236 ", timestampBase = %d, mirrorMode = %d",
237 __FUNCTION__, mRotation, mSurfaceSetID, mSurfaceType,
238 String8(mPhysicalCameraId).string(), mIsMultiResolution, mStreamUseCase, timestampBase,
239 mMirrorMode);
240
241 return err;
242 }
243
OutputConfiguration(sp<IGraphicBufferProducer> & gbp,int rotation,const String16 & physicalId,int surfaceSetID,bool isShared)244 OutputConfiguration::OutputConfiguration(sp<IGraphicBufferProducer>& gbp, int rotation,
245 const String16& physicalId,
246 int surfaceSetID, bool isShared) {
247 mGbps.push_back(gbp);
248 mRotation = rotation;
249 mSurfaceSetID = surfaceSetID;
250 mIsDeferred = false;
251 mIsShared = isShared;
252 mPhysicalCameraId = physicalId;
253 mIsMultiResolution = false;
254 mDynamicRangeProfile = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
255 mStreamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
256 mTimestampBase = TIMESTAMP_BASE_DEFAULT;
257 mMirrorMode = MIRROR_MODE_AUTO;
258 }
259
OutputConfiguration(const std::vector<sp<IGraphicBufferProducer>> & gbps,int rotation,const String16 & physicalCameraId,int surfaceSetID,int surfaceType,int width,int height,bool isShared)260 OutputConfiguration::OutputConfiguration(
261 const std::vector<sp<IGraphicBufferProducer>>& gbps,
262 int rotation, const String16& physicalCameraId, int surfaceSetID, int surfaceType,
263 int width, int height, bool isShared)
264 : mGbps(gbps), mRotation(rotation), mSurfaceSetID(surfaceSetID), mSurfaceType(surfaceType),
265 mWidth(width), mHeight(height), mIsDeferred(false), mIsShared(isShared),
266 mPhysicalCameraId(physicalCameraId), mIsMultiResolution(false),
267 mDynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
268 mStreamUseCase(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT),
269 mTimestampBase(TIMESTAMP_BASE_DEFAULT),
270 mMirrorMode(MIRROR_MODE_AUTO) { }
271
writeToParcel(android::Parcel * parcel) const272 status_t OutputConfiguration::writeToParcel(android::Parcel* parcel) const {
273
274 if (parcel == nullptr) return BAD_VALUE;
275 status_t err = OK;
276
277 err = parcel->writeInt32(mRotation);
278 if (err != OK) return err;
279
280 err = parcel->writeInt32(mSurfaceSetID);
281 if (err != OK) return err;
282
283 err = parcel->writeInt32(mSurfaceType);
284 if (err != OK) return err;
285
286 err = parcel->writeInt32(mWidth);
287 if (err != OK) return err;
288
289 err = parcel->writeInt32(mHeight);
290 if (err != OK) return err;
291
292 err = parcel->writeInt32(mIsDeferred ? 1 : 0);
293 if (err != OK) return err;
294
295 err = parcel->writeInt32(mIsShared ? 1 : 0);
296 if (err != OK) return err;
297
298 std::vector<view::Surface> surfaceShims;
299 for (auto& gbp : mGbps) {
300 view::Surface surfaceShim;
301 surfaceShim.name = String16("unknown_name"); // name of surface
302 surfaceShim.graphicBufferProducer = gbp;
303 surfaceShims.push_back(surfaceShim);
304 }
305 err = parcel->writeParcelableVector(surfaceShims);
306 if (err != OK) return err;
307
308 err = parcel->writeString16(mPhysicalCameraId);
309 if (err != OK) return err;
310
311 err = parcel->writeInt32(mIsMultiResolution ? 1 : 0);
312 if (err != OK) return err;
313
314 err = parcel->writeParcelableVector(mSensorPixelModesUsed);
315 if (err != OK) return err;
316
317 err = parcel->writeInt64(mDynamicRangeProfile);
318 if (err != OK) return err;
319
320 err = parcel->writeInt64(mStreamUseCase);
321 if (err != OK) return err;
322
323 err = parcel->writeInt32(mTimestampBase);
324 if (err != OK) return err;
325
326 err = parcel->writeInt32(mMirrorMode);
327 if (err != OK) return err;
328
329 return OK;
330 }
331
332 template <typename T>
simpleVectorsEqual(T first,T second)333 static bool simpleVectorsEqual(T first, T second) {
334 if (first.size() != second.size()) {
335 return false;
336 }
337
338 for (size_t i = 0; i < first.size(); i++) {
339 if (first[i] != second[i]) {
340 return false;
341 }
342 }
343 return true;
344 }
345
gbpsEqual(const OutputConfiguration & other) const346 bool OutputConfiguration::gbpsEqual(const OutputConfiguration& other) const {
347 const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
348 other.getGraphicBufferProducers();
349 return simpleVectorsEqual(otherGbps, mGbps);
350 }
351
sensorPixelModesUsedEqual(const OutputConfiguration & other) const352 bool OutputConfiguration::sensorPixelModesUsedEqual(const OutputConfiguration& other) const {
353 const std::vector<int32_t>& othersensorPixelModesUsed = other.getSensorPixelModesUsed();
354 return simpleVectorsEqual(othersensorPixelModesUsed, mSensorPixelModesUsed);
355 }
356
sensorPixelModesUsedLessThan(const OutputConfiguration & other) const357 bool OutputConfiguration::sensorPixelModesUsedLessThan(const OutputConfiguration& other) const {
358 const std::vector<int32_t>& spms = other.getSensorPixelModesUsed();
359
360 if (mSensorPixelModesUsed.size() != spms.size()) {
361 return mSensorPixelModesUsed.size() < spms.size();
362 }
363
364 for (size_t i = 0; i < spms.size(); i++) {
365 if (mSensorPixelModesUsed[i] != spms[i]) {
366 return mSensorPixelModesUsed[i] < spms[i];
367 }
368 }
369
370 return false;
371 }
372
gbpsLessThan(const OutputConfiguration & other) const373 bool OutputConfiguration::gbpsLessThan(const OutputConfiguration& other) const {
374 const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
375 other.getGraphicBufferProducers();
376
377 if (mGbps.size() != otherGbps.size()) {
378 return mGbps.size() < otherGbps.size();
379 }
380
381 for (size_t i = 0; i < mGbps.size(); i++) {
382 if (mGbps[i] != otherGbps[i]) {
383 return mGbps[i] < otherGbps[i];
384 }
385 }
386
387 return false;
388 }
389 }; // namespace android
390