keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 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 | |
Lingfeng Yang | abc6516 | 2018-12-21 18:14:48 -0800 | [diff] [blame] | 19 | #include "EmulatorFeatureInfo.h" |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 20 | #include "IOStream.h" |
| 21 | #include "renderControl_enc.h" |
Yahan Zhou | b7f0908 | 2016-03-10 11:45:02 -0800 | [diff] [blame] | 22 | #include "ChecksumCalculator.h" |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 23 | #include "goldfish_dma.h" |
Roman Kiryanov | 382ce56 | 2019-06-03 12:39:58 -0700 | [diff] [blame^] | 24 | #include "goldfish_address_space.h" |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 25 | |
Alistair Strachan | 694745b | 2018-03-30 18:11:39 -0700 | [diff] [blame] | 26 | #include <cutils/native_handle.h> |
Lingfeng Yang | 1ec6f43 | 2019-01-23 10:04:52 -0800 | [diff] [blame] | 27 | |
Lingfeng Yang | ad6f67d | 2019-02-19 15:47:53 -0800 | [diff] [blame] | 28 | #ifdef GOLDFISH_VULKAN |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 29 | #include <mutex> |
Lingfeng Yang | ad6f67d | 2019-02-19 15:47:53 -0800 | [diff] [blame] | 30 | #else |
| 31 | #include <utils/threads.h> |
| 32 | #endif |
| 33 | |
Lingfeng Yang | 1883535 | 2016-05-23 12:16:04 -0700 | [diff] [blame] | 34 | #include <string> |
| 35 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 36 | class GLEncoder; |
Yahan Zhou | e8cf63d | 2016-09-22 12:33:50 -0700 | [diff] [blame] | 37 | struct gl_client_context_t; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 38 | class GL2Encoder; |
Yahan Zhou | e8cf63d | 2016-09-22 12:33:50 -0700 | [diff] [blame] | 39 | struct gl2_client_context_t; |
Lingfeng Yang | b1d9010 | 2018-11-29 16:10:36 -0800 | [diff] [blame] | 40 | |
| 41 | namespace goldfish_vk { |
Lingfeng Yang | 6a28367 | 2018-11-01 13:37:56 -0700 | [diff] [blame] | 42 | class VkEncoder; |
Lingfeng Yang | b1d9010 | 2018-11-29 16:10:36 -0800 | [diff] [blame] | 43 | } |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 44 | |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 45 | // ExtendedRCEncoderContext is an extended version of renderControl_encoder_context_t |
Lingfeng Yang | abc6516 | 2018-12-21 18:14:48 -0800 | [diff] [blame] | 46 | // that will be used to track available emulator features. |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 47 | class ExtendedRCEncoderContext : public renderControl_encoder_context_t { |
| 48 | public: |
| 49 | ExtendedRCEncoderContext(IOStream *stream, ChecksumCalculator *checksumCalculator) |
Lingfeng Yang | abc6516 | 2018-12-21 18:14:48 -0800 | [diff] [blame] | 50 | : renderControl_encoder_context_t(stream, checksumCalculator), |
Roman Kiryanov | 382ce56 | 2019-06-03 12:39:58 -0700 | [diff] [blame^] | 51 | m_dmaCxt(NULL), m_addressSpaceBlock(NULL) { } |
Lingfeng Yang | abc6516 | 2018-12-21 18:14:48 -0800 | [diff] [blame] | 52 | void setSyncImpl(SyncImpl syncImpl) { m_featureInfo.syncImpl = syncImpl; } |
| 53 | void setDmaImpl(DmaImpl dmaImpl) { m_featureInfo.dmaImpl = dmaImpl; } |
huans | d96d5bd | 2018-10-15 14:32:46 -0700 | [diff] [blame] | 54 | void setHostComposition(HostComposition hostComposition) { |
Lingfeng Yang | abc6516 | 2018-12-21 18:14:48 -0800 | [diff] [blame] | 55 | m_featureInfo.hostComposition = hostComposition; } |
| 56 | bool hasNativeSync() const { return m_featureInfo.syncImpl >= SYNC_IMPL_NATIVE_SYNC_V2; } |
| 57 | bool hasNativeSyncV3() const { return m_featureInfo.syncImpl >= SYNC_IMPL_NATIVE_SYNC_V3; } |
huans | d96d5bd | 2018-10-15 14:32:46 -0700 | [diff] [blame] | 58 | bool hasHostCompositionV1() const { |
Lingfeng Yang | abc6516 | 2018-12-21 18:14:48 -0800 | [diff] [blame] | 59 | return m_featureInfo.hostComposition == HOST_COMPOSITION_V1; } |
| 60 | DmaImpl getDmaVersion() const { return m_featureInfo.dmaImpl; } |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 61 | void bindDmaContext(struct goldfish_dma_context* cxt) { m_dmaCxt = cxt; } |
Roman Kiryanov | 382ce56 | 2019-06-03 12:39:58 -0700 | [diff] [blame^] | 62 | void bindAddressSpaceBlock(GoldfishAddressSpaceBlock* block) { |
| 63 | m_addressSpaceBlock = block; |
| 64 | } |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 65 | virtual uint64_t lockAndWriteDma(void* data, uint32_t size) { |
Roman Kiryanov | 382ce56 | 2019-06-03 12:39:58 -0700 | [diff] [blame^] | 66 | if (m_addressSpaceBlock) { |
| 67 | return writeAddressSpaceBlock(data, size, m_addressSpaceBlock); |
| 68 | } else if (m_dmaCxt) { |
| 69 | return writeGoldfishDma(data, size, m_dmaCxt); |
Roman Kiryanov | 943161f | 2019-03-22 12:57:52 -0700 | [diff] [blame] | 70 | } else { |
| 71 | ALOGE("%s: ERROR: No DMA context bound!", __func__); |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 74 | } |
Lingfeng Yang | abc6516 | 2018-12-21 18:14:48 -0800 | [diff] [blame] | 75 | void setGLESMaxVersion(GLESMaxVersion ver) { m_featureInfo.glesMaxVersion = ver; } |
| 76 | GLESMaxVersion getGLESMaxVersion() const { return m_featureInfo.glesMaxVersion; } |
Roman Kiryanov | 382ce56 | 2019-06-03 12:39:58 -0700 | [diff] [blame^] | 77 | bool hasDirectMem() const { |
| 78 | #ifdef HOST_BUILD |
| 79 | // unit tests do not support restoring "guest" ram because there is no VM |
| 80 | return false; |
| 81 | #else |
| 82 | return m_featureInfo.hasDirectMem; |
| 83 | #endif |
| 84 | } |
Lingfeng Yang | abc6516 | 2018-12-21 18:14:48 -0800 | [diff] [blame] | 85 | |
| 86 | const EmulatorFeatureInfo* featureInfo_const() const { return &m_featureInfo; } |
| 87 | EmulatorFeatureInfo* featureInfo() { return &m_featureInfo; } |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 88 | private: |
Roman Kiryanov | 382ce56 | 2019-06-03 12:39:58 -0700 | [diff] [blame^] | 89 | static uint64_t writeGoldfishDma(void* data, uint32_t size, |
| 90 | struct goldfish_dma_context* dmaCxt) { |
Roman Kiryanov | 943161f | 2019-03-22 12:57:52 -0700 | [diff] [blame] | 91 | ALOGV("%s(data=%p, size=%u): call", __func__, data, size); |
| 92 | |
| 93 | goldfish_dma_write(dmaCxt, data, size); |
| 94 | uint64_t paddr = goldfish_dma_guest_paddr(dmaCxt); |
| 95 | |
| 96 | ALOGV("%s: paddr=0x%llx", __func__, (unsigned long long)paddr); |
| 97 | return paddr; |
| 98 | } |
| 99 | |
Roman Kiryanov | 382ce56 | 2019-06-03 12:39:58 -0700 | [diff] [blame^] | 100 | static uint64_t writeAddressSpaceBlock(void* data, uint32_t size, |
| 101 | GoldfishAddressSpaceBlock* block) { |
| 102 | ALOGV("%s(data=%p, size=%u): call", __func__, data, size); |
| 103 | |
| 104 | memcpy(block->guestPtr(), data, size); |
| 105 | const uint64_t paddr = block->physAddr(); |
| 106 | |
| 107 | ALOGV("%s: paddr=0x%llx", __func__, (unsigned long long)paddr); |
| 108 | return paddr; |
| 109 | } |
| 110 | |
Lingfeng Yang | abc6516 | 2018-12-21 18:14:48 -0800 | [diff] [blame] | 111 | EmulatorFeatureInfo m_featureInfo; |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 112 | struct goldfish_dma_context* m_dmaCxt; |
Roman Kiryanov | 382ce56 | 2019-06-03 12:39:58 -0700 | [diff] [blame^] | 113 | GoldfishAddressSpaceBlock* m_addressSpaceBlock; |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
Alistair Strachan | 694745b | 2018-03-30 18:11:39 -0700 | [diff] [blame] | 116 | // Abstraction for gralloc handle conversion |
| 117 | class Gralloc { |
| 118 | public: |
| 119 | virtual uint32_t getHostHandle(native_handle_t const* handle) = 0; |
| 120 | virtual int getFormat(native_handle_t const* handle) = 0; |
| 121 | virtual ~Gralloc() {} |
| 122 | }; |
| 123 | |
| 124 | // Abstraction for process pipe helper |
| 125 | class ProcessPipe { |
| 126 | public: |
| 127 | virtual bool processPipeInit(renderControl_encoder_context_t *rcEnc) = 0; |
| 128 | virtual ~ProcessPipe() {} |
| 129 | }; |
| 130 | |
Lingfeng Yang | 6e99bcf | 2017-04-07 13:48:13 -0700 | [diff] [blame] | 131 | struct EGLThreadInfo; |
| 132 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 133 | class HostConnection |
| 134 | { |
| 135 | public: |
| 136 | static HostConnection *get(); |
Lingfeng Yang | 6e99bcf | 2017-04-07 13:48:13 -0700 | [diff] [blame] | 137 | static HostConnection *getWithThreadInfo(EGLThreadInfo* tInfo); |
Lingfeng Yang | 200c716 | 2016-06-13 08:39:28 -0700 | [diff] [blame] | 138 | static void exit(); |
Lingfeng Yang | 1ec6f43 | 2019-01-23 10:04:52 -0800 | [diff] [blame] | 139 | |
| 140 | static HostConnection *createUnique(); |
| 141 | static void teardownUnique(HostConnection* con); |
| 142 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 143 | ~HostConnection(); |
| 144 | |
| 145 | GLEncoder *glEncoder(); |
| 146 | GL2Encoder *gl2Encoder(); |
Lingfeng Yang | b1d9010 | 2018-11-29 16:10:36 -0800 | [diff] [blame] | 147 | goldfish_vk::VkEncoder *vkEncoder(); |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 148 | ExtendedRCEncoderContext *rcEncoder(); |
Yahan Zhou | b7f0908 | 2016-03-10 11:45:02 -0800 | [diff] [blame] | 149 | ChecksumCalculator *checksumHelper() { return &m_checksumHelper; } |
Alistair Strachan | 694745b | 2018-03-30 18:11:39 -0700 | [diff] [blame] | 150 | Gralloc *grallocHelper() { return m_grallocHelper; } |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 151 | |
| 152 | void flush() { |
| 153 | if (m_stream) { |
| 154 | m_stream->flush(); |
| 155 | } |
| 156 | } |
| 157 | |
Lingfeng Yang | 200c716 | 2016-06-13 08:39:28 -0700 | [diff] [blame] | 158 | void setGrallocOnly(bool gralloc_only) { |
| 159 | m_grallocOnly = gralloc_only; |
| 160 | } |
| 161 | |
| 162 | bool isGrallocOnly() const { return m_grallocOnly; } |
| 163 | |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 164 | #ifdef __clang__ |
| 165 | #pragma clang diagnostic push |
| 166 | #pragma clang diagnostic ignored "-Wthread-safety-analysis" |
| 167 | #endif |
Lingfeng Yang | 1ec6f43 | 2019-01-23 10:04:52 -0800 | [diff] [blame] | 168 | void lock() const { m_lock.lock(); } |
| 169 | void unlock() const { m_lock.unlock(); } |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 170 | #ifdef __clang__ |
| 171 | #pragma clang diagnostic pop |
| 172 | #endif |
Lingfeng Yang | 1ec6f43 | 2019-01-23 10:04:52 -0800 | [diff] [blame] | 173 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 174 | private: |
Lingfeng Yang | 1ec6f43 | 2019-01-23 10:04:52 -0800 | [diff] [blame] | 175 | // If the connection failed, |conn| is deleted. |
| 176 | // Returns NULL if connection failed. |
| 177 | static HostConnection* connect(HostConnection* con); |
| 178 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 179 | HostConnection(); |
| 180 | static gl_client_context_t *s_getGLContext(); |
| 181 | static gl2_client_context_t *s_getGL2Context(); |
Lingfeng Yang | 1883535 | 2016-05-23 12:16:04 -0700 | [diff] [blame] | 182 | |
Yurii Zubrytskyi | b232764 | 2016-11-04 12:01:36 -0700 | [diff] [blame] | 183 | const std::string& queryGLExtensions(ExtendedRCEncoderContext *rcEnc); |
Yahan Zhou | b7f0908 | 2016-03-10 11:45:02 -0800 | [diff] [blame] | 184 | // setProtocol initilizes GL communication protocol for checksums |
| 185 | // should be called when m_rcEnc is created |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 186 | void setChecksumHelper(ExtendedRCEncoderContext *rcEnc); |
| 187 | void queryAndSetSyncImpl(ExtendedRCEncoderContext *rcEnc); |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 188 | void queryAndSetDmaImpl(ExtendedRCEncoderContext *rcEnc); |
Lingfeng Yang | e6556dc | 2017-01-09 12:04:12 -0800 | [diff] [blame] | 189 | void queryAndSetGLESMaxVersion(ExtendedRCEncoderContext *rcEnc); |
Lingfeng Yang | e980c7a | 2018-01-12 14:50:18 -0800 | [diff] [blame] | 190 | void queryAndSetNoErrorState(ExtendedRCEncoderContext *rcEnc); |
huans | d96d5bd | 2018-10-15 14:32:46 -0700 | [diff] [blame] | 191 | void queryAndSetHostCompositionImpl(ExtendedRCEncoderContext *rcEnc); |
Lingfeng Yang | abc6516 | 2018-12-21 18:14:48 -0800 | [diff] [blame] | 192 | void queryAndSetDirectMemSupport(ExtendedRCEncoderContext *rcEnc); |
| 193 | void queryAndSetVulkanSupport(ExtendedRCEncoderContext *rcEnc); |
Lingfeng Yang | c53e747 | 2019-03-27 08:50:55 -0700 | [diff] [blame] | 194 | void queryAndSetDeferredVulkanCommandsSupport(ExtendedRCEncoderContext *rcEnc); |
Lingfeng Yang | b55ed1c | 2019-06-20 15:57:08 -0700 | [diff] [blame] | 195 | void queryAndSetVulkanNullOptionalStringsSupport(ExtendedRCEncoderContext *rcEnc); |
Lingfeng Yang | 87f1196 | 2019-06-27 16:28:53 +0000 | [diff] [blame] | 196 | void queryAndSetVulkanCreateResourcesWithRequirementsSupport(ExtendedRCEncoderContext *rcEnc); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 197 | |
| 198 | private: |
| 199 | IOStream *m_stream; |
| 200 | GLEncoder *m_glEnc; |
| 201 | GL2Encoder *m_gl2Enc; |
Lingfeng Yang | b1d9010 | 2018-11-29 16:10:36 -0800 | [diff] [blame] | 202 | goldfish_vk::VkEncoder *m_vkEnc; |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 203 | ExtendedRCEncoderContext *m_rcEnc; |
Yahan Zhou | b7f0908 | 2016-03-10 11:45:02 -0800 | [diff] [blame] | 204 | ChecksumCalculator m_checksumHelper; |
Alistair Strachan | 694745b | 2018-03-30 18:11:39 -0700 | [diff] [blame] | 205 | Gralloc *m_grallocHelper; |
| 206 | ProcessPipe *m_processPipe; |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 207 | std::string m_glExtensions; |
Lingfeng Yang | 200c716 | 2016-06-13 08:39:28 -0700 | [diff] [blame] | 208 | bool m_grallocOnly; |
Lingfeng Yang | e980c7a | 2018-01-12 14:50:18 -0800 | [diff] [blame] | 209 | bool m_noHostError; |
Lingfeng Yang | ad6f67d | 2019-02-19 15:47:53 -0800 | [diff] [blame] | 210 | #ifdef GOLDFISH_VULKAN |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 211 | mutable std::mutex m_lock; |
Lingfeng Yang | ad6f67d | 2019-02-19 15:47:53 -0800 | [diff] [blame] | 212 | #else |
| 213 | mutable android::Mutex m_lock; |
| 214 | #endif |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | #endif |