blob: 3dfed30d8f38402cc19eaab585b64c728c65f464 [file] [log] [blame]
Emilian Peev434248e2022-10-06 14:58:54 -07001/*
2 * Copyright (C) 2022 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#ifndef ANDROID_SERVERS_CAMERA_CAMERA3_JPEG_R_COMPOSITE_STREAM_H
18#define ANDROID_SERVERS_CAMERA_CAMERA3_JPEG_R_COMPOSITE_STREAM_H
19
20#include <gui/CpuConsumer.h>
21#include "aidl/android/hardware/graphics/common/Dataspace.h"
22#include "system/graphics-base-v1.1.h"
23
24#include "api1/client2/JpegProcessor.h"
Emilian Peev567c31c2023-03-06 15:02:37 -080025#include "utils/SessionStatsBuilder.h"
Emilian Peev434248e2022-10-06 14:58:54 -070026
27#include "CompositeStream.h"
28
29namespace android {
30
31class CameraDeviceClient;
32class CameraMetadata;
33class Surface;
34
35namespace camera3 {
36
37class JpegRCompositeStream : public CompositeStream, public Thread,
38 public CpuConsumer::FrameAvailableListener {
39
40public:
41 JpegRCompositeStream(sp<CameraDeviceBase> device,
42 wp<hardware::camera2::ICameraDeviceCallbacks> cb);
43 ~JpegRCompositeStream() override;
44
45 static bool isJpegRCompositeStream(const sp<Surface> &surface);
46
47 // CompositeStream overrides
48 status_t createInternalStreams(const std::vector<sp<Surface>>& consumers,
49 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
50 camera_stream_rotation_t rotation, int *id, const String8& physicalCameraId,
51 const std::unordered_set<int32_t> &sensorPixelModesUsed,
52 std::vector<int> *surfaceIds,
53 int streamSetId, bool isShared, int32_t colorSpace,
Shuzhen Wangbce53db2022-12-03 00:38:20 +000054 int64_t dynamicProfile, int64_t streamUseCase, bool useReadoutTimestamp) override;
Emilian Peev434248e2022-10-06 14:58:54 -070055 status_t deleteInternalStreams() override;
56 status_t configureStream() override;
57 status_t insertGbp(SurfaceMap* /*out*/outSurfaceMap, Vector<int32_t>* /*out*/outputStreamIds,
58 int32_t* /*out*/currentStreamId) override;
59 status_t insertCompositeStreamIds(std::vector<int32_t>* compositeStreamIds /*out*/) override;
60 int getStreamId() override { return mP010StreamId; }
61
62 // CpuConsumer listener implementation
63 void onFrameAvailable(const BufferItem& item) override;
64
65 // Return stream information about the internal camera streams
66 static status_t getCompositeStreamInfo(const OutputStreamInfo &streamInfo,
67 const CameraMetadata& ch, std::vector<OutputStreamInfo>* compositeOutput /*out*/);
68
Emilian Peev567c31c2023-03-06 15:02:37 -080069 // Get composite stream stats
70 void getStreamStats(hardware::CameraStreamStats* streamStats) override;
71
Emilian Peev434248e2022-10-06 14:58:54 -070072protected:
73
74 bool threadLoop() override;
75 bool onStreamBufferError(const CaptureResultExtras& resultExtras) override;
76 void onResultError(const CaptureResultExtras& resultExtras) override;
77
78private:
79 struct InputFrame {
80 CpuConsumer::LockedBuffer p010Buffer;
81 CpuConsumer::LockedBuffer jpegBuffer;
82 CameraMetadata result;
83 bool error;
84 bool errorNotified;
85 int64_t frameNumber;
86 int32_t requestId;
Emilian Peev567c31c2023-03-06 15:02:37 -080087 nsecs_t requestTimeNs;
Emilian Peev434248e2022-10-06 14:58:54 -070088
Emilian Peev567c31c2023-03-06 15:02:37 -080089 InputFrame() : error(false), errorNotified(false), frameNumber(-1), requestId(-1),
90 requestTimeNs(-1) { }
Emilian Peev434248e2022-10-06 14:58:54 -070091 };
92
93 status_t processInputFrame(nsecs_t ts, const InputFrame &inputFrame);
94
95 // Buffer/Results handling
96 void compilePendingInputLocked();
97 void releaseInputFrameLocked(InputFrame *inputFrame /*out*/);
98 void releaseInputFramesLocked(int64_t currentTs);
99
100 // Find first complete and valid frame with smallest timestamp
101 bool getNextReadyInputLocked(int64_t *currentTs /*inout*/);
102
103 // Find next failing frame number with smallest timestamp and return respective frame number
104 int64_t getNextFailingInputLocked(int64_t *currentTs /*inout*/);
105
106 static void deriveDynamicRangeAndDataspace(int64_t dynamicProfile, int64_t* /*out*/dynamicRange,
107 int64_t* /*out*/dataSpace);
108
109 static const nsecs_t kWaitDuration = 10000000; // 10 ms
110 static const auto kP010PixelFormat = HAL_PIXEL_FORMAT_YCBCR_P010;
111 static const auto kP010DefaultDataSpace = HAL_DATASPACE_BT2020_ITU_HLG;
112 static const auto kP010DefaultDynamicRange =
113 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HLG10;
114 static const auto kJpegDataSpace = HAL_DATASPACE_V0_JFIF;
115 static const auto kJpegRDataSpace =
116 aidl::android::hardware::graphics::common::Dataspace::JPEG_R;
117
118 bool mSupportInternalJpeg = false;
119 int64_t mP010DataSpace = HAL_DATASPACE_BT2020_HLG;
120 int64_t mP010DynamicRange =
121 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HLG10;
122 int mBlobStreamId, mBlobSurfaceId, mP010StreamId, mP010SurfaceId;
123 size_t mBlobWidth, mBlobHeight;
124 sp<CpuConsumer> mBlobConsumer, mP010Consumer;
125 bool mP010BufferAcquired, mBlobBufferAcquired;
126 sp<Surface> mP010Surface, mBlobSurface, mOutputSurface;
127 int32_t mOutputColorSpace;
Emilian Peev567c31c2023-03-06 15:02:37 -0800128 int64_t mOutputStreamUseCase;
129 nsecs_t mFirstRequestLatency;
Emilian Peev434248e2022-10-06 14:58:54 -0700130 sp<ProducerListener> mProducerListener;
131
132 ssize_t mMaxJpegBufferSize;
133 ssize_t mUHRMaxJpegBufferSize;
134
135 camera3::Size mDefaultMaxJpegSize;
136 camera3::Size mUHRMaxJpegSize;
137
138 // Keep all incoming P010 buffer timestamps pending further processing.
139 std::vector<int64_t> mInputP010Buffers;
140
141 // Keep all incoming Jpeg/Blob buffer timestamps pending further processing.
142 std::vector<int64_t> mInputJpegBuffers;
143
144 // Map of all input frames pending further processing.
145 std::unordered_map<int64_t, InputFrame> mPendingInputFrames;
146
147 const CameraMetadata mStaticInfo;
Emilian Peev567c31c2023-03-06 15:02:37 -0800148
149 SessionStatsBuilder mSessionStatsBuilder;
Emilian Peev434248e2022-10-06 14:58:54 -0700150};
151
152}; //namespace camera3
153}; //namespace android
154
155#endif