Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / tests / gl_chromium_framebuffer_multisample_unittest.cc
1 // Copyright (c) 2012 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 #include <GLES2/gl2extchromium.h>
8
9 #include "gpu/command_buffer/tests/gl_manager.h"
10 #include "gpu/command_buffer/tests/gl_test_utils.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace gpu {
15
16 class GLChromiumFramebufferMultisampleTest : public testing::Test {
17  protected:
18   void SetUp() override { gl_.Initialize(GLManager::Options()); }
19
20   void TearDown() override { gl_.Destroy(); }
21
22   GLManager gl_;
23 };
24
25 // Test that GL is at least minimally working.
26 TEST_F(GLChromiumFramebufferMultisampleTest, CachedBindingsTest) {
27   if (!GLTestHelper::HasExtension("GL_CHROMIUM_framebuffer_multisample")) {
28     return;
29   }
30
31   GLuint fbo = 0;
32   glGenFramebuffers(1, &fbo);
33   glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
34   glBindFramebuffer(GL_FRAMEBUFFER, 0);
35
36   // If the caching is bad the second call to glBindFramebuffer will do nothing.
37   // which means the draw buffer is bad and will not return
38   // GL_FRAMEBUFFER_COMPLETE and rendering will generate an error.
39   EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
40             glCheckFramebufferStatus(GL_FRAMEBUFFER));
41
42   glClear(GL_COLOR_BUFFER_BIT);
43   GLTestHelper::CheckGLError("no errors", __LINE__);
44 }
45
46 TEST_F(GLChromiumFramebufferMultisampleTest, DrawAndResolve) {
47   if (!GLTestHelper::HasExtension("GL_CHROMIUM_framebuffer_multisample")) {
48     return;
49   }
50
51   static const char* v_shader_str =
52       "attribute vec4 a_Position;\n"
53       "void main()\n"
54       "{\n"
55       "   gl_Position = a_Position;\n"
56       "}\n";
57   static const char* f_shader_str =
58       "precision mediump float;\n"
59       "void main()\n"
60       "{\n"
61       "  gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
62       "}\n";
63
64   GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
65   glUseProgram(program);
66   GLuint position_loc = glGetAttribLocation(program, "a_Position");
67
68   GLTestHelper::SetupUnitQuad(position_loc);
69
70   const GLuint width = 100;
71   const GLuint height = 100;
72
73   // Create a sample buffer.
74   GLsizei num_samples = 4, max_samples = 0;
75   glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
76   num_samples = std::min(num_samples, max_samples);
77
78   GLuint sample_fbo, sample_rb;
79   glGenRenderbuffers(1, &sample_rb);
80   glBindRenderbuffer(GL_RENDERBUFFER, sample_rb);
81   glRenderbufferStorageMultisampleCHROMIUM(
82       GL_RENDERBUFFER, num_samples, GL_RGBA8_OES, width, height);
83   GLint param = 0;
84   glGetRenderbufferParameteriv(
85       GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &param);
86   EXPECT_GE(param, num_samples);
87
88   glGenFramebuffers(1, &sample_fbo);
89   glBindFramebuffer(GL_FRAMEBUFFER, sample_fbo);
90   glFramebufferRenderbuffer(GL_FRAMEBUFFER,
91                             GL_COLOR_ATTACHMENT0,
92                             GL_RENDERBUFFER,
93                             sample_rb);
94   EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
95             glCheckFramebufferStatus(GL_FRAMEBUFFER));
96
97   // Create another FBO to resolve the multisample buffer into.
98   GLuint resolve_fbo, resolve_tex;
99   glGenTextures(1, &resolve_tex);
100   glBindTexture(GL_TEXTURE_2D, resolve_tex);
101   glTexImage2D(GL_TEXTURE_2D,
102                0,
103                GL_RGBA,
104                width,
105                height,
106                0,
107                GL_RGBA,
108                GL_UNSIGNED_BYTE,
109                NULL);
110   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
111   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
112   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
113   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
114   glGenFramebuffers(1, &resolve_fbo);
115   glBindFramebuffer(GL_FRAMEBUFFER, resolve_fbo);
116   glFramebufferTexture2D(GL_FRAMEBUFFER,
117                          GL_COLOR_ATTACHMENT0,
118                          GL_TEXTURE_2D,
119                          resolve_tex,
120                          0);
121   EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
122             glCheckFramebufferStatus(GL_FRAMEBUFFER));
123
124   // Draw one triangle (bottom left half).
125   glViewport(0, 0, width, height);
126   glBindFramebuffer(GL_FRAMEBUFFER, sample_fbo);
127   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
128   glClear(GL_COLOR_BUFFER_BIT);
129   glDrawArrays(GL_TRIANGLES, 0, 3);
130
131   // Resolve.
132   glBindFramebuffer(GL_READ_FRAMEBUFFER, sample_fbo);
133   glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolve_fbo);
134   glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
135   glClear(GL_COLOR_BUFFER_BIT);
136   glBlitFramebufferCHROMIUM(0,
137                             0,
138                             width,
139                             height,
140                             0,
141                             0,
142                             width,
143                             height,
144                             GL_COLOR_BUFFER_BIT,
145                             GL_NEAREST);
146
147   // Verify.
148   const uint8 green[] = {0, 255, 0, 255};
149   const uint8 black[] = {0, 0, 0, 0};
150   glBindFramebuffer(GL_READ_FRAMEBUFFER, resolve_fbo);
151   EXPECT_TRUE(
152       GLTestHelper::CheckPixels(width / 4, (3 * height) / 4, 1, 1, 0, green));
153   EXPECT_TRUE(GLTestHelper::CheckPixels(width - 1, 0, 1, 1, 0, black));
154 }
155
156 }  // namespace gpu
157