2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "HostConnection.h"
17 #include "TcpStream.h"
18 #include "QemuPipeStream.h"
19 #include "ThreadInfo.h"
20 #include <cutils/log.h>
21 #include "GLEncoder.h"
22 #include "GL2Encoder.h"
25 #define STREAM_BUFFER_SIZE 4*1024*1024
26 #define STREAM_PORT_NUM 22468
28 /* Set to 1 to use a QEMU pipe, or 0 for a TCP connection */
29 #define USE_QEMU_PIPE 1
31 HostConnection::HostConnection() :
40 HostConnection::~HostConnection()
48 HostConnection *HostConnection::get()
50 /* TODO: Make this configurable with a system property */
51 const int useQemuPipe = USE_QEMU_PIPE;
54 EGLThreadInfo *tinfo = getEGLThreadInfo();
59 if (tinfo->hostConn == NULL) {
60 HostConnection *con = new HostConnection();
66 QemuPipeStream *stream = new QemuPipeStream(STREAM_BUFFER_SIZE);
68 ALOGE("Failed to create QemuPipeStream for host connection!!!\n");
72 if (stream->connect() < 0) {
73 ALOGE("Failed to connect to host (QemuPipeStream)!!!\n");
78 con->m_stream = stream;
80 else /* !useQemuPipe */
82 TcpStream *stream = new TcpStream(STREAM_BUFFER_SIZE);
84 ALOGE("Failed to create TcpStream for host connection!!!\n");
89 if (stream->connect("10.0.2.2", STREAM_PORT_NUM) < 0) {
90 ALOGE("Failed to connect to host (TcpStream)!!!\n");
95 con->m_stream = stream;
98 // send zero 'clientFlags' to the host.
99 unsigned int *pClientFlags =
100 (unsigned int *)con->m_stream->allocBuffer(sizeof(unsigned int));
102 con->m_stream->commitBuffer(sizeof(unsigned int));
104 ALOGD("HostConnection::get() New Host Connection established %p, tid %d\n", con, gettid());
105 tinfo->hostConn = con;
108 return tinfo->hostConn;
111 GLEncoder *HostConnection::glEncoder()
114 m_glEnc = new GLEncoder(m_stream, checksumHelper());
115 DBG("HostConnection::glEncoder new encoder %p, tid %d", m_glEnc, gettid());
116 m_glEnc->setContextAccessor(s_getGLContext);
121 GL2Encoder *HostConnection::gl2Encoder()
124 m_gl2Enc = new GL2Encoder(m_stream, checksumHelper());
125 DBG("HostConnection::gl2Encoder new encoder %p, tid %d", m_gl2Enc, gettid());
126 m_gl2Enc->setContextAccessor(s_getGL2Context);
131 renderControl_encoder_context_t *HostConnection::rcEncoder()
134 m_rcEnc = new renderControl_encoder_context_t(m_stream, checksumHelper());
135 // TODO: disable checksum as a workaround in a glTexSubImage2D problem
136 // Uncomment the following line when the root cause is solved
137 //setChecksumHelper(m_rcEnc);
142 gl_client_context_t *HostConnection::s_getGLContext()
144 EGLThreadInfo *ti = getEGLThreadInfo();
146 return ti->hostConn->m_glEnc;
151 gl2_client_context_t *HostConnection::s_getGL2Context()
153 EGLThreadInfo *ti = getEGLThreadInfo();
155 return ti->hostConn->m_gl2Enc;
160 void HostConnection::setChecksumHelper(renderControl_encoder_context_t *rcEnc) {
161 std::unique_ptr<char[]> glExtensions;
162 int extensionSize = rcEnc->rcGetGLString(rcEnc, GL_EXTENSIONS, NULL, 0);
163 if (extensionSize < 0) {
164 glExtensions = std::unique_ptr<char[]>(new char[-extensionSize]);
165 extensionSize = rcEnc->rcGetGLString(rcEnc, GL_EXTENSIONS, glExtensions.get(), -extensionSize);
166 if (extensionSize <= 0) {
167 glExtensions.reset();
170 // check the host supported version
171 uint32_t checksumVersion = 0;
172 const char* checksumPrefix = ChecksumCalculator::getMaxVersionStrPrefix();
173 const char* glProtocolStr = glExtensions.get() ?
174 strstr(glExtensions.get(), checksumPrefix) : NULL;
176 uint32_t maxVersion = ChecksumCalculator::getMaxVersion();
177 sscanf(glProtocolStr+strlen(checksumPrefix), "%d", &checksumVersion);
178 if (maxVersion < checksumVersion) {
179 checksumVersion = maxVersion;
181 // The ordering of the following two commands matters!
182 // Must tell the host first before setting it in the guest
183 rcEnc->rcSelectChecksumCalculator(rcEnc, checksumVersion, 0);
184 m_checksumHelper.setVersion(checksumVersion);