gltrace_eglapi.cpp revision 0832fb6407d1c85cba20a8cc0aff828db3c134de
1/*
2 * Copyright 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
17#include <arpa/inet.h>
18#include <stdlib.h>
19#include <cutils/log.h>
20#include <cutils/properties.h>
21
22#include "hooks.h"
23#include "glestrace.h"
24
25#include "gltrace_context.h"
26#include "gltrace_egl.h"
27#include "gltrace_hooks.h"
28#include "gltrace_transport.h"
29
30namespace android {
31
32using gltrace::GLTraceState;
33using gltrace::GLTraceContext;
34using gltrace::TCPStream;
35
36static GLTraceState *sGLTraceState;
37static pthread_t sReceiveThreadId;
38
39/**
40 * Task that monitors the control stream from the host and updates
41 * the trace status according to commands received from the host.
42 */
43static void *commandReceiveTask(void *arg) {
44    GLTraceState *state = (GLTraceState *)arg;
45    TCPStream *stream = state->getStream();
46
47    // Currently, there are very few user configurable settings.
48    // As a result, they can be encoded in a single integer.
49    int cmd;
50    enum TraceSettingsMasks {
51        READ_FB_ON_EGLSWAP_MASK = 1 << 0,
52        READ_FB_ON_GLDRAW_MASK = 1 << 1,
53        READ_TEXTURE_DATA_ON_GLTEXIMAGE_MASK = 1 << 2,
54    };
55
56    while (true) {
57        int n = stream->receive(&cmd, 4);
58        if (n != 4) {
59            break;
60        }
61
62        cmd = ntohl(cmd);
63
64        bool collectFbOnEglSwap = (cmd & READ_FB_ON_EGLSWAP_MASK) != 0;
65        bool collectFbOnGlDraw = (cmd & READ_FB_ON_GLDRAW_MASK) != 0;
66        bool collectTextureData = (cmd & READ_TEXTURE_DATA_ON_GLTEXIMAGE_MASK) != 0;
67
68        state->setCollectFbOnEglSwap(collectFbOnEglSwap);
69        state->setCollectFbOnGlDraw(collectFbOnGlDraw);
70        state->setCollectTextureDataOnGlTexImage(collectTextureData);
71
72        ALOGD("trace options: eglswap: %d, gldraw: %d, texImage: %d",
73            collectFbOnEglSwap, collectFbOnGlDraw, collectTextureData);
74    }
75
76    return NULL;
77}
78
79void GLTrace_start() {
80    char udsName[PROPERTY_VALUE_MAX];
81
82    property_get("debug.egl.debug_portname", udsName, "gltrace");
83    int clientSocket = gltrace::acceptClientConnection(udsName);
84    if (clientSocket < 0) {
85        ALOGE("Error creating GLTrace server socket. Quitting application.");
86        exit(-1);
87    }
88
89    // create communication channel to the host
90    TCPStream *stream = new TCPStream(clientSocket);
91
92    // initialize tracing state
93    sGLTraceState = new GLTraceState(stream);
94
95    pthread_create(&sReceiveThreadId, NULL, commandReceiveTask, sGLTraceState);
96}
97
98void GLTrace_stop() {
99    delete sGLTraceState;
100    sGLTraceState = NULL;
101}
102
103void GLTrace_eglCreateContext(int version, EGLContext c) {
104    // update trace state for new EGL context
105    GLTraceContext *traceContext = sGLTraceState->createTraceContext(version, c);
106    gltrace::setupTraceContextThreadSpecific(traceContext);
107
108    // trace command through to the host
109    gltrace::GLTrace_eglCreateContext(version, traceContext->getId());
110}
111
112void GLTrace_eglMakeCurrent(const unsigned version, gl_hooks_t *hooks, EGLContext c) {
113    // setup per context state
114    GLTraceContext *traceContext = sGLTraceState->getTraceContext(c);
115    traceContext->hooks = hooks;
116    gltrace::setupTraceContextThreadSpecific(traceContext);
117
118    // trace command through to the host
119    gltrace::GLTrace_eglMakeCurrent(traceContext->getId());
120}
121
122void GLTrace_eglReleaseThread() {
123    gltrace::releaseContext();
124}
125
126void GLTrace_eglSwapBuffers(void *dpy, void *draw) {
127    gltrace::GLTrace_eglSwapBuffers(dpy, draw);
128}
129
130gl_hooks_t *GLTrace_getGLHooks() {
131    return gltrace::getGLHooks();
132}
133
134}
135