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 <stdio.h>
17 #include <stdlib.h>
18 #include "ServerConnection.h"
19 #include "TcpStream.h"
20 #include "QemuPipeStream.h"
21 #include <log/log.h>
22 #include "ThreadInfo.h"
23 
s_getGlContext()24 gl_client_context_t *ServerConnection::s_getGlContext()
25 {
26     EGLThreadInfo *ti = getEGLThreadInfo();
27     if (ti->serverConn) {
28         return ti->serverConn->m_glEnc;
29     }
30     return NULL;
31 }
32 
s_getGl2Context()33 gl2_client_context_t *ServerConnection::s_getGl2Context()
34 {
35     EGLThreadInfo *ti = getEGLThreadInfo();
36     if (ti->serverConn) {
37         return ti->serverConn->m_gl2Enc;
38     }
39     return NULL;
40 }
41 
s_getServerConnection()42 ServerConnection *ServerConnection::s_getServerConnection()
43 {
44     EGLThreadInfo *ti = getEGLThreadInfo();
45     if (!ti->serverConn)
46     {
47         ti->serverConn = new ServerConnection();
48         if (ti->serverConn->create() < 0) {
49             delete ti->serverConn;
50             ti->serverConn = NULL;
51         }
52     }
53 
54     return ti->serverConn;
55 }
56 
57 
ServerConnection()58 ServerConnection::ServerConnection() :
59     m_stream(NULL),
60     m_glEnc(NULL),
61     m_ut_enc(NULL)
62 {
63 }
64 
~ServerConnection()65 ServerConnection::~ServerConnection()
66 {
67     delete m_ut_enc;
68     delete m_glEnc;
69     delete m_stream;
70 }
71 
72 
73 
create(size_t bufsize,const char * defaultServer)74 int ServerConnection::create(size_t bufsize,
75                              const char *defaultServer)
76 {
77     /* XXX: Make configurable through system property */
78     int useQemuPipe = 1;
79 
80     if (m_stream != NULL) delete(m_stream);
81 
82     if (useQemuPipe) {
83         QemuPipeStream*  pipeStream = new QemuPipeStream(bufsize);
84 
85         if (pipeStream->connect() < 0) {
86             ALOGE("couldn't connect to host server\n");
87             delete pipeStream;
88             return -1;
89         }
90         m_stream = pipeStream;
91     }
92     else /* !useQemuPipe */
93     {
94         TcpStream*  tcpStream = new TcpStream(bufsize);
95 
96         char *s = getenv(ENV_RGL_SERVER);
97         char *hostname;
98         if (s == NULL) {
99             hostname = strdup(defaultServer);
100         } else {
101             hostname = strdup(s);
102         }
103 
104         if (tcpStream->connect(hostname, CODEC_SERVER_PORT) < 0) {
105             ALOGE("couldn't connect to %s\n", hostname);
106             free(hostname);
107             delete tcpStream;
108             return -1;
109         }
110         LOGI("connecting to server %s\n", hostname);
111         free(hostname);
112 
113         m_stream = tcpStream;
114     }
115 
116     m_glEnc = new GLEncoder(m_stream);
117     m_glEnc->setContextAccessor(s_getGlContext);
118 
119     m_gl2Enc = new GL2Encoder(m_stream);
120     m_gl2Enc->setContextAccessor(s_getGl2Context);
121 
122     m_ut_enc = new ut_rendercontrol_encoder_context_t(m_stream);
123     return 0;
124 }
125 
126