1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // #define LOG_NDEBUG 0
18 #define LOG_TAG "CameraSessionStats"
19 
20 #include <utils/Log.h>
21 #include <utils/String16.h>
22 
23 #include <camera/CameraSessionStats.h>
24 #include <camera/StringUtils.h>
25 
26 #include <binder/Parcel.h>
27 
28 namespace android {
29 namespace hardware {
30 
readFromParcel(const android::Parcel * parcel)31 status_t CameraStreamStats::readFromParcel(const android::Parcel* parcel) {
32     if (parcel == NULL) {
33         ALOGE("%s: Null parcel", __FUNCTION__);
34         return BAD_VALUE;
35     }
36 
37     status_t err = OK;
38 
39     int width = 0;
40     if ((err = parcel->readInt32(&width)) != OK) {
41         ALOGE("%s: Failed to read width from parcel", __FUNCTION__);
42         return err;
43     }
44 
45     int height = 0;
46     if ((err = parcel->readInt32(&height)) != OK) {
47         ALOGE("%s: Failed to read height from parcel", __FUNCTION__);
48         return err;
49     }
50 
51     int format = 0;
52     if ((err = parcel->readInt32(&format)) != OK) {
53         ALOGE("%s: Failed to read format from parcel", __FUNCTION__);
54         return err;
55     }
56 
57     float maxPreviewFps = 0;
58     if ((err = parcel->readFloat(&maxPreviewFps)) != OK) {
59         ALOGE("%s: Failed to read maxPreviewFps from parcel", __FUNCTION__);
60         return err;
61     }
62 
63     int dataSpace = 0;
64     if ((err = parcel->readInt32(&dataSpace)) != OK) {
65         ALOGE("%s: Failed to read dataSpace from parcel", __FUNCTION__);
66         return err;
67     }
68 
69     int64_t usage = 0;
70     if ((err = parcel->readInt64(&usage)) != OK) {
71         ALOGE("%s: Failed to read usage from parcel", __FUNCTION__);
72         return err;
73     }
74 
75     int64_t requestCount = 0;
76     if ((err = parcel->readInt64(&requestCount)) != OK) {
77         ALOGE("%s: Failed to read request count from parcel", __FUNCTION__);
78         return err;
79     }
80 
81     int64_t errorCount = 0;
82     if ((err = parcel->readInt64(&errorCount)) != OK) {
83         ALOGE("%s: Failed to read error count from parcel", __FUNCTION__);
84         return err;
85     }
86 
87     int startLatencyMs = 0;
88     if ((err = parcel->readInt32(&startLatencyMs)) != OK) {
89         ALOGE("%s: Failed to read start latency from parcel", __FUNCTION__);
90         return err;
91     }
92 
93     int maxHalBuffers = 0;
94     if ((err = parcel->readInt32(&maxHalBuffers)) != OK) {
95         ALOGE("%s: Failed to read max Hal buffers from parcel", __FUNCTION__);
96         return err;
97     }
98 
99     int maxAppBuffers = 0;
100     if ((err = parcel->readInt32(&maxAppBuffers)) != OK) {
101         ALOGE("%s: Failed to read max app buffers from parcel", __FUNCTION__);
102         return err;
103     }
104 
105     int histogramType = HISTOGRAM_TYPE_UNKNOWN;
106     if ((err = parcel->readInt32(&histogramType)) != OK) {
107         ALOGE("%s: Failed to read histogram type from parcel", __FUNCTION__);
108         return err;
109     }
110 
111     std::vector<float> histogramBins;
112     if ((err = parcel->readFloatVector(&histogramBins)) != OK) {
113         ALOGE("%s: Failed to read histogram bins from parcel", __FUNCTION__);
114         return err;
115     }
116 
117     std::vector<int64_t> histogramCounts;
118     if ((err = parcel->readInt64Vector(&histogramCounts)) != OK) {
119         ALOGE("%s: Failed to read histogram counts from parcel", __FUNCTION__);
120         return err;
121     }
122 
123     int64_t dynamicRangeProfile = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
124     if ((err = parcel->readInt64(&dynamicRangeProfile)) != OK) {
125         ALOGE("%s: Failed to read dynamic range profile type from parcel", __FUNCTION__);
126         return err;
127     }
128 
129     int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
130     if ((err = parcel->readInt64(&streamUseCase)) != OK) {
131         ALOGE("%s: Failed to read stream use case from parcel", __FUNCTION__);
132         return err;
133     }
134 
135     int32_t colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
136     if ((err = parcel->readInt32(&colorSpace)) != OK) {
137         ALOGE("%s: Failed to read color space from parcel", __FUNCTION__);
138         return err;
139     }
140 
141     mWidth = width;
142     mHeight = height;
143     mFormat = format;
144     mMaxPreviewFps = maxPreviewFps;
145     mDataSpace = dataSpace;
146     mUsage = usage;
147     mRequestCount = requestCount;
148     mErrorCount = errorCount;
149     mStartLatencyMs = startLatencyMs;
150     mMaxHalBuffers = maxHalBuffers;
151     mMaxAppBuffers = maxAppBuffers;
152     mHistogramType = histogramType;
153     mHistogramBins = std::move(histogramBins);
154     mHistogramCounts = std::move(histogramCounts);
155     mDynamicRangeProfile = dynamicRangeProfile;
156     mStreamUseCase = streamUseCase;
157     mColorSpace = colorSpace;
158 
159     return OK;
160 }
161 
writeToParcel(android::Parcel * parcel) const162 status_t CameraStreamStats::writeToParcel(android::Parcel* parcel) const {
163     if (parcel == NULL) {
164         ALOGE("%s: Null parcel", __FUNCTION__);
165         return BAD_VALUE;
166     }
167 
168     status_t err = OK;
169 
170     if ((err = parcel->writeInt32(mWidth)) != OK) {
171         ALOGE("%s: Failed to write stream width!", __FUNCTION__);
172         return err;
173     }
174 
175     if ((err = parcel->writeInt32(mHeight)) != OK) {
176         ALOGE("%s: Failed to write stream height!", __FUNCTION__);
177         return err;
178     }
179 
180     if ((err = parcel->writeInt32(mFormat)) != OK) {
181         ALOGE("%s: Failed to write stream format!", __FUNCTION__);
182         return err;
183     }
184 
185     if ((err = parcel->writeFloat(mMaxPreviewFps)) != OK) {
186         ALOGE("%s: Failed to write stream maxPreviewFps!", __FUNCTION__);
187         return err;
188     }
189 
190     if ((err = parcel->writeInt32(mDataSpace)) != OK) {
191         ALOGE("%s: Failed to write stream dataSpace!", __FUNCTION__);
192         return err;
193     }
194 
195     if ((err = parcel->writeInt64(mUsage)) != OK) {
196         ALOGE("%s: Failed to write stream usage!", __FUNCTION__);
197         return err;
198     }
199 
200     if ((err = parcel->writeInt64(mRequestCount)) != OK) {
201         ALOGE("%s: Failed to write stream request count!", __FUNCTION__);
202         return err;
203     }
204 
205     if ((err = parcel->writeInt64(mErrorCount)) != OK) {
206         ALOGE("%s: Failed to write stream error count!", __FUNCTION__);
207         return err;
208     }
209 
210     if ((err = parcel->writeInt32(mStartLatencyMs)) != OK) {
211         ALOGE("%s: Failed to write stream start latency!", __FUNCTION__);
212         return err;
213     }
214 
215     if ((err = parcel->writeInt32(mMaxHalBuffers)) != OK) {
216         ALOGE("%s: Failed to write max hal buffers", __FUNCTION__);
217         return err;
218     }
219 
220     if ((err = parcel->writeInt32(mMaxAppBuffers)) != OK) {
221         ALOGE("%s: Failed to write max app buffers", __FUNCTION__);
222         return err;
223     }
224 
225     if ((err = parcel->writeInt32(mHistogramType)) != OK) {
226         ALOGE("%s: Failed to write histogram type", __FUNCTION__);
227         return err;
228     }
229 
230     if ((err = parcel->writeFloatVector(mHistogramBins)) != OK) {
231         ALOGE("%s: Failed to write histogram bins!", __FUNCTION__);
232         return err;
233     }
234 
235     if ((err = parcel->writeInt64Vector(mHistogramCounts)) != OK) {
236         ALOGE("%s: Failed to write histogram counts!", __FUNCTION__);
237         return err;
238     }
239 
240     if ((err = parcel->writeInt64(mDynamicRangeProfile)) != OK) {
241         ALOGE("%s: Failed to write dynamic range profile type", __FUNCTION__);
242         return err;
243     }
244 
245     if ((err = parcel->writeInt64(mStreamUseCase)) != OK) {
246         ALOGE("%s: Failed to write stream use case!", __FUNCTION__);
247         return err;
248     }
249 
250     if ((err = parcel->writeInt32(mColorSpace)) != OK) {
251         ALOGE("%s: Failed to write color space", __FUNCTION__);
252         return err;
253     }
254 
255     return OK;
256 }
257 
258 const int CameraSessionStats::CAMERA_STATE_OPEN = 0;
259 const int CameraSessionStats::CAMERA_STATE_ACTIVE = 1;
260 const int CameraSessionStats::CAMERA_STATE_IDLE = 2;
261 const int CameraSessionStats::CAMERA_STATE_CLOSED = 3;
262 
263 const int CameraSessionStats::CAMERA_FACING_BACK = 0;
264 const int CameraSessionStats::CAMERA_FACING_FRONT = 1;
265 const int CameraSessionStats::CAMERA_FACING_EXTERNAL = 2;
266 
267 const int CameraSessionStats::CAMERA_API_LEVEL_1 = 1;
268 const int CameraSessionStats::CAMERA_API_LEVEL_2 = 2;
269 
CameraSessionStats()270 CameraSessionStats::CameraSessionStats() :
271         mFacing(CAMERA_FACING_BACK),
272         mNewCameraState(CAMERA_STATE_CLOSED),
273         mApiLevel(0),
274         mIsNdk(false),
275         mLatencyMs(-1),
276         mLogId(0),
277         mMaxPreviewFps(0),
278         mSessionType(0),
279         mInternalReconfigure(0),
280         mRequestCount(0),
281         mResultErrorCount(0),
282         mDeviceError(false),
283         mVideoStabilizationMode(-1),
284         mSessionIndex(0),
285         mCameraExtensionSessionStats() {}
286 
CameraSessionStats(const std::string & cameraId,int facing,int newCameraState,const std::string & clientName,int apiLevel,bool isNdk,int32_t latencyMs,int64_t logId)287 CameraSessionStats::CameraSessionStats(const std::string& cameraId,
288         int facing, int newCameraState, const std::string& clientName,
289         int apiLevel, bool isNdk, int32_t latencyMs, int64_t logId) :
290                 mCameraId(cameraId),
291                 mFacing(facing),
292                 mNewCameraState(newCameraState),
293                 mClientName(clientName),
294                 mApiLevel(apiLevel),
295                 mIsNdk(isNdk),
296                 mLatencyMs(latencyMs),
297                 mLogId(logId),
298                 mMaxPreviewFps(0),
299                 mSessionType(0),
300                 mInternalReconfigure(0),
301                 mRequestCount(0),
302                 mResultErrorCount(0),
303                 mDeviceError(0),
304                 mVideoStabilizationMode(-1),
305                 mSessionIndex(0),
306                 mCameraExtensionSessionStats() {}
307 
readFromParcel(const android::Parcel * parcel)308 status_t CameraSessionStats::readFromParcel(const android::Parcel* parcel) {
309     if (parcel == NULL) {
310         ALOGE("%s: Null parcel", __FUNCTION__);
311         return BAD_VALUE;
312     }
313 
314     status_t err = OK;
315 
316     String16 id;
317     if ((err = parcel->readString16(&id)) != OK) {
318         ALOGE("%s: Failed to read camera id!", __FUNCTION__);
319         return BAD_VALUE;
320     }
321 
322     int facing = 0;
323     if ((err = parcel->readInt32(&facing)) != OK) {
324         ALOGE("%s: Failed to read camera facing from parcel", __FUNCTION__);
325         return err;
326     }
327 
328     int32_t newCameraState;
329     if ((err = parcel->readInt32(&newCameraState)) != OK) {
330         ALOGE("%s: Failed to read new camera state from parcel", __FUNCTION__);
331         return err;
332     }
333 
334     String16 clientName;
335     if ((err = parcel->readString16(&clientName)) != OK) {
336         ALOGE("%s: Failed to read client name!", __FUNCTION__);
337         return BAD_VALUE;
338     }
339 
340     int32_t apiLevel;
341     if ((err = parcel->readInt32(&apiLevel)) != OK) {
342         ALOGE("%s: Failed to read api level from parcel", __FUNCTION__);
343         return err;
344     }
345 
346     bool isNdk;
347     if ((err = parcel->readBool(&isNdk)) != OK) {
348         ALOGE("%s: Failed to read isNdk flag from parcel", __FUNCTION__);
349         return err;
350     }
351 
352     int32_t latencyMs;
353     if ((err = parcel->readInt32(&latencyMs)) != OK) {
354         ALOGE("%s: Failed to read latencyMs from parcel", __FUNCTION__);
355         return err;
356     }
357 
358     int64_t logId;
359     if ((err = parcel->readInt64(&logId)) != OK) {
360         ALOGE("%s: Failed to read log ID from parcel", __FUNCTION__);
361         return err;
362     }
363 
364     float maxPreviewFps;
365     if ((err = parcel->readFloat(&maxPreviewFps)) != OK) {
366         ALOGE("%s: Failed to read maxPreviewFps from parcel", __FUNCTION__);
367         return err;
368     }
369 
370     int32_t sessionType;
371     if ((err = parcel->readInt32(&sessionType)) != OK) {
372         ALOGE("%s: Failed to read session type from parcel", __FUNCTION__);
373         return err;
374     }
375 
376     int32_t internalReconfigure;
377     if ((err = parcel->readInt32(&internalReconfigure)) != OK) {
378         ALOGE("%s: Failed to read internal reconfigure count from parcel", __FUNCTION__);
379         return err;
380     }
381 
382     int64_t requestCount;
383     if ((err = parcel->readInt64(&requestCount)) != OK) {
384         ALOGE("%s: Failed to read request count from parcel", __FUNCTION__);
385         return err;
386     }
387 
388     int64_t resultErrorCount;
389     if ((err = parcel->readInt64(&resultErrorCount)) != OK) {
390         ALOGE("%s: Failed to read result error count from parcel", __FUNCTION__);
391         return err;
392     }
393 
394     bool deviceError;
395     if ((err = parcel->readBool(&deviceError)) != OK) {
396         ALOGE("%s: Failed to read device error flag from parcel", __FUNCTION__);
397         return err;
398     }
399 
400     std::vector<CameraStreamStats> streamStats;
401     if ((err = parcel->readParcelableVector(&streamStats)) != OK) {
402         ALOGE("%s: Failed to read stream state from parcel", __FUNCTION__);
403         return err;
404     }
405 
406     String16 userTag;
407     if ((err = parcel->readString16(&userTag)) != OK) {
408         ALOGE("%s: Failed to read user tag!", __FUNCTION__);
409         return BAD_VALUE;
410     }
411 
412     int32_t videoStabilizationMode;
413     if ((err = parcel->readInt32(&videoStabilizationMode)) != OK) {
414         ALOGE("%s: Failed to read video stabilization mode from parcel", __FUNCTION__);
415         return err;
416     }
417 
418     bool usedUltraWide = false;
419     if ((err = parcel->readBool(&usedUltraWide)) != OK) {
420         ALOGE("%s: Failed to read ultrawide usage from parcel", __FUNCTION__);
421         return err;
422     }
423 
424     bool usedZoomOverride = false;
425     if ((err = parcel->readBool(&usedZoomOverride)) != OK) {
426         ALOGE("%s: Failed to read zoom override usage from parcel", __FUNCTION__);
427         return err;
428     }
429 
430     int32_t sessionIdx;
431     if ((err = parcel->readInt32(&sessionIdx)) != OK) {
432         ALOGE("%s: Failed to read session index from parcel", __FUNCTION__);
433         return err;
434     }
435 
436     CameraExtensionSessionStats extStats{};
437     if ((err = extStats.readFromParcel(parcel)) != OK) {
438         ALOGE("%s: Failed to read extension session stats from parcel", __FUNCTION__);
439         return err;
440     }
441 
442     auto mostRequestedFpsRange = std::make_pair(0,0);
443     if ((err = parcel->readInt32(&mostRequestedFpsRange.first)) != OK) {
444         ALOGE("%s: Failed to read frame rate range min info!", __FUNCTION__);
445         return err;
446     }
447     if ((err = parcel->readInt32(&mostRequestedFpsRange.second)) != OK) {
448         ALOGE("%s: Failed to read frame rate range max info!", __FUNCTION__);
449         return err;
450     }
451 
452     mCameraId = toStdString(id);
453     mFacing = facing;
454     mNewCameraState = newCameraState;
455     mClientName = toStdString(clientName);
456     mApiLevel = apiLevel;
457     mIsNdk = isNdk;
458     mLatencyMs = latencyMs;
459     mLogId = logId;
460     mMaxPreviewFps = maxPreviewFps;
461     mSessionType = sessionType;
462     mInternalReconfigure = internalReconfigure;
463     mRequestCount = requestCount;
464     mResultErrorCount = resultErrorCount;
465     mDeviceError = deviceError;
466     mStreamStats = std::move(streamStats);
467     mUserTag = toStdString(userTag);
468     mVideoStabilizationMode = videoStabilizationMode;
469     mUsedUltraWide = usedUltraWide;
470     mUsedZoomOverride = usedZoomOverride;
471     mSessionIndex = sessionIdx;
472     mCameraExtensionSessionStats = extStats;
473     mMostRequestedFpsRange = mostRequestedFpsRange;
474 
475     return OK;
476 }
477 
writeToParcel(android::Parcel * parcel) const478 status_t CameraSessionStats::writeToParcel(android::Parcel* parcel) const {
479     if (parcel == NULL) {
480         ALOGE("%s: Null parcel", __FUNCTION__);
481         return BAD_VALUE;
482     }
483 
484     status_t err = OK;
485 
486     if ((err = parcel->writeString16(toString16(mCameraId))) != OK) {
487         ALOGE("%s: Failed to write camera id!", __FUNCTION__);
488         return err;
489     }
490 
491     if ((err = parcel->writeInt32(mFacing)) != OK) {
492         ALOGE("%s: Failed to write camera facing!", __FUNCTION__);
493         return err;
494     }
495 
496     if ((err = parcel->writeInt32(mNewCameraState)) != OK) {
497         ALOGE("%s: Failed to write new camera state!", __FUNCTION__);
498         return err;
499     }
500 
501     if ((err = parcel->writeString16(toString16(mClientName))) != OK) {
502         ALOGE("%s: Failed to write client name!", __FUNCTION__);
503         return err;
504     }
505 
506     if ((err = parcel->writeInt32(mApiLevel)) != OK) {
507         ALOGE("%s: Failed to write api level!", __FUNCTION__);
508         return err;
509     }
510 
511     if ((err = parcel->writeBool(mIsNdk)) != OK) {
512         ALOGE("%s: Failed to write isNdk flag!", __FUNCTION__);
513         return err;
514     }
515 
516     if ((err = parcel->writeInt32(mLatencyMs)) != OK) {
517         ALOGE("%s: Failed to write latency in Ms!", __FUNCTION__);
518         return err;
519     }
520 
521     if ((err = parcel->writeInt64(mLogId)) != OK) {
522         ALOGE("%s: Failed to write log ID!", __FUNCTION__);
523         return err;
524     }
525 
526     if ((err = parcel->writeFloat(mMaxPreviewFps)) != OK) {
527         ALOGE("%s: Failed to write maxPreviewFps!", __FUNCTION__);
528         return err;
529     }
530 
531     if ((err = parcel->writeInt32(mSessionType)) != OK) {
532         ALOGE("%s: Failed to write session type!", __FUNCTION__);
533         return err;
534     }
535 
536     if ((err = parcel->writeInt32(mInternalReconfigure)) != OK) {
537         ALOGE("%s: Failed to write internal reconfigure count!", __FUNCTION__);
538         return err;
539     }
540 
541     if ((err = parcel->writeInt64(mRequestCount)) != OK) {
542         ALOGE("%s: Failed to write request count!", __FUNCTION__);
543         return err;
544     }
545 
546     if ((err = parcel->writeInt64(mResultErrorCount)) != OK) {
547         ALOGE("%s: Failed to write result error count!", __FUNCTION__);
548         return err;
549     }
550 
551     if ((err = parcel->writeBool(mDeviceError)) != OK) {
552         ALOGE("%s: Failed to write device error flag!", __FUNCTION__);
553         return err;
554     }
555 
556     if ((err = parcel->writeParcelableVector(mStreamStats)) != OK) {
557         ALOGE("%s: Failed to write stream states!", __FUNCTION__);
558         return err;
559     }
560 
561     if ((err = parcel->writeString16(toString16(mUserTag))) != OK) {
562         ALOGE("%s: Failed to write user tag!", __FUNCTION__);
563         return err;
564     }
565 
566     if ((err = parcel->writeInt32(mVideoStabilizationMode)) != OK) {
567         ALOGE("%s: Failed to write video stabilization mode!", __FUNCTION__);
568         return err;
569     }
570 
571     if ((err = parcel->writeBool(mUsedUltraWide)) != OK) {
572         ALOGE("%s: Failed to write ultrawide usage!", __FUNCTION__);
573         return err;
574     }
575 
576     if ((err = parcel->writeBool(mUsedZoomOverride)) != OK) {
577         ALOGE("%s: Failed to write zoom override usage!", __FUNCTION__);
578         return err;
579     }
580 
581     if ((err = parcel->writeInt32(mSessionIndex)) != OK) {
582         ALOGE("%s: Failed to write session index!", __FUNCTION__);
583         return err;
584     }
585 
586     if ((err = mCameraExtensionSessionStats.writeToParcel(parcel)) != OK) {
587         ALOGE("%s: Failed to write extension sessions stats!", __FUNCTION__);
588         return err;
589     }
590 
591     if ((err = parcel->writeInt32(mMostRequestedFpsRange.first)) != OK) {
592         ALOGE("%s: Failed to write frame rate range min info!", __FUNCTION__);
593         return err;
594     }
595 
596     if ((err = parcel->writeInt32(mMostRequestedFpsRange.second)) != OK) {
597         ALOGE("%s: Failed to write frame rate range max info!", __FUNCTION__);
598         return err;
599     }
600 
601     return OK;
602 }
603 
604 } // namespace hardware
605 } // namesmpace android
606