gltrace_context.h revision 3ef9e9671c224af82d2efec1a1e9a275fb9acf44
10469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy/*
20469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * Copyright 2011, The Android Open Source Project
30469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy *
40469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * Licensed under the Apache License, Version 2.0 (the "License");
50469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * you may not use this file except in compliance with the License.
60469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * You may obtain a copy of the License at
70469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy *
80469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy *     http://www.apache.org/licenses/LICENSE-2.0
90469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy *
100469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * Unless required by applicable law or agreed to in writing, software
110469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * distributed under the License is distributed on an "AS IS" BASIS,
120469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * See the License for the specific language governing permissions and
140469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * limitations under the License.
150469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy */
160469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
170469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#ifndef __GLTRACE_CONTEXT_H_
180469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#define __GLTRACE_CONTEXT_H_
190469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
2093a826f78f6313db791e6fc880439189897651b3Siva Velusamy#include <map>
213f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy#include <pthread.h>
2250129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy#include <utils/KeyedVector.h>
2393a826f78f6313db791e6fc880439189897651b3Siva Velusamy
240469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#include "hooks.h"
2593a826f78f6313db791e6fc880439189897651b3Siva Velusamy#include "gltrace_transport.h"
260469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
270469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamynamespace android {
280469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamynamespace gltrace {
290469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
300469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamyusing ::android::gl_hooks_t;
310469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
32f132ac35d82a2960542619fb3fb91d22ab256dc7Siva Velusamyenum FBBinding {CURRENTLY_BOUND_FB, FB0};
33f132ac35d82a2960542619fb3fb91d22ab256dc7Siva Velusamy
343f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamyclass GLTraceState;
353f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy
3650129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamyclass ElementArrayBuffer {
3750129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    GLvoid *mBuf;
3850129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    GLsizeiptr mSize;
3950129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy
4050129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamypublic:
4150129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    ElementArrayBuffer():mBuf(NULL), mSize(0) {}
4250129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    ElementArrayBuffer(GLvoid *buf, GLsizeiptr size);
4350129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    ~ElementArrayBuffer();
4450129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy
4550129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    void updateSubBuffer(GLintptr offset, const GLvoid* data, GLsizeiptr size);
4650129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    GLvoid *getBuffer();
4750129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    GLsizeiptr getSize();
4850129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy};
4950129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy
5093a826f78f6313db791e6fc880439189897651b3Siva Velusamy/** GL Trace Context info associated with each EGLContext */
510469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamyclass GLTraceContext {
5293a826f78f6313db791e6fc880439189897651b3Siva Velusamy    int mId;                    /* unique context id */
533ef9e9671c224af82d2efec1a1e9a275fb9acf44Siva Velusamy    int mVersion;               /* GL version, e.g: egl_connection_t::GLESv2_INDEX */
543f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    GLTraceState *mState;       /* parent GL Trace state (for per process GL Trace State Info) */
5593a826f78f6313db791e6fc880439189897651b3Siva Velusamy
560469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy    void *fbcontents;           /* memory area to read framebuffer contents */
570469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy    void *fbcompressed;         /* destination for lzf compressed framebuffer */
580469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy    unsigned fbcontentsSize;    /* size of fbcontents & fbcompressed buffers */
590469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
6093a826f78f6313db791e6fc880439189897651b3Siva Velusamy    BufferedOutputStream *mBufferedOutputStream; /* stream where trace info is sent */
6193a826f78f6313db791e6fc880439189897651b3Siva Velusamy
6250129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    /* list of element array buffers in use. */
6350129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    DefaultKeyedVector<GLuint, ElementArrayBuffer*> mElementArrayBuffers;
6450129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy
650469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy    void resizeFBMemory(unsigned minSize);
660469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamypublic:
670469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy    gl_hooks_t *hooks;
680469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
693ef9e9671c224af82d2efec1a1e9a275fb9acf44Siva Velusamy    GLTraceContext(int id, int version, GLTraceState *state, BufferedOutputStream *stream);
7093a826f78f6313db791e6fc880439189897651b3Siva Velusamy    int getId();
713ef9e9671c224af82d2efec1a1e9a275fb9acf44Siva Velusamy    int getVersion();
723f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    GLTraceState *getGlobalTraceState();
73f132ac35d82a2960542619fb3fb91d22ab256dc7Siva Velusamy    void getCompressedFB(void **fb, unsigned *fbsize,
74f132ac35d82a2960542619fb3fb91d22ab256dc7Siva Velusamy                            unsigned *fbwidth, unsigned *fbheight,
75f132ac35d82a2960542619fb3fb91d22ab256dc7Siva Velusamy                            FBBinding fbToRead);
7650129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy
7750129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    // Methods to work with element array buffers
7850129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    void bindBuffer(GLuint bufferId, GLvoid *data, GLsizeiptr size);
7950129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    void getBuffer(GLuint bufferId, GLvoid **data, GLsizeiptr *size);
8050129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    void updateBufferSubData(GLuint bufferId, GLintptr offset, GLvoid *data, GLsizeiptr size);
8150129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy    void deleteBuffer(GLuint bufferId);
8250129e4ae2777dfbe0738f0f69b17f4d8f9400e2Siva Velusamy
8393a826f78f6313db791e6fc880439189897651b3Siva Velusamy    void traceGLMessage(GLMessage *msg);
8493a826f78f6313db791e6fc880439189897651b3Siva Velusamy};
8593a826f78f6313db791e6fc880439189897651b3Siva Velusamy
8693a826f78f6313db791e6fc880439189897651b3Siva Velusamy/** Per process trace state. */
8793a826f78f6313db791e6fc880439189897651b3Siva Velusamyclass GLTraceState {
8893a826f78f6313db791e6fc880439189897651b3Siva Velusamy    int mTraceContextIds;
8993a826f78f6313db791e6fc880439189897651b3Siva Velusamy    TCPStream *mStream;
9093a826f78f6313db791e6fc880439189897651b3Siva Velusamy    std::map<EGLContext, GLTraceContext*> mPerContextState;
913f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy
923f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    /* Options controlling additional data to be collected on
933f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy       certain trace calls. */
943f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    bool mCollectFbOnEglSwap;
953f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    bool mCollectFbOnGlDraw;
963f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    bool mCollectTextureDataOnGlTexImage;
973f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    pthread_rwlock_t mTraceOptionsRwLock;
983f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy
993f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    /* helper methods to get/set values using provided lock for mutual exclusion. */
1003f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    void safeSetValue(bool *ptr, bool value, pthread_rwlock_t *lock);
1013f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    bool safeGetValue(bool *ptr, pthread_rwlock_t *lock);
10293a826f78f6313db791e6fc880439189897651b3Siva Velusamypublic:
10393a826f78f6313db791e6fc880439189897651b3Siva Velusamy    GLTraceState(TCPStream *stream);
10493a826f78f6313db791e6fc880439189897651b3Siva Velusamy    ~GLTraceState();
10593a826f78f6313db791e6fc880439189897651b3Siva Velusamy
10693a826f78f6313db791e6fc880439189897651b3Siva Velusamy    GLTraceContext *createTraceContext(int version, EGLContext c);
10793a826f78f6313db791e6fc880439189897651b3Siva Velusamy    GLTraceContext *getTraceContext(EGLContext c);
10893a826f78f6313db791e6fc880439189897651b3Siva Velusamy
10993a826f78f6313db791e6fc880439189897651b3Siva Velusamy    TCPStream *getStream();
1103f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy
1113f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    /* Methods to set trace options. */
1123f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    void setCollectFbOnEglSwap(bool en);
1133f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    void setCollectFbOnGlDraw(bool en);
1143f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    void setCollectTextureDataOnGlTexImage(bool en);
1153f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy
1163f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    /* Methods to retrieve trace options. */
1173f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    bool shouldCollectFbOnEglSwap();
1183f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    bool shouldCollectFbOnGlDraw();
1193f194e6e3a62cbb846e8948eac8e4ce9aa7444a6Siva Velusamy    bool shouldCollectTextureDataOnGlTexImage();
1200469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy};
1210469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
12293a826f78f6313db791e6fc880439189897651b3Siva Velusamyvoid setupTraceContextThreadSpecific(GLTraceContext *context);
1230469dd6d55fa331bfd7de9431da98b6340d82271Siva VelusamyGLTraceContext *getGLTraceContext();
1240469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamyvoid releaseContext();
1250469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
1260469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy};
1270469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy};
1280469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
1290469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#endif
130