1 /* 2 * Copyright (C) 2011 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 __COMMON_HOST_CONNECTION_H 17 #define __COMMON_HOST_CONNECTION_H 18 19 #include <cstring> 20 #include <memory> 21 #include <mutex> 22 #include <optional> 23 #include <string> 24 25 #include "ANativeWindow.h" 26 #include "EmulatorFeatureInfo.h" 27 #include "Gralloc.h" 28 #include "Sync.h" 29 #include "VirtGpu.h" 30 #include "gfxstream/guest/ChecksumCalculator.h" 31 #include "gfxstream/guest/IOStream.h" 32 33 #if GFXSTREAM_ENABLE_GUEST_GLES 34 #include "HostConnectionGles.h" 35 #endif 36 37 namespace gfxstream { 38 namespace vk { 39 class VkEncoder; 40 } // namespace vk 41 } // namespace gfxstream 42 43 struct EGLThreadInfo; 44 45 class HostConnection 46 { 47 public: 48 static HostConnection *get(); 49 static HostConnection* getOrCreate(enum VirtGpuCapset capset = kCapsetNone); 50 51 static HostConnection* getWithThreadInfo(EGLThreadInfo* tInfo, 52 enum VirtGpuCapset capset = kCapsetNone); 53 static void exit(); 54 static void exitUnclean(); // for testing purposes 55 56 static std::unique_ptr<HostConnection> createUnique(enum VirtGpuCapset capset = kCapsetNone); 57 HostConnection(const HostConnection&) = delete; 58 59 ~HostConnection(); 60 61 #if GFXSTREAM_ENABLE_GUEST_GLES 62 GLEncoder *glEncoder(); 63 GL2Encoder* gl2Encoder(); 64 ExtendedRCEncoderContext *rcEncoder(); checksumHelper()65 gfxstream::guest::ChecksumCalculator* checksumHelper() { return &m_checksumHelper; } 66 #endif 67 68 gfxstream::vk::VkEncoder* vkEncoder(); 69 getRendernodeFd()70 int getRendernodeFd() { return m_rendernodeFd; } 71 grallocHelper()72 gfxstream::Gralloc* grallocHelper() { return m_grallocHelper; } setGrallocHelperForTesting(gfxstream::Gralloc * gralloc)73 void setGrallocHelperForTesting(gfxstream::Gralloc* gralloc) { m_grallocHelper = gralloc; } 74 syncHelper()75 gfxstream::SyncHelper* syncHelper() { return m_syncHelper.get(); } setSyncHelperForTesting(gfxstream::SyncHelper * sync)76 void setSyncHelperForTesting(gfxstream::SyncHelper* sync) { m_syncHelper.reset(sync); } 77 anwHelper()78 gfxstream::ANativeWindowHelper* anwHelper() { return m_anwHelper; } setANativeWindowHelperForTesting(gfxstream::ANativeWindowHelper * anw)79 void setANativeWindowHelperForTesting(gfxstream::ANativeWindowHelper* anw) { m_anwHelper = anw; } 80 flush()81 void flush() { 82 if (m_stream) { 83 m_stream->flush(); 84 } 85 } 86 87 #ifdef __clang__ 88 #pragma clang diagnostic push 89 #pragma clang diagnostic ignored "-Wthread-safety-analysis" 90 #endif lock()91 void lock() const { m_lock.lock(); } unlock()92 void unlock() const { m_lock.unlock(); } 93 #ifdef __clang__ 94 #pragma clang diagnostic pop 95 #endif 96 97 bool exitUncleanly; // for testing purposes 98 99 private: 100 // If the connection failed, |conn| is deleted. 101 // Returns NULL if connection failed. 102 static std::unique_ptr<HostConnection> connect(enum VirtGpuCapset capset); 103 104 HostConnection(); 105 106 #if GFXSTREAM_ENABLE_GUEST_GLES 107 static gl_client_context_t* s_getGLContext(); 108 static gl2_client_context_t* s_getGL2Context(); 109 110 const std::string& queryHostExtensions(ExtendedRCEncoderContext* rcEnc); 111 void setChecksumHelper(ExtendedRCEncoderContext* rcEnc); 112 void queryAndSetSyncImpl(ExtendedRCEncoderContext* rcEnc); 113 void queryAndSetDmaImpl(ExtendedRCEncoderContext* rcEnc); 114 void queryAndSetGLESMaxVersion(ExtendedRCEncoderContext* rcEnc); 115 void queryAndSetNoErrorState(ExtendedRCEncoderContext* rcEnc); 116 void queryAndSetHostCompositionImpl(ExtendedRCEncoderContext* rcEnc); 117 void queryAndSetDirectMemSupport(ExtendedRCEncoderContext* rcEnc); 118 void queryAndSetVulkanSupport(ExtendedRCEncoderContext* rcEnc); 119 void queryAndSetDeferredVulkanCommandsSupport(ExtendedRCEncoderContext* rcEnc); 120 void queryAndSetVulkanNullOptionalStringsSupport(ExtendedRCEncoderContext* rcEnc); 121 void queryAndSetVulkanCreateResourcesWithRequirementsSupport(ExtendedRCEncoderContext* rcEnc); 122 void queryAndSetVulkanIgnoredHandles(ExtendedRCEncoderContext* rcEnc); 123 void queryAndSetYUVCache(ExtendedRCEncoderContext* mrcEnc); 124 void queryAndSetAsyncUnmapBuffer(ExtendedRCEncoderContext* rcEnc); 125 void queryAndSetVirtioGpuNext(ExtendedRCEncoderContext* rcEnc); 126 void queryHasSharedSlotsHostMemoryAllocator(ExtendedRCEncoderContext* rcEnc); 127 void queryAndSetVulkanFreeMemorySync(ExtendedRCEncoderContext* rcEnc); 128 void queryAndSetVirtioGpuNativeSync(ExtendedRCEncoderContext* rcEnc); 129 void queryAndSetVulkanShaderFloat16Int8Support(ExtendedRCEncoderContext* rcEnc); 130 void queryAndSetVulkanAsyncQueueSubmitSupport(ExtendedRCEncoderContext* rcEnc); 131 void queryAndSetHostSideTracingSupport(ExtendedRCEncoderContext* rcEnc); 132 void queryAndSetAsyncFrameCommands(ExtendedRCEncoderContext* rcEnc); 133 void queryAndSetVulkanQueueSubmitWithCommandsSupport(ExtendedRCEncoderContext* rcEnc); 134 void queryAndSetVulkanBatchedDescriptorSetUpdateSupport(ExtendedRCEncoderContext* rcEnc); 135 void queryAndSetSyncBufferData(ExtendedRCEncoderContext* rcEnc); 136 void queryAndSetVulkanAsyncQsri(ExtendedRCEncoderContext* rcEnc); 137 void queryAndSetReadColorBufferDma(ExtendedRCEncoderContext* rcEnc); 138 void queryAndSetHWCMultiConfigs(ExtendedRCEncoderContext* rcEnc); 139 void queryAndSetVulkanAuxCommandBufferMemory(ExtendedRCEncoderContext* rcEnc); 140 GLint queryVersion(ExtendedRCEncoderContext* rcEnc); 141 #endif 142 143 private: 144 HostConnectionType m_connectionType; 145 GrallocType m_grallocType; 146 147 // intrusively refcounted 148 gfxstream::guest::IOStream* m_stream = nullptr; 149 150 #if GFXSTREAM_ENABLE_GUEST_GLES 151 std::unique_ptr<GLEncoder> m_glEnc; 152 std::unique_ptr<GL2Encoder> m_gl2Enc; 153 std::unique_ptr<ExtendedRCEncoderContext> m_rcEnc; 154 gfxstream::guest::ChecksumCalculator m_checksumHelper; 155 #endif 156 157 // intrusively refcounted 158 gfxstream::vk::VkEncoder* m_vkEnc = nullptr; 159 160 gfxstream::ANativeWindowHelper* m_anwHelper = nullptr; 161 gfxstream::Gralloc* m_grallocHelper = nullptr; 162 std::unique_ptr<gfxstream::SyncHelper> m_syncHelper; 163 std::string m_hostExtensions; 164 bool m_noHostError; 165 mutable std::mutex m_lock; 166 int m_rendernodeFd; 167 }; 168 169 #endif 170