aboutsummaryrefslogtreecommitdiffstats
path: root/emulator/opengl/host/libs/libOpenglRender/render_api.cpp
blob: 95860918e52df8a612d1e7fb7e405a84e33a5219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "libOpenglRender/render_api.h"
#include "IOStream.h"
#include "FrameBuffer.h"
#include "RenderServer.h"
#include "TimeUtils.h"

#include "TcpStream.h"
#ifdef _WIN32
#include "Win32PipeStream.h"
#else
#include "UnixStream.h"
#endif

#include "EGLDispatch.h"
#include "GLDispatch.h"
#include "GL2Dispatch.h"

static RenderServer *s_renderThread = NULL;
static char s_renderAddr[256];

static IOStream *createRenderThread(int p_stream_buffer_size,
                                    unsigned int clientFlags);

int initLibrary(void)
{
    //
    // Load EGL Plugin
    //
    if (!init_egl_dispatch()) {
        // Failed to load EGL
        printf("Failed to init_egl_dispatch\n");
        return false;
    }

    //
    // Load GLES Plugin
    //
    if (!init_gl_dispatch()) {
        // Failed to load GLES
        ERR("Failed to init_gl_dispatch\n");
        return false;
    }

    /* failure to init the GLES2 dispatch table is not fatal */
    init_gl2_dispatch();

    return true;
}

int initOpenGLRenderer(int width, int height, char* addr, size_t addrLen)
{

    //
    // Fail if renderer is already initialized
    //
    if (s_renderThread) {
        return false;
    }

    //
    // initialize the renderer and listen to connections
    // on a thread in the current process.
    //
    bool inited = FrameBuffer::initialize(width, height);
    if (!inited) {
        return false;
    }

    s_renderThread = RenderServer::create(addr, addrLen);
    if (!s_renderThread) {
        return false;
    }
    strncpy(s_renderAddr, addr, sizeof(s_renderAddr));

    s_renderThread->start();

    return true;
}

void setPostCallback(OnPostFn onPost, void* onPostContext)
{
    FrameBuffer* fb = FrameBuffer::getFB();
    if (fb) {
        fb->setPostCallback(onPost, onPostContext);
    }
}

void getHardwareStrings(const char** vendor, const char** renderer, const char** version)
{
    FrameBuffer* fb = FrameBuffer::getFB();
    if (fb) {
        fb->getGLStrings(vendor, renderer, version);
    } else {
        *vendor = *renderer = *version = NULL;
    }
}

int stopOpenGLRenderer(void)
{
    bool ret = false;

    // open a dummy connection to the renderer to make it
    // realize the exit request.
    // (send the exit request in clientFlags)
    IOStream *dummy = createRenderThread(8, IOSTREAM_CLIENT_EXIT_SERVER);
    if (!dummy) return false;

    if (s_renderThread) {

        // wait for the thread to exit
        ret = s_renderThread->wait(NULL);

        delete s_renderThread;
        s_renderThread = NULL;
    }

    return ret;
}

int createOpenGLSubwindow(FBNativeWindowType window,
                           int x, int y, int width, int height, float zRot)
{
    if (s_renderThread) {
        return FrameBuffer::setupSubWindow(window,x,y,width,height, zRot);
    }
    else {
        //
        // XXX: should be implemented by sending the renderer process
        //      a request
        ERR("%s not implemented for separate renderer process !!!\n",
            __FUNCTION__);
    }
    return false;
}

int destroyOpenGLSubwindow(void)
{
    if (s_renderThread) {
        return FrameBuffer::removeSubWindow();
    }
    else {
        //
        // XXX: should be implemented by sending the renderer process
        //      a request
        ERR("%s not implemented for separate renderer process !!!\n",
                __FUNCTION__);
        return false;
    }
}

void setOpenGLDisplayRotation(float zRot)
{
    if (s_renderThread) {
        FrameBuffer *fb = FrameBuffer::getFB();
        if (fb) {
            fb->setDisplayRotation(zRot);
        }
    }
    else {
        //
        // XXX: should be implemented by sending the renderer process
        //      a request
        ERR("%s not implemented for separate renderer process !!!\n",
                __FUNCTION__);
    }
}

void repaintOpenGLDisplay(void)
{
    if (s_renderThread) {
        FrameBuffer *fb = FrameBuffer::getFB();
        if (fb) {
            fb->repost();
        }
    }
    else {
        //
        // XXX: should be implemented by sending the renderer process
        //      a request
        ERR("%s not implemented for separate renderer process !!!\n",
                __FUNCTION__);
    }
}


/* NOTE: For now, always use TCP mode by default, until the emulator
 *        has been updated to support Unix and Win32 pipes
 */
#define  DEFAULT_STREAM_MODE  STREAM_MODE_TCP

int gRendererStreamMode = DEFAULT_STREAM_MODE;

IOStream *createRenderThread(int p_stream_buffer_size, unsigned int clientFlags)
{
    SocketStream*  stream = NULL;

    if (gRendererStreamMode == STREAM_MODE_TCP) {
        stream = new TcpStream(p_stream_buffer_size);
    } else {
#ifdef _WIN32
        stream = new Win32PipeStream(p_stream_buffer_size);
#else /* !_WIN32 */
        stream = new UnixStream(p_stream_buffer_size);
#endif
    }

    if (!stream) {
        ERR("createRenderThread failed to create stream\n");
        return NULL;
    }
    if (stream->connect(s_renderAddr) < 0) {
        ERR("createRenderThread failed to connect\n");
        delete stream;
        return NULL;
    }

    //
    // send clientFlags to the renderer
    //
    unsigned int *pClientFlags =
                (unsigned int *)stream->allocBuffer(sizeof(unsigned int));
    *pClientFlags = clientFlags;
    stream->commitBuffer(sizeof(unsigned int));

    return stream;
}

int
setStreamMode(int mode)
{
    switch (mode) {
        case STREAM_MODE_DEFAULT:
            mode = DEFAULT_STREAM_MODE;
            break;

        case STREAM_MODE_TCP:
            break;

#ifndef _WIN32
        case STREAM_MODE_UNIX:
            break;
#else /* _WIN32 */
        case STREAM_MODE_PIPE:
            break;
#endif /* _WIN32 */
        default:
            // Invalid stream mode
            return false;
    }
    gRendererStreamMode = mode;
    return true;
}