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 #ifndef ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERATESTINSTANCE_H 17 #define ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERATESTINSTANCE_H 18 19 #include <atomic> 20 #include <map> 21 #include <memory> 22 #include <thread> 23 24 #include "aidl/android/companion/virtualcamera/BnVirtualCameraCallback.h" 25 #include "android/native_window.h" 26 #include "utils/Mutex.h" 27 28 namespace android { 29 namespace companion { 30 namespace virtualcamera { 31 32 // Wraps render loop run in a dedicated thread, rendering test pattern to 33 // provided Surface (a.k.a. native window) at configured FPS. 34 class TestPatternRenderer 35 : public std::enable_shared_from_this<TestPatternRenderer> { 36 public: 37 TestPatternRenderer(std::shared_ptr<ANativeWindow> nativeWindow, int fps); 38 39 // Start rendering. 40 void start() EXCLUDES(mLock); 41 42 // Stop rendering. 43 // Call returns immediatelly, render thread might take some time (1 frame) 44 // to finish rendering and terminate the thread. 45 void stop() EXCLUDES(mLock); 46 47 private: 48 // Render thread entry point. 49 void renderThreadLoop(std::shared_ptr<ANativeWindow> nativeWindow); 50 51 const int mFps; 52 53 std::shared_ptr<ANativeWindow> mNativeWindow; 54 55 std::mutex mLock; 56 std::atomic_bool mRunning; 57 std::thread mThread GUARDED_BY(mLock); 58 }; 59 60 // VirtualCamera callback implementation for test camera. 61 // 62 // For every configure call, starts rendering of test pattern on provided surface. 63 class VirtualCameraTestInstance 64 : public aidl::android::companion::virtualcamera::BnVirtualCameraCallback { 65 public: 66 explicit VirtualCameraTestInstance(int fps = 30); 67 68 ::ndk::ScopedAStatus onStreamConfigured( 69 int32_t streamId, const ::aidl::android::view::Surface& surface, 70 int32_t width, int32_t height, 71 ::aidl::android::companion::virtualcamera::Format pixelFormat) override 72 EXCLUDES(mLock); 73 74 ::ndk::ScopedAStatus onProcessCaptureRequest(int32_t in_streamId, 75 int32_t in_frameId) override; 76 77 ::ndk::ScopedAStatus onStreamClosed(int32_t streamId) override EXCLUDES(mLock); 78 79 private: 80 const int mFps; 81 82 std::mutex mLock; 83 // Map maintaining streamId -> TestPatternRenderer mapping for active 84 // input streams. 85 std::map<int, std::shared_ptr<TestPatternRenderer>> mInputRenderers 86 GUARDED_BY(mLock); 87 }; 88 89 } // namespace virtualcamera 90 } // namespace companion 91 } // namespace android 92 93 #endif // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERATESTINSTANCE_H 94