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.
5 #ifndef GL_GLEXT_PROTOTYPES
6 #define GL_GLEXT_PROTOTYPES
10 #include <GLES2/gl2ext.h>
11 #include <GLES2/gl2extchromium.h>
13 #include "gpu/command_buffer/tests/gl_manager.h"
14 #include "gpu/command_buffer/tests/gl_test_utils.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 // A collection of tests that exercise the GL_CHROMIUM_copy_texture extension.
21 class GLCopyTextureCHROMIUMTest
: public testing::Test
{
23 virtual void SetUp() {
24 gl_
.Initialize(GLManager::Options());
26 glGenTextures(2, textures_
);
27 glBindTexture(GL_TEXTURE_2D
, textures_
[1]);
29 // Some drivers (NVidia/SGX) require texture settings to be a certain way or
30 // they won't report FRAMEBUFFER_COMPLETE.
31 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
32 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
33 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
34 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
36 glGenFramebuffers(1, &framebuffer_id_
);
37 glBindFramebuffer(GL_FRAMEBUFFER
, framebuffer_id_
);
38 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
,
42 virtual void TearDown() {
43 glDeleteTextures(2, textures_
);
44 glDeleteFramebuffers(1, &framebuffer_id_
);
50 GLuint framebuffer_id_
;
53 // Test to ensure that the basic functionality of the extension works.
54 TEST_F(GLCopyTextureCHROMIUMTest
, Basic
) {
55 uint8 pixels
[1 * 4] = { 255u, 0u, 0u, 255u };
57 glBindTexture(GL_TEXTURE_2D
, textures_
[0]);
58 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 0, GL_RGBA
, GL_UNSIGNED_BYTE
,
61 glCopyTextureCHROMIUM(GL_TEXTURE_2D
, textures_
[0], textures_
[1], 0, GL_RGBA
,
63 EXPECT_TRUE(glGetError() == GL_NO_ERROR
);
65 // Check the FB is still bound.
67 glGetIntegerv(GL_FRAMEBUFFER_BINDING
, &value
);
69 EXPECT_EQ(framebuffer_id_
, fb_id
);
71 // Check that FB is complete.
72 EXPECT_EQ(static_cast<GLenum
>(GL_FRAMEBUFFER_COMPLETE
),
73 glCheckFramebufferStatus(GL_FRAMEBUFFER
));
75 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, pixels
);
76 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
79 // Test that the extension respects the flip-y pixel storage setting.
80 TEST_F(GLCopyTextureCHROMIUMTest
, FlipY
) {
81 uint8 pixels
[2][2][4];
82 for (int x
= 0; x
< 2; ++x
) {
83 for (int y
= 0; y
< 2; ++y
) {
84 pixels
[y
][x
][0] = x
+ y
;
85 pixels
[y
][x
][1] = x
+ y
;
86 pixels
[y
][x
][2] = x
+ y
;
87 pixels
[y
][x
][3] = 255u;
91 glBindTexture(GL_TEXTURE_2D
, textures_
[0]);
92 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2, 0, GL_RGBA
, GL_UNSIGNED_BYTE
,
95 glPixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM
, GL_TRUE
);
96 glCopyTextureCHROMIUM(GL_TEXTURE_2D
, textures_
[0], textures_
[1], 0, GL_RGBA
,
98 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
100 uint8 copied_pixels
[2][2][4] = {{{0}}};
101 glReadPixels(0, 0, 2, 2, GL_RGBA
, GL_UNSIGNED_BYTE
, copied_pixels
);
102 for (int x
= 0; x
< 2; ++x
) {
103 for (int y
= 0; y
< 2; ++y
)
104 EXPECT_EQ(pixels
[1-y
][x
][0], copied_pixels
[y
][x
][0]);
107 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
110 // Test that the extension respects the GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM
112 TEST_F(GLCopyTextureCHROMIUMTest
, PremultiplyAlpha
) {
113 uint8 pixels
[1 * 4] = { 2, 2, 2, 128 };
115 glBindTexture(GL_TEXTURE_2D
, textures_
[0]);
116 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 0, GL_RGBA
, GL_UNSIGNED_BYTE
,
119 glPixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM
, GL_TRUE
);
120 glCopyTextureCHROMIUM(GL_TEXTURE_2D
, textures_
[0], textures_
[1], 0, GL_RGBA
,
122 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
124 uint8 copied_pixels
[1 * 4] = {0};
125 glReadPixels(0, 0, 1, 1, GL_RGBA
, GL_UNSIGNED_BYTE
, copied_pixels
);
126 EXPECT_EQ(1u, copied_pixels
[0]);
127 EXPECT_EQ(1u, copied_pixels
[1]);
128 EXPECT_EQ(1u, copied_pixels
[2]);
129 EXPECT_EQ(128u, copied_pixels
[3]);
131 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
134 // Test that the extension respects the GL_UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM
136 TEST_F(GLCopyTextureCHROMIUMTest
, UnpremultiplyAlpha
) {
137 uint8 pixels
[1 * 4] = { 16, 16, 16, 128 };
139 glBindTexture(GL_TEXTURE_2D
, textures_
[0]);
140 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 0, GL_RGBA
, GL_UNSIGNED_BYTE
,
143 glPixelStorei(GL_UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM
, GL_TRUE
);
144 glCopyTextureCHROMIUM(GL_TEXTURE_2D
, textures_
[0], textures_
[1], 0, GL_RGBA
,
146 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
148 uint8 copied_pixels
[1 * 4] = {0};
149 glReadPixels(0, 0, 1, 1, GL_RGBA
, GL_UNSIGNED_BYTE
, copied_pixels
);
150 EXPECT_EQ(32u, copied_pixels
[0]);
151 EXPECT_EQ(32u, copied_pixels
[1]);
152 EXPECT_EQ(32u, copied_pixels
[2]);
153 EXPECT_EQ(128u, copied_pixels
[3]);
155 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
158 TEST_F(GLCopyTextureCHROMIUMTest
, FlipYAndPremultiplyAlpha
) {
159 uint8 pixels
[2][2][4];
160 for (int x
= 0; x
< 2; ++x
) {
161 for (int y
= 0; y
< 2; ++y
) {
162 uint8 color
= 16 * x
+ 16 * y
;
163 pixels
[y
][x
][0] = color
;
164 pixels
[y
][x
][1] = color
;
165 pixels
[y
][x
][2] = color
;
166 pixels
[y
][x
][3] = 128u;
170 glBindTexture(GL_TEXTURE_2D
, textures_
[0]);
171 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2, 0, GL_RGBA
, GL_UNSIGNED_BYTE
,
174 glPixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM
, GL_TRUE
);
175 glPixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM
, GL_TRUE
);
176 glCopyTextureCHROMIUM(GL_TEXTURE_2D
, textures_
[0], textures_
[1], 0, GL_RGBA
,
178 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
180 uint8 copied_pixels
[2][2][4] = {{{0}}};
181 glReadPixels(0, 0, 2, 2, GL_RGBA
, GL_UNSIGNED_BYTE
, copied_pixels
);
182 for (int x
= 0; x
< 2; ++x
) {
183 for (int y
= 0; y
< 2; ++y
) {
184 EXPECT_EQ(pixels
[1-y
][x
][0] / 2, copied_pixels
[y
][x
][0]);
185 EXPECT_EQ(pixels
[1-y
][x
][1] / 2, copied_pixels
[y
][x
][1]);
186 EXPECT_EQ(pixels
[1-y
][x
][2] / 2, copied_pixels
[y
][x
][2]);
187 EXPECT_EQ(pixels
[1-y
][x
][3], copied_pixels
[y
][x
][3]);
191 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
194 TEST_F(GLCopyTextureCHROMIUMTest
, FlipYAndUnpremultiplyAlpha
) {
195 uint8 pixels
[2][2][4];
196 for (int x
= 0; x
< 2; ++x
) {
197 for (int y
= 0; y
< 2; ++y
) {
198 uint8 color
= 16 * x
+ 16 * y
;
199 pixels
[y
][x
][0] = color
;
200 pixels
[y
][x
][1] = color
;
201 pixels
[y
][x
][2] = color
;
202 pixels
[y
][x
][3] = 128u;
206 glBindTexture(GL_TEXTURE_2D
, textures_
[0]);
207 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2, 0, GL_RGBA
, GL_UNSIGNED_BYTE
,
210 glPixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM
, GL_TRUE
);
211 glPixelStorei(GL_UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM
, GL_TRUE
);
212 glCopyTextureCHROMIUM(GL_TEXTURE_2D
, textures_
[0], textures_
[1], 0, GL_RGBA
,
214 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
216 uint8 copied_pixels
[2][2][4] = {{{0}}};
217 glReadPixels(0, 0, 2, 2, GL_RGBA
, GL_UNSIGNED_BYTE
, copied_pixels
);
218 for (int x
= 0; x
< 2; ++x
) {
219 for (int y
= 0; y
< 2; ++y
) {
220 EXPECT_EQ(pixels
[1-y
][x
][0] * 2, copied_pixels
[y
][x
][0]);
221 EXPECT_EQ(pixels
[1-y
][x
][1] * 2, copied_pixels
[y
][x
][1]);
222 EXPECT_EQ(pixels
[1-y
][x
][2] * 2, copied_pixels
[y
][x
][2]);
223 EXPECT_EQ(pixels
[1-y
][x
][3], copied_pixels
[y
][x
][3]);
227 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
232 void glEnableDisable(GLint param
, GLboolean value
) {
239 } // unnamed namespace
241 // Validate that some basic GL state is not touched upon execution of
243 TEST_F(GLCopyTextureCHROMIUMTest
, BasicStatePreservation
) {
244 uint8 pixels
[1 * 4] = { 255u, 0u, 0u, 255u };
246 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
248 glBindTexture(GL_TEXTURE_2D
, textures_
[0]);
249 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 0, GL_RGBA
, GL_UNSIGNED_BYTE
,
252 GLboolean reference_settings
[2] = { GL_TRUE
, GL_FALSE
};
253 for (int x
= 0; x
< 2; ++x
) {
254 GLboolean setting
= reference_settings
[x
];
255 glEnableDisable(GL_DEPTH_TEST
, setting
);
256 glEnableDisable(GL_SCISSOR_TEST
, setting
);
257 glEnableDisable(GL_STENCIL_TEST
, setting
);
258 glEnableDisable(GL_CULL_FACE
, setting
);
259 glEnableDisable(GL_BLEND
, setting
);
260 glColorMask(setting
, setting
, setting
, setting
);
261 glDepthMask(setting
);
263 glActiveTexture(GL_TEXTURE1
+ x
);
265 glCopyTextureCHROMIUM(GL_TEXTURE_2D
, textures_
[0], textures_
[1], 0,
266 GL_RGBA
, GL_UNSIGNED_BYTE
);
267 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
269 EXPECT_EQ(setting
, glIsEnabled(GL_DEPTH_TEST
));
270 EXPECT_EQ(setting
, glIsEnabled(GL_SCISSOR_TEST
));
271 EXPECT_EQ(setting
, glIsEnabled(GL_STENCIL_TEST
));
272 EXPECT_EQ(setting
, glIsEnabled(GL_CULL_FACE
));
273 EXPECT_EQ(setting
, glIsEnabled(GL_BLEND
));
275 GLboolean bool_array
[4] = { GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
};
276 glGetBooleanv(GL_DEPTH_WRITEMASK
, bool_array
);
277 EXPECT_EQ(setting
, bool_array
[0]);
279 bool_array
[0] = GL_FALSE
;
280 glGetBooleanv(GL_COLOR_WRITEMASK
, bool_array
);
281 EXPECT_EQ(setting
, bool_array
[0]);
282 EXPECT_EQ(setting
, bool_array
[1]);
283 EXPECT_EQ(setting
, bool_array
[2]);
284 EXPECT_EQ(setting
, bool_array
[3]);
286 GLint active_texture
= 0;
287 glGetIntegerv(GL_ACTIVE_TEXTURE
, &active_texture
);
288 EXPECT_EQ(GL_TEXTURE1
+ x
, active_texture
);
291 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
294 // Verify that invocation of the extension does not modify the bound
296 TEST_F(GLCopyTextureCHROMIUMTest
, TextureStatePreserved
) {
297 // Setup the texture used for the extension invocation.
298 uint8 pixels
[1 * 4] = { 255u, 0u, 0u, 255u };
299 glBindTexture(GL_TEXTURE_2D
, textures_
[0]);
300 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 0, GL_RGBA
, GL_UNSIGNED_BYTE
,
303 GLuint texture_ids
[2];
304 glGenTextures(2, texture_ids
);
306 glActiveTexture(GL_TEXTURE0
);
307 glBindTexture(GL_TEXTURE_2D
, texture_ids
[0]);
309 glActiveTexture(GL_TEXTURE1
);
310 glBindTexture(GL_TEXTURE_2D
, texture_ids
[1]);
312 glCopyTextureCHROMIUM(GL_TEXTURE_2D
, textures_
[0], textures_
[1], 0,
313 GL_RGBA
, GL_UNSIGNED_BYTE
);
314 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
316 GLint active_texture
= 0;
317 glGetIntegerv(GL_ACTIVE_TEXTURE
, &active_texture
);
318 EXPECT_EQ(GL_TEXTURE1
, active_texture
);
320 GLint bound_texture
= 0;
321 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &bound_texture
);
322 EXPECT_EQ(texture_ids
[1], static_cast<GLuint
>(bound_texture
));
323 glBindTexture(GL_TEXTURE_2D
, 0);
326 glActiveTexture(GL_TEXTURE0
);
327 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &bound_texture
);
328 EXPECT_EQ(texture_ids
[0], static_cast<GLuint
>(bound_texture
));
329 glBindTexture(GL_TEXTURE_2D
, 0);
331 glDeleteTextures(2, texture_ids
);
333 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
336 // Verify that invocation of the extension does not perturb the currently
338 TEST_F(GLCopyTextureCHROMIUMTest
, FBOStatePreserved
) {
339 // Setup the texture used for the extension invocation.
340 uint8 pixels
[1 * 4] = { 255u, 0u, 0u, 255u };
341 glBindTexture(GL_TEXTURE_2D
, textures_
[0]);
342 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 0, GL_RGBA
, GL_UNSIGNED_BYTE
,
346 glGenTextures(1, &texture_id
);
347 glBindTexture(GL_TEXTURE_2D
, texture_id
);
348 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 0, GL_RGBA
, GL_UNSIGNED_BYTE
,
351 GLuint renderbuffer_id
;
352 glGenRenderbuffers(1, &renderbuffer_id
);
353 glBindRenderbuffer(GL_RENDERBUFFER
, renderbuffer_id
);
354 glRenderbufferStorage(GL_RENDERBUFFER
, GL_DEPTH_COMPONENT16
, 1, 1);
356 GLuint framebuffer_id
;
357 glGenFramebuffers(1, &framebuffer_id
);
358 glBindFramebuffer(GL_FRAMEBUFFER
, framebuffer_id
);
359 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
,
361 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_DEPTH_ATTACHMENT
,
362 GL_RENDERBUFFER
, renderbuffer_id
);
364 GL_FRAMEBUFFER_COMPLETE
== glCheckFramebufferStatus(GL_FRAMEBUFFER
));
366 // Test that we can write to the bound framebuffer
367 uint8 expected_color
[4] = { 255u, 255u, 0, 255u };
368 glClearColor(1.0, 1.0, 0, 1.0);
369 glClear(GL_COLOR_BUFFER_BIT
);
370 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color
);
372 glCopyTextureCHROMIUM(GL_TEXTURE_2D
, textures_
[0], textures_
[1], 0,
373 GL_RGBA
, GL_UNSIGNED_BYTE
);
374 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
376 EXPECT_TRUE(glIsFramebuffer(framebuffer_id
));
378 // Ensure that reading from the framebuffer produces correct pixels.
379 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color
);
381 uint8 expected_color2
[4] = { 255u, 0, 255u, 255u };
382 glClearColor(1.0, 0, 1.0, 1.0);
383 glClear(GL_COLOR_BUFFER_BIT
);
384 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color2
);
387 glGetIntegerv(GL_FRAMEBUFFER_BINDING
, &bound_fbo
);
388 EXPECT_EQ(framebuffer_id
, static_cast<GLuint
>(bound_fbo
));
390 GLint fbo_params
= 0;
391 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
392 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
,
394 EXPECT_EQ(GL_TEXTURE
, fbo_params
);
397 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
398 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
,
400 EXPECT_EQ(texture_id
, static_cast<GLuint
>(fbo_params
));
403 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER
, GL_DEPTH_ATTACHMENT
,
404 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
,
406 EXPECT_EQ(GL_RENDERBUFFER
, fbo_params
);
409 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER
, GL_DEPTH_ATTACHMENT
,
410 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
,
412 EXPECT_EQ(renderbuffer_id
, static_cast<GLuint
>(fbo_params
));
414 glDeleteRenderbuffers(1, &renderbuffer_id
);
415 glDeleteTextures(1, &texture_id
);
416 glDeleteFramebuffers(1, &framebuffer_id
);
418 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
421 TEST_F(GLCopyTextureCHROMIUMTest
, ProgramStatePreservation
) {
422 // unbind the one created in setup.
423 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
424 glBindTexture(GL_TEXTURE_2D
, 0);
427 GLManager::Options options
;
428 options
.size
= gfx::Size(16, 16);
429 options
.share_group_manager
= &gl_
;
430 gl2
.Initialize(options
);
433 static const char* v_shader_str
=
434 "attribute vec4 g_Position;\n"
437 " gl_Position = g_Position;\n"
439 static const char* f_shader_str
=
440 "precision mediump float;\n"
443 " gl_FragColor = vec4(0,1,0,1);\n"
446 GLuint program
= GLTestHelper::LoadProgram(v_shader_str
, f_shader_str
);
447 glUseProgram(program
);
448 GLuint position_loc
= glGetAttribLocation(program
, "g_Position");
451 // Delete program from other context.
453 glDeleteProgram(program
);
454 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
457 // Program should still be usable on this context.
460 GLTestHelper::SetupUnitQuad(position_loc
);
462 // test using program before
463 uint8 expected
[] = { 0, 255, 0, 255, };
464 uint8 zero
[] = { 0, 0, 0, 0, };
465 glClear(GL_COLOR_BUFFER_BIT
);
466 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, zero
));
467 glDrawArrays(GL_TRIANGLES
, 0, 6);
468 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected
));
470 // Call copyTextureCHROMIUM
471 uint8 pixels
[1 * 4] = { 255u, 0u, 0u, 255u };
472 glBindTexture(GL_TEXTURE_2D
, textures_
[0]);
473 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 1, 1, 0, GL_RGBA
, GL_UNSIGNED_BYTE
,
475 glCopyTextureCHROMIUM(GL_TEXTURE_2D
, textures_
[0], textures_
[1], 0, GL_RGBA
,
478 // test using program after
479 glClear(GL_COLOR_BUFFER_BIT
);
480 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, zero
));
481 glDrawArrays(GL_TRIANGLES
, 0, 6);
482 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected
));
484 EXPECT_TRUE(GL_NO_ERROR
== glGetError());