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    GLTraceState *mState;       /* parent GL Trace state (for per process GL Trace State Info) */
54
55    void *fbcontents;           /* memory area to read framebuffer contents */
56    void *fbcompressed;         /* destination for lzf compressed framebuffer */
57    unsigned fbcontentsSize;    /* size of fbcontents & fbcompressed buffers */
58
59    BufferedOutputStream *mBufferedOutputStream; /* stream where trace info is sent */
60
61    /* list of element array buffers in use. */
62    DefaultKeyedVector<GLuint, ElementArrayBuffer*> mElementArrayBuffers;
63
64    void resizeFBMemory(unsigned minSize);
65public:
66    gl_hooks_t *hooks;
67
68    GLTraceContext(int id, GLTraceState *state, BufferedOutputStream *stream);
69    int getId();
70    GLTraceState *getGlobalTraceState();
71    void getCompressedFB(void **fb, unsigned *fbsize,
72                            unsigned *fbwidth, unsigned *fbheight,
73                            FBBinding fbToRead);
74
75    // Methods to work with element array buffers
76    void bindBuffer(GLuint bufferId, GLvoid *data, GLsizeiptr size);
77    void getBuffer(GLuint bufferId, GLvoid **data, GLsizeiptr *size);
78    void updateBufferSubData(GLuint bufferId, GLintptr offset, GLvoid *data, GLsizeiptr size);
79    void deleteBuffer(GLuint bufferId);
80
81    void traceGLMessage(GLMessage *msg);
82};
83
84/** Per process trace state. */
85class GLTraceState {
86    int mTraceContextIds;
87    TCPStream *mStream;
88    std::map<EGLContext, GLTraceContext*> mPerContextState;
89
90    /* Options controlling additional data to be collected on
91       certain trace calls. */
92    bool mCollectFbOnEglSwap;
93    bool mCollectFbOnGlDraw;
94    bool mCollectTextureDataOnGlTexImage;
95    pthread_rwlock_t mTraceOptionsRwLock;
96
97    /* helper methods to get/set values using provided lock for mutual exclusion. */
98    void safeSetValue(bool *ptr, bool value, pthread_rwlock_t *lock);
99    bool safeGetValue(bool *ptr, pthread_rwlock_t *lock);
100public:
101    GLTraceState(TCPStream *stream);
102    ~GLTraceState();
103
104    GLTraceContext *createTraceContext(int version, EGLContext c);
105    GLTraceContext *getTraceContext(EGLContext c);
106
107    TCPStream *getStream();
108
109    /* Methods to set trace options. */
110    void setCollectFbOnEglSwap(bool en);
111    void setCollectFbOnGlDraw(bool en);
112    void setCollectTextureDataOnGlTexImage(bool en);
113
114    /* Methods to retrieve trace options. */
115    bool shouldCollectFbOnEglSwap();
116    bool shouldCollectFbOnGlDraw();
117    bool shouldCollectTextureDataOnGlTexImage();
118};
119
120void setupTraceContextThreadSpecific(GLTraceContext *context);
121GLTraceContext *getGLTraceContext();
122void releaseContext();
123
124};
125};
126
127#endif
128