Siva Velusamy | db97468 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 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 | |
Siva Velusamy | 157fea64 | 2012-01-03 14:39:31 -0800 | [diff] [blame] | 17 | #include <arpa/inet.h> |
Siva Velusamy | db97468 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 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 | |
| 30 | namespace android { |
| 31 | |
Siva Velusamy | 1e81e71 | 2011-12-14 12:19:56 -0800 | [diff] [blame] | 32 | using gltrace::GLTraceState; |
| 33 | using gltrace::GLTraceContext; |
| 34 | using gltrace::TCPStream; |
Siva Velusamy | db97468 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 35 | |
Siva Velusamy | 1e81e71 | 2011-12-14 12:19:56 -0800 | [diff] [blame] | 36 | static GLTraceState *sGLTraceState; |
Siva Velusamy | 157fea64 | 2012-01-03 14:39:31 -0800 | [diff] [blame] | 37 | static 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 | */ |
| 43 | static 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 Velusamy | db97468 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 78 | |
| 79 | void 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 Velusamy | 1e81e71 | 2011-12-14 12:19:56 -0800 | [diff] [blame] | 85 | 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 Velusamy | 157fea64 | 2012-01-03 14:39:31 -0800 | [diff] [blame] | 96 | |
| 97 | pthread_create(&sReceiveThreadId, NULL, commandReceiveTask, sGLTraceState); |
Siva Velusamy | db97468 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void GLTrace_stop() { |
Siva Velusamy | 1e81e71 | 2011-12-14 12:19:56 -0800 | [diff] [blame] | 101 | delete sGLTraceState; |
| 102 | sGLTraceState = NULL; |
Siva Velusamy | db97468 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Siva Velusamy | 1e81e71 | 2011-12-14 12:19:56 -0800 | [diff] [blame] | 105 | void 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 | |
| 114 | void 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 | |
| 124 | void GLTrace_eglReleaseThread() { |
| 125 | gltrace::releaseContext(); |
Siva Velusamy | db97468 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void GLTrace_eglSwapBuffers(void *dpy, void *draw) { |
| 129 | gltrace::GLTrace_eglSwapBuffers(dpy, draw); |
| 130 | } |
| 131 | |
Siva Velusamy | 1e81e71 | 2011-12-14 12:19:56 -0800 | [diff] [blame] | 132 | gl_hooks_t *GLTrace_getGLHooks() { |
| 133 | return gltrace::getGLHooks(); |
| 134 | } |
| 135 | |
Siva Velusamy | db97468 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 136 | } |