• 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 emugl {
26 
createBuffer(const GLESv2Dispatch * gl,GlBufferData data)27 GLuint createBuffer(const GLESv2Dispatch* gl, GlBufferData data) {
28     // We bind to GL_ARRAY_BUFFER in order to set up buffer data,
29     // so let's hold on to what the old binding was so we can restore it
30     GLuint currentArrayBuffer;
31     gl->glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&currentArrayBuffer);
32     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
33 
34     GLuint name;
35     gl->glGenBuffers(1, &name);
36     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
37 
38     gl->glBindBuffer(GL_ARRAY_BUFFER, name);
39     gl->glBufferData(GL_ARRAY_BUFFER, data.size, data.bytes, data.usage);
40 
41     // Restore the old binding
42     gl->glBindBuffer(GL_ARRAY_BUFFER, currentArrayBuffer);
43     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
44     return name;
45 };
46 
loadAndCompileShader(const GLESv2Dispatch * gl,GLenum shaderType,const char * src)47 GLuint loadAndCompileShader(const GLESv2Dispatch* gl,
48                             GLenum shaderType,
49                             const char* src) {
50     GLuint shader = gl->glCreateShader(shaderType);
51     gl->glShaderSource(shader, 1, (const GLchar* const*)&src, nullptr);
52     gl->glCompileShader(shader);
53 
54     GLint compileStatus;
55     gl->glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
56     EXPECT_EQ(GL_TRUE, compileStatus);
57 
58     if (compileStatus != GL_TRUE) {
59         GLsizei infoLogLength;
60         gl->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
61         std::vector<char> infoLog;
62         infoLog.resize(infoLogLength);
63         gl->glGetShaderInfoLog(shader, infoLogLength, nullptr, &infoLog[0]);
64         fprintf(stderr, "%s: fail to compile. infolog %s\n", __func__,
65                 &infoLog[0]);
66     }
67 
68     return shader;
69 }
70 
getTextureImageData(const GLESv2Dispatch * gl,GLuint texture,GLenum target,GLint level,GLsizei width,GLsizei height,GLenum format,GLenum type)71 std::vector<GLubyte> getTextureImageData(const GLESv2Dispatch* gl,
72                                          GLuint texture,
73                                          GLenum target,
74                                          GLint level,
75                                          GLsizei width,
76                                          GLsizei height,
77                                          GLenum format,
78                                          GLenum type) {
79     std::vector<GLubyte> out = {};
80     out.resize(width * height *
81                glUtilsPixelBitSize(GL_RGBA /* format */,
82                                    GL_UNSIGNED_BYTE /* type */) / 8);
83 
84     // switch to auxiliary framebuffer
85     GLint oldFramebuffer;
86     gl->glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oldFramebuffer);
87 
88     GLuint auxFramebuffer;
89     gl->glGenFramebuffers(1, &auxFramebuffer);
90     gl->glBindFramebuffer(GL_FRAMEBUFFER, auxFramebuffer);
91     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
92 
93     gl->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target,
94                                texture, level);
95     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
96     gl->glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
97                      out.data());  // TODO(benzene): flexible format/type?
98                                    // seems like RGBA/UNSIGNED_BYTE is the only
99                                    // guaranteed supported format+type
100     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
101 
102     // restore old framebuffer
103     gl->glBindFramebuffer(GL_FRAMEBUFFER, oldFramebuffer);
104     gl->glDeleteFramebuffers(1, &auxFramebuffer);
105     EXPECT_EQ(GL_NO_ERROR, gl->glGetError());
106 
107     return out;
108 }
109 
110 }  // namespace emugl
111