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.
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"
24 using testing::IgnoreResult
;
25 using testing::InvokeWithoutArgs
;
26 using testing::Invoke
;
27 using testing::Return
;
28 using testing::SetArgPointee
;
29 using testing::StrictMock
;
34 static const int kImageWidth
= 32;
35 static const int kImageHeight
= 32;
37 class GpuMemoryBufferTest
38 : public testing::TestWithParam
<gfx::GpuMemoryBuffer::Format
> {
40 void SetUp() override
{
41 GLManager::Options options
;
42 options
.size
= gfx::Size(kImageWidth
, kImageHeight
);
43 gl_
.Initialize(options
);
47 void TearDown() override
{
56 #define SHADER(Src) #Src
59 const char kVertexShader
[] =
61 attribute vec4 a_position
;
62 varying vec2 v_texCoord
;
64 gl_Position
= a_position
;
65 v_texCoord
= vec2((a_position
.x
+ 1.0) * 0.5, (a_position
.y
+ 1.0) * 0.5);
69 const char* kFragmentShader
=
71 precision mediump
float;
72 uniform sampler2D a_texture
;
73 varying vec2 v_texCoord
;
75 gl_FragColor
= texture2D(a_texture
, v_texCoord
);
80 void SetRow(gfx::GpuMemoryBuffer::Format format
,
85 case gfx::GpuMemoryBuffer::R_8
:
86 for (int i
= 0; i
< width
; ++i
)
89 case gfx::GpuMemoryBuffer::RGBA_4444
:
90 for (int i
= 0; i
< width
* 2; i
+= 2) {
91 buffer
[i
+ 0] = (pixel
[1] << 4) | (pixel
[0] & 0xf);
92 buffer
[i
+ 1] = (pixel
[3] << 4) | (pixel
[2] & 0xf);
95 case gfx::GpuMemoryBuffer::RGBA_8888
:
96 for (int i
= 0; i
< width
* 4; i
+= 4) {
97 buffer
[i
+ 0] = pixel
[0];
98 buffer
[i
+ 1] = pixel
[1];
99 buffer
[i
+ 2] = pixel
[2];
100 buffer
[i
+ 3] = pixel
[3];
103 case gfx::GpuMemoryBuffer::BGRA_8888
:
104 for (int i
= 0; i
< width
* 4; i
+= 4) {
105 buffer
[i
+ 0] = pixel
[2];
106 buffer
[i
+ 1] = pixel
[1];
107 buffer
[i
+ 2] = pixel
[0];
108 buffer
[i
+ 3] = pixel
[3];
111 case gfx::GpuMemoryBuffer::ATC
:
112 case gfx::GpuMemoryBuffer::ATCIA
:
113 case gfx::GpuMemoryBuffer::DXT1
:
114 case gfx::GpuMemoryBuffer::DXT5
:
115 case gfx::GpuMemoryBuffer::ETC1
:
116 case gfx::GpuMemoryBuffer::RGBX_8888
:
117 case gfx::GpuMemoryBuffer::YUV_420
:
125 GLenum
InternalFormat(gfx::GpuMemoryBuffer::Format format
) {
127 case gfx::GpuMemoryBuffer::R_8
:
129 case gfx::GpuMemoryBuffer::RGBA_4444
:
130 case gfx::GpuMemoryBuffer::RGBA_8888
:
132 case gfx::GpuMemoryBuffer::BGRA_8888
:
134 case gfx::GpuMemoryBuffer::ATC
:
135 case gfx::GpuMemoryBuffer::ATCIA
:
136 case gfx::GpuMemoryBuffer::DXT1
:
137 case gfx::GpuMemoryBuffer::DXT5
:
138 case gfx::GpuMemoryBuffer::ETC1
:
139 case gfx::GpuMemoryBuffer::RGBX_8888
:
140 case gfx::GpuMemoryBuffer::YUV_420
:
151 // An end to end test that tests the whole GpuMemoryBuffer lifecycle.
152 TEST_P(GpuMemoryBufferTest
, Lifecycle
) {
153 ASSERT_TRUE((GetParam() != gfx::GpuMemoryBuffer::R_8
) ||
154 gl_
.GetCapabilities().texture_rg
);
156 GLuint texture_id
= 0;
157 glGenTextures(1, &texture_id
);
158 ASSERT_NE(0u, texture_id
);
159 glBindTexture(GL_TEXTURE_2D
, texture_id
);
160 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
161 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
162 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
163 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
165 // Create the gpu memory buffer.
166 scoped_ptr
<gfx::GpuMemoryBuffer
> buffer(gl_
.CreateGpuMemoryBuffer(
167 gfx::Size(kImageWidth
, kImageHeight
), GetParam()));
169 // Map buffer for writing.
171 bool rv
= buffer
->Map(&data
);
174 uint8_t* mapped_buffer
= static_cast<uint8_t*>(data
);
175 ASSERT_TRUE(mapped_buffer
!= NULL
);
177 uint8_t pixel
[] = {255u, 0u, 0u, 255u};
179 // Assign a value to each pixel.
181 buffer
->GetStride(&stride
);
182 ASSERT_NE(stride
, 0);
183 for (int y
= 0; y
< kImageHeight
; ++y
)
184 SetRow(GetParam(), mapped_buffer
+ y
* stride
, kImageWidth
, pixel
);
189 // Create the image. This should add the image ID to the ImageManager.
191 glCreateImageCHROMIUM(buffer
->AsClientBuffer(), kImageWidth
, kImageHeight
,
192 InternalFormat(GetParam()));
193 ASSERT_NE(0u, image_id
);
194 ASSERT_TRUE(gl_
.decoder()->GetImageManager()->LookupImage(image_id
) != NULL
);
197 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D
, image_id
);
199 // Build program, buffers and draw the texture.
200 GLuint vertex_shader
=
201 GLTestHelper::LoadShader(GL_VERTEX_SHADER
, kVertexShader
);
202 GLuint fragment_shader
=
203 GLTestHelper::LoadShader(GL_FRAGMENT_SHADER
, kFragmentShader
);
204 GLuint program
= GLTestHelper::SetupProgram(vertex_shader
, fragment_shader
);
205 ASSERT_NE(0u, program
);
206 glUseProgram(program
);
208 GLint sampler_location
= glGetUniformLocation(program
, "a_texture");
209 ASSERT_NE(-1, sampler_location
);
210 glUniform1i(sampler_location
, 0);
213 GLTestHelper::SetupUnitQuad(glGetAttribLocation(program
, "a_position"));
215 glViewport(0, 0, kImageWidth
, kImageHeight
);
216 glDrawArrays(GL_TRIANGLES
, 0, 6);
217 ASSERT_TRUE(glGetError() == GL_NO_ERROR
);
219 // Check if pixels match the values that were assigned to the mapped buffer.
220 GLTestHelper::CheckPixels(0, 0, kImageWidth
, kImageHeight
, 0, pixel
);
221 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
223 // Release the image.
224 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D
, image_id
);
227 glDeleteProgram(program
);
228 glDeleteShader(vertex_shader
);
229 glDeleteShader(fragment_shader
);
230 glDeleteBuffers(1, &vbo
);
231 glDestroyImageCHROMIUM(image_id
);
232 glDeleteTextures(1, &texture_id
);
235 INSTANTIATE_TEST_CASE_P(GpuMemoryBufferTests
,
237 ::testing::Values(gfx::GpuMemoryBuffer::R_8
,
238 gfx::GpuMemoryBuffer::RGBA_4444
,
239 gfx::GpuMemoryBuffer::RGBA_8888
,
240 gfx::GpuMemoryBuffer::BGRA_8888
));