1 /*
2  * Copyright 2017 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 #ifndef __HDRPLUSCLIENTLISTENERHANDLERTHREAD__
17 #define __HDRPLUSCLIENTLISTENERHANDLERTHREAD__
18 
19 // System dependencies
20 #include <utils/Thread.h>
21 #include <queue>
22 
23 #include "EaselManagerClient.h"
24 #include "HdrPlusClient.h"
25 
26 using ::android::hardware::camera::common::V1_0::helper::CameraMetadata;
27 using namespace android;
28 
29 namespace qcamera {
30 
31 /*
32  * A thread to handle callbacks from HDR+ client. When a callback from HDR+ client is invoked,
33  * HDR+ client callback thread will return and the threadloop of QCamera3HdrPlusListenerThread
34  * will call the callback handlers in QCamera3HWI, to avoid deadlock in HDR+ client callback thread.
35  */
36 class QCamera3HdrPlusListenerThread : public HdrPlusClientListener, public Thread
37 {
38 public:
39     // listener is an HdrPlusClientListener to forward the callbacks in the thread loop.
40     QCamera3HdrPlusListenerThread(HdrPlusClientListener *listener);
41     virtual ~QCamera3HdrPlusListenerThread();
42 
43     // Request the thread to exit.
44     void requestExit() override;
45 
46 private:
47     // HDR+ client callbacks.
48     void onOpened(std::unique_ptr<HdrPlusClient> client) override;
49     void onOpenFailed(status_t err) override;
50     void onFatalError() override;
51     void onCaptureResult(pbcamera::CaptureResult *result,
52             const camera_metadata_t &resultMetadata) override;
53     void onFailedCaptureResult(pbcamera::CaptureResult *failedResult) override;
54     void onShutter(uint32_t requestId, int64_t apSensorTimestampNs) override;
55     void onNextCaptureReady(uint32_t requestId) override;
56     void onPostview(uint32_t requestId, std::unique_ptr<std::vector<uint8_t>> postview,
57             uint32_t width, uint32_t height, uint32_t stride, int32_t format) override;
58 
59     bool threadLoop() override;
60 
61     // The following functions handle the pending callbacks by calling the callback handlers
62     // in QCamera3HWI.
63     bool hasPendingEventsLocked();
64     void handlePendingClient();
65     void handleNextCaptureReady();
66     void handleCaptureResult();
67     void handleFatalError();
68     void handleOpenError();
69     void handleShutter();
70     void handlePostview();
71 
72     struct PendingResult {
73         pbcamera::CaptureResult result;
74         camera_metadata_t *metadata;
75         bool isFailed;
76     };
77 
78     struct PendingPostview {
79         uint32_t requestId;
80         std::unique_ptr<std::vector<uint8_t>> postview;
81         uint32_t width;
82         uint32_t height;
83         uint32_t stride;
84         int32_t format;
85     };
86 
87     enum CallbackType {
88         CALLBACK_TYPE_OPENED = 0,
89         CALLBACK_TYPE_OPENFAILED,
90         CALLBACK_TYPE_FATAL_ERROR,
91         CALLBACK_TYPE_CAPTURE_RESULT,
92         CALLBACK_TYPE_SHUTTER,
93         CALLBACK_TYPE_NEXT_CAPTURE_READY,
94         CALLBACK_TYPE_POSTVIEW,
95     };
96 
97     HdrPlusClientListener *mListener;
98 
99     std::mutex mCallbackLock;
100 
101     // Condition for a new callback. Protected by mCallbackLock.
102     std::condition_variable mCallbackCond;
103     // If exit has been requested. Protected by mCallbackLock.
104     bool mExitRequested;
105 
106     // The following variables store pending callbacks. Protected by mCallbackLock.
107     std::unique_ptr<HdrPlusClient> mClient;
108     std::queue<uint32_t> mNextCaptureReadyIds;
109     std::queue<PendingResult> mResults;
110     bool mFatalError;
111     status_t mOpenError;
112     std::queue<std::pair<uint32_t, int64_t>> mShutters;
113     std::queue<PendingPostview> mPostviews;
114 
115     // A queue of pending callback types, in the same order as invoked by HDR+ client.
116     // Protected by mCallbackLock.
117     std::queue<CallbackType> mPendingCallbacks;
118 };
119 
120 }; // namespace qcamera
121 
122 #endif /* __HDRPLUSCLIENTLISTENERHANDLERTHREAD__ */