Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / gpu / command_buffer / tests / gl_gpu_memory_buffer_unittest.cc
blobe3648a6dad10d74fc961f7f51f19bce4734f9bc4
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::BGRX_8888:
116 case gfx::BufferFormat::YUV_420:
117 case gfx::BufferFormat::UYVY_422:
118 NOTREACHED();
119 return;
122 NOTREACHED();
125 GLenum InternalFormat(gfx::BufferFormat format) {
126 switch (format) {
127 case gfx::BufferFormat::R_8:
128 return GL_R8;
129 case gfx::BufferFormat::RGBA_4444:
130 case gfx::BufferFormat::RGBA_8888:
131 return GL_RGBA;
132 case gfx::BufferFormat::BGRA_8888:
133 return GL_BGRA_EXT;
134 case gfx::BufferFormat::ATC:
135 case gfx::BufferFormat::ATCIA:
136 case gfx::BufferFormat::DXT1:
137 case gfx::BufferFormat::DXT5:
138 case gfx::BufferFormat::ETC1:
139 case gfx::BufferFormat::BGRX_8888:
140 case gfx::BufferFormat::YUV_420:
141 case gfx::BufferFormat::UYVY_422:
142 NOTREACHED();
143 return 0;
146 NOTREACHED();
147 return 0;
150 } // namespace
152 // An end to end test that tests the whole GpuMemoryBuffer lifecycle.
153 TEST_P(GpuMemoryBufferTest, Lifecycle) {
154 ASSERT_TRUE((GetParam() != gfx::BufferFormat::R_8) ||
155 gl_.GetCapabilities().texture_rg);
157 GLuint texture_id = 0;
158 glGenTextures(1, &texture_id);
159 ASSERT_NE(0u, texture_id);
160 glBindTexture(GL_TEXTURE_2D, texture_id);
161 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
162 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
163 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
166 // Create the gpu memory buffer.
167 scoped_ptr<gfx::GpuMemoryBuffer> buffer(gl_.CreateGpuMemoryBuffer(
168 gfx::Size(kImageWidth, kImageHeight), GetParam()));
170 // Map buffer for writing.
171 void* data;
172 bool rv = buffer->Map(&data);
173 DCHECK(rv);
175 uint8_t* mapped_buffer = static_cast<uint8_t*>(data);
176 ASSERT_TRUE(mapped_buffer != NULL);
178 uint8_t pixel[] = {255u, 0u, 0u, 255u};
180 // Assign a value to each pixel.
181 int stride = 0;
182 buffer->GetStride(&stride);
183 ASSERT_NE(stride, 0);
184 for (int y = 0; y < kImageHeight; ++y)
185 SetRow(GetParam(), mapped_buffer + y * stride, kImageWidth, pixel);
187 // Unmap the buffer.
188 buffer->Unmap();
190 // Create the image. This should add the image ID to the ImageManager.
191 GLuint image_id =
192 glCreateImageCHROMIUM(buffer->AsClientBuffer(), kImageWidth, kImageHeight,
193 InternalFormat(GetParam()));
194 ASSERT_NE(0u, image_id);
195 ASSERT_TRUE(gl_.decoder()->GetImageManager()->LookupImage(image_id) != NULL);
197 // Bind the image.
198 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id);
200 // Build program, buffers and draw the texture.
201 GLuint vertex_shader =
202 GLTestHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader);
203 GLuint fragment_shader =
204 GLTestHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader);
205 GLuint program = GLTestHelper::SetupProgram(vertex_shader, fragment_shader);
206 ASSERT_NE(0u, program);
207 glUseProgram(program);
209 GLint sampler_location = glGetUniformLocation(program, "a_texture");
210 ASSERT_NE(-1, sampler_location);
211 glUniform1i(sampler_location, 0);
213 GLuint vbo =
214 GLTestHelper::SetupUnitQuad(glGetAttribLocation(program, "a_position"));
215 ASSERT_NE(0u, vbo);
216 glViewport(0, 0, kImageWidth, kImageHeight);
217 glDrawArrays(GL_TRIANGLES, 0, 6);
218 ASSERT_TRUE(glGetError() == GL_NO_ERROR);
220 // Check if pixels match the values that were assigned to the mapped buffer.
221 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, pixel);
222 EXPECT_TRUE(GL_NO_ERROR == glGetError());
224 // Release the image.
225 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id);
227 // Clean up.
228 glDeleteProgram(program);
229 glDeleteShader(vertex_shader);
230 glDeleteShader(fragment_shader);
231 glDeleteBuffers(1, &vbo);
232 glDestroyImageCHROMIUM(image_id);
233 glDeleteTextures(1, &texture_id);
236 INSTANTIATE_TEST_CASE_P(GpuMemoryBufferTests,
237 GpuMemoryBufferTest,
238 ::testing::Values(gfx::BufferFormat::R_8,
239 gfx::BufferFormat::RGBA_4444,
240 gfx::BufferFormat::RGBA_8888,
241 gfx::BufferFormat::BGRA_8888));
243 } // namespace gles2
244 } // namespace gpu