blob: cf50387fd2e77dfaf5eb81c62e141d0e4a9a6344 [file] [log] [blame]
Haoxiang Lid2454a52019-06-18 13:23:12 -07001/*
2 * Copyright (C) 2019 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#include <hidl/HidlTransportSupport.h>
17#include <log/log.h>
18#include <utils/SystemClock.h>
19
20#include "DisplayUseCase.h"
21#include "RenderDirectView.h"
Haoxiang Li11f58a82019-08-30 14:50:06 -070022#include "Utils.h"
Haoxiang Lid2454a52019-06-18 13:23:12 -070023
24namespace android {
25namespace automotive {
26namespace evs {
27namespace support {
28
29using android::hardware::configureRpcThreadpool;
30using android::hardware::joinRpcThreadpool;
31
32// TODO(b/130246434): since we don't support multi-display use case, there
33// should only be one DisplayUseCase. Add the logic to prevent more than
34// one DisplayUseCases running at the same time.
Haoxiang Li31ae4ed2019-09-06 16:35:09 -070035DisplayUseCase::DisplayUseCase(string cameraId, BaseRenderCallback* callback)
36 : BaseUseCase(vector<string>(1, cameraId)) {
Haoxiang Lid2454a52019-06-18 13:23:12 -070037 mRenderCallback = callback;
38}
39
40DisplayUseCase::~DisplayUseCase() {
41 if (mCurrentRenderer != nullptr) {
42 mCurrentRenderer->deactivate();
43 mCurrentRenderer = nullptr; // It's a smart pointer, so destructs on assignment to null
44 }
45
46 mIsReadyToRun = false;
47 if (mWorkerThread.joinable()) {
48 mWorkerThread.join();
49 }
50}
51
52bool DisplayUseCase::initialize() {
Haoxiang Lid2454a52019-06-18 13:23:12 -070053 // Load our configuration information
54 ConfigManager config;
55 if (!config.initialize("/system/etc/automotive/evs_support_lib/camera_config.json")) {
56 ALOGE("Missing or improper configuration for the EVS application. Exiting.");
57 return false;
58 }
59
60 // Set thread pool size to one to avoid concurrent events from the HAL.
61 // This pool will handle the EvsCameraStream callbacks.
62 // Note: This _will_ run in parallel with the EvsListener run() loop below which
63 // runs the application logic that reacts to the async events.
64 configureRpcThreadpool(1, false /* callerWillJoin */);
65
Haoxiang Lic8888592019-09-19 16:39:53 -070066 mResourceManager = ResourceManager::getInstance();
67 if (mResourceManager == nullptr) {
68 ALOGE("Failed to get resource manager instance. Initialization failed.");
Haoxiang Lid2454a52019-06-18 13:23:12 -070069 return false;
70 }
71
72 // Request exclusive access to the EVS display
73 ALOGI("Acquiring EVS Display");
74
Haoxiang Lic8888592019-09-19 16:39:53 -070075 mDisplay = mResourceManager->openDisplay();
Haoxiang Lid2454a52019-06-18 13:23:12 -070076 if (mDisplay.get() == nullptr) {
77 ALOGE("EVS Display unavailable. Exiting.");
78 return false;
79 }
80
81 ALOGD("Requesting camera list");
82 for (auto&& info : config.getCameras()) {
Haoxiang Li31ae4ed2019-09-06 16:35:09 -070083 // This use case is currently a single camera use case.
84 // Only one element is available in the camera id list.
Haoxiang Li5cb69052019-08-30 16:24:25 -070085 string cameraId = mCameraIds[0];
86 if (cameraId == info.cameraId) {
Haoxiang Lic8888592019-09-19 16:39:53 -070087 mStreamHandler = mResourceManager->obtainStreamHandler(cameraId);
Haoxiang Li5cb69052019-08-30 16:24:25 -070088 if (mStreamHandler.get() == nullptr) {
89 ALOGE("Failed to get a valid StreamHandler for %s",
90 cameraId.c_str());
91 return false;
92 }
93
Haoxiang Lid2454a52019-06-18 13:23:12 -070094 mIsInitialized = true;
95 return true;
96 }
97 }
98
99 ALOGE("Cannot find a match camera. Exiting");
100 return false;
101}
102
Haoxiang Li1709b372019-09-18 15:08:52 -0700103// TODO(b/130246434): if user accidentally call this function twice, there is
104// no logic to handle that and it will causes issues. For example, the
105// mWorkerThread will be assigned twice and cause unexpected behavior.
106// We need to fix this issue.
Haoxiang Li2010d392019-09-04 14:06:20 -0700107bool DisplayUseCase::startVideoStream() {
Haoxiang Lid2454a52019-06-18 13:23:12 -0700108 // Initialize the use case.
109 if (!mIsInitialized && !initialize()) {
110 ALOGE("There is an error while initializing the use case. Exiting");
111 return false;
112 }
113
Haoxiang Li5cb69052019-08-30 16:24:25 -0700114 ALOGD("Attach use case to StreamHandler");
115 if (mRenderCallback != nullptr) {
116 mStreamHandler->attachRenderCallback(mRenderCallback);
117 }
Haoxiang Lid2454a52019-06-18 13:23:12 -0700118
Haoxiang Li5cb69052019-08-30 16:24:25 -0700119 ALOGD("Start video streaming using worker thread");
Haoxiang Lid2454a52019-06-18 13:23:12 -0700120 mIsReadyToRun = true;
121 mWorkerThread = std::thread([this]() {
122 // We have a camera assigned to this state for direct view
Haoxiang Li1709b372019-09-18 15:08:52 -0700123 mCurrentRenderer = std::make_unique<RenderDirectView>();
Haoxiang Lid2454a52019-06-18 13:23:12 -0700124 if (!mCurrentRenderer) {
125 ALOGE("Failed to construct direct renderer. Exiting.");
126 mIsReadyToRun = false;
127 return;
128 }
129
Haoxiang Lid2454a52019-06-18 13:23:12 -0700130 // Now set the display state based on whether we have a video feed to show
131 // Start the camera stream
132 ALOGD("EvsStartCameraStreamTiming start time: %" PRId64 "ms", android::elapsedRealtime());
133 if (!mCurrentRenderer->activate()) {
134 ALOGE("New renderer failed to activate. Exiting");
135 mIsReadyToRun = false;
136 return;
137 }
138
139 // Activate the display
140 ALOGD("EvsActivateDisplayTiming start time: %" PRId64 "ms", android::elapsedRealtime());
141 Return<EvsResult> result = mDisplay->setDisplayState(DisplayState::VISIBLE_ON_NEXT_FRAME);
142 if (result != EvsResult::OK) {
143 ALOGE("setDisplayState returned an error (%d). Exiting.", (EvsResult)result);
144 mIsReadyToRun = false;
145 return;
146 }
147
Haoxiang Li1709b372019-09-18 15:08:52 -0700148 if (!mStreamHandler->startStream()) {
149 ALOGE("failed to start stream handler");
150 mIsReadyToRun = false;
151 return;
152 }
153
Haoxiang Lid2454a52019-06-18 13:23:12 -0700154 while (mIsReadyToRun && streamFrame());
155
156 ALOGD("Worker thread stops.");
157 });
158
159 return true;
160}
161
Haoxiang Li2010d392019-09-04 14:06:20 -0700162void DisplayUseCase::stopVideoStream() {
Haoxiang Lid2454a52019-06-18 13:23:12 -0700163 ALOGD("Stop video streaming in worker thread.");
164 mIsReadyToRun = false;
Haoxiang Li1709b372019-09-18 15:08:52 -0700165
Haoxiang Lic8888592019-09-19 16:39:53 -0700166 if (mStreamHandler == nullptr) {
167 ALOGE("Failed to detach render callback since stream handler is null");
168
169 // Something may go wrong. Instead of to return this method right away,
170 // we want to finish the remaining logic of this method to try to
171 // release other resources.
172 } else {
173 mStreamHandler->detachRenderCallback();
174 }
175
176 if (mResourceManager == nullptr) {
177 ALOGE("Failed to release resources since resource manager is null");
178 } else {
179 mResourceManager->releaseStreamHandler(mCameraIds[0]);
180 mStreamHandler = nullptr;
181
182 mResourceManager->closeDisplay(mDisplay);
183 mDisplay = nullptr;
184
185 // TODO(b/130246434): with the current logic, the initialize method will
186 // be triggered every time when a pair of
187 // stopVideoStream/startVideoStream is called. We might want to move
188 // some heavy work away from initialize method so increase the
189 // performance.
190
191 // Sets mIsInitialzed to false so the initialize method will be
192 // triggered when startVideoStream is called again.
193 mIsInitialized = false;
194 }
Haoxiang Lid2454a52019-06-18 13:23:12 -0700195 return;
196}
197
198bool DisplayUseCase::streamFrame() {
199 // Get the output buffer we'll use to display the imagery
200 BufferDesc tgtBuffer = {};
201 mDisplay->getTargetBuffer([&tgtBuffer](const BufferDesc& buff) { tgtBuffer = buff; });
202
Haoxiang Li1709b372019-09-18 15:08:52 -0700203 // TODO(b/130246434): if there is no new display frame available, shall we
204 // still get display buffer? Shall we just skip and keep the display
205 // un-refreshed?
206 // We should explore this option.
207
208 // If there is no display buffer available, skip it.
Haoxiang Lid2454a52019-06-18 13:23:12 -0700209 if (tgtBuffer.memHandle == nullptr) {
Haoxiang Li1709b372019-09-18 15:08:52 -0700210 ALOGW("Didn't get requested output buffer -- skipping this frame.");
211
212 // Return true since it won't affect next call.
213 return true;
Haoxiang Lid2454a52019-06-18 13:23:12 -0700214 } else {
Haoxiang Li1709b372019-09-18 15:08:52 -0700215 // If there is no new display frame available, re-use the old (held)
216 // frame for display.
217 // Otherwise, return the old (held) frame, fetch the newly available
218 // frame from stream handler, and use the new frame for display
219 // purposes.
220 if (!mStreamHandler->newDisplayFrameAvailable()) {
221 ALOGD("No new display frame is available. Re-use the old frame.");
222 } else {
223 ALOGD("Get new display frame, refreshing");
224
225 // If we already hold a camera image for display purposes, it's
226 // time to return it to evs camera driver.
227 if (mImageBuffer.memHandle.getNativeHandle() != nullptr) {
228 mStreamHandler->doneWithFrame(mImageBuffer);
229 }
230
231 // Get the new image we want to use as our display content
232 mImageBuffer = mStreamHandler->getNewDisplayFrame();
Haoxiang Lid2454a52019-06-18 13:23:12 -0700233 }
234
Haoxiang Li1709b372019-09-18 15:08:52 -0700235 // Render the image buffer to the display buffer
236 bool result = mCurrentRenderer->drawFrame(tgtBuffer, mImageBuffer);
237
238 // Send the finished display buffer back to display driver
239 // Even if the rendering fails, we still want to return the display
240 // buffer.
Haoxiang Lid2454a52019-06-18 13:23:12 -0700241 mDisplay->returnTargetBufferForDisplay(tgtBuffer);
Haoxiang Li1709b372019-09-18 15:08:52 -0700242
243 return result;
Haoxiang Lid2454a52019-06-18 13:23:12 -0700244 }
Haoxiang Lid2454a52019-06-18 13:23:12 -0700245}
246
247DisplayUseCase DisplayUseCase::createDefaultUseCase(string cameraId, BaseRenderCallback* callback) {
248 return DisplayUseCase(cameraId, callback);
249}
250
251} // namespace support
252} // namespace evs
253} // namespace automotive
254} // namespace android