blob: ff4a2d80d8e1df1d2c2bf60ed8d6b4de347608e1 [file] [log] [blame]
Jan Sebechlebsky288900f2024-05-24 14:47:54 +02001/*
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
36namespace android {
37namespace companion {
38namespace virtualcamera {
39
40using ::aidl::android::companion::virtualcamera::Format;
41using ::aidl::android::view::Surface;
42using ::ndk::ScopedAStatus;
43
44namespace {
45
46std::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
54std::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
61TestPatternRenderer::TestPatternRenderer(
62 std::shared_ptr<ANativeWindow> nativeWindow, int fps)
63 : mFps(fps), mNativeWindow(nativeWindow) {
64}
65
66void 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
76void 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
86void 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.
Jan Sebechlebsky58a7ac42024-06-03 16:41:03 +0200110 testPatternProgram.draw(ts);
Jan Sebechlebsky288900f2024-05-24 14:47:54 +0200111 eglDisplayContext.swapBuffers();
112 lastFrameTs = getCurrentTimestamp();
113 }
114
115 ALOGV("Terminating test client render loop");
116}
117
118VirtualCameraTestInstance::VirtualCameraTestInstance(const int fps)
119 : mFps(fps) {
120}
121
122ScopedAStatus VirtualCameraTestInstance::onStreamConfigured(
123 const int32_t streamId, const Surface& surface, const int32_t width,
124 const int32_t height, const Format pixelFormat) {
Jan Sebechlebskya8542ea2024-05-28 11:44:46 +0200125 ALOGV("%s: streamId %d, %dx%d pixFmt=%s", __func__, streamId, width, height,
126 toString(pixelFormat).c_str());
Jan Sebechlebsky288900f2024-05-24 14:47:54 +0200127
Jan Sebechlebskyf8bb9752024-06-04 10:20:42 +0200128 auto renderer = std::make_shared<TestPatternRenderer>(
Jan Sebechlebsky288900f2024-05-24 14:47:54 +0200129 nativeWindowFromSurface(surface), mFps);
Jan Sebechlebskyf8bb9752024-06-04 10:20:42 +0200130
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 }
Jan Sebechlebsky288900f2024-05-24 14:47:54 +0200140
141 return ScopedAStatus::ok();
142}
143
144ScopedAStatus VirtualCameraTestInstance::onProcessCaptureRequest(
145 const int32_t /*in_streamId*/, const int32_t /*in_frameId*/) {
146 return ScopedAStatus::ok();
147}
148
149ScopedAStatus VirtualCameraTestInstance::onStreamClosed(const int32_t streamId) {
150 ALOGV("%s: streamId %d", __func__, streamId);
151
Jan Sebechlebskyf8bb9752024-06-04 10:20:42 +0200152 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();
Jan Sebechlebsky288900f2024-05-24 14:47:54 +0200163 }
164 return ScopedAStatus::ok();
165}
166
167} // namespace virtualcamera
168} // namespace companion
169} // namespace android