• 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 <camera/StringUtils.h>
25 #include <binder/Parcel.h>
26 #include <gui/view/Surface.h>
27 #include <system/camera_metadata.h>
28 #include <system/graphics.h>
29 #include <utils/String8.h>
30 
31 
32 namespace android {
33 
34 const int OutputConfiguration::INVALID_ROTATION = -1;
35 const int OutputConfiguration::INVALID_SET_ID = -1;
36 
37 const std::vector<sp<IGraphicBufferProducer>>&
getGraphicBufferProducers() const38         OutputConfiguration::getGraphicBufferProducers() const {
39     return mGbps;
40 }
41 
getRotation() const42 int OutputConfiguration::getRotation() const {
43     return mRotation;
44 }
45 
getSurfaceSetID() const46 int OutputConfiguration::getSurfaceSetID() const {
47     return mSurfaceSetID;
48 }
49 
getSurfaceType() const50 int OutputConfiguration::getSurfaceType() const {
51     return mSurfaceType;
52 }
53 
getWidth() const54 int OutputConfiguration::getWidth() const {
55     return mWidth;
56 }
57 
getHeight() const58 int OutputConfiguration::getHeight() const {
59     return mHeight;
60 }
61 
isDeferred() const62 bool OutputConfiguration::isDeferred() const {
63     return mIsDeferred;
64 }
65 
isShared() const66 bool OutputConfiguration::isShared() const {
67     return mIsShared;
68 }
69 
getPhysicalCameraId() const70 std::string OutputConfiguration::getPhysicalCameraId() const {
71     return mPhysicalCameraId;
72 }
73 
isMultiResolution() const74 bool OutputConfiguration::isMultiResolution() const {
75     return mIsMultiResolution;
76 }
77 
getSensorPixelModesUsed() const78 const std::vector<int32_t> &OutputConfiguration::getSensorPixelModesUsed() const {
79     return mSensorPixelModesUsed;
80 }
81 
getDynamicRangeProfile() const82 int64_t OutputConfiguration::getDynamicRangeProfile() const {
83     return mDynamicRangeProfile;
84 }
85 
getColorSpace() const86 int32_t OutputConfiguration::getColorSpace() const {
87     return mColorSpace;
88 }
89 
getStreamUseCase() const90 int64_t OutputConfiguration::getStreamUseCase() const {
91     return mStreamUseCase;
92 }
93 
getTimestampBase() const94 int OutputConfiguration::getTimestampBase() const {
95     return mTimestampBase;
96 }
97 
getMirrorMode() const98 int OutputConfiguration::getMirrorMode() const {
99     return mMirrorMode;
100 }
101 
useReadoutTimestamp() const102 bool OutputConfiguration::useReadoutTimestamp() const {
103     return mUseReadoutTimestamp;
104 }
105 
getFormat() const106 int OutputConfiguration::getFormat() const {
107     return mFormat;
108 }
109 
getDataspace() const110 int OutputConfiguration::getDataspace() const {
111     return mDataspace;
112 }
113 
getUsage() const114 int64_t OutputConfiguration::getUsage() const {
115     return mUsage;
116 }
117 
isComplete() const118 bool OutputConfiguration::isComplete() const {
119     return !((mSurfaceType == SURFACE_TYPE_MEDIA_RECORDER ||
120              mSurfaceType == SURFACE_TYPE_MEDIA_CODEC ||
121              mSurfaceType == SURFACE_TYPE_IMAGE_READER) &&
122              mGbps.empty());
123 }
124 
OutputConfiguration()125 OutputConfiguration::OutputConfiguration() :
126         mRotation(INVALID_ROTATION),
127         mSurfaceSetID(INVALID_SET_ID),
128         mSurfaceType(SURFACE_TYPE_UNKNOWN),
129         mWidth(0),
130         mHeight(0),
131         mIsDeferred(false),
132         mIsShared(false),
133         mIsMultiResolution(false),
134         mDynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
135         mColorSpace(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED),
136         mStreamUseCase(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT),
137         mTimestampBase(TIMESTAMP_BASE_DEFAULT),
138         mMirrorMode(MIRROR_MODE_AUTO),
139         mUseReadoutTimestamp(false),
140         mFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
141         mDataspace(0),
142         mUsage(0) {
143 }
144 
OutputConfiguration(const android::Parcel & parcel)145 OutputConfiguration::OutputConfiguration(const android::Parcel& parcel) :
146         mRotation(INVALID_ROTATION),
147         mSurfaceSetID(INVALID_SET_ID) {
148     readFromParcel(&parcel);
149 }
150 
readFromParcel(const android::Parcel * parcel)151 status_t OutputConfiguration::readFromParcel(const android::Parcel* parcel) {
152     status_t err = OK;
153     int rotation = 0;
154 
155     if (parcel == nullptr) return BAD_VALUE;
156 
157     if ((err = parcel->readInt32(&rotation)) != OK) {
158         ALOGE("%s: Failed to read rotation from parcel", __FUNCTION__);
159         return err;
160     }
161 
162     int setID = INVALID_SET_ID;
163     if ((err = parcel->readInt32(&setID)) != OK) {
164         ALOGE("%s: Failed to read surface set ID from parcel", __FUNCTION__);
165         return err;
166     }
167 
168     int surfaceType = SURFACE_TYPE_UNKNOWN;
169     if ((err = parcel->readInt32(&surfaceType)) != OK) {
170         ALOGE("%s: Failed to read surface type from parcel", __FUNCTION__);
171         return err;
172     }
173 
174     int width = 0;
175     if ((err = parcel->readInt32(&width)) != OK) {
176         ALOGE("%s: Failed to read surface width from parcel", __FUNCTION__);
177         return err;
178     }
179 
180     int height = 0;
181     if ((err = parcel->readInt32(&height)) != OK) {
182         ALOGE("%s: Failed to read surface height from parcel", __FUNCTION__);
183         return err;
184     }
185 
186     int isDeferred = 0;
187     if ((err = parcel->readInt32(&isDeferred)) != OK) {
188         ALOGE("%s: Failed to read surface isDeferred flag from parcel", __FUNCTION__);
189         return err;
190     }
191 
192     int isShared = 0;
193     if ((err = parcel->readInt32(&isShared)) != OK) {
194         ALOGE("%s: Failed to read surface isShared flag from parcel", __FUNCTION__);
195         return err;
196     }
197 
198     if (isDeferred && surfaceType != SURFACE_TYPE_SURFACE_VIEW &&
199             surfaceType != SURFACE_TYPE_SURFACE_TEXTURE) {
200         ALOGE("%s: Invalid surface type for deferred configuration", __FUNCTION__);
201         return BAD_VALUE;
202     }
203 
204     std::vector<view::Surface> surfaceShims;
205     if ((err = parcel->readParcelableVector(&surfaceShims)) != OK) {
206         ALOGE("%s: Failed to read surface(s) from parcel", __FUNCTION__);
207         return err;
208     }
209 
210     String16 physicalCameraId;
211     parcel->readString16(&physicalCameraId);
212     mPhysicalCameraId = toStdString(physicalCameraId);
213 
214     int isMultiResolution = 0;
215     if ((err = parcel->readInt32(&isMultiResolution)) != OK) {
216         ALOGE("%s: Failed to read surface isMultiResolution flag from parcel", __FUNCTION__);
217         return err;
218     }
219 
220     std::vector<int32_t> sensorPixelModesUsed;
221     if ((err = parcel->readInt32Vector(&sensorPixelModesUsed)) != OK) {
222         ALOGE("%s: Failed to read sensor pixel mode(s) from parcel", __FUNCTION__);
223         return err;
224     }
225     int64_t dynamicProfile;
226     if ((err = parcel->readInt64(&dynamicProfile)) != OK) {
227         ALOGE("%s: Failed to read surface dynamic range profile flag from parcel", __FUNCTION__);
228         return err;
229     }
230     int32_t colorSpace;
231     if ((err = parcel->readInt32(&colorSpace)) != OK) {
232         ALOGE("%s: Failed to read surface color space flag from parcel", __FUNCTION__);
233         return err;
234     }
235 
236     int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
237     if ((err = parcel->readInt64(&streamUseCase)) != OK) {
238         ALOGE("%s: Failed to read stream use case from parcel", __FUNCTION__);
239         return err;
240     }
241 
242     int timestampBase = TIMESTAMP_BASE_DEFAULT;
243     if ((err = parcel->readInt32(&timestampBase)) != OK) {
244         ALOGE("%s: Failed to read timestamp base from parcel", __FUNCTION__);
245         return err;
246     }
247 
248     int mirrorMode = MIRROR_MODE_AUTO;
249     if ((err = parcel->readInt32(&mirrorMode)) != OK) {
250         ALOGE("%s: Failed to read mirroring mode from parcel", __FUNCTION__);
251         return err;
252     }
253 
254     int useReadoutTimestamp = 0;
255     if ((err = parcel->readInt32(&useReadoutTimestamp)) != OK) {
256         ALOGE("%s: Failed to read useReadoutTimestamp flag from parcel", __FUNCTION__);
257         return err;
258     }
259 
260     int format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
261     if ((err = parcel->readInt32(&format)) != OK) {
262         ALOGE("%s: Failed to read format from parcel", __FUNCTION__);
263         return err;
264     }
265 
266     int dataspace = 0;
267     if ((err = parcel->readInt32(&dataspace)) != OK) {
268         ALOGE("%s: Failed to read dataspace from parcel", __FUNCTION__);
269         return err;
270     }
271 
272     int64_t usage = 0;
273     if ((err = parcel->readInt64(&usage)) != OK) {
274         ALOGE("%s: Failed to read usage flag from parcel", __FUNCTION__);
275         return err;
276     }
277 
278     mRotation = rotation;
279     mSurfaceSetID = setID;
280     mSurfaceType = surfaceType;
281     mWidth = width;
282     mHeight = height;
283     mIsDeferred = isDeferred != 0;
284     mIsShared = isShared != 0;
285     mIsMultiResolution = isMultiResolution != 0;
286     mStreamUseCase = streamUseCase;
287     mTimestampBase = timestampBase;
288     mMirrorMode = mirrorMode;
289     mUseReadoutTimestamp = useReadoutTimestamp != 0;
290     for (auto& surface : surfaceShims) {
291         ALOGV("%s: OutputConfiguration: %p, name %s", __FUNCTION__,
292                 surface.graphicBufferProducer.get(),
293                 toString8(surface.name).c_str());
294         mGbps.push_back(surface.graphicBufferProducer);
295     }
296 
297     mSensorPixelModesUsed = std::move(sensorPixelModesUsed);
298     mDynamicRangeProfile = dynamicProfile;
299     mColorSpace = colorSpace;
300     mFormat = format;
301     mDataspace = dataspace;
302     mUsage = usage;
303 
304     ALOGV("%s: OutputConfiguration: rotation = %d, setId = %d, surfaceType = %d,"
305           " physicalCameraId = %s, isMultiResolution = %d, streamUseCase = %" PRId64
306           ", timestampBase = %d, mirrorMode = %d, useReadoutTimestamp = %d, format = %d, "
307           "dataspace = %d, usage = %" PRId64,
308           __FUNCTION__, mRotation, mSurfaceSetID, mSurfaceType,
309           mPhysicalCameraId.c_str(), mIsMultiResolution, mStreamUseCase, timestampBase,
310           mMirrorMode, mUseReadoutTimestamp, mFormat, mDataspace, mUsage);
311 
312     return err;
313 }
314 
OutputConfiguration(sp<IGraphicBufferProducer> & gbp,int rotation,const std::string & physicalId,int surfaceSetID,bool isShared)315 OutputConfiguration::OutputConfiguration(sp<IGraphicBufferProducer>& gbp, int rotation,
316         const std::string& physicalId,
317         int surfaceSetID, bool isShared) {
318     mGbps.push_back(gbp);
319     mRotation = rotation;
320     mSurfaceSetID = surfaceSetID;
321     mIsDeferred = false;
322     mIsShared = isShared;
323     mPhysicalCameraId = physicalId;
324     mIsMultiResolution = false;
325     mDynamicRangeProfile = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
326     mColorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
327     mStreamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
328     mTimestampBase = TIMESTAMP_BASE_DEFAULT;
329     mMirrorMode = MIRROR_MODE_AUTO;
330     mUseReadoutTimestamp = false;
331     mFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
332     mDataspace = 0;
333     mUsage = 0;
334 }
335 
OutputConfiguration(const std::vector<sp<IGraphicBufferProducer>> & gbps,int rotation,const std::string & physicalCameraId,int surfaceSetID,int surfaceType,int width,int height,bool isShared)336 OutputConfiguration::OutputConfiguration(
337         const std::vector<sp<IGraphicBufferProducer>>& gbps,
338     int rotation, const std::string& physicalCameraId, int surfaceSetID,  int surfaceType,
339     int width, int height, bool isShared)
340   : mGbps(gbps), mRotation(rotation), mSurfaceSetID(surfaceSetID), mSurfaceType(surfaceType),
341     mWidth(width), mHeight(height), mIsDeferred(false), mIsShared(isShared),
342     mPhysicalCameraId(physicalCameraId), mIsMultiResolution(false),
343     mDynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
344     mColorSpace(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED),
345     mStreamUseCase(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT),
346     mTimestampBase(TIMESTAMP_BASE_DEFAULT),
347     mMirrorMode(MIRROR_MODE_AUTO), mUseReadoutTimestamp(false),
348     mFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED), mDataspace(0),
349     mUsage(0) { }
350 
writeToParcel(android::Parcel * parcel) const351 status_t OutputConfiguration::writeToParcel(android::Parcel* parcel) const {
352 
353     if (parcel == nullptr) return BAD_VALUE;
354     status_t err = OK;
355 
356     err = parcel->writeInt32(mRotation);
357     if (err != OK) return err;
358 
359     err = parcel->writeInt32(mSurfaceSetID);
360     if (err != OK) return err;
361 
362     err = parcel->writeInt32(mSurfaceType);
363     if (err != OK) return err;
364 
365     err = parcel->writeInt32(mWidth);
366     if (err != OK) return err;
367 
368     err = parcel->writeInt32(mHeight);
369     if (err != OK) return err;
370 
371     err = parcel->writeInt32(mIsDeferred ? 1 : 0);
372     if (err != OK) return err;
373 
374     err = parcel->writeInt32(mIsShared ? 1 : 0);
375     if (err != OK) return err;
376 
377     std::vector<view::Surface> surfaceShims;
378     for (auto& gbp : mGbps) {
379         view::Surface surfaceShim;
380         surfaceShim.name = String16("unknown_name"); // name of surface
381         surfaceShim.graphicBufferProducer = gbp;
382         surfaceShims.push_back(surfaceShim);
383     }
384     err = parcel->writeParcelableVector(surfaceShims);
385     if (err != OK) return err;
386 
387     String16 physicalCameraId = toString16(mPhysicalCameraId);
388     err = parcel->writeString16(physicalCameraId);
389     if (err != OK) return err;
390 
391     err = parcel->writeInt32(mIsMultiResolution ? 1 : 0);
392     if (err != OK) return err;
393 
394     err = parcel->writeParcelableVector(mSensorPixelModesUsed);
395     if (err != OK) return err;
396 
397     err = parcel->writeInt64(mDynamicRangeProfile);
398     if (err != OK) return err;
399 
400     err = parcel->writeInt32(mColorSpace);
401     if (err != OK) return err;
402 
403     err = parcel->writeInt64(mStreamUseCase);
404     if (err != OK) return err;
405 
406     err = parcel->writeInt32(mTimestampBase);
407     if (err != OK) return err;
408 
409     err = parcel->writeInt32(mMirrorMode);
410     if (err != OK) return err;
411 
412     err = parcel->writeInt32(mUseReadoutTimestamp ? 1 : 0);
413     if (err != OK) return err;
414 
415     err = parcel->writeInt32(mFormat);
416     if (err != OK) return err;
417 
418     err = parcel->writeInt32(mDataspace);
419     if (err != OK) return err;
420 
421     err = parcel->writeInt64(mUsage);
422     if (err != OK) return err;
423 
424     return OK;
425 }
426 
427 template <typename T>
simpleVectorsEqual(T first,T second)428 static bool simpleVectorsEqual(T first, T second) {
429     if (first.size() != second.size()) {
430         return false;
431     }
432 
433     for (size_t i = 0; i < first.size(); i++) {
434         if (first[i] != second[i]) {
435             return false;
436         }
437     }
438     return true;
439 }
440 
gbpsEqual(const OutputConfiguration & other) const441 bool OutputConfiguration::gbpsEqual(const OutputConfiguration& other) const {
442     const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
443             other.getGraphicBufferProducers();
444     return simpleVectorsEqual(otherGbps, mGbps);
445 }
446 
sensorPixelModesUsedEqual(const OutputConfiguration & other) const447 bool OutputConfiguration::sensorPixelModesUsedEqual(const OutputConfiguration& other) const {
448     const std::vector<int32_t>& othersensorPixelModesUsed = other.getSensorPixelModesUsed();
449     return simpleVectorsEqual(othersensorPixelModesUsed, mSensorPixelModesUsed);
450 }
451 
sensorPixelModesUsedLessThan(const OutputConfiguration & other) const452 bool OutputConfiguration::sensorPixelModesUsedLessThan(const OutputConfiguration& other) const {
453     const std::vector<int32_t>& spms = other.getSensorPixelModesUsed();
454 
455     if (mSensorPixelModesUsed.size() !=  spms.size()) {
456         return mSensorPixelModesUsed.size() < spms.size();
457     }
458 
459     for (size_t i = 0; i < spms.size(); i++) {
460         if (mSensorPixelModesUsed[i] != spms[i]) {
461             return mSensorPixelModesUsed[i] < spms[i];
462         }
463     }
464 
465     return false;
466 }
467 
gbpsLessThan(const OutputConfiguration & other) const468 bool OutputConfiguration::gbpsLessThan(const OutputConfiguration& other) const {
469     const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
470             other.getGraphicBufferProducers();
471 
472     if (mGbps.size() !=  otherGbps.size()) {
473         return mGbps.size() < otherGbps.size();
474     }
475 
476     for (size_t i = 0; i < mGbps.size(); i++) {
477         if (mGbps[i] != otherGbps[i]) {
478             return mGbps[i] < otherGbps[i];
479         }
480     }
481 
482     return false;
483 }
484 }; // namespace android
485