blob: 0fe97ceb25f8f726bdc82822289e81fc4523e086 [file] [log] [blame]
Siva Velusamydb974682011-11-30 15:05:37 -08001/*
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
Siva Velusamy157fea642012-01-03 14:39:31 -080017#include <arpa/inet.h>
Siva Velusamydb974682011-11-30 15:05:37 -080018#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
Siva Velusamy1e81e712011-12-14 12:19:56 -080032using gltrace::GLTraceState;
33using gltrace::GLTraceContext;
34using gltrace::TCPStream;
Siva Velusamydb974682011-11-30 15:05:37 -080035
Siva Velusamy1e81e712011-12-14 12:19:56 -080036static GLTraceState *sGLTraceState;
Siva Velusamy157fea642012-01-03 14:39:31 -080037static 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}
Siva Velusamydb974682011-11-30 15:05:37 -080078
79void GLTrace_start() {
80 char value[PROPERTY_VALUE_MAX];
81
82 property_get("debug.egl.debug_port", value, "5039");
83 const unsigned short port = (unsigned short)atoi(value);
84
Siva Velusamy1e81e712011-12-14 12:19:56 -080085 int clientSocket = gltrace::acceptClientConnection(port);
86 if (clientSocket < 0) {
87 LOGE("Error creating GLTrace server socket. Quitting application.");
88 exit(-1);
89 }
90
91 // create communication channel to the host
92 TCPStream *stream = new TCPStream(clientSocket);
93
94 // initialize tracing state
95 sGLTraceState = new GLTraceState(stream);
Siva Velusamy157fea642012-01-03 14:39:31 -080096
97 pthread_create(&sReceiveThreadId, NULL, commandReceiveTask, sGLTraceState);
Siva Velusamydb974682011-11-30 15:05:37 -080098}
99
100void GLTrace_stop() {
Siva Velusamy1e81e712011-12-14 12:19:56 -0800101 delete sGLTraceState;
102 sGLTraceState = NULL;
Siva Velusamydb974682011-11-30 15:05:37 -0800103}
104
Siva Velusamy1e81e712011-12-14 12:19:56 -0800105void GLTrace_eglCreateContext(int version, EGLContext c) {
106 // update trace state for new EGL context
107 GLTraceContext *traceContext = sGLTraceState->createTraceContext(version, c);
108 gltrace::setupTraceContextThreadSpecific(traceContext);
109
110 // trace command through to the host
111 gltrace::GLTrace_eglCreateContext(version, traceContext->getId());
112}
113
114void GLTrace_eglMakeCurrent(const unsigned version, gl_hooks_t *hooks, EGLContext c) {
115 // setup per context state
116 GLTraceContext *traceContext = sGLTraceState->getTraceContext(c);
117 traceContext->hooks = hooks;
118 gltrace::setupTraceContextThreadSpecific(traceContext);
119
120 // trace command through to the host
121 gltrace::GLTrace_eglMakeCurrent(traceContext->getId());
122}
123
124void GLTrace_eglReleaseThread() {
125 gltrace::releaseContext();
Siva Velusamydb974682011-11-30 15:05:37 -0800126}
127
128void GLTrace_eglSwapBuffers(void *dpy, void *draw) {
129 gltrace::GLTrace_eglSwapBuffers(dpy, draw);
130}
131
Siva Velusamy1e81e712011-12-14 12:19:56 -0800132gl_hooks_t *GLTrace_getGLHooks() {
133 return gltrace::getGLHooks();
134}
135
Siva Velusamydb974682011-11-30 15:05:37 -0800136}