1 /* 2 * Copyright (C) 2016 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 #pragma once 18 19 #if GFXSTREAM_ENABLE_HOST_GLES 20 #include <EGL/egl.h> 21 #include <GLES2/gl2.h> 22 #include <GLES2/gl2ext.h> 23 24 #include "gl/EmulatedEglFenceSync.h" 25 #endif 26 27 #include <functional> 28 #include <future> 29 #include <string> 30 #include <type_traits> 31 32 #include "aemu/base/HealthMonitor.h" 33 #include "aemu/base/Optional.h" 34 #include "aemu/base/synchronization/ConditionVariable.h" 35 #include "aemu/base/synchronization/Lock.h" 36 #include "aemu/base/synchronization/MessageChannel.h" 37 #include "aemu/base/threads/Thread.h" 38 #include "aemu/base/threads/ThreadPool.h" 39 #include "render-utils/virtio_gpu_ops.h" 40 #include "vulkan/VkDecoderGlobalState.h" 41 42 namespace gfxstream { 43 44 using emugl::HealthMonitor; 45 using emugl::HealthWatchdog; 46 47 // SyncThread/////////////////////////////////////////////////////////////////// 48 // The purpose of SyncThread is to track sync device timelines and give out + 49 // signal FD's that correspond to the completion of host-side GL fence commands. 50 51 struct RenderThreadInfo; 52 class SyncThread : public android::base::Thread { 53 public: 54 // - constructor: start up the sync worker threads for a given context. 55 // The initialization of the sync threads is nonblocking. 56 // - Triggers a |SyncThreadCmd| with op code |SYNC_THREAD_EGL_INIT| 57 SyncThread(bool hasGl, HealthMonitor<>* healthMonitor); 58 ~SyncThread(); 59 60 // |triggerWaitVk|: async wait with a given VkFence object. 61 // The |vkFence| argument is a *boxed* host Vulkan handle of the fence. 62 // 63 // We call vkWaitForFences() on host Vulkan device to wait for the fence. 64 // After wait is over, the timeline will be incremented, 65 // which should signal the guest-side fence FD / Zircon eventpair. 66 // This method is how the goldfish sync virtual device 67 // knows when to increment timelines / signal native fence FD's. 68 void triggerWaitVk(VkFence vkFence, uint64_t timeline); 69 70 #if GFXSTREAM_ENABLE_HOST_GLES 71 // |triggerWait|: async wait with a given EmulatedEglFenceSync object. 72 // We use the wait() method to do a eglClientWaitSyncKHR. 73 // After wait is over, the timeline will be incremented, 74 // which should signal the guest-side fence FD. 75 // This method is how the goldfish sync virtual device 76 // knows when to increment timelines / signal native fence FD's. 77 void triggerWait(gl::EmulatedEglFenceSync* fenceSync, uint64_t timeline); 78 79 // For use with virtio-gpu and async fence completion callback. This is async like triggerWait, 80 // but takes a fence completion callback instead of incrementing some timeline directly. 81 void triggerWaitWithCompletionCallback(gl::EmulatedEglFenceSync* fenceSync, 82 FenceCompletionCallback); 83 84 // |initSyncContext| creates an EGL context expressly for calling 85 // eglClientWaitSyncKHR in the processing caused by |triggerWait|. 86 // This is used by the constructor only. It is non-blocking. 87 // - Triggers a |SyncThreadCmd| with op code |SYNC_THREAD_EGL_INIT| 88 void initSyncEGLContext(); 89 90 void doSyncWait(gl::EmulatedEglFenceSync* fenceSync, std::function<void()> onComplete); 91 #endif 92 93 // This increments the timeline after the QSRI completes. 94 void triggerWaitVkQsri(VkImage vkImage, uint64_t timeline); 95 96 void triggerWaitVkWithCompletionCallback(VkFence fenceHandle, FenceCompletionCallback); 97 void triggerWaitVkQsriWithCompletionCallback(VkImage image, FenceCompletionCallback); 98 void triggerGeneral(FenceCompletionCallback, std::string description); 99 100 // |cleanup|: for use with destructors and other cleanup functions. 101 // it destroys the sync context and exits the sync thread. 102 // This is blocking; after this function returns, we're sure 103 // the sync thread is gone. 104 // - Triggers a |SyncThreadCmd| with op code |SYNC_THREAD_EXIT| 105 void cleanup(); 106 107 // Initialize the global sync thread. 108 static void initialize(bool hasGl, HealthMonitor<>* healthMonitor); 109 110 // Obtains the global sync thread. 111 static SyncThread* get(); 112 113 // Destroys and cleanup the global sync thread. 114 static void destroy(); 115 116 private: 117 using WorkerId = android::base::ThreadPoolWorkerId; 118 struct Command { 119 std::packaged_task<int(WorkerId)> mTask; 120 std::string mDescription; 121 }; 122 using ThreadPool = android::base::ThreadPool<Command>; 123 124 // Thread function. 125 // It keeps the workers runner until |mExiting| is set. 126 virtual intptr_t main() override final; 127 128 // These two functions are used to communicate with the sync thread from another thread: 129 // - |sendAndWaitForResult| issues |job| to the sync thread, and blocks until it receives the 130 // result of the job. 131 // - |sendAsync| issues |job| to the sync thread and does not wait for the result, returning 132 // immediately after. 133 int sendAndWaitForResult(std::function<int(WorkerId)> job, std::string description); 134 void sendAsync(std::function<void(WorkerId)> job, std::string description); 135 136 // |doSyncThreadCmd| execute the actual task. These run on the sync thread. 137 void doSyncThreadCmd(Command&& command, ThreadPool::WorkerId); 138 139 static int doSyncWaitVk(VkFence, std::function<void()> onComplete); 140 141 // EGL objects / object handles specific to 142 // a sync thread. 143 static const uint32_t kNumWorkerThreads = 4u; 144 145 #if GFXSTREAM_ENABLE_HOST_GLES 146 EGLDisplay mDisplay = EGL_NO_DISPLAY; 147 EGLSurface mSurface[kNumWorkerThreads]; 148 EGLContext mContext[kNumWorkerThreads]; 149 #endif 150 151 bool mExiting = false; 152 android::base::Lock mLock; 153 android::base::ConditionVariable mCv; 154 ThreadPool mWorkerThreadPool; 155 bool mHasGl; 156 157 HealthMonitor<>* mHealthMonitor; 158 }; 159 160 } // namespace gfxstream 161