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 #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" 23 24 #define STREAM_BUFFER_SIZE 4*1024*1024 25 #define STREAM_PORT_NUM 22468 26 27 /* Set to 1 to use a QEMU pipe, or 0 for a TCP connection */ 28 #define USE_QEMU_PIPE 1 29 30 HostConnection::HostConnection() : 31 m_stream(NULL), 32 m_glEnc(NULL), 33 m_gl2Enc(NULL), 34 m_rcEnc(NULL) 35 { 36 } 37 38 HostConnection::~HostConnection() 39 { 40 delete m_stream; 41 delete m_glEnc; 42 delete m_gl2Enc; 43 delete m_rcEnc; 44 } 45 46 HostConnection *HostConnection::get() 47 { 48 /* TODO: Make this configurable with a system property */ 49 const int useQemuPipe = USE_QEMU_PIPE; 50 51 // Get thread info 52 EGLThreadInfo *tinfo = getEGLThreadInfo(); 53 if (!tinfo) { 54 return NULL; 55 } 56 57 if (tinfo->hostConn == NULL) { 58 HostConnection *con = new HostConnection(); 59 if (NULL == con) { 60 return NULL; 61 } 62 63 if (useQemuPipe) { 64 QemuPipeStream *stream = new QemuPipeStream(STREAM_BUFFER_SIZE); 65 if (!stream) { 66 LOGE("Failed to create QemuPipeStream for host connection!!!\n"); 67 delete con; 68 return NULL; 69 } 70 if (stream->connect() < 0) { 71 LOGE("Failed to connect to host (QemuPipeStream)!!!\n"); 72 delete con; 73 return NULL; 74 } 75 con->m_stream = stream; 76 } 77 else /* !useQemuPipe */ 78 { 79 TcpStream *stream = new TcpStream(STREAM_BUFFER_SIZE); 80 if (!stream) { 81 LOGE("Failed to create TcpStream for host connection!!!\n"); 82 delete con; 83 return NULL; 84 } 85 86 if (stream->connect("10.0.2.2", STREAM_PORT_NUM) < 0) { 87 LOGE("Failed to connect to host (TcpStream)!!!\n"); 88 delete con; 89 return NULL; 90 } 91 con->m_stream = stream; 92 } 93 94 // send zero 'clientFlags' to the host. 95 unsigned int *pClientFlags = 96 (unsigned int *)con->m_stream->allocBuffer(sizeof(unsigned int)); 97 *pClientFlags = 0; 98 con->m_stream->commitBuffer(sizeof(unsigned int)); 99 100 LOGD("HostConnection::get() New Host Connection established %p, tid %d\n", con, gettid()); 101 tinfo->hostConn = con; 102 } 103 104 return tinfo->hostConn; 105 } 106 107 GLEncoder *HostConnection::glEncoder() 108 { 109 if (!m_glEnc) { 110 m_glEnc = new GLEncoder(m_stream); 111 DBG("HostConnection::glEncoder new encoder %p, tid %d", m_glEnc, gettid()); 112 m_glEnc->setContextAccessor(s_getGLContext); 113 } 114 return m_glEnc; 115 } 116 117 GL2Encoder *HostConnection::gl2Encoder() 118 { 119 if (!m_gl2Enc) { 120 m_gl2Enc = new GL2Encoder(m_stream); 121 DBG("HostConnection::gl2Encoder new encoder %p, tid %d", m_gl2Enc, gettid()); 122 m_gl2Enc->setContextAccessor(s_getGL2Context); 123 } 124 return m_gl2Enc; 125 } 126 127 renderControl_encoder_context_t *HostConnection::rcEncoder() 128 { 129 if (!m_rcEnc) { 130 m_rcEnc = new renderControl_encoder_context_t(m_stream); 131 } 132 return m_rcEnc; 133 } 134 135 gl_client_context_t *HostConnection::s_getGLContext() 136 { 137 EGLThreadInfo *ti = getEGLThreadInfo(); 138 if (ti->hostConn) { 139 return ti->hostConn->m_glEnc; 140 } 141 return NULL; 142 } 143 144 gl2_client_context_t *HostConnection::s_getGL2Context() 145 { 146 EGLThreadInfo *ti = getEGLThreadInfo(); 147 if (ti->hostConn) { 148 return ti->hostConn->m_gl2Enc; 149 } 150 return NULL; 151 } 152