blob: bae871d6acebaa443a64d07b977e42ac88a95a0b [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.
110 testPatternProgram.draw(frameNumber++);
111 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
128 std::lock_guard<std::mutex> lock(mLock);
129 mRenderer = std::make_shared<TestPatternRenderer>(
130 nativeWindowFromSurface(surface), mFps);
131 mRenderer->start();
132
133 return ScopedAStatus::ok();
134}
135
136ScopedAStatus VirtualCameraTestInstance::onProcessCaptureRequest(
137 const int32_t /*in_streamId*/, const int32_t /*in_frameId*/) {
138 return ScopedAStatus::ok();
139}
140
141ScopedAStatus VirtualCameraTestInstance::onStreamClosed(const int32_t streamId) {
142 ALOGV("%s: streamId %d", __func__, streamId);
143
144 std::lock_guard<std::mutex> lock(mLock);
145 if (mRenderer != nullptr) {
146 mRenderer->stop();
147 mRenderer.reset();
148 }
149 return ScopedAStatus::ok();
150}
151
152} // namespace virtualcamera
153} // namespace companion
154} // namespace android