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
30 namespace android {
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
getColorSpace() const84 int32_t OutputConfiguration::getColorSpace() const {
85 return mColorSpace;
86 }
87
getStreamUseCase() const88 int64_t OutputConfiguration::getStreamUseCase() const {
89 return mStreamUseCase;
90 }
91
getTimestampBase() const92 int OutputConfiguration::getTimestampBase() const {
93 return mTimestampBase;
94 }
95
getMirrorMode() const96 int OutputConfiguration::getMirrorMode() const {
97 return mMirrorMode;
98 }
99
useReadoutTimestamp() const100 bool OutputConfiguration::useReadoutTimestamp() const {
101 return mUseReadoutTimestamp;
102 }
103
OutputConfiguration()104 OutputConfiguration::OutputConfiguration() :
105 mRotation(INVALID_ROTATION),
106 mSurfaceSetID(INVALID_SET_ID),
107 mSurfaceType(SURFACE_TYPE_UNKNOWN),
108 mWidth(0),
109 mHeight(0),
110 mIsDeferred(false),
111 mIsShared(false),
112 mIsMultiResolution(false),
113 mDynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
114 mColorSpace(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED),
115 mStreamUseCase(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT),
116 mTimestampBase(TIMESTAMP_BASE_DEFAULT),
117 mMirrorMode(MIRROR_MODE_AUTO),
118 mUseReadoutTimestamp(false) {
119 }
120
OutputConfiguration(const android::Parcel & parcel)121 OutputConfiguration::OutputConfiguration(const android::Parcel& parcel) :
122 mRotation(INVALID_ROTATION),
123 mSurfaceSetID(INVALID_SET_ID) {
124 readFromParcel(&parcel);
125 }
126
readFromParcel(const android::Parcel * parcel)127 status_t OutputConfiguration::readFromParcel(const android::Parcel* parcel) {
128 status_t err = OK;
129 int rotation = 0;
130
131 if (parcel == nullptr) return BAD_VALUE;
132
133 if ((err = parcel->readInt32(&rotation)) != OK) {
134 ALOGE("%s: Failed to read rotation from parcel", __FUNCTION__);
135 return err;
136 }
137
138 int setID = INVALID_SET_ID;
139 if ((err = parcel->readInt32(&setID)) != OK) {
140 ALOGE("%s: Failed to read surface set ID from parcel", __FUNCTION__);
141 return err;
142 }
143
144 int surfaceType = SURFACE_TYPE_UNKNOWN;
145 if ((err = parcel->readInt32(&surfaceType)) != OK) {
146 ALOGE("%s: Failed to read surface type from parcel", __FUNCTION__);
147 return err;
148 }
149
150 int width = 0;
151 if ((err = parcel->readInt32(&width)) != OK) {
152 ALOGE("%s: Failed to read surface width from parcel", __FUNCTION__);
153 return err;
154 }
155
156 int height = 0;
157 if ((err = parcel->readInt32(&height)) != OK) {
158 ALOGE("%s: Failed to read surface height from parcel", __FUNCTION__);
159 return err;
160 }
161
162 int isDeferred = 0;
163 if ((err = parcel->readInt32(&isDeferred)) != OK) {
164 ALOGE("%s: Failed to read surface isDeferred flag from parcel", __FUNCTION__);
165 return err;
166 }
167
168 int isShared = 0;
169 if ((err = parcel->readInt32(&isShared)) != OK) {
170 ALOGE("%s: Failed to read surface isShared flag from parcel", __FUNCTION__);
171 return err;
172 }
173
174 if (isDeferred && surfaceType != SURFACE_TYPE_SURFACE_VIEW &&
175 surfaceType != SURFACE_TYPE_SURFACE_TEXTURE) {
176 ALOGE("%s: Invalid surface type for deferred configuration", __FUNCTION__);
177 return BAD_VALUE;
178 }
179
180 std::vector<view::Surface> surfaceShims;
181 if ((err = parcel->readParcelableVector(&surfaceShims)) != OK) {
182 ALOGE("%s: Failed to read surface(s) from parcel", __FUNCTION__);
183 return err;
184 }
185
186 parcel->readString16(&mPhysicalCameraId);
187
188 int isMultiResolution = 0;
189 if ((err = parcel->readInt32(&isMultiResolution)) != OK) {
190 ALOGE("%s: Failed to read surface isMultiResolution flag from parcel", __FUNCTION__);
191 return err;
192 }
193
194 std::vector<int32_t> sensorPixelModesUsed;
195 if ((err = parcel->readInt32Vector(&sensorPixelModesUsed)) != OK) {
196 ALOGE("%s: Failed to read sensor pixel mode(s) from parcel", __FUNCTION__);
197 return err;
198 }
199 int64_t dynamicProfile;
200 if ((err = parcel->readInt64(&dynamicProfile)) != OK) {
201 ALOGE("%s: Failed to read surface dynamic range profile flag from parcel", __FUNCTION__);
202 return err;
203 }
204 int32_t colorSpace;
205 if ((err = parcel->readInt32(&colorSpace)) != OK) {
206 ALOGE("%s: Failed to read surface color space flag from parcel", __FUNCTION__);
207 return err;
208 }
209
210 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
211 if ((err = parcel->readInt64(&streamUseCase)) != OK) {
212 ALOGE("%s: Failed to read stream use case from parcel", __FUNCTION__);
213 return err;
214 }
215
216 int timestampBase = TIMESTAMP_BASE_DEFAULT;
217 if ((err = parcel->readInt32(×tampBase)) != OK) {
218 ALOGE("%s: Failed to read timestamp base from parcel", __FUNCTION__);
219 return err;
220 }
221
222 int mirrorMode = MIRROR_MODE_AUTO;
223 if ((err = parcel->readInt32(&mirrorMode)) != OK) {
224 ALOGE("%s: Failed to read mirroring mode from parcel", __FUNCTION__);
225 return err;
226 }
227
228 int useReadoutTimestamp = 0;
229 if ((err = parcel->readInt32(&useReadoutTimestamp)) != OK) {
230 ALOGE("%s: Failed to read useReadoutTimestamp flag from parcel", __FUNCTION__);
231 return err;
232 }
233
234 mRotation = rotation;
235 mSurfaceSetID = setID;
236 mSurfaceType = surfaceType;
237 mWidth = width;
238 mHeight = height;
239 mIsDeferred = isDeferred != 0;
240 mIsShared = isShared != 0;
241 mIsMultiResolution = isMultiResolution != 0;
242 mStreamUseCase = streamUseCase;
243 mTimestampBase = timestampBase;
244 mMirrorMode = mirrorMode;
245 mUseReadoutTimestamp = useReadoutTimestamp != 0;
246 for (auto& surface : surfaceShims) {
247 ALOGV("%s: OutputConfiguration: %p, name %s", __FUNCTION__,
248 surface.graphicBufferProducer.get(),
249 String8(surface.name).string());
250 mGbps.push_back(surface.graphicBufferProducer);
251 }
252
253 mSensorPixelModesUsed = std::move(sensorPixelModesUsed);
254 mDynamicRangeProfile = dynamicProfile;
255 mColorSpace = colorSpace;
256
257 ALOGV("%s: OutputConfiguration: rotation = %d, setId = %d, surfaceType = %d,"
258 " physicalCameraId = %s, isMultiResolution = %d, streamUseCase = %" PRId64
259 ", timestampBase = %d, mirrorMode = %d, useReadoutTimestamp = %d",
260 __FUNCTION__, mRotation, mSurfaceSetID, mSurfaceType,
261 String8(mPhysicalCameraId).string(), mIsMultiResolution, mStreamUseCase, timestampBase,
262 mMirrorMode, mUseReadoutTimestamp);
263
264 return err;
265 }
266
OutputConfiguration(sp<IGraphicBufferProducer> & gbp,int rotation,const String16 & physicalId,int surfaceSetID,bool isShared)267 OutputConfiguration::OutputConfiguration(sp<IGraphicBufferProducer>& gbp, int rotation,
268 const String16& physicalId,
269 int surfaceSetID, bool isShared) {
270 mGbps.push_back(gbp);
271 mRotation = rotation;
272 mSurfaceSetID = surfaceSetID;
273 mIsDeferred = false;
274 mIsShared = isShared;
275 mPhysicalCameraId = physicalId;
276 mIsMultiResolution = false;
277 mDynamicRangeProfile = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
278 mColorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
279 mStreamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
280 mTimestampBase = TIMESTAMP_BASE_DEFAULT;
281 mMirrorMode = MIRROR_MODE_AUTO;
282 mUseReadoutTimestamp = false;
283 }
284
OutputConfiguration(const std::vector<sp<IGraphicBufferProducer>> & gbps,int rotation,const String16 & physicalCameraId,int surfaceSetID,int surfaceType,int width,int height,bool isShared)285 OutputConfiguration::OutputConfiguration(
286 const std::vector<sp<IGraphicBufferProducer>>& gbps,
287 int rotation, const String16& physicalCameraId, int surfaceSetID, int surfaceType,
288 int width, int height, bool isShared)
289 : mGbps(gbps), mRotation(rotation), mSurfaceSetID(surfaceSetID), mSurfaceType(surfaceType),
290 mWidth(width), mHeight(height), mIsDeferred(false), mIsShared(isShared),
291 mPhysicalCameraId(physicalCameraId), mIsMultiResolution(false),
292 mDynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
293 mColorSpace(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED),
294 mStreamUseCase(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT),
295 mTimestampBase(TIMESTAMP_BASE_DEFAULT),
296 mMirrorMode(MIRROR_MODE_AUTO), mUseReadoutTimestamp(false) { }
297
writeToParcel(android::Parcel * parcel) const298 status_t OutputConfiguration::writeToParcel(android::Parcel* parcel) const {
299
300 if (parcel == nullptr) return BAD_VALUE;
301 status_t err = OK;
302
303 err = parcel->writeInt32(mRotation);
304 if (err != OK) return err;
305
306 err = parcel->writeInt32(mSurfaceSetID);
307 if (err != OK) return err;
308
309 err = parcel->writeInt32(mSurfaceType);
310 if (err != OK) return err;
311
312 err = parcel->writeInt32(mWidth);
313 if (err != OK) return err;
314
315 err = parcel->writeInt32(mHeight);
316 if (err != OK) return err;
317
318 err = parcel->writeInt32(mIsDeferred ? 1 : 0);
319 if (err != OK) return err;
320
321 err = parcel->writeInt32(mIsShared ? 1 : 0);
322 if (err != OK) return err;
323
324 std::vector<view::Surface> surfaceShims;
325 for (auto& gbp : mGbps) {
326 view::Surface surfaceShim;
327 surfaceShim.name = String16("unknown_name"); // name of surface
328 surfaceShim.graphicBufferProducer = gbp;
329 surfaceShims.push_back(surfaceShim);
330 }
331 err = parcel->writeParcelableVector(surfaceShims);
332 if (err != OK) return err;
333
334 err = parcel->writeString16(mPhysicalCameraId);
335 if (err != OK) return err;
336
337 err = parcel->writeInt32(mIsMultiResolution ? 1 : 0);
338 if (err != OK) return err;
339
340 err = parcel->writeParcelableVector(mSensorPixelModesUsed);
341 if (err != OK) return err;
342
343 err = parcel->writeInt64(mDynamicRangeProfile);
344 if (err != OK) return err;
345
346 err = parcel->writeInt32(mColorSpace);
347 if (err != OK) return err;
348
349 err = parcel->writeInt64(mStreamUseCase);
350 if (err != OK) return err;
351
352 err = parcel->writeInt32(mTimestampBase);
353 if (err != OK) return err;
354
355 err = parcel->writeInt32(mMirrorMode);
356 if (err != OK) return err;
357
358 err = parcel->writeInt32(mUseReadoutTimestamp ? 1 : 0);
359 if (err != OK) return err;
360
361 return OK;
362 }
363
364 template <typename T>
simpleVectorsEqual(T first,T second)365 static bool simpleVectorsEqual(T first, T second) {
366 if (first.size() != second.size()) {
367 return false;
368 }
369
370 for (size_t i = 0; i < first.size(); i++) {
371 if (first[i] != second[i]) {
372 return false;
373 }
374 }
375 return true;
376 }
377
gbpsEqual(const OutputConfiguration & other) const378 bool OutputConfiguration::gbpsEqual(const OutputConfiguration& other) const {
379 const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
380 other.getGraphicBufferProducers();
381 return simpleVectorsEqual(otherGbps, mGbps);
382 }
383
sensorPixelModesUsedEqual(const OutputConfiguration & other) const384 bool OutputConfiguration::sensorPixelModesUsedEqual(const OutputConfiguration& other) const {
385 const std::vector<int32_t>& othersensorPixelModesUsed = other.getSensorPixelModesUsed();
386 return simpleVectorsEqual(othersensorPixelModesUsed, mSensorPixelModesUsed);
387 }
388
sensorPixelModesUsedLessThan(const OutputConfiguration & other) const389 bool OutputConfiguration::sensorPixelModesUsedLessThan(const OutputConfiguration& other) const {
390 const std::vector<int32_t>& spms = other.getSensorPixelModesUsed();
391
392 if (mSensorPixelModesUsed.size() != spms.size()) {
393 return mSensorPixelModesUsed.size() < spms.size();
394 }
395
396 for (size_t i = 0; i < spms.size(); i++) {
397 if (mSensorPixelModesUsed[i] != spms[i]) {
398 return mSensorPixelModesUsed[i] < spms[i];
399 }
400 }
401
402 return false;
403 }
404
gbpsLessThan(const OutputConfiguration & other) const405 bool OutputConfiguration::gbpsLessThan(const OutputConfiguration& other) const {
406 const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
407 other.getGraphicBufferProducers();
408
409 if (mGbps.size() != otherGbps.size()) {
410 return mGbps.size() < otherGbps.size();
411 }
412
413 for (size_t i = 0; i < mGbps.size(); i++) {
414 if (mGbps[i] != otherGbps[i]) {
415 return mGbps[i] < otherGbps[i];
416 }
417 }
418
419 return false;
420 }
421 }; // namespace android
422