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 
27 namespace android {
28 namespace gltrace {
29 
30 using ::android::gl_hooks_t;
31 
32 enum FBBinding {CURRENTLY_BOUND_FB, FB0};
33 
34 class GLTraceState;
35 
36 class ElementArrayBuffer {
37     GLvoid *mBuf;
38     GLsizeiptr mSize;
39 
40 public:
ElementArrayBuffer()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 */
51 class GLTraceContext {
52     int mId;                    /* unique context id */
53     int mVersion;               /* GL version, e.g: egl_connection_t::GLESv2_INDEX */
54     int mVersionMajor;          /* GL major version. Lazily parsed in getVersionX(). */
55     int mVersionMinor;          /* GL minor version. Lazily parsed in getVersionX(). */
56     bool mVersionParsed;        /* True if major and minor versions have been parsed. */
57     GLTraceState *mState;       /* parent GL Trace state (for per process GL Trace State Info) */
58 
59     void *fbcontents;           /* memory area to read framebuffer contents */
60     void *fbcompressed;         /* destination for lzf compressed framebuffer */
61     unsigned fbcontentsSize;    /* size of fbcontents & fbcompressed buffers */
62 
63     BufferedOutputStream *mBufferedOutputStream; /* stream where trace info is sent */
64 
65     /* list of element array buffers in use. */
66     DefaultKeyedVector<GLuint, ElementArrayBuffer*> mElementArrayBuffers;
67 
68     /* Parses the GL version string returned from glGetString(GL_VERSION) to get find the major and
69        minor versions of the GLES API. The context must be current before calling. */
70     void parseGlesVersion();
71     void resizeFBMemory(unsigned minSize);
72 public:
73     gl_hooks_t *hooks;
74 
75     GLTraceContext(int id, int version, GLTraceState *state, BufferedOutputStream *stream);
76     int getId();
77     int getVersion();
78     int getVersionMajor();
79     int getVersionMinor();
80     GLTraceState *getGlobalTraceState();
81     void getCompressedFB(void **fb, unsigned *fbsize,
82                             unsigned *fbwidth, unsigned *fbheight,
83                             FBBinding fbToRead);
84 
85     // Methods to work with element array buffers
86     void bindBuffer(GLuint bufferId, GLvoid *data, GLsizeiptr size);
87     void getBuffer(GLuint bufferId, GLvoid **data, GLsizeiptr *size);
88     void updateBufferSubData(GLuint bufferId, GLintptr offset, GLvoid *data, GLsizeiptr size);
89     void deleteBuffer(GLuint bufferId);
90 
91     void traceGLMessage(GLMessage *msg);
92 };
93 
94 /** Per process trace state. */
95 class GLTraceState {
96     int mTraceContextIds;
97     TCPStream *mStream;
98     std::map<EGLContext, GLTraceContext*> mPerContextState;
99 
100     /* Options controlling additional data to be collected on
101        certain trace calls. */
102     bool mCollectFbOnEglSwap;
103     bool mCollectFbOnGlDraw;
104     bool mCollectTextureDataOnGlTexImage;
105     pthread_rwlock_t mTraceOptionsRwLock;
106 
107     /* helper methods to get/set values using provided lock for mutual exclusion. */
108     void safeSetValue(bool *ptr, bool value, pthread_rwlock_t *lock);
109     bool safeGetValue(bool *ptr, pthread_rwlock_t *lock);
110 public:
111     GLTraceState(TCPStream *stream);
112     ~GLTraceState();
113 
114     GLTraceContext *createTraceContext(int version, EGLContext c);
115     GLTraceContext *getTraceContext(EGLContext c);
116 
117     TCPStream *getStream();
118 
119     /* Methods to set trace options. */
120     void setCollectFbOnEglSwap(bool en);
121     void setCollectFbOnGlDraw(bool en);
122     void setCollectTextureDataOnGlTexImage(bool en);
123 
124     /* Methods to retrieve trace options. */
125     bool shouldCollectFbOnEglSwap();
126     bool shouldCollectFbOnGlDraw();
127     bool shouldCollectTextureDataOnGlTexImage();
128 };
129 
130 void setupTraceContextThreadSpecific(GLTraceContext *context);
131 GLTraceContext *getGLTraceContext();
132 void releaseContext();
133 
134 };
135 };
136 
137 #endif
138