1 #include "GLSnapshot.h"
2
3 #include <GLES2/gl2.h>
4 #include <GLES3/gl31.h>
5
6 #include <stdio.h>
7
8 #define DEBUG 0
9
10 #if DEBUG
11
12 #define D(fmt,...) do { \
13 fprintf(stderr, "%s:%s:%d: " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
14 } while(0) \
15
16 #else
17 #define D(...)
18 #endif
19
20 namespace gfxstream {
21 namespace gl {
22 namespace snapshot {
23
GLSnapshotState(const GLESv2Dispatch * gl)24 GLSnapshotState::GLSnapshotState(const GLESv2Dispatch* gl) : mGL(gl) {
25 D("init snapshot state");
26 }
27
getGlobalStateEnum(GLenum name,int size)28 void GLSnapshotState::getGlobalStateEnum(GLenum name, int size) {
29 D("save 0x%x", name);
30 std::vector<GLenum>& store = mGlobals[name].enums;
31 store.resize(size);
32 mGL->glGetIntegerv(name, (GLint*)&store[0]);
33 }
34
getGlobalStateByte(GLenum name,int size)35 void GLSnapshotState::getGlobalStateByte(GLenum name, int size) {
36 D("save 0x%x", name);
37 std::vector<unsigned char>& store = mGlobals[name].bytes;
38 store.resize(size);
39 mGL->glGetBooleanv(name, (GLboolean*)&store[0]);
40 }
41
getGlobalStateInt(GLenum name,int size)42 void GLSnapshotState::getGlobalStateInt(GLenum name, int size) {
43 D("save 0x%x", name);
44 std::vector<uint32_t>& store = mGlobals[name].ints;
45 store.resize(size);
46 mGL->glGetIntegerv(name, (GLint*)&store[0]);
47 }
48
getGlobalStateFloat(GLenum name,int size)49 void GLSnapshotState::getGlobalStateFloat(GLenum name, int size) {
50 D("save 0x%x", name);
51 std::vector<float>& store = mGlobals[name].floats;
52 store.resize(size);
53 mGL->glGetFloatv(name, (GLfloat*)&store[0]);
54 }
55
getGlobalStateInt64(GLenum name,int size)56 void GLSnapshotState::getGlobalStateInt64(GLenum name, int size) {
57 D("save 0x%x", name);
58 std::vector<uint64_t>& store = mGlobals[name].int64s;
59 store.resize(size);
60 mGL->glGetInteger64v(name, (GLint64*)&store[0]);
61 }
62
getGlobalStateEnable(GLenum name)63 void GLSnapshotState::getGlobalStateEnable(GLenum name) {
64 D("save 0x%x", name);
65 mEnables[name] = mGL->glIsEnabled(name) == GL_TRUE;
66 }
67
save()68 void GLSnapshotState::save() {
69 getGlobalStateEnable(GL_DEPTH_TEST);
70
71 getGlobalStateFloat(GL_COLOR_CLEAR_VALUE, 4);
72 getGlobalStateInt(GL_ACTIVE_TEXTURE, 1);
73 }
74
restore()75 void GLSnapshotState::restore() {
76 for (const auto& it : mEnables) {
77 if (it.second) {
78 mGL->glEnable(it.first);
79 } else {
80 mGL->glDisable(it.first);
81 }
82 }
83
84 for (auto& it: mProgramNames) {
85 GLShaderState& shaderState = mShaderState[it.first];
86 it.second = mGL->glCreateShader(shaderState.type);
87 if (shaderState.source.size()) {
88 GLint len = shaderState.source.size();
89 const char* source = shaderState.source.c_str();
90 const char** sources = &source;
91 mGL->glShaderSource(it.second, 1, sources, &len);
92 }
93 if (shaderState.compileStatus) {
94 mGL->glCompileShader(it.second);
95 }
96 }
97
98 std::vector<float> clearColor = mGlobals[GL_COLOR_CLEAR_VALUE].floats;
99 mGL->glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
100
101 mGL->glActiveTexture(mGlobals[GL_ACTIVE_TEXTURE].ints[0]);
102 }
103
createShader(GLuint shader,GLenum shaderType)104 GLuint GLSnapshotState::createShader(GLuint shader, GLenum shaderType) {
105 GLuint shaderName = mProgramCounter++;
106 mProgramNames[shaderName] = shader;
107 mProgramNamesBack[shader] = shaderName;
108 mShaderState[shaderName].type = shaderType;
109 mShaderState[shaderName].source = "";
110 mShaderState[shaderName].compileStatus = false;
111 return shaderName;
112 }
113
createProgram(GLuint program)114 GLuint GLSnapshotState::createProgram(GLuint program) {
115 return program;
116 }
117
118
shaderString(GLuint shader,const GLchar * string)119 void GLSnapshotState::shaderString(GLuint shader, const GLchar* string) {
120 mShaderState[mProgramNamesBack[shader]].source = std::string(string);
121 }
122
genBuffers(GLsizei n,GLuint * buffers)123 void GLSnapshotState::genBuffers(GLsizei n, GLuint* buffers) {
124 return;
125 }
126
getProgramName(GLuint name)127 GLuint GLSnapshotState::getProgramName(GLuint name) {
128 return mProgramNames[name];
129 }
130
131 } // namespace snapshot
132 } // namespace gl
133 } // namespace gfxstream
134