1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <GLES2/gl2.h>
6#include <GLES2/gl2ext.h>
7
8#include "gpu/command_buffer/tests/gl_manager.h"
9#include "gpu/command_buffer/tests/gl_test_utils.h"
10#include "testing/gmock/include/gmock/gmock.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace gpu {
14
15class GLTest : public testing::Test {
16 protected:
17  virtual void SetUp() {
18    gl_.Initialize(GLManager::Options());
19  }
20
21  virtual void TearDown() {
22    gl_.Destroy();
23  }
24
25  GLManager gl_;
26};
27
28// Test that GL is at least minimally working.
29TEST_F(GLTest, Basic) {
30  glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
31  glClear(GL_COLOR_BUFFER_BIT);
32  uint8 expected[] = { 0, 255, 0, 255, };
33  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected));
34  GLTestHelper::CheckGLError("no errors", __LINE__);
35}
36
37TEST_F(GLTest, BasicFBO) {
38  GLuint tex = 0;
39  glGenTextures(1, &tex);
40  GLuint fbo = 0;
41  glGenFramebuffers(1, &fbo);
42  glBindTexture(GL_TEXTURE_2D, tex);
43  scoped_ptr<uint8[]> pixels(new uint8 [16*16*4]);
44  memset(pixels.get(), 0, 16*16*4);
45  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE,
46               pixels.get());
47  glGenerateMipmap(GL_TEXTURE_2D);
48  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
49  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
50  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
51  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
52  glBindFramebuffer(GL_FRAMEBUFFER, fbo);
53  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
54                         tex, 0);
55  EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
56            glCheckFramebufferStatus(GL_FRAMEBUFFER));
57  glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
58  glClear(GL_COLOR_BUFFER_BIT);
59  uint8 expected[] = { 0, 255, 0, 255, };
60  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 16, 16, 0, expected));
61  glDeleteFramebuffers(1, &fbo);
62  glDeleteTextures(1, &tex);
63  GLTestHelper::CheckGLError("no errors", __LINE__);
64}
65
66TEST_F(GLTest, SimpleShader) {
67  static const char* v_shader_str =
68      "attribute vec4 a_Position;\n"
69      "void main()\n"
70      "{\n"
71      "   gl_Position = a_Position;\n"
72      "}\n";
73  static const char* f_shader_str =
74      "precision mediump float;\n"
75      "void main()\n"
76      "{\n"
77      "  gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
78      "}\n";
79
80  GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
81  glUseProgram(program);
82  GLuint position_loc = glGetAttribLocation(program, "a_Position");
83
84  GLTestHelper::SetupUnitQuad(position_loc);
85
86  uint8 expected_clear[] = { 127, 0, 255, 0, };
87  glClearColor(0.5f, 0.0f, 1.0f, 0.0f);
88  glClear(GL_COLOR_BUFFER_BIT);
89  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 1, expected_clear));
90  uint8 expected_draw[] = { 0, 255, 0, 255, };
91  glDrawArrays(GL_TRIANGLES, 0, 6);
92  EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_draw));
93}
94
95TEST_F(GLTest, GetString) {
96  EXPECT_STREQ(
97      "OpenGL ES 2.0 Chromium",
98      reinterpret_cast<const char*>(glGetString(GL_VERSION)));
99  EXPECT_STREQ(
100      "OpenGL ES GLSL ES 1.0 Chromium",
101      reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION)));
102  EXPECT_STREQ(
103      "Chromium",
104      reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
105  EXPECT_STREQ(
106      "Chromium",
107      reinterpret_cast<const char*>(glGetString(GL_VENDOR)));
108}
109
110}  // namespace gpu
111
112