1 /*
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"
22 #include "Utils.h"
23 
24 namespace android {
25 namespace automotive {
26 namespace evs {
27 namespace support {
28 
29 using android::hardware::configureRpcThreadpool;
30 using 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.
DisplayUseCase(string cameraId,BaseRenderCallback * callback)35 DisplayUseCase::DisplayUseCase(string cameraId, BaseRenderCallback* callback)
36               : BaseUseCase(vector<string>(1, cameraId)) {
37     mRenderCallback = callback;
38 }
39 
~DisplayUseCase()40 DisplayUseCase::~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 
initialize()52 bool DisplayUseCase::initialize() {
53     // 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 
66     mResourceManager = ResourceManager::getInstance();
67     if (mResourceManager == nullptr) {
68         ALOGE("Failed to get resource manager instance. Initialization failed.");
69         return false;
70     }
71 
72     // Request exclusive access to the EVS display
73     ALOGI("Acquiring EVS Display");
74 
75     mDisplay = mResourceManager->openDisplay();
76     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()) {
83         // This use case is currently a single camera use case.
84         // Only one element is available in the camera id list.
85         string cameraId = mCameraIds[0];
86         if (cameraId == info.cameraId) {
87             mStreamHandler = mResourceManager->obtainStreamHandler(cameraId);
88             if (mStreamHandler.get() == nullptr) {
89                 ALOGE("Failed to get a valid StreamHandler for %s",
90                       cameraId.c_str());
91                 return false;
92             }
93 
94             mIsInitialized = true;
95             return true;
96         }
97     }
98 
99     ALOGE("Cannot find a match camera. Exiting");
100     return false;
101 }
102 
103 // 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.
startVideoStream()107 bool DisplayUseCase::startVideoStream() {
108     // 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 
114     ALOGD("Attach use case to StreamHandler");
115     if (mRenderCallback != nullptr) {
116         mStreamHandler->attachRenderCallback(mRenderCallback);
117     }
118 
119     ALOGD("Start video streaming using worker thread");
120     mIsReadyToRun = true;
121     mWorkerThread = std::thread([this]() {
122         // We have a camera assigned to this state for direct view
123         mCurrentRenderer = std::make_unique<RenderDirectView>();
124         if (!mCurrentRenderer) {
125             ALOGE("Failed to construct direct renderer. Exiting.");
126             mIsReadyToRun = false;
127             return;
128         }
129 
130         // 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 
148         if (!mStreamHandler->startStream()) {
149             ALOGE("failed to start stream handler");
150             mIsReadyToRun = false;
151             return;
152         }
153 
154         while (mIsReadyToRun && streamFrame());
155 
156         ALOGD("Worker thread stops.");
157     });
158 
159     return true;
160 }
161 
stopVideoStream()162 void DisplayUseCase::stopVideoStream() {
163     ALOGD("Stop video streaming in worker thread.");
164     mIsReadyToRun = false;
165 
166     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     }
195     return;
196 }
197 
streamFrame()198 bool 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 
203     // 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.
209     if (tgtBuffer.memHandle == nullptr) {
210         ALOGW("Didn't get requested output buffer -- skipping this frame.");
211 
212         // Return true since it won't affect next call.
213         return true;
214     } else {
215         // 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();
233         }
234 
235         // 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.
241         mDisplay->returnTargetBufferForDisplay(tgtBuffer);
242 
243         return result;
244     }
245 }
246 
createDefaultUseCase(string cameraId,BaseRenderCallback * callback)247 DisplayUseCase 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
255