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