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#ifndef __GLTRACE_CONTEXT_H_
18#define __GLTRACE_CONTEXT_H_
19
20#include <map>
21#include <pthread.h>
22#include <utils/KeyedVector.h>
23
24#include "hooks.h"
25#include "gltrace_transport.h"
26
27namespace android {
28namespace gltrace {
29
30using ::android::gl_hooks_t;
31
32enum FBBinding {CURRENTLY_BOUND_FB, FB0};
33
34class GLTraceState;
35
36class ElementArrayBuffer {
37    GLvoid *mBuf;
38    GLsizeiptr mSize;
39
40public:
41    ElementArrayBuffer():mBuf(NULL), mSize(0) {}
42    ElementArrayBuffer(GLvoid *buf, GLsizeiptr size);
43    ~ElementArrayBuffer();
44
45    void updateSubBuffer(GLintptr offset, const GLvoid* data, GLsizeiptr size);
46    GLvoid *getBuffer();
47    GLsizeiptr getSize();
48};
49
50/** GL Trace Context info associated with each EGLContext */
51class GLTraceContext {
52    int mId;                    /* unique context id */
53    int mVersion;               /* GL version, e.g: egl_connection_t::GLESv2_INDEX */
54    GLTraceState *mState;       /* parent GL Trace state (for per process GL Trace State Info) */
55
56    void *fbcontents;           /* memory area to read framebuffer contents */
57    void *fbcompressed;         /* destination for lzf compressed framebuffer */
58    unsigned fbcontentsSize;    /* size of fbcontents & fbcompressed buffers */
59
60    BufferedOutputStream *mBufferedOutputStream; /* stream where trace info is sent */
61
62    /* list of element array buffers in use. */
63    DefaultKeyedVector<GLuint, ElementArrayBuffer*> mElementArrayBuffers;
64
65    void resizeFBMemory(unsigned minSize);
66public:
67    gl_hooks_t *hooks;
68
69    GLTraceContext(int id, int version, GLTraceState *state, BufferedOutputStream *stream);
70    int getId();
71    int getVersion();
72    GLTraceState *getGlobalTraceState();
73    void getCompressedFB(void **fb, unsigned *fbsize,
74                            unsigned *fbwidth, unsigned *fbheight,
75                            FBBinding fbToRead);
76
77    // Methods to work with element array buffers
78    void bindBuffer(GLuint bufferId, GLvoid *data, GLsizeiptr size);
79    void getBuffer(GLuint bufferId, GLvoid **data, GLsizeiptr *size);
80    void updateBufferSubData(GLuint bufferId, GLintptr offset, GLvoid *data, GLsizeiptr size);
81    void deleteBuffer(GLuint bufferId);
82
83    void traceGLMessage(GLMessage *msg);
84};
85
86/** Per process trace state. */
87class GLTraceState {
88    int mTraceContextIds;
89    TCPStream *mStream;
90    std::map<EGLContext, GLTraceContext*> mPerContextState;
91
92    /* Options controlling additional data to be collected on
93       certain trace calls. */
94    bool mCollectFbOnEglSwap;
95    bool mCollectFbOnGlDraw;
96    bool mCollectTextureDataOnGlTexImage;
97    pthread_rwlock_t mTraceOptionsRwLock;
98
99    /* helper methods to get/set values using provided lock for mutual exclusion. */
100    void safeSetValue(bool *ptr, bool value, pthread_rwlock_t *lock);
101    bool safeGetValue(bool *ptr, pthread_rwlock_t *lock);
102public:
103    GLTraceState(TCPStream *stream);
104    ~GLTraceState();
105
106    GLTraceContext *createTraceContext(int version, EGLContext c);
107    GLTraceContext *getTraceContext(EGLContext c);
108
109    TCPStream *getStream();
110
111    /* Methods to set trace options. */
112    void setCollectFbOnEglSwap(bool en);
113    void setCollectFbOnGlDraw(bool en);
114    void setCollectTextureDataOnGlTexImage(bool en);
115
116    /* Methods to retrieve trace options. */
117    bool shouldCollectFbOnEglSwap();
118    bool shouldCollectFbOnGlDraw();
119    bool shouldCollectTextureDataOnGlTexImage();
120};
121
122void setupTraceContextThreadSpecific(GLTraceContext *context);
123GLTraceContext *getGLTraceContext();
124void releaseContext();
125
126};
127};
128
129#endif
130