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 <camera/StringUtils.h>
25 #include <com_android_internal_camera_flags.h>
26 #include <binder/Parcel.h>
27 #include <gui/view/Surface.h>
28 #include <system/camera_metadata.h>
29 #include <system/graphics.h>
30 #include <utils/String8.h>
31
32 namespace flags = com::android::internal::camera::flags;
33
34 namespace android {
35
36 const int OutputConfiguration::INVALID_ROTATION = -1;
37 const int OutputConfiguration::ROTATION_0 = 0;
38 const int OutputConfiguration::INVALID_SET_ID = -1;
39
getSurfaces() const40 const std::vector<ParcelableSurfaceType>& OutputConfiguration::getSurfaces() const {
41 return mSurfaces;
42 }
43
getRotation() const44 int OutputConfiguration::getRotation() const {
45 return mRotation;
46 }
47
getSurfaceSetID() const48 int OutputConfiguration::getSurfaceSetID() const {
49 return mSurfaceSetID;
50 }
51
getSurfaceType() const52 int OutputConfiguration::getSurfaceType() const {
53 return mSurfaceType;
54 }
55
getWidth() const56 int OutputConfiguration::getWidth() const {
57 return mWidth;
58 }
59
getHeight() const60 int OutputConfiguration::getHeight() const {
61 return mHeight;
62 }
63
isDeferred() const64 bool OutputConfiguration::isDeferred() const {
65 return mIsDeferred;
66 }
67
isShared() const68 bool OutputConfiguration::isShared() const {
69 return mIsShared;
70 }
71
getPhysicalCameraId() const72 std::string OutputConfiguration::getPhysicalCameraId() const {
73 return mPhysicalCameraId;
74 }
75
isMultiResolution() const76 bool OutputConfiguration::isMultiResolution() const {
77 return mIsMultiResolution;
78 }
79
getSensorPixelModesUsed() const80 const std::vector<int32_t> &OutputConfiguration::getSensorPixelModesUsed() const {
81 return mSensorPixelModesUsed;
82 }
83
getDynamicRangeProfile() const84 int64_t OutputConfiguration::getDynamicRangeProfile() const {
85 return mDynamicRangeProfile;
86 }
87
getColorSpace() const88 int32_t OutputConfiguration::getColorSpace() const {
89 return mColorSpace;
90 }
91
getStreamUseCase() const92 int64_t OutputConfiguration::getStreamUseCase() const {
93 return mStreamUseCase;
94 }
95
getTimestampBase() const96 int OutputConfiguration::getTimestampBase() const {
97 return mTimestampBase;
98 }
99
getMirrorMode() const100 int OutputConfiguration::getMirrorMode() const {
101 return mMirrorMode;
102 }
103
getMirrorMode(ParcelableSurfaceType surface) const104 int OutputConfiguration::getMirrorMode(ParcelableSurfaceType surface) const {
105 if (!flags::mirror_mode_shared_surfaces()) {
106 return mMirrorMode;
107 }
108
109 if (mSurfaces.size() != mMirrorModeForProducers.size()) {
110 ALOGE("%s: mSurfaces size doesn't match mMirrorModeForProducers: %zu vs %zu",
111 __FUNCTION__, mSurfaces.size(), mMirrorModeForProducers.size());
112 return mMirrorMode;
113 }
114
115 // Use per-producer mirror mode if available.
116 for (size_t i = 0; i < mSurfaces.size(); i++) {
117 if (mSurfaces[i] == surface) {
118 return mMirrorModeForProducers[i];
119 }
120 }
121 // For surface that doesn't belong to this output configuration, use
122 // mMirrorMode as default.
123 ALOGW("%s: Surface doesn't belong to this OutputConfiguration!", __FUNCTION__);
124 return mMirrorMode;
125 }
126
useReadoutTimestamp() const127 bool OutputConfiguration::useReadoutTimestamp() const {
128 return mUseReadoutTimestamp;
129 }
130
getFormat() const131 int OutputConfiguration::getFormat() const {
132 return mFormat;
133 }
134
getDataspace() const135 int OutputConfiguration::getDataspace() const {
136 return mDataspace;
137 }
138
getUsage() const139 int64_t OutputConfiguration::getUsage() const {
140 return mUsage;
141 }
142
isComplete() const143 bool OutputConfiguration::isComplete() const {
144 return !((mSurfaceType == SURFACE_TYPE_MEDIA_RECORDER ||
145 mSurfaceType == SURFACE_TYPE_MEDIA_CODEC ||
146 mSurfaceType == SURFACE_TYPE_IMAGE_READER) &&
147 mSurfaces.empty());
148 }
149
OutputConfiguration()150 OutputConfiguration::OutputConfiguration() :
151 mRotation(INVALID_ROTATION),
152 mSurfaceSetID(INVALID_SET_ID),
153 mSurfaceType(SURFACE_TYPE_UNKNOWN),
154 mWidth(0),
155 mHeight(0),
156 mIsDeferred(false),
157 mIsShared(false),
158 mIsMultiResolution(false),
159 mDynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
160 mColorSpace(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED),
161 mStreamUseCase(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT),
162 mTimestampBase(TIMESTAMP_BASE_DEFAULT),
163 mMirrorMode(MIRROR_MODE_AUTO),
164 mUseReadoutTimestamp(false),
165 mFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
166 mDataspace(0),
167 mUsage(0) {
168 }
169
OutputConfiguration(int surfaceType,int width,int height,int format,int32_t colorSpace,int mirrorMode,bool useReadoutTimestamp,int timestampBase,int dataspace,int64_t usage,int64_t streamusecase,std::string physicalCamId)170 OutputConfiguration::OutputConfiguration(int surfaceType, int width, int height, int format,
171 int32_t colorSpace, int mirrorMode, bool useReadoutTimestamp, int timestampBase,
172 int dataspace, int64_t usage, int64_t streamusecase, std::string physicalCamId):
173 mRotation(ROTATION_0),
174 mSurfaceSetID(INVALID_SET_ID),
175 mSurfaceType(surfaceType),
176 mWidth(width),
177 mHeight(height),
178 mIsDeferred(false),
179 mIsShared(false),
180 mPhysicalCameraId(physicalCamId),
181 mIsMultiResolution(false),
182 mDynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
183 mColorSpace(colorSpace),
184 mStreamUseCase(streamusecase),
185 mTimestampBase(timestampBase),
186 mMirrorMode(mirrorMode),
187 mUseReadoutTimestamp(useReadoutTimestamp),
188 mFormat(format),
189 mDataspace(dataspace),
190 mUsage(usage){
191 }
192
OutputConfiguration(const android::Parcel & parcel)193 OutputConfiguration::OutputConfiguration(const android::Parcel& parcel) :
194 mRotation(INVALID_ROTATION),
195 mSurfaceSetID(INVALID_SET_ID) {
196 readFromParcel(&parcel);
197 }
198
readFromParcel(const android::Parcel * parcel)199 status_t OutputConfiguration::readFromParcel(const android::Parcel* parcel) {
200 status_t err = OK;
201 int rotation = 0;
202
203 if (parcel == nullptr) return BAD_VALUE;
204
205 if ((err = parcel->readInt32(&rotation)) != OK) {
206 ALOGE("%s: Failed to read rotation from parcel", __FUNCTION__);
207 return err;
208 }
209
210 int setID = INVALID_SET_ID;
211 if ((err = parcel->readInt32(&setID)) != OK) {
212 ALOGE("%s: Failed to read surface set ID from parcel", __FUNCTION__);
213 return err;
214 }
215
216 int surfaceType = SURFACE_TYPE_UNKNOWN;
217 if ((err = parcel->readInt32(&surfaceType)) != OK) {
218 ALOGE("%s: Failed to read surface type from parcel", __FUNCTION__);
219 return err;
220 }
221
222 int width = 0;
223 if ((err = parcel->readInt32(&width)) != OK) {
224 ALOGE("%s: Failed to read surface width from parcel", __FUNCTION__);
225 return err;
226 }
227
228 int height = 0;
229 if ((err = parcel->readInt32(&height)) != OK) {
230 ALOGE("%s: Failed to read surface height from parcel", __FUNCTION__);
231 return err;
232 }
233
234 int isDeferred = 0;
235 if ((err = parcel->readInt32(&isDeferred)) != OK) {
236 ALOGE("%s: Failed to read surface isDeferred flag from parcel", __FUNCTION__);
237 return err;
238 }
239
240 int isShared = 0;
241 if ((err = parcel->readInt32(&isShared)) != OK) {
242 ALOGE("%s: Failed to read surface isShared flag from parcel", __FUNCTION__);
243 return err;
244 }
245
246 if (isDeferred && surfaceType != SURFACE_TYPE_SURFACE_VIEW &&
247 surfaceType != SURFACE_TYPE_SURFACE_TEXTURE) {
248 ALOGE("%s: Invalid surface type for deferred configuration", __FUNCTION__);
249 return BAD_VALUE;
250 }
251
252 std::vector<view::Surface> surfaceShims;
253 if ((err = parcel->readParcelableVector(&surfaceShims)) != OK) {
254 ALOGE("%s: Failed to read surface(s) from parcel", __FUNCTION__);
255 return err;
256 }
257
258 String16 physicalCameraId;
259 parcel->readString16(&physicalCameraId);
260 mPhysicalCameraId = toStdString(physicalCameraId);
261
262 int isMultiResolution = 0;
263 if ((err = parcel->readInt32(&isMultiResolution)) != OK) {
264 ALOGE("%s: Failed to read surface isMultiResolution flag from parcel", __FUNCTION__);
265 return err;
266 }
267
268 std::vector<int32_t> sensorPixelModesUsed;
269 if ((err = parcel->readInt32Vector(&sensorPixelModesUsed)) != OK) {
270 ALOGE("%s: Failed to read sensor pixel mode(s) from parcel", __FUNCTION__);
271 return err;
272 }
273 int64_t dynamicProfile;
274 if ((err = parcel->readInt64(&dynamicProfile)) != OK) {
275 ALOGE("%s: Failed to read surface dynamic range profile flag from parcel", __FUNCTION__);
276 return err;
277 }
278 int32_t colorSpace;
279 if ((err = parcel->readInt32(&colorSpace)) != OK) {
280 ALOGE("%s: Failed to read surface color space flag from parcel", __FUNCTION__);
281 return err;
282 }
283
284 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
285 if ((err = parcel->readInt64(&streamUseCase)) != OK) {
286 ALOGE("%s: Failed to read stream use case from parcel", __FUNCTION__);
287 return err;
288 }
289
290 int timestampBase = TIMESTAMP_BASE_DEFAULT;
291 if ((err = parcel->readInt32(×tampBase)) != OK) {
292 ALOGE("%s: Failed to read timestamp base from parcel", __FUNCTION__);
293 return err;
294 }
295
296 int mirrorMode = MIRROR_MODE_AUTO;
297 if ((err = parcel->readInt32(&mirrorMode)) != OK) {
298 ALOGE("%s: Failed to read mirroring mode from parcel", __FUNCTION__);
299 return err;
300 }
301
302 std::vector<int> mirrorModeForProducers;
303 if ((err = parcel->readInt32Vector(&mirrorModeForProducers)) != OK) {
304 ALOGE("%s: Failed to read mirroring mode for surfaces from parcel", __FUNCTION__);
305 return err;
306 }
307
308 int useReadoutTimestamp = 0;
309 if ((err = parcel->readInt32(&useReadoutTimestamp)) != OK) {
310 ALOGE("%s: Failed to read useReadoutTimestamp flag from parcel", __FUNCTION__);
311 return err;
312 }
313
314 int format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
315 if ((err = parcel->readInt32(&format)) != OK) {
316 ALOGE("%s: Failed to read format from parcel", __FUNCTION__);
317 return err;
318 }
319
320 int dataspace = 0;
321 if ((err = parcel->readInt32(&dataspace)) != OK) {
322 ALOGE("%s: Failed to read dataspace from parcel", __FUNCTION__);
323 return err;
324 }
325
326 int64_t usage = 0;
327 if ((err = parcel->readInt64(&usage)) != OK) {
328 ALOGE("%s: Failed to read usage flag from parcel", __FUNCTION__);
329 return err;
330 }
331
332 mRotation = rotation;
333 mSurfaceSetID = setID;
334 mSurfaceType = surfaceType;
335 mWidth = width;
336 mHeight = height;
337 mIsDeferred = isDeferred != 0;
338 mIsShared = isShared != 0;
339 mIsMultiResolution = isMultiResolution != 0;
340 mStreamUseCase = streamUseCase;
341 mTimestampBase = timestampBase;
342 mMirrorMode = mirrorMode;
343 mMirrorModeForProducers = std::move(mirrorModeForProducers);
344 mUseReadoutTimestamp = useReadoutTimestamp != 0;
345 for (auto& surface : surfaceShims) {
346 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
347 IF_ALOGV() {
348 uint64_t bufferID;
349 surface.getUniqueId(&bufferID);
350 ALOGV("%s: OutputConfiguration: %" PRIu64 ", name %s", __FUNCTION__,
351 bufferID, toString8(surface.name).c_str());
352 }
353 #else
354 ALOGV("%s: OutputConfiguration: %p, name %s", __FUNCTION__,
355 surface.graphicBufferProducer.get(),
356 toString8(surface.name).c_str());
357 #endif
358 mSurfaces.push_back(flagtools::toParcelableSurfaceType(surface));
359 }
360
361 mSensorPixelModesUsed = std::move(sensorPixelModesUsed);
362 mDynamicRangeProfile = dynamicProfile;
363 mColorSpace = colorSpace;
364 mFormat = format;
365 mDataspace = dataspace;
366 mUsage = usage;
367
368 ALOGV("%s: OutputConfiguration: rotation = %d, setId = %d, surfaceType = %d,"
369 " physicalCameraId = %s, isMultiResolution = %d, streamUseCase = %" PRId64
370 ", timestampBase = %d, mirrorMode = %d, useReadoutTimestamp = %d, format = %d, "
371 "dataspace = %d, usage = %" PRId64,
372 __FUNCTION__, mRotation, mSurfaceSetID, mSurfaceType,
373 mPhysicalCameraId.c_str(), mIsMultiResolution, mStreamUseCase, timestampBase,
374 mMirrorMode, mUseReadoutTimestamp, mFormat, mDataspace, mUsage);
375
376 return err;
377 }
378
OutputConfiguration(ParcelableSurfaceType & surface,int rotation,const std::string & physicalId,int surfaceSetID,bool isShared)379 OutputConfiguration::OutputConfiguration(ParcelableSurfaceType& surface, int rotation,
380 const std::string& physicalId,
381 int surfaceSetID, bool isShared) {
382 mSurfaces.push_back(surface);
383 mRotation = rotation;
384 mSurfaceSetID = surfaceSetID;
385 mIsDeferred = false;
386 mIsShared = isShared;
387 mPhysicalCameraId = physicalId;
388 mIsMultiResolution = false;
389 mDynamicRangeProfile = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
390 mColorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
391 mStreamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
392 mTimestampBase = TIMESTAMP_BASE_DEFAULT;
393 mMirrorMode = MIRROR_MODE_AUTO;
394 mMirrorModeForProducers.push_back(mMirrorMode);
395 mUseReadoutTimestamp = false;
396 mFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
397 mDataspace = 0;
398 mUsage = 0;
399 }
400
OutputConfiguration(const std::vector<ParcelableSurfaceType> & surfaces,int rotation,const std::string & physicalCameraId,int surfaceSetID,int surfaceType,int width,int height,bool isShared)401 OutputConfiguration::OutputConfiguration(
402 const std::vector<ParcelableSurfaceType>& surfaces,
403 int rotation, const std::string& physicalCameraId, int surfaceSetID, int surfaceType,
404 int width, int height, bool isShared)
405 : mSurfaces(surfaces), mRotation(rotation), mSurfaceSetID(surfaceSetID),
406 mSurfaceType(surfaceType), mWidth(width), mHeight(height), mIsDeferred(false),
407 mIsShared(isShared), mPhysicalCameraId(physicalCameraId), mIsMultiResolution(false),
408 mDynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
409 mColorSpace(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED),
410 mStreamUseCase(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT),
411 mTimestampBase(TIMESTAMP_BASE_DEFAULT),
412 mMirrorMode(MIRROR_MODE_AUTO), mMirrorModeForProducers(surfaces.size(), mMirrorMode),
413 mUseReadoutTimestamp(false), mFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
414 mDataspace(0), mUsage(0) { }
415
writeToParcel(android::Parcel * parcel) const416 status_t OutputConfiguration::writeToParcel(android::Parcel* parcel) const {
417
418 if (parcel == nullptr) return BAD_VALUE;
419 status_t err = OK;
420
421 err = parcel->writeInt32(mRotation);
422 if (err != OK) return err;
423
424 err = parcel->writeInt32(mSurfaceSetID);
425 if (err != OK) return err;
426
427 err = parcel->writeInt32(mSurfaceType);
428 if (err != OK) return err;
429
430 err = parcel->writeInt32(mWidth);
431 if (err != OK) return err;
432
433 err = parcel->writeInt32(mHeight);
434 if (err != OK) return err;
435
436 err = parcel->writeInt32(mIsDeferred ? 1 : 0);
437 if (err != OK) return err;
438
439 err = parcel->writeInt32(mIsShared ? 1 : 0);
440 if (err != OK) return err;
441
442 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
443 err = parcel->writeParcelableVector(mSurfaces);
444 #else
445 std::vector<view::Surface> surfaceShims;
446 for (auto& gbp : mSurfaces) {
447 view::Surface surfaceShim;
448 surfaceShim.name = String16("unknown_name"); // name of surface
449 surfaceShim.graphicBufferProducer = gbp;
450 surfaceShims.push_back(surfaceShim);
451 }
452 err = parcel->writeParcelableVector(surfaceShims);
453 #endif
454 if (err != OK) return err;
455
456 String16 physicalCameraId = toString16(mPhysicalCameraId);
457 err = parcel->writeString16(physicalCameraId);
458 if (err != OK) return err;
459
460 err = parcel->writeInt32(mIsMultiResolution ? 1 : 0);
461 if (err != OK) return err;
462
463 err = parcel->writeParcelableVector(mSensorPixelModesUsed);
464 if (err != OK) return err;
465
466 err = parcel->writeInt64(mDynamicRangeProfile);
467 if (err != OK) return err;
468
469 err = parcel->writeInt32(mColorSpace);
470 if (err != OK) return err;
471
472 err = parcel->writeInt64(mStreamUseCase);
473 if (err != OK) return err;
474
475 err = parcel->writeInt32(mTimestampBase);
476 if (err != OK) return err;
477
478 err = parcel->writeInt32(mMirrorMode);
479 if (err != OK) return err;
480
481 err = parcel->writeInt32Vector(mMirrorModeForProducers);
482 if (err != OK) return err;
483
484 err = parcel->writeInt32(mUseReadoutTimestamp ? 1 : 0);
485 if (err != OK) return err;
486
487 err = parcel->writeInt32(mFormat);
488 if (err != OK) return err;
489
490 err = parcel->writeInt32(mDataspace);
491 if (err != OK) return err;
492
493 err = parcel->writeInt64(mUsage);
494 if (err != OK) return err;
495
496 return OK;
497 }
498
499 template <typename T>
simpleVectorsEqual(T first,T second)500 static bool simpleVectorsEqual(T first, T second) {
501 if (first.size() != second.size()) {
502 return false;
503 }
504
505 for (size_t i = 0; i < first.size(); i++) {
506 if (first[i] != second[i]) {
507 return false;
508 }
509 }
510 return true;
511 }
512
513 template <typename T>
simpleVectorsLessThan(T first,T second)514 static bool simpleVectorsLessThan(T first, T second) {
515 if (first.size() != second.size()) {
516 return first.size() < second.size();
517 }
518
519 for (size_t i = 0; i < first.size(); i++) {
520 if (first[i] != second[i]) {
521 return first[i] < second[i];
522 }
523 }
524 return false;
525 }
526
surfacesEqual(const OutputConfiguration & other) const527 bool OutputConfiguration::surfacesEqual(const OutputConfiguration& other) const {
528 const std::vector<ParcelableSurfaceType>& otherSurfaces = other.getSurfaces();
529 return simpleVectorsEqual(otherSurfaces, mSurfaces);
530 }
531
sensorPixelModesUsedEqual(const OutputConfiguration & other) const532 bool OutputConfiguration::sensorPixelModesUsedEqual(const OutputConfiguration& other) const {
533 const std::vector<int32_t>& othersensorPixelModesUsed = other.getSensorPixelModesUsed();
534 return simpleVectorsEqual(othersensorPixelModesUsed, mSensorPixelModesUsed);
535 }
536
mirrorModesEqual(const OutputConfiguration & other) const537 bool OutputConfiguration::mirrorModesEqual(const OutputConfiguration& other) const {
538 const std::vector<int>& otherMirrorModes = other.getMirrorModes();
539 return simpleVectorsEqual(otherMirrorModes, mMirrorModeForProducers);
540 }
541
sensorPixelModesUsedLessThan(const OutputConfiguration & other) const542 bool OutputConfiguration::sensorPixelModesUsedLessThan(const OutputConfiguration& other) const {
543 const std::vector<int32_t>& spms = other.getSensorPixelModesUsed();
544 return simpleVectorsLessThan(mSensorPixelModesUsed, spms);
545 }
546
mirrorModesLessThan(const OutputConfiguration & other) const547 bool OutputConfiguration::mirrorModesLessThan(const OutputConfiguration& other) const {
548 const std::vector<int>& otherMirrorModes = other.getMirrorModes();
549 return simpleVectorsLessThan(mMirrorModeForProducers, otherMirrorModes);
550 }
551
surfacesLessThan(const OutputConfiguration & other) const552 bool OutputConfiguration::surfacesLessThan(const OutputConfiguration& other) const {
553 const std::vector<ParcelableSurfaceType>& otherSurfaces = other.getSurfaces();
554
555 if (mSurfaces.size() != otherSurfaces.size()) {
556 return mSurfaces.size() < otherSurfaces.size();
557 }
558
559 for (size_t i = 0; i < mSurfaces.size(); i++) {
560 if (mSurfaces[i] != otherSurfaces[i]) {
561 return mSurfaces[i] < otherSurfaces[i];
562 }
563 }
564
565 return false;
566 }
567 }; // namespace android
568