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 | |
| 19 | #include "IOStream.h" |
| 20 | #include "renderControl_enc.h" |
Yahan Zhou | b7f0908 | 2016-03-10 11:45:02 -0800 | [diff] [blame] | 21 | #include "ChecksumCalculator.h" |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 22 | #include "goldfish_dma.h" |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 23 | |
Lingfeng Yang | 1883535 | 2016-05-23 12:16:04 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 26 | class GLEncoder; |
Yahan Zhou | e8cf63d | 2016-09-22 12:33:50 -0700 | [diff] [blame] | 27 | struct gl_client_context_t; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 28 | class GL2Encoder; |
Yahan Zhou | e8cf63d | 2016-09-22 12:33:50 -0700 | [diff] [blame] | 29 | struct gl2_client_context_t; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 30 | |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 31 | // SyncImpl determines the presence of host/guest OpenGL fence sync |
| 32 | // capabilities. It corresponds exactly to EGL_ANDROID_native_fence_sync |
| 33 | // capability, but for the emulator, we need to make sure that |
| 34 | // OpenGL pipe protocols match, so we use a special extension name |
| 35 | // here. |
| 36 | // SYNC_IMPL_NONE means that the native fence sync capability is |
| 37 | // not present, and we will end up using the equivalent of glFinish |
| 38 | // in order to preserve buffer swapping order. |
| 39 | // SYNC_IMPL_NATIVE_SYNC means that we do have native fence sync |
| 40 | // capability, and we will use a fence fd to synchronize buffer swaps. |
| 41 | enum SyncImpl { |
| 42 | SYNC_IMPL_NONE = 0, |
| 43 | SYNC_IMPL_NATIVE_SYNC = 1 |
| 44 | }; |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 45 | |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 46 | // Interface: |
| 47 | // If this GL extension string shows up, we use |
| 48 | // SYNC_IMPL_NATIVE_SYNC, otherwise we use SYNC_IMPL_NONE. |
Lingfeng Yang | 2ce4c31 | 2016-08-31 14:46:11 -0700 | [diff] [blame] | 49 | // This string is always updated to require the _latest_ |
| 50 | // version of Android emulator native sync in this system image; |
| 51 | // otherwise, we do not use the feature. |
| 52 | static const char kRCNativeSync[] = "ANDROID_EMU_native_sync_v2"; |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 53 | |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 54 | // DMA for OpenGL |
| 55 | enum DmaImpl { |
| 56 | DMA_IMPL_NONE = 0, |
| 57 | DMA_IMPL_v1 = 1, |
| 58 | }; |
| 59 | |
| 60 | static const char kDmaExtStr_v1[] = "ANDROID_EMU_dma_v1"; |
| 61 | |
Lingfeng Yang | e6556dc | 2017-01-09 12:04:12 -0800 | [diff] [blame^] | 62 | // OpenGL ES max supported version |
| 63 | enum GLESMaxVersion { |
| 64 | GLES_MAX_VERSION_2 = 0, |
| 65 | GLES_MAX_VERSION_3_0 = 1, |
| 66 | GLES_MAX_VERSION_3_1 = 2, |
| 67 | GLES_MAX_VERSION_3_2 = 3, |
| 68 | }; |
| 69 | |
| 70 | static const char kGLESMaxVersion_2[] = "ANDROID_EMU_gles_max_version_2"; |
| 71 | static const char kGLESMaxVersion_3_0[] = "ANDROID_EMU_gles_max_version_3_0"; |
| 72 | static const char kGLESMaxVersion_3_1[] = "ANDROID_EMU_gles_max_version_3_1"; |
| 73 | static const char kGLESMaxVersion_3_2[] = "ANDROID_EMU_gles_max_version_3_2"; |
| 74 | |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 75 | // ExtendedRCEncoderContext is an extended version of renderControl_encoder_context_t |
| 76 | // that will be used to track SyncImpl. |
| 77 | class ExtendedRCEncoderContext : public renderControl_encoder_context_t { |
| 78 | public: |
| 79 | ExtendedRCEncoderContext(IOStream *stream, ChecksumCalculator *checksumCalculator) |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 80 | : renderControl_encoder_context_t(stream, checksumCalculator) { |
| 81 | m_dmaCxt = NULL; |
| 82 | } |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 83 | void setSyncImpl(SyncImpl syncImpl) { m_syncImpl = syncImpl; } |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 84 | void setDmaImpl(DmaImpl dmaImpl) { m_dmaImpl = dmaImpl; } |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 85 | bool hasNativeSync() const { return m_syncImpl == SYNC_IMPL_NATIVE_SYNC; } |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 86 | DmaImpl getDmaVersion() const { return m_dmaImpl; } |
| 87 | void bindDmaContext(struct goldfish_dma_context* cxt) { m_dmaCxt = cxt; } |
| 88 | virtual uint64_t lockAndWriteDma(void* data, uint32_t size) { |
| 89 | ALOGV("%s: call", __FUNCTION__); |
| 90 | if (!m_dmaCxt) { |
| 91 | ALOGE("%s: ERROR: No DMA context bound!", |
| 92 | __FUNCTION__); |
| 93 | return 0; |
| 94 | } |
| 95 | goldfish_dma_lock(m_dmaCxt); |
| 96 | goldfish_dma_write(m_dmaCxt, data, size); |
| 97 | uint64_t paddr = goldfish_dma_guest_paddr(m_dmaCxt); |
| 98 | ALOGV("%s: paddr=0x%llx", __FUNCTION__, paddr); |
| 99 | return paddr; |
| 100 | } |
Lingfeng Yang | e6556dc | 2017-01-09 12:04:12 -0800 | [diff] [blame^] | 101 | void setGLESMaxVersion(GLESMaxVersion ver) { m_glesMaxVersion = ver; } |
| 102 | GLESMaxVersion getGLESMaxVersion() const { return m_glesMaxVersion; } |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 103 | private: |
| 104 | SyncImpl m_syncImpl; |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 105 | DmaImpl m_dmaImpl; |
| 106 | struct goldfish_dma_context* m_dmaCxt; |
Lingfeng Yang | e6556dc | 2017-01-09 12:04:12 -0800 | [diff] [blame^] | 107 | GLESMaxVersion m_glesMaxVersion; |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 110 | class HostConnection |
| 111 | { |
| 112 | public: |
| 113 | static HostConnection *get(); |
Lingfeng Yang | 200c716 | 2016-06-13 08:39:28 -0700 | [diff] [blame] | 114 | static void exit(); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 115 | ~HostConnection(); |
| 116 | |
| 117 | GLEncoder *glEncoder(); |
| 118 | GL2Encoder *gl2Encoder(); |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 119 | ExtendedRCEncoderContext *rcEncoder(); |
Yahan Zhou | b7f0908 | 2016-03-10 11:45:02 -0800 | [diff] [blame] | 120 | ChecksumCalculator *checksumHelper() { return &m_checksumHelper; } |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 121 | |
| 122 | void flush() { |
| 123 | if (m_stream) { |
| 124 | m_stream->flush(); |
| 125 | } |
| 126 | } |
| 127 | |
Lingfeng Yang | 200c716 | 2016-06-13 08:39:28 -0700 | [diff] [blame] | 128 | void setGrallocOnly(bool gralloc_only) { |
| 129 | m_grallocOnly = gralloc_only; |
| 130 | } |
| 131 | |
| 132 | bool isGrallocOnly() const { return m_grallocOnly; } |
| 133 | |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 134 | int getPipeFd() const { return m_pipeFd; } |
| 135 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 136 | private: |
| 137 | HostConnection(); |
| 138 | static gl_client_context_t *s_getGLContext(); |
| 139 | static gl2_client_context_t *s_getGL2Context(); |
Lingfeng Yang | 1883535 | 2016-05-23 12:16:04 -0700 | [diff] [blame] | 140 | |
Yurii Zubrytskyi | b232764 | 2016-11-04 12:01:36 -0700 | [diff] [blame] | 141 | const std::string& queryGLExtensions(ExtendedRCEncoderContext *rcEnc); |
Yahan Zhou | b7f0908 | 2016-03-10 11:45:02 -0800 | [diff] [blame] | 142 | // setProtocol initilizes GL communication protocol for checksums |
| 143 | // should be called when m_rcEnc is created |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 144 | void setChecksumHelper(ExtendedRCEncoderContext *rcEnc); |
| 145 | void queryAndSetSyncImpl(ExtendedRCEncoderContext *rcEnc); |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 146 | void queryAndSetDmaImpl(ExtendedRCEncoderContext *rcEnc); |
Lingfeng Yang | e6556dc | 2017-01-09 12:04:12 -0800 | [diff] [blame^] | 147 | void queryAndSetGLESMaxVersion(ExtendedRCEncoderContext *rcEnc); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 148 | |
| 149 | private: |
| 150 | IOStream *m_stream; |
| 151 | GLEncoder *m_glEnc; |
| 152 | GL2Encoder *m_gl2Enc; |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 153 | ExtendedRCEncoderContext *m_rcEnc; |
Yahan Zhou | b7f0908 | 2016-03-10 11:45:02 -0800 | [diff] [blame] | 154 | ChecksumCalculator m_checksumHelper; |
Lingfeng Yang | 4f12b8d | 2016-07-13 16:26:10 -0700 | [diff] [blame] | 155 | std::string m_glExtensions; |
Lingfeng Yang | 200c716 | 2016-06-13 08:39:28 -0700 | [diff] [blame] | 156 | bool m_grallocOnly; |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 157 | int m_pipeFd; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | #endif |