blob: 2c8151717d3c0553aef6f0f3a998c21c3df7d588 [file] [log] [blame]
keunyoungb85b2752013-03-08 12:28:03 -08001/*
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 Zhoub7f09082016-03-10 11:45:02 -080021#include "ChecksumCalculator.h"
Lingfeng Yang88c170c2016-11-30 00:52:35 +000022#include "goldfish_dma.h"
keunyoungb85b2752013-03-08 12:28:03 -080023
Lingfeng Yang18835352016-05-23 12:16:04 -070024#include <string>
25
keunyoungb85b2752013-03-08 12:28:03 -080026class GLEncoder;
Yahan Zhoue8cf63d2016-09-22 12:33:50 -070027struct gl_client_context_t;
keunyoungb85b2752013-03-08 12:28:03 -080028class GL2Encoder;
Yahan Zhoue8cf63d2016-09-22 12:33:50 -070029struct gl2_client_context_t;
keunyoungb85b2752013-03-08 12:28:03 -080030
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -070031// 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.
41enum SyncImpl {
42 SYNC_IMPL_NONE = 0,
43 SYNC_IMPL_NATIVE_SYNC = 1
44};
Lingfeng Yang88c170c2016-11-30 00:52:35 +000045
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -070046// Interface:
47// If this GL extension string shows up, we use
48// SYNC_IMPL_NATIVE_SYNC, otherwise we use SYNC_IMPL_NONE.
Lingfeng Yang2ce4c312016-08-31 14:46:11 -070049// 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.
52static const char kRCNativeSync[] = "ANDROID_EMU_native_sync_v2";
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -070053
Lingfeng Yang88c170c2016-11-30 00:52:35 +000054// DMA for OpenGL
55enum DmaImpl {
56 DMA_IMPL_NONE = 0,
57 DMA_IMPL_v1 = 1,
58};
59
60static const char kDmaExtStr_v1[] = "ANDROID_EMU_dma_v1";
61
Lingfeng Yange6556dc2017-01-09 12:04:12 -080062// OpenGL ES max supported version
63enum 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
70static const char kGLESMaxVersion_2[] = "ANDROID_EMU_gles_max_version_2";
71static const char kGLESMaxVersion_3_0[] = "ANDROID_EMU_gles_max_version_3_0";
72static const char kGLESMaxVersion_3_1[] = "ANDROID_EMU_gles_max_version_3_1";
73static const char kGLESMaxVersion_3_2[] = "ANDROID_EMU_gles_max_version_3_2";
74
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -070075// ExtendedRCEncoderContext is an extended version of renderControl_encoder_context_t
76// that will be used to track SyncImpl.
77class ExtendedRCEncoderContext : public renderControl_encoder_context_t {
78public:
79 ExtendedRCEncoderContext(IOStream *stream, ChecksumCalculator *checksumCalculator)
Lingfeng Yang88c170c2016-11-30 00:52:35 +000080 : renderControl_encoder_context_t(stream, checksumCalculator) {
81 m_dmaCxt = NULL;
82 }
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -070083 void setSyncImpl(SyncImpl syncImpl) { m_syncImpl = syncImpl; }
Lingfeng Yang88c170c2016-11-30 00:52:35 +000084 void setDmaImpl(DmaImpl dmaImpl) { m_dmaImpl = dmaImpl; }
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -070085 bool hasNativeSync() const { return m_syncImpl == SYNC_IMPL_NATIVE_SYNC; }
Lingfeng Yang88c170c2016-11-30 00:52:35 +000086 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 Yange6556dc2017-01-09 12:04:12 -0800101 void setGLESMaxVersion(GLESMaxVersion ver) { m_glesMaxVersion = ver; }
102 GLESMaxVersion getGLESMaxVersion() const { return m_glesMaxVersion; }
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -0700103private:
104 SyncImpl m_syncImpl;
Lingfeng Yang88c170c2016-11-30 00:52:35 +0000105 DmaImpl m_dmaImpl;
106 struct goldfish_dma_context* m_dmaCxt;
Lingfeng Yange6556dc2017-01-09 12:04:12 -0800107 GLESMaxVersion m_glesMaxVersion;
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -0700108};
109
keunyoungb85b2752013-03-08 12:28:03 -0800110class HostConnection
111{
112public:
113 static HostConnection *get();
Lingfeng Yang200c7162016-06-13 08:39:28 -0700114 static void exit();
keunyoungb85b2752013-03-08 12:28:03 -0800115 ~HostConnection();
116
117 GLEncoder *glEncoder();
118 GL2Encoder *gl2Encoder();
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -0700119 ExtendedRCEncoderContext *rcEncoder();
Yahan Zhoub7f09082016-03-10 11:45:02 -0800120 ChecksumCalculator *checksumHelper() { return &m_checksumHelper; }
keunyoungb85b2752013-03-08 12:28:03 -0800121
122 void flush() {
123 if (m_stream) {
124 m_stream->flush();
125 }
126 }
127
Lingfeng Yang200c7162016-06-13 08:39:28 -0700128 void setGrallocOnly(bool gralloc_only) {
129 m_grallocOnly = gralloc_only;
130 }
131
132 bool isGrallocOnly() const { return m_grallocOnly; }
133
Lingfeng Yang88c170c2016-11-30 00:52:35 +0000134 int getPipeFd() const { return m_pipeFd; }
135
keunyoungb85b2752013-03-08 12:28:03 -0800136private:
137 HostConnection();
138 static gl_client_context_t *s_getGLContext();
139 static gl2_client_context_t *s_getGL2Context();
Lingfeng Yang18835352016-05-23 12:16:04 -0700140
Yurii Zubrytskyib2327642016-11-04 12:01:36 -0700141 const std::string& queryGLExtensions(ExtendedRCEncoderContext *rcEnc);
Yahan Zhoub7f09082016-03-10 11:45:02 -0800142 // setProtocol initilizes GL communication protocol for checksums
143 // should be called when m_rcEnc is created
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -0700144 void setChecksumHelper(ExtendedRCEncoderContext *rcEnc);
145 void queryAndSetSyncImpl(ExtendedRCEncoderContext *rcEnc);
Lingfeng Yang88c170c2016-11-30 00:52:35 +0000146 void queryAndSetDmaImpl(ExtendedRCEncoderContext *rcEnc);
Lingfeng Yange6556dc2017-01-09 12:04:12 -0800147 void queryAndSetGLESMaxVersion(ExtendedRCEncoderContext *rcEnc);
keunyoungb85b2752013-03-08 12:28:03 -0800148
149private:
150 IOStream *m_stream;
151 GLEncoder *m_glEnc;
152 GL2Encoder *m_gl2Enc;
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -0700153 ExtendedRCEncoderContext *m_rcEnc;
Yahan Zhoub7f09082016-03-10 11:45:02 -0800154 ChecksumCalculator m_checksumHelper;
Lingfeng Yang4f12b8d2016-07-13 16:26:10 -0700155 std::string m_glExtensions;
Lingfeng Yang200c7162016-06-13 08:39:28 -0700156 bool m_grallocOnly;
Lingfeng Yang88c170c2016-11-30 00:52:35 +0000157 int m_pipeFd;
keunyoungb85b2752013-03-08 12:28:03 -0800158};
159
160#endif