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