1 /*
2  * Copyright (C) 2016 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 #include <debug/NullGlesDriver.h>
18 
19 namespace android {
20 namespace uirenderer {
21 namespace debug {
22 
getSkiaInterface()23 sk_sp<const GrGLInterface> NullGlesDriver::getSkiaInterface() {
24     sk_sp<const GrGLInterface> skiaInterface(GrGLCreateNullInterface());
25     return skiaInterface;
26 }
27 
28 struct {
29     GLboolean scissorEnabled;
30 } gState;
31 
nullglGenCommon(GLsizei n,GLuint * buffers)32 static void nullglGenCommon(GLsizei n, GLuint* buffers) {
33     static GLuint nextId = 0;
34     int i;
35     for (i = 0; i < n; i++) {
36         buffers[i] = ++nextId;
37     }
38 }
39 
glGenBuffers_(GLsizei n,GLuint * buffers)40 void NullGlesDriver::glGenBuffers_(GLsizei n, GLuint* buffers) {
41     nullglGenCommon(n, buffers);
42 }
43 
glGenFramebuffers_(GLsizei n,GLuint * framebuffers)44 void NullGlesDriver::glGenFramebuffers_(GLsizei n, GLuint* framebuffers) {
45     nullglGenCommon(n, framebuffers);
46 }
47 
glGenRenderbuffers_(GLsizei n,GLuint * renderbuffers)48 void NullGlesDriver::glGenRenderbuffers_(GLsizei n, GLuint* renderbuffers) {
49     nullglGenCommon(n, renderbuffers);
50 }
51 
glGenTextures_(GLsizei n,GLuint * textures)52 void NullGlesDriver::glGenTextures_(GLsizei n, GLuint* textures) {
53     nullglGenCommon(n, textures);
54 }
55 
glCreateProgram_(void)56 GLuint NullGlesDriver::glCreateProgram_(void) {
57     static GLuint nextProgram = 0;
58     return ++nextProgram;
59 }
60 
glCreateShader_(GLenum type)61 GLuint NullGlesDriver::glCreateShader_(GLenum type) {
62     static GLuint nextShader = 0;
63     return ++nextShader;
64 }
65 
glGetProgramiv_(GLuint program,GLenum pname,GLint * params)66 void NullGlesDriver::glGetProgramiv_(GLuint program, GLenum pname, GLint* params) {
67     switch (pname) {
68         case GL_DELETE_STATUS:
69         case GL_LINK_STATUS:
70         case GL_VALIDATE_STATUS:
71             *params = GL_TRUE;
72             break;
73         case GL_INFO_LOG_LENGTH:
74             *params = 16;
75             break;
76     }
77 }
78 
glGetProgramInfoLog_(GLuint program,GLsizei bufSize,GLsizei * length,GLchar * infoLog)79 void NullGlesDriver::glGetProgramInfoLog_(GLuint program, GLsizei bufSize, GLsizei* length,
80                                           GLchar* infoLog) {
81     *length = snprintf(infoLog, bufSize, "success");
82     if (*length >= bufSize) {
83         *length = bufSize - 1;
84     }
85 }
86 
glGetShaderiv_(GLuint shader,GLenum pname,GLint * params)87 void NullGlesDriver::glGetShaderiv_(GLuint shader, GLenum pname, GLint* params) {
88     switch (pname) {
89         case GL_COMPILE_STATUS:
90         case GL_DELETE_STATUS:
91             *params = GL_TRUE;
92     }
93 }
94 
glGetShaderInfoLog_(GLuint shader,GLsizei bufSize,GLsizei * length,GLchar * infoLog)95 void NullGlesDriver::glGetShaderInfoLog_(GLuint shader, GLsizei bufSize, GLsizei* length,
96                                          GLchar* infoLog) {
97     *length = snprintf(infoLog, bufSize, "success");
98     if (*length >= bufSize) {
99         *length = bufSize - 1;
100     }
101 }
102 
setBooleanState(GLenum cap,GLboolean value)103 void setBooleanState(GLenum cap, GLboolean value) {
104     switch (cap) {
105         case GL_SCISSOR_TEST:
106             gState.scissorEnabled = value;
107             break;
108     }
109 }
110 
glEnable_(GLenum cap)111 void NullGlesDriver::glEnable_(GLenum cap) {
112     setBooleanState(cap, GL_TRUE);
113 }
114 
glDisable_(GLenum cap)115 void NullGlesDriver::glDisable_(GLenum cap) {
116     setBooleanState(cap, GL_FALSE);
117 }
118 
glIsEnabled_(GLenum cap)119 GLboolean NullGlesDriver::glIsEnabled_(GLenum cap) {
120     switch (cap) {
121         case GL_SCISSOR_TEST:
122             return gState.scissorEnabled;
123         default:
124             return GL_FALSE;
125     }
126 }
127 
glGetIntegerv_(GLenum pname,GLint * data)128 void NullGlesDriver::glGetIntegerv_(GLenum pname, GLint* data) {
129     switch (pname) {
130         case GL_MAX_TEXTURE_SIZE:
131             *data = 2048;
132             break;
133         case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
134             *data = 4;
135             break;
136         default:
137             *data = 0;
138     }
139 }
140 
glCheckFramebufferStatus_(GLenum target)141 GLenum NullGlesDriver::glCheckFramebufferStatus_(GLenum target) {
142     switch (target) {
143         case GL_FRAMEBUFFER:
144             return GL_FRAMEBUFFER_COMPLETE;
145         default:
146             return 0;  // error case
147     }
148 }
149 
getString(GLenum name)150 static const char* getString(GLenum name) {
151     switch (name) {
152         case GL_VENDOR:
153             return "android";
154         case GL_RENDERER:
155             return "null";
156         case GL_VERSION:
157             return "OpenGL ES 2.0 rev1";
158         case GL_SHADING_LANGUAGE_VERSION:
159             return "OpenGL ES GLSL ES 2.0 rev1";
160         case GL_EXTENSIONS:
161         default:
162             return "";
163     }
164 }
165 
glGetString_(GLenum name)166 const GLubyte* NullGlesDriver::glGetString_(GLenum name) {
167     return (GLubyte*)getString(name);
168 }
169 
170 }  // namespace debug
171 }  // namespace uirenderer
172 }  // namespace android
173