1 /*
2  * Copyright (C) 2024 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 // #define LOG_NDEBUG 0
18 #define LOG_TAG "VirtualCameraTestInstance"
19 
20 #include "VirtualCameraTestInstance.h"
21 
22 #include <atomic>
23 #include <chrono>
24 #include <memory>
25 #include <mutex>
26 #include <ratio>
27 #include <thread>
28 
29 #include "GLES/gl.h"
30 #include "android/binder_auto_utils.h"
31 #include "android/native_window.h"
32 #include "log/log.h"
33 #include "util/EglDisplayContext.h"
34 #include "util/EglProgram.h"
35 
36 namespace android {
37 namespace companion {
38 namespace virtualcamera {
39 
40 using ::aidl::android::companion::virtualcamera::Format;
41 using ::aidl::android::view::Surface;
42 using ::ndk::ScopedAStatus;
43 
44 namespace {
45 
nativeWindowFromSurface(const Surface & surface)46 std::shared_ptr<ANativeWindow> nativeWindowFromSurface(const Surface& surface) {
47   ANativeWindow* nativeWindow = surface.get();
48   if (nativeWindow != nullptr) {
49     ANativeWindow_acquire(nativeWindow);
50   }
51   return std::shared_ptr<ANativeWindow>(nativeWindow, ANativeWindow_release);
52 }
53 
getCurrentTimestamp()54 std::chrono::nanoseconds getCurrentTimestamp() {
55   return std::chrono::duration_cast<std::chrono::nanoseconds>(
56       std::chrono::steady_clock::now().time_since_epoch());
57 }
58 
59 }  // namespace
60 
TestPatternRenderer(std::shared_ptr<ANativeWindow> nativeWindow,int fps)61 TestPatternRenderer::TestPatternRenderer(
62     std::shared_ptr<ANativeWindow> nativeWindow, int fps)
63     : mFps(fps), mNativeWindow(nativeWindow) {
64 }
65 
start()66 void TestPatternRenderer::start() {
67   std::lock_guard<std::mutex> lock(mLock);
68   if (mRunning.exchange(true, std::memory_order_relaxed)) {
69     ALOGW("Render thread already started.");
70     return;
71   }
72   mThread =
73       std::thread(&TestPatternRenderer::renderThreadLoop, this, mNativeWindow);
74 }
75 
stop()76 void TestPatternRenderer::stop() {
77   std::lock_guard<std::mutex> lock(mLock);
78   if (!mRunning.exchange(false, std::memory_order_relaxed)) {
79     ALOGW("Render thread already stopped.");
80     return;
81   }
82   mThread.detach();
83   mRunning = false;
84 }
85 
renderThreadLoop(std::shared_ptr<ANativeWindow> nativeWindow)86 void TestPatternRenderer::renderThreadLoop(
87     std::shared_ptr<ANativeWindow> nativeWindow) {
88   // Prevent destruction of this instance until the thread terminates.
89   std::shared_ptr<TestPatternRenderer> thiz = shared_from_this();
90 
91   ALOGV("Starting test client render loop");
92 
93   EglDisplayContext eglDisplayContext(nativeWindow);
94   EglTestPatternProgram testPatternProgram;
95 
96   const std::chrono::nanoseconds frameDuration(
97       static_cast<uint64_t>(1e9 / mFps));
98 
99   std::chrono::nanoseconds lastFrameTs(0);
100   int frameNumber = 0;
101   while (mRunning) {
102     // Wait for appropriate amount of time to meet configured FPS.
103     std::chrono::nanoseconds ts = getCurrentTimestamp();
104     std::chrono::nanoseconds currentDuration = ts - lastFrameTs;
105     if (currentDuration < frameDuration) {
106       std::this_thread::sleep_for(frameDuration - currentDuration);
107     }
108 
109     // Render the test pattern and update timestamp.
110     testPatternProgram.draw(ts);
111     eglDisplayContext.swapBuffers();
112     lastFrameTs = getCurrentTimestamp();
113   }
114 
115   ALOGV("Terminating test client render loop");
116 }
117 
VirtualCameraTestInstance(const int fps)118 VirtualCameraTestInstance::VirtualCameraTestInstance(const int fps)
119     : mFps(fps) {
120 }
121 
onStreamConfigured(const int32_t streamId,const Surface & surface,const int32_t width,const int32_t height,const Format pixelFormat)122 ScopedAStatus VirtualCameraTestInstance::onStreamConfigured(
123     const int32_t streamId, const Surface& surface, const int32_t width,
124     const int32_t height, const Format pixelFormat) {
125   ALOGV("%s: streamId %d, %dx%d pixFmt=%s", __func__, streamId, width, height,
126         toString(pixelFormat).c_str());
127 
128   auto renderer = std::make_shared<TestPatternRenderer>(
129       nativeWindowFromSurface(surface), mFps);
130 
131   std::lock_guard<std::mutex> lock(mLock);
132   if (mInputRenderers.try_emplace(streamId, renderer).second) {
133     renderer->start();
134   } else {
135     ALOGE(
136         "%s: Input stream with id %d is already active, ignoring "
137         "onStreamConfigured call",
138         __func__, streamId);
139   }
140 
141   return ScopedAStatus::ok();
142 }
143 
onProcessCaptureRequest(const int32_t,const int32_t)144 ScopedAStatus VirtualCameraTestInstance::onProcessCaptureRequest(
145     const int32_t /*in_streamId*/, const int32_t /*in_frameId*/) {
146   return ScopedAStatus::ok();
147 }
148 
onStreamClosed(const int32_t streamId)149 ScopedAStatus VirtualCameraTestInstance::onStreamClosed(const int32_t streamId) {
150   ALOGV("%s: streamId %d", __func__, streamId);
151 
152   std::shared_ptr<TestPatternRenderer> renderer;
153   {
154     std::lock_guard<std::mutex> lock(mLock);
155     auto it = mInputRenderers.find(streamId);
156     if (it != mInputRenderers.end()) {
157       renderer = std::move(it->second);
158       mInputRenderers.erase(it);
159     }
160   }
161   if (renderer != nullptr) {
162     renderer->stop();
163   }
164   return ScopedAStatus::ok();
165 }
166 
167 }  // namespace virtualcamera
168 }  // namespace companion
169 }  // namespace android
170