[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / gpu / command_buffer / tests / gl_gpu_memory_buffer_unittest.cc
blobd544d10c90ca4c8f338933b9359510420242ab3b
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.
5 #include <GLES2/gl2.h>
6 #include <GLES2/gl2chromium.h>
7 #include <GLES2/gl2ext.h>
8 #include <GLES2/gl2extchromium.h>
10 #include "base/bind.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/process/process_handle.h"
13 #include "gpu/command_buffer/client/gles2_implementation.h"
14 #include "gpu/command_buffer/service/command_buffer_service.h"
15 #include "gpu/command_buffer/service/image_manager.h"
16 #include "gpu/command_buffer/tests/gl_manager.h"
17 #include "gpu/command_buffer/tests/gl_test_utils.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/gfx/gpu_memory_buffer.h"
21 #include "ui/gl/gl_image.h"
23 using testing::_;
24 using testing::IgnoreResult;
25 using testing::InvokeWithoutArgs;
26 using testing::Invoke;
27 using testing::Return;
28 using testing::SetArgPointee;
29 using testing::StrictMock;
31 namespace gpu {
32 namespace gles2 {
34 static const int kImageWidth = 32;
35 static const int kImageHeight = 32;
37 class GpuMemoryBufferTest : public testing::TestWithParam<gfx::BufferFormat> {
38 protected:
39 void SetUp() override {
40 GLManager::Options options;
41 options.size = gfx::Size(kImageWidth, kImageHeight);
42 gl_.Initialize(options);
43 gl_.MakeCurrent();
46 void TearDown() override {
47 gl_.Destroy();
50 GLManager gl_;
53 namespace {
55 #define SHADER(Src) #Src
57 // clang-format off
58 const char kVertexShader[] =
59 SHADER(
60 attribute vec4 a_position;
61 varying vec2 v_texCoord;
62 void main() {
63 gl_Position = a_position;
64 v_texCoord = vec2((a_position.x + 1.0) * 0.5, (a_position.y + 1.0) * 0.5);
68 const char* kFragmentShader =
69 SHADER(
70 precision mediump float;
71 uniform sampler2D a_texture;
72 varying vec2 v_texCoord;
73 void main() {
74 gl_FragColor = texture2D(a_texture, v_texCoord);
77 // clang-format on
79 void SetRow(gfx::BufferFormat format,
80 uint8_t* buffer,
81 int width,
82 uint8_t pixel[4]) {
83 switch (format) {
84 case gfx::BufferFormat::R_8:
85 for (int i = 0; i < width; ++i)
86 buffer[i] = pixel[0];
87 return;
88 case gfx::BufferFormat::RGBA_4444:
89 for (int i = 0; i < width * 2; i += 2) {
90 buffer[i + 0] = (pixel[1] << 4) | (pixel[0] & 0xf);
91 buffer[i + 1] = (pixel[3] << 4) | (pixel[2] & 0xf);
93 return;
94 case gfx::BufferFormat::RGBA_8888:
95 for (int i = 0; i < width * 4; i += 4) {
96 buffer[i + 0] = pixel[0];
97 buffer[i + 1] = pixel[1];
98 buffer[i + 2] = pixel[2];
99 buffer[i + 3] = pixel[3];
101 return;
102 case gfx::BufferFormat::BGRA_8888:
103 for (int i = 0; i < width * 4; i += 4) {
104 buffer[i + 0] = pixel[2];
105 buffer[i + 1] = pixel[1];
106 buffer[i + 2] = pixel[0];
107 buffer[i + 3] = pixel[3];
109 return;
110 case gfx::BufferFormat::ATC:
111 case gfx::BufferFormat::ATCIA:
112 case gfx::BufferFormat::DXT1:
113 case gfx::BufferFormat::DXT5:
114 case gfx::BufferFormat::ETC1:
115 case gfx::BufferFormat::RGBX_8888:
116 case gfx::BufferFormat::YUV_420:
117 NOTREACHED();
118 return;
121 NOTREACHED();
124 GLenum InternalFormat(gfx::BufferFormat format) {
125 switch (format) {
126 case gfx::BufferFormat::R_8:
127 return GL_R8;
128 case gfx::BufferFormat::RGBA_4444:
129 case gfx::BufferFormat::RGBA_8888:
130 return GL_RGBA;
131 case gfx::BufferFormat::BGRA_8888:
132 return GL_BGRA_EXT;
133 case gfx::BufferFormat::ATC:
134 case gfx::BufferFormat::ATCIA:
135 case gfx::BufferFormat::DXT1:
136 case gfx::BufferFormat::DXT5:
137 case gfx::BufferFormat::ETC1:
138 case gfx::BufferFormat::RGBX_8888:
139 case gfx::BufferFormat::YUV_420:
140 NOTREACHED();
141 return 0;
144 NOTREACHED();
145 return 0;
148 } // namespace
150 // An end to end test that tests the whole GpuMemoryBuffer lifecycle.
151 TEST_P(GpuMemoryBufferTest, Lifecycle) {
152 ASSERT_TRUE((GetParam() != gfx::BufferFormat::R_8) ||
153 gl_.GetCapabilities().texture_rg);
155 GLuint texture_id = 0;
156 glGenTextures(1, &texture_id);
157 ASSERT_NE(0u, texture_id);
158 glBindTexture(GL_TEXTURE_2D, texture_id);
159 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
160 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
164 // Create the gpu memory buffer.
165 scoped_ptr<gfx::GpuMemoryBuffer> buffer(gl_.CreateGpuMemoryBuffer(
166 gfx::Size(kImageWidth, kImageHeight), GetParam()));
168 // Map buffer for writing.
169 void* data;
170 bool rv = buffer->Map(&data);
171 DCHECK(rv);
173 uint8_t* mapped_buffer = static_cast<uint8_t*>(data);
174 ASSERT_TRUE(mapped_buffer != NULL);
176 uint8_t pixel[] = {255u, 0u, 0u, 255u};
178 // Assign a value to each pixel.
179 int stride = 0;
180 buffer->GetStride(&stride);
181 ASSERT_NE(stride, 0);
182 for (int y = 0; y < kImageHeight; ++y)
183 SetRow(GetParam(), mapped_buffer + y * stride, kImageWidth, pixel);
185 // Unmap the buffer.
186 buffer->Unmap();
188 // Create the image. This should add the image ID to the ImageManager.
189 GLuint image_id =
190 glCreateImageCHROMIUM(buffer->AsClientBuffer(), kImageWidth, kImageHeight,
191 InternalFormat(GetParam()));
192 ASSERT_NE(0u, image_id);
193 ASSERT_TRUE(gl_.decoder()->GetImageManager()->LookupImage(image_id) != NULL);
195 // Bind the image.
196 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id);
198 // Build program, buffers and draw the texture.
199 GLuint vertex_shader =
200 GLTestHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader);
201 GLuint fragment_shader =
202 GLTestHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader);
203 GLuint program = GLTestHelper::SetupProgram(vertex_shader, fragment_shader);
204 ASSERT_NE(0u, program);
205 glUseProgram(program);
207 GLint sampler_location = glGetUniformLocation(program, "a_texture");
208 ASSERT_NE(-1, sampler_location);
209 glUniform1i(sampler_location, 0);
211 GLuint vbo =
212 GLTestHelper::SetupUnitQuad(glGetAttribLocation(program, "a_position"));
213 ASSERT_NE(0u, vbo);
214 glViewport(0, 0, kImageWidth, kImageHeight);
215 glDrawArrays(GL_TRIANGLES, 0, 6);
216 ASSERT_TRUE(glGetError() == GL_NO_ERROR);
218 // Check if pixels match the values that were assigned to the mapped buffer.
219 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, pixel);
220 EXPECT_TRUE(GL_NO_ERROR == glGetError());
222 // Release the image.
223 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id);
225 // Clean up.
226 glDeleteProgram(program);
227 glDeleteShader(vertex_shader);
228 glDeleteShader(fragment_shader);
229 glDeleteBuffers(1, &vbo);
230 glDestroyImageCHROMIUM(image_id);
231 glDeleteTextures(1, &texture_id);
234 INSTANTIATE_TEST_CASE_P(GpuMemoryBufferTests,
235 GpuMemoryBufferTest,
236 ::testing::Values(gfx::BufferFormat::R_8,
237 gfx::BufferFormat::RGBA_4444,
238 gfx::BufferFormat::RGBA_8888,
239 gfx::BufferFormat::BGRA_8888));
241 } // namespace gles2
242 } // namespace gpu