xref: /aosp_15_r20/frameworks/av/camera/camera2/OutputConfiguration.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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(&timestampBase)) != 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         ALOGV("%s: OutputConfiguration: %p, name %s", __FUNCTION__,
347                 surface.graphicBufferProducer.get(),
348                 toString8(surface.name).c_str());
349         mSurfaces.push_back(flagtools::toParcelableSurfaceType(surface));
350     }
351 
352     mSensorPixelModesUsed = std::move(sensorPixelModesUsed);
353     mDynamicRangeProfile = dynamicProfile;
354     mColorSpace = colorSpace;
355     mFormat = format;
356     mDataspace = dataspace;
357     mUsage = usage;
358 
359     ALOGV("%s: OutputConfiguration: rotation = %d, setId = %d, surfaceType = %d,"
360           " physicalCameraId = %s, isMultiResolution = %d, streamUseCase = %" PRId64
361           ", timestampBase = %d, mirrorMode = %d, useReadoutTimestamp = %d, format = %d, "
362           "dataspace = %d, usage = %" PRId64,
363           __FUNCTION__, mRotation, mSurfaceSetID, mSurfaceType,
364           mPhysicalCameraId.c_str(), mIsMultiResolution, mStreamUseCase, timestampBase,
365           mMirrorMode, mUseReadoutTimestamp, mFormat, mDataspace, mUsage);
366 
367     return err;
368 }
369 
OutputConfiguration(ParcelableSurfaceType & surface,int rotation,const std::string & physicalId,int surfaceSetID,bool isShared)370 OutputConfiguration::OutputConfiguration(ParcelableSurfaceType& surface, int rotation,
371         const std::string& physicalId,
372         int surfaceSetID, bool isShared) {
373     mSurfaces.push_back(surface);
374     mRotation = rotation;
375     mSurfaceSetID = surfaceSetID;
376     mIsDeferred = false;
377     mIsShared = isShared;
378     mPhysicalCameraId = physicalId;
379     mIsMultiResolution = false;
380     mDynamicRangeProfile = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
381     mColorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
382     mStreamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
383     mTimestampBase = TIMESTAMP_BASE_DEFAULT;
384     mMirrorMode = MIRROR_MODE_AUTO;
385     mMirrorModeForProducers.push_back(mMirrorMode);
386     mUseReadoutTimestamp = false;
387     mFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
388     mDataspace = 0;
389     mUsage = 0;
390 }
391 
OutputConfiguration(const std::vector<ParcelableSurfaceType> & surfaces,int rotation,const std::string & physicalCameraId,int surfaceSetID,int surfaceType,int width,int height,bool isShared)392 OutputConfiguration::OutputConfiguration(
393         const std::vector<ParcelableSurfaceType>& surfaces,
394     int rotation, const std::string& physicalCameraId, int surfaceSetID,  int surfaceType,
395     int width, int height, bool isShared)
396   : mSurfaces(surfaces), mRotation(rotation), mSurfaceSetID(surfaceSetID),
397     mSurfaceType(surfaceType), mWidth(width), mHeight(height), mIsDeferred(false),
398     mIsShared(isShared), mPhysicalCameraId(physicalCameraId), mIsMultiResolution(false),
399     mDynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
400     mColorSpace(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED),
401     mStreamUseCase(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT),
402     mTimestampBase(TIMESTAMP_BASE_DEFAULT),
403     mMirrorMode(MIRROR_MODE_AUTO), mMirrorModeForProducers(surfaces.size(), mMirrorMode),
404     mUseReadoutTimestamp(false), mFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
405     mDataspace(0), mUsage(0) { }
406 
writeToParcel(android::Parcel * parcel) const407 status_t OutputConfiguration::writeToParcel(android::Parcel* parcel) const {
408 
409     if (parcel == nullptr) return BAD_VALUE;
410     status_t err = OK;
411 
412     err = parcel->writeInt32(mRotation);
413     if (err != OK) return err;
414 
415     err = parcel->writeInt32(mSurfaceSetID);
416     if (err != OK) return err;
417 
418     err = parcel->writeInt32(mSurfaceType);
419     if (err != OK) return err;
420 
421     err = parcel->writeInt32(mWidth);
422     if (err != OK) return err;
423 
424     err = parcel->writeInt32(mHeight);
425     if (err != OK) return err;
426 
427     err = parcel->writeInt32(mIsDeferred ? 1 : 0);
428     if (err != OK) return err;
429 
430     err = parcel->writeInt32(mIsShared ? 1 : 0);
431     if (err != OK) return err;
432 
433 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
434     err = parcel->writeParcelableVector(mSurfaces);
435 #else
436     std::vector<view::Surface> surfaceShims;
437     for (auto& gbp : mSurfaces) {
438         view::Surface surfaceShim;
439         surfaceShim.name = String16("unknown_name"); // name of surface
440         surfaceShim.graphicBufferProducer = gbp;
441         surfaceShims.push_back(surfaceShim);
442     }
443     err = parcel->writeParcelableVector(surfaceShims);
444 #endif
445     if (err != OK) return err;
446 
447     String16 physicalCameraId = toString16(mPhysicalCameraId);
448     err = parcel->writeString16(physicalCameraId);
449     if (err != OK) return err;
450 
451     err = parcel->writeInt32(mIsMultiResolution ? 1 : 0);
452     if (err != OK) return err;
453 
454     err = parcel->writeParcelableVector(mSensorPixelModesUsed);
455     if (err != OK) return err;
456 
457     err = parcel->writeInt64(mDynamicRangeProfile);
458     if (err != OK) return err;
459 
460     err = parcel->writeInt32(mColorSpace);
461     if (err != OK) return err;
462 
463     err = parcel->writeInt64(mStreamUseCase);
464     if (err != OK) return err;
465 
466     err = parcel->writeInt32(mTimestampBase);
467     if (err != OK) return err;
468 
469     err = parcel->writeInt32(mMirrorMode);
470     if (err != OK) return err;
471 
472     err = parcel->writeInt32Vector(mMirrorModeForProducers);
473     if (err != OK) return err;
474 
475     err = parcel->writeInt32(mUseReadoutTimestamp ? 1 : 0);
476     if (err != OK) return err;
477 
478     err = parcel->writeInt32(mFormat);
479     if (err != OK) return err;
480 
481     err = parcel->writeInt32(mDataspace);
482     if (err != OK) return err;
483 
484     err = parcel->writeInt64(mUsage);
485     if (err != OK) return err;
486 
487     return OK;
488 }
489 
490 template <typename T>
simpleVectorsEqual(T first,T second)491 static bool simpleVectorsEqual(T first, T second) {
492     if (first.size() != second.size()) {
493         return false;
494     }
495 
496     for (size_t i = 0; i < first.size(); i++) {
497         if (first[i] != second[i]) {
498             return false;
499         }
500     }
501     return true;
502 }
503 
504 template <typename T>
simpleVectorsLessThan(T first,T second)505 static bool simpleVectorsLessThan(T first, T second) {
506     if (first.size() != second.size()) {
507         return first.size() < second.size();
508     }
509 
510     for (size_t i = 0; i < first.size(); i++) {
511         if (first[i] != second[i]) {
512             return first[i] < second[i];
513         }
514     }
515     return false;
516 }
517 
surfacesEqual(const OutputConfiguration & other) const518 bool OutputConfiguration::surfacesEqual(const OutputConfiguration& other) const {
519     const std::vector<ParcelableSurfaceType>& otherSurfaces = other.getSurfaces();
520     return simpleVectorsEqual(otherSurfaces, mSurfaces);
521 }
522 
sensorPixelModesUsedEqual(const OutputConfiguration & other) const523 bool OutputConfiguration::sensorPixelModesUsedEqual(const OutputConfiguration& other) const {
524     const std::vector<int32_t>& othersensorPixelModesUsed = other.getSensorPixelModesUsed();
525     return simpleVectorsEqual(othersensorPixelModesUsed, mSensorPixelModesUsed);
526 }
527 
mirrorModesEqual(const OutputConfiguration & other) const528 bool OutputConfiguration::mirrorModesEqual(const OutputConfiguration& other) const {
529     const std::vector<int>& otherMirrorModes = other.getMirrorModes();
530     return simpleVectorsEqual(otherMirrorModes, mMirrorModeForProducers);
531 }
532 
sensorPixelModesUsedLessThan(const OutputConfiguration & other) const533 bool OutputConfiguration::sensorPixelModesUsedLessThan(const OutputConfiguration& other) const {
534     const std::vector<int32_t>& spms = other.getSensorPixelModesUsed();
535     return simpleVectorsLessThan(mSensorPixelModesUsed, spms);
536 }
537 
mirrorModesLessThan(const OutputConfiguration & other) const538 bool OutputConfiguration::mirrorModesLessThan(const OutputConfiguration& other) const {
539     const std::vector<int>& otherMirrorModes = other.getMirrorModes();
540     return simpleVectorsLessThan(mMirrorModeForProducers, otherMirrorModes);
541 }
542 
surfacesLessThan(const OutputConfiguration & other) const543 bool OutputConfiguration::surfacesLessThan(const OutputConfiguration& other) const {
544     const std::vector<ParcelableSurfaceType>& otherSurfaces = other.getSurfaces();
545 
546     if (mSurfaces.size() != otherSurfaces.size()) {
547         return mSurfaces.size() < otherSurfaces.size();
548     }
549 
550     for (size_t i = 0; i < mSurfaces.size(); i++) {
551         if (mSurfaces[i] != otherSurfaces[i]) {
552             return mSurfaces[i] < otherSurfaces[i];
553         }
554     }
555 
556     return false;
557 }
558 }; // namespace android
559