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