• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "GLSnapshotTestStateUtils.h"
16 
17 #include "GLSnapshotTesting.h"
18 #include "apigen-codec-common/glUtils.h"
19 
20 #include <gtest/gtest.h>
21 
22 #include <GLES2/gl2.h>
23 #include <GLES3/gl31.h>
24 
25 namespace gfxstream {
26 namespace gl {
27 
createBuffer(const GLESv2Dispatch * gl,GlBufferData data)28 GLuint createBuffer(const GLESv2Dispatch* gl, GlBufferData data) {
29     // We bind to GL_ARRAY_BUFFER in order to set up buffer data,
30     // so let's hold on to what the old binding was so we can restore it
31     GLuint currentArrayBuffer;
32     gl->glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&currentArrayBuffer);
33     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
34 
35     GLuint name;
36     gl->glGenBuffers(1, &name);
37     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
38 
39     gl->glBindBuffer(GL_ARRAY_BUFFER, name);
40     gl->glBufferData(GL_ARRAY_BUFFER, data.size, data.bytes, data.usage);
41 
42     // Restore the old binding
43     gl->glBindBuffer(GL_ARRAY_BUFFER, currentArrayBuffer);
44     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
45     return name;
46 };
47 
loadAndCompileShader(const GLESv2Dispatch * gl,GLenum shaderType,const char * src)48 GLuint loadAndCompileShader(const GLESv2Dispatch* gl,
49                             GLenum shaderType,
50                             const char* src) {
51     GLuint shader = gl->glCreateShader(shaderType);
52     gl->glShaderSource(shader, 1, (const GLchar* const*)&src, nullptr);
53     gl->glCompileShader(shader);
54 
55     GLint compileStatus;
56     gl->glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
57     EXPECT_EQ(GL_TRUE, compileStatus);
58 
59     if (compileStatus != GL_TRUE) {
60         GLsizei infoLogLength;
61         gl->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
62         std::vector<char> infoLog;
63         infoLog.resize(infoLogLength);
64         gl->glGetShaderInfoLog(shader, infoLogLength, nullptr, &infoLog[0]);
65         fprintf(stderr, "%s: fail to compile. infolog %s\n", __func__,
66                 &infoLog[0]);
67     }
68 
69     return shader;
70 }
71 
getTextureImageData(const GLESv2Dispatch * gl,GLuint texture,GLenum target,GLint level,GLsizei width,GLsizei height,GLenum format,GLenum type)72 std::vector<GLubyte> getTextureImageData(const GLESv2Dispatch* gl,
73                                          GLuint texture,
74                                          GLenum target,
75                                          GLint level,
76                                          GLsizei width,
77                                          GLsizei height,
78                                          GLenum format,
79                                          GLenum type) {
80     std::vector<GLubyte> out = {};
81     out.resize(width * height *
82                glUtilsPixelBitSize(GL_RGBA /* format */,
83                                    GL_UNSIGNED_BYTE /* type */) / 8);
84 
85     // switch to auxiliary framebuffer
86     GLint oldFramebuffer;
87     gl->glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oldFramebuffer);
88 
89     GLuint auxFramebuffer;
90     gl->glGenFramebuffers(1, &auxFramebuffer);
91     gl->glBindFramebuffer(GL_FRAMEBUFFER, auxFramebuffer);
92     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
93 
94     gl->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target,
95                                texture, level);
96     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
97     gl->glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
98                      out.data());  // TODO(benzene): flexible format/type?
99                                    // seems like RGBA/UNSIGNED_BYTE is the only
100                                    // guaranteed supported format+type
101     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
102 
103     // restore old framebuffer
104     gl->glBindFramebuffer(GL_FRAMEBUFFER, oldFramebuffer);
105     gl->glDeleteFramebuffers(1, &auxFramebuffer);
106     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
107 
108     return out;
109 }
110 
111 }  // namespace gl
112 }  // namespace gfxstream
113