Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / gpu / command_buffer / tests / gl_copy_texture_CHROMIUM_unittest.cc
bloba256fff89ef563a2bf3c393d475b94187e2aeaa4
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
7 #endif
9 #include <GLES2/gl2.h>
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"
18 namespace gpu {
20 namespace {
21 enum CopyType { TexImage, TexSubImage };
22 const CopyType kCopyTypes[] = {
23 TexImage,
24 TexSubImage,
28 // A collection of tests that exercise the GL_CHROMIUM_copy_texture extension.
29 class GLCopyTextureCHROMIUMTest
30 : public testing::Test,
31 public ::testing::WithParamInterface<CopyType> {
32 protected:
33 void SetUp() override {
34 gl_.Initialize(GLManager::Options());
36 glGenTextures(2, textures_);
37 glBindTexture(GL_TEXTURE_2D, textures_[1]);
39 // Some drivers (NVidia/SGX) require texture settings to be a certain way or
40 // they won't report FRAMEBUFFER_COMPLETE.
41 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
42 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
46 glGenFramebuffers(1, &framebuffer_id_);
47 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_);
48 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
49 textures_[1], 0);
52 void TearDown() override {
53 glDeleteTextures(2, textures_);
54 glDeleteFramebuffers(1, &framebuffer_id_);
55 gl_.Destroy();
58 GLManager gl_;
59 GLuint textures_[2];
60 GLuint framebuffer_id_;
63 INSTANTIATE_TEST_CASE_P(CopyType,
64 GLCopyTextureCHROMIUMTest,
65 ::testing::ValuesIn(kCopyTypes));
67 // Test to ensure that the basic functionality of the extension works.
68 TEST_P(GLCopyTextureCHROMIUMTest, Basic) {
69 CopyType copy_type = GetParam();
70 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u };
72 glBindTexture(GL_TEXTURE_2D, textures_[0]);
73 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
74 pixels);
76 if (copy_type == TexImage) {
77 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
78 GL_UNSIGNED_BYTE, false, false, false);
79 } else {
80 glBindTexture(GL_TEXTURE_2D, textures_[1]);
81 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
82 nullptr);
84 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0, 0,
85 0, 1, 1, false, false, false);
87 EXPECT_TRUE(glGetError() == GL_NO_ERROR);
89 // Check the FB is still bound.
90 GLint value = 0;
91 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &value);
92 GLuint fb_id = value;
93 EXPECT_EQ(framebuffer_id_, fb_id);
95 // Check that FB is complete.
96 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
97 glCheckFramebufferStatus(GL_FRAMEBUFFER));
99 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, pixels);
100 EXPECT_TRUE(GL_NO_ERROR == glGetError());
103 TEST_P(GLCopyTextureCHROMIUMTest, ImmutableTexture) {
104 if (!GLTestHelper::HasExtension("GL_EXT_texture_storage")) {
105 LOG(INFO) << "GL_EXT_texture_storage not supported. Skipping test...";
106 return;
108 CopyType copy_type = GetParam();
110 uint8 pixels[1 * 4] = {255u, 0u, 0u, 255u};
112 glBindTexture(GL_TEXTURE_2D, textures_[0]);
113 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8_OES, 1, 1);
114 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
115 pixels);
117 glBindTexture(GL_TEXTURE_2D, textures_[1]);
118 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8_OES, 1, 1);
119 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
120 textures_[1], 0);
121 EXPECT_TRUE(glGetError() == GL_NO_ERROR);
123 if (copy_type == TexImage) {
124 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
125 GL_UNSIGNED_BYTE, false, false, false);
126 EXPECT_TRUE(glGetError() == GL_INVALID_OPERATION);
127 } else {
128 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0, 0,
129 0, 1, 1, false, false, false);
130 EXPECT_TRUE(glGetError() == GL_NO_ERROR);
132 // Check the FB is still bound.
133 GLint value = 0;
134 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &value);
135 GLuint fb_id = value;
136 EXPECT_EQ(framebuffer_id_, fb_id);
138 // Check that FB is complete.
139 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
140 glCheckFramebufferStatus(GL_FRAMEBUFFER));
142 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, pixels);
143 EXPECT_TRUE(GL_NO_ERROR == glGetError());
147 TEST_P(GLCopyTextureCHROMIUMTest, InternalFormat) {
148 CopyType copy_type = GetParam();
149 GLint src_formats[] = {GL_ALPHA, GL_RGB, GL_RGBA,
150 GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_BGRA_EXT};
151 GLint dest_formats[] = {GL_RGB, GL_RGBA};
153 for (size_t src_index = 0; src_index < arraysize(src_formats); src_index++) {
154 for (size_t dest_index = 0; dest_index < arraysize(dest_formats);
155 dest_index++) {
156 glBindTexture(GL_TEXTURE_2D, textures_[0]);
157 glTexImage2D(GL_TEXTURE_2D, 0, src_formats[src_index], 1, 1, 0,
158 src_formats[src_index], GL_UNSIGNED_BYTE, nullptr);
159 EXPECT_TRUE(GL_NO_ERROR == glGetError());
161 if (copy_type == TexImage) {
162 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1],
163 dest_formats[dest_index], GL_UNSIGNED_BYTE,
164 false, false, false);
165 } else {
166 glBindTexture(GL_TEXTURE_2D, textures_[1]);
167 glTexImage2D(GL_TEXTURE_2D, 0, dest_formats[dest_index], 1, 1, 0,
168 dest_formats[dest_index], GL_UNSIGNED_BYTE, nullptr);
169 EXPECT_TRUE(GL_NO_ERROR == glGetError());
171 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0,
172 0, 0, 0, 1, 1, false, false, false);
175 EXPECT_TRUE(GL_NO_ERROR == glGetError()) << "src_index:" << src_index
176 << " dest_index:" << dest_index;
181 TEST_P(GLCopyTextureCHROMIUMTest, InternalFormatNotSupported) {
182 CopyType copy_type = GetParam();
183 glBindTexture(GL_TEXTURE_2D, textures_[0]);
184 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
185 nullptr);
186 EXPECT_TRUE(GL_NO_ERROR == glGetError());
188 // Check unsupported format reports error.
189 GLint unsupported_dest_formats[] = {GL_ALPHA, GL_LUMINANCE,
190 GL_LUMINANCE_ALPHA};
191 for (size_t dest_index = 0; dest_index < arraysize(unsupported_dest_formats);
192 dest_index++) {
193 if (copy_type == TexImage) {
194 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1],
195 unsupported_dest_formats[dest_index],
196 GL_UNSIGNED_BYTE, false, false, false);
197 } else {
198 glBindTexture(GL_TEXTURE_2D, textures_[1]);
199 glTexImage2D(GL_TEXTURE_2D, 0, unsupported_dest_formats[dest_index], 1, 1,
200 0, unsupported_dest_formats[dest_index], GL_UNSIGNED_BYTE,
201 nullptr);
202 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0,
203 0, 0, 1, 1, false, false, false);
205 EXPECT_TRUE(GL_INVALID_OPERATION == glGetError())
206 << "dest_index:" << dest_index;
210 // Test to ensure that the destination texture is redefined if the properties
211 // are different.
212 TEST_F(GLCopyTextureCHROMIUMTest, RedefineDestinationTexture) {
213 uint8 pixels[4 * 4] = {255u, 0u, 0u, 255u, 255u, 0u, 0u, 255u,
214 255u, 0u, 0u, 255u, 255u, 0u, 0u, 255u};
216 glBindTexture(GL_TEXTURE_2D, textures_[0]);
217 glTexImage2D(
218 GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
220 glBindTexture(GL_TEXTURE_2D, textures_[1]);
221 glTexImage2D(GL_TEXTURE_2D,
223 GL_BGRA_EXT,
227 GL_BGRA_EXT,
228 GL_UNSIGNED_BYTE,
229 pixels);
230 EXPECT_TRUE(GL_NO_ERROR == glGetError());
232 // GL_INVALID_OPERATION due to "intrinsic format" != "internal format".
233 glTexSubImage2D(
234 GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
235 EXPECT_TRUE(GL_INVALID_OPERATION == glGetError());
236 // GL_INVALID_VALUE due to bad dimensions.
237 glTexSubImage2D(
238 GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
239 EXPECT_TRUE(GL_INVALID_VALUE == glGetError());
241 // If the dest texture has different properties, glCopyTextureCHROMIUM()
242 // redefines them.
243 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
244 GL_UNSIGNED_BYTE, false, false, false);
245 EXPECT_TRUE(GL_NO_ERROR == glGetError());
247 // glTexSubImage2D() succeeds because textures_[1] is redefined into 2x2
248 // dimension and GL_RGBA format.
249 glBindTexture(GL_TEXTURE_2D, textures_[1]);
250 glTexSubImage2D(
251 GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
252 EXPECT_TRUE(GL_NO_ERROR == glGetError());
254 // Check the FB is still bound.
255 GLint value = 0;
256 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &value);
257 GLuint fb_id = value;
258 EXPECT_EQ(framebuffer_id_, fb_id);
260 // Check that FB is complete.
261 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
262 glCheckFramebufferStatus(GL_FRAMEBUFFER));
264 GLTestHelper::CheckPixels(1, 1, 1, 1, 0, &pixels[12]);
265 EXPECT_TRUE(GL_NO_ERROR == glGetError());
268 namespace {
270 void glEnableDisable(GLint param, GLboolean value) {
271 if (value)
272 glEnable(param);
273 else
274 glDisable(param);
277 } // unnamed namespace
279 // Validate that some basic GL state is not touched upon execution of
280 // the extension.
281 TEST_P(GLCopyTextureCHROMIUMTest, BasicStatePreservation) {
282 CopyType copy_type = GetParam();
283 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u };
285 glBindFramebuffer(GL_FRAMEBUFFER, 0);
287 glBindTexture(GL_TEXTURE_2D, textures_[0]);
288 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
289 pixels);
291 if (copy_type == TexSubImage) {
292 glBindTexture(GL_TEXTURE_2D, textures_[1]);
293 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
294 nullptr);
297 GLboolean reference_settings[2] = { GL_TRUE, GL_FALSE };
298 for (int x = 0; x < 2; ++x) {
299 GLboolean setting = reference_settings[x];
300 glEnableDisable(GL_DEPTH_TEST, setting);
301 glEnableDisable(GL_SCISSOR_TEST, setting);
302 glEnableDisable(GL_STENCIL_TEST, setting);
303 glEnableDisable(GL_CULL_FACE, setting);
304 glEnableDisable(GL_BLEND, setting);
305 glColorMask(setting, setting, setting, setting);
306 glDepthMask(setting);
308 glActiveTexture(GL_TEXTURE1 + x);
310 if (copy_type == TexImage) {
311 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
312 GL_UNSIGNED_BYTE, false, false, false);
313 } else {
314 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0,
315 0, 0, 1, 1, false, false, false);
317 EXPECT_TRUE(GL_NO_ERROR == glGetError());
319 EXPECT_EQ(setting, glIsEnabled(GL_DEPTH_TEST));
320 EXPECT_EQ(setting, glIsEnabled(GL_SCISSOR_TEST));
321 EXPECT_EQ(setting, glIsEnabled(GL_STENCIL_TEST));
322 EXPECT_EQ(setting, glIsEnabled(GL_CULL_FACE));
323 EXPECT_EQ(setting, glIsEnabled(GL_BLEND));
325 GLboolean bool_array[4] = { GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE };
326 glGetBooleanv(GL_DEPTH_WRITEMASK, bool_array);
327 EXPECT_EQ(setting, bool_array[0]);
329 bool_array[0] = GL_FALSE;
330 glGetBooleanv(GL_COLOR_WRITEMASK, bool_array);
331 EXPECT_EQ(setting, bool_array[0]);
332 EXPECT_EQ(setting, bool_array[1]);
333 EXPECT_EQ(setting, bool_array[2]);
334 EXPECT_EQ(setting, bool_array[3]);
336 GLint active_texture = 0;
337 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
338 EXPECT_EQ(GL_TEXTURE1 + x, active_texture);
341 EXPECT_TRUE(GL_NO_ERROR == glGetError());
344 // Verify that invocation of the extension does not modify the bound
345 // texture state.
346 TEST_P(GLCopyTextureCHROMIUMTest, TextureStatePreserved) {
347 CopyType copy_type = GetParam();
348 // Setup the texture used for the extension invocation.
349 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u };
350 glBindTexture(GL_TEXTURE_2D, textures_[0]);
351 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
352 pixels);
354 if (copy_type == TexSubImage) {
355 glBindTexture(GL_TEXTURE_2D, textures_[1]);
356 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
357 nullptr);
360 GLuint texture_ids[2];
361 glGenTextures(2, texture_ids);
363 glActiveTexture(GL_TEXTURE0);
364 glBindTexture(GL_TEXTURE_2D, texture_ids[0]);
366 glActiveTexture(GL_TEXTURE1);
367 glBindTexture(GL_TEXTURE_2D, texture_ids[1]);
369 if (copy_type == TexImage) {
370 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
371 GL_UNSIGNED_BYTE, false, false, false);
372 } else {
373 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0, 0,
374 0, 1, 1, false, false, false);
376 EXPECT_TRUE(GL_NO_ERROR == glGetError());
378 GLint active_texture = 0;
379 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
380 EXPECT_EQ(GL_TEXTURE1, active_texture);
382 GLint bound_texture = 0;
383 glGetIntegerv(GL_TEXTURE_BINDING_2D, &bound_texture);
384 EXPECT_EQ(texture_ids[1], static_cast<GLuint>(bound_texture));
385 glBindTexture(GL_TEXTURE_2D, 0);
387 bound_texture = 0;
388 glActiveTexture(GL_TEXTURE0);
389 glGetIntegerv(GL_TEXTURE_BINDING_2D, &bound_texture);
390 EXPECT_EQ(texture_ids[0], static_cast<GLuint>(bound_texture));
391 glBindTexture(GL_TEXTURE_2D, 0);
393 glDeleteTextures(2, texture_ids);
395 EXPECT_TRUE(GL_NO_ERROR == glGetError());
398 // Verify that invocation of the extension does not perturb the currently
399 // bound FBO state.
400 TEST_P(GLCopyTextureCHROMIUMTest, FBOStatePreserved) {
401 CopyType copy_type = GetParam();
402 // Setup the texture used for the extension invocation.
403 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u };
404 glBindTexture(GL_TEXTURE_2D, textures_[0]);
405 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
406 pixels);
408 if (copy_type == TexSubImage) {
409 glBindTexture(GL_TEXTURE_2D, textures_[1]);
410 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
411 nullptr);
414 GLuint texture_id;
415 glGenTextures(1, &texture_id);
416 glBindTexture(GL_TEXTURE_2D, texture_id);
417 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
420 GLuint renderbuffer_id;
421 glGenRenderbuffers(1, &renderbuffer_id);
422 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer_id);
423 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 1, 1);
425 GLuint framebuffer_id;
426 glGenFramebuffers(1, &framebuffer_id);
427 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
428 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
429 texture_id, 0);
430 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
431 GL_RENDERBUFFER, renderbuffer_id);
432 EXPECT_TRUE(
433 GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER));
435 // Test that we can write to the bound framebuffer
436 uint8 expected_color[4] = { 255u, 255u, 0, 255u };
437 glClearColor(1.0, 1.0, 0, 1.0);
438 glClear(GL_COLOR_BUFFER_BIT);
439 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color);
441 if (copy_type == TexImage) {
442 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
443 GL_UNSIGNED_BYTE, false, false, false);
444 } else {
445 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0, 0,
446 0, 1, 1, false, false, false);
448 EXPECT_TRUE(GL_NO_ERROR == glGetError());
450 EXPECT_TRUE(glIsFramebuffer(framebuffer_id));
452 // Ensure that reading from the framebuffer produces correct pixels.
453 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color);
455 uint8 expected_color2[4] = { 255u, 0, 255u, 255u };
456 glClearColor(1.0, 0, 1.0, 1.0);
457 glClear(GL_COLOR_BUFFER_BIT);
458 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color2);
460 GLint bound_fbo = 0;
461 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &bound_fbo);
462 EXPECT_EQ(framebuffer_id, static_cast<GLuint>(bound_fbo));
464 GLint fbo_params = 0;
465 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
466 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
467 &fbo_params);
468 EXPECT_EQ(GL_TEXTURE, fbo_params);
470 fbo_params = 0;
471 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
472 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
473 &fbo_params);
474 EXPECT_EQ(texture_id, static_cast<GLuint>(fbo_params));
476 fbo_params = 0;
477 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
478 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
479 &fbo_params);
480 EXPECT_EQ(GL_RENDERBUFFER, fbo_params);
482 fbo_params = 0;
483 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
484 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
485 &fbo_params);
486 EXPECT_EQ(renderbuffer_id, static_cast<GLuint>(fbo_params));
488 glDeleteRenderbuffers(1, &renderbuffer_id);
489 glDeleteTextures(1, &texture_id);
490 glDeleteFramebuffers(1, &framebuffer_id);
492 EXPECT_TRUE(GL_NO_ERROR == glGetError());
495 TEST_P(GLCopyTextureCHROMIUMTest, ProgramStatePreservation) {
496 CopyType copy_type = GetParam();
497 // unbind the one created in setup.
498 glBindFramebuffer(GL_FRAMEBUFFER, 0);
499 glBindTexture(GL_TEXTURE_2D, 0);
501 GLManager gl2;
502 GLManager::Options options;
503 options.size = gfx::Size(16, 16);
504 options.share_group_manager = &gl_;
505 gl2.Initialize(options);
506 gl_.MakeCurrent();
508 static const char* v_shader_str =
509 "attribute vec4 g_Position;\n"
510 "void main()\n"
511 "{\n"
512 " gl_Position = g_Position;\n"
513 "}\n";
514 static const char* f_shader_str =
515 "precision mediump float;\n"
516 "void main()\n"
517 "{\n"
518 " gl_FragColor = vec4(0,1,0,1);\n"
519 "}\n";
521 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
522 glUseProgram(program);
523 GLuint position_loc = glGetAttribLocation(program, "g_Position");
524 glFlush();
526 // Delete program from other context.
527 gl2.MakeCurrent();
528 glDeleteProgram(program);
529 EXPECT_TRUE(GL_NO_ERROR == glGetError());
530 glFlush();
532 // Program should still be usable on this context.
533 gl_.MakeCurrent();
535 GLTestHelper::SetupUnitQuad(position_loc);
537 // test using program before
538 uint8 expected[] = { 0, 255, 0, 255, };
539 uint8 zero[] = { 0, 0, 0, 0, };
540 glClear(GL_COLOR_BUFFER_BIT);
541 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, zero));
542 glDrawArrays(GL_TRIANGLES, 0, 6);
543 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected));
545 // Call copyTextureCHROMIUM
546 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u };
547 glBindTexture(GL_TEXTURE_2D, textures_[0]);
548 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
549 pixels);
550 if (copy_type == TexImage) {
551 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
552 GL_UNSIGNED_BYTE, false, false, false);
553 } else {
554 glBindTexture(GL_TEXTURE_2D, textures_[1]);
555 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
556 nullptr);
557 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0, 0,
558 0, 1, 1, false, false, false);
561 // test using program after
562 glClear(GL_COLOR_BUFFER_BIT);
563 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, zero));
564 glDrawArrays(GL_TRIANGLES, 0, 6);
565 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected));
567 EXPECT_TRUE(GL_NO_ERROR == glGetError());
569 gl2.MakeCurrent();
570 gl2.Destroy();
571 gl_.MakeCurrent();
574 // Test that glCopyTextureCHROMIUM doesn't leak uninitialized textures.
575 TEST_P(GLCopyTextureCHROMIUMTest, UninitializedSource) {
576 CopyType copy_type = GetParam();
577 const GLsizei kWidth = 64, kHeight = 64;
578 glBindTexture(GL_TEXTURE_2D, textures_[0]);
579 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kWidth, kHeight, 0, GL_RGBA,
580 GL_UNSIGNED_BYTE, nullptr);
582 if (copy_type == TexImage) {
583 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
584 GL_UNSIGNED_BYTE, false, false, false);
585 } else {
586 glBindTexture(GL_TEXTURE_2D, textures_[1]);
587 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kWidth, kHeight, 0, GL_RGBA,
588 GL_UNSIGNED_BYTE, nullptr);
589 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0, 0,
590 0, kWidth, kHeight, false, false, false);
592 EXPECT_TRUE(GL_NO_ERROR == glGetError());
594 uint8 pixels[kHeight][kWidth][4] = {{{1}}};
595 glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
596 for (int x = 0; x < kWidth; ++x) {
597 for (int y = 0; y < kHeight; ++y) {
598 EXPECT_EQ(0, pixels[y][x][0]);
599 EXPECT_EQ(0, pixels[y][x][1]);
600 EXPECT_EQ(0, pixels[y][x][2]);
601 EXPECT_EQ(0, pixels[y][x][3]);
605 EXPECT_TRUE(GL_NO_ERROR == glGetError());
608 TEST_F(GLCopyTextureCHROMIUMTest, CopySubTextureDimension) {
609 glBindTexture(GL_TEXTURE_2D, textures_[0]);
610 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
611 nullptr);
613 glBindTexture(GL_TEXTURE_2D, textures_[1]);
614 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 3, 0, GL_RGBA, GL_UNSIGNED_BYTE,
615 nullptr);
617 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 1, 1, 0,
618 0, 1, 1, false, false, false);
619 EXPECT_TRUE(GL_NO_ERROR == glGetError());
621 // xoffset < 0
622 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], -1, 1, 0,
623 0, 1, 1, false, false, false);
624 EXPECT_TRUE(glGetError() == GL_INVALID_VALUE);
626 // x < 0
627 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 1, 1, -1,
628 0, 1, 1, false, false, false);
629 EXPECT_TRUE(glGetError() == GL_INVALID_VALUE);
631 // xoffset + width > dest_width
632 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 2, 2, 0,
633 0, 2, 2, false, false, false);
634 EXPECT_TRUE(glGetError() == GL_INVALID_VALUE);
636 // x + width > source_width
637 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0, 1,
638 1, 2, 2, false, false, false);
639 EXPECT_TRUE(glGetError() == GL_INVALID_VALUE);
642 TEST_F(GLCopyTextureCHROMIUMTest, CopySubTextureOffset) {
643 uint8 rgba_pixels[4 * 4] = {255u,
646 255u,
648 255u,
650 255u,
653 255u,
654 255u,
658 255u};
659 glBindTexture(GL_TEXTURE_2D, textures_[0]);
660 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
661 rgba_pixels);
663 uint8 transparent_pixels[4 * 4] = {
664 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u};
665 glBindTexture(GL_TEXTURE_2D, textures_[1]);
666 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
667 transparent_pixels);
669 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 1, 1, 0,
670 0, 1, 1, false, false, false);
671 EXPECT_TRUE(glGetError() == GL_NO_ERROR);
672 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 1, 0, 1,
673 0, 1, 1, false, false, false);
674 EXPECT_TRUE(glGetError() == GL_NO_ERROR);
675 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 1, 0,
676 1, 1, 1, false, false, false);
677 EXPECT_TRUE(glGetError() == GL_NO_ERROR);
679 // Check the FB is still bound.
680 GLint value = 0;
681 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &value);
682 GLuint fb_id = value;
683 EXPECT_EQ(framebuffer_id_, fb_id);
685 // Check that FB is complete.
686 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
687 glCheckFramebufferStatus(GL_FRAMEBUFFER));
689 uint8 transparent[1 * 4] = {0u, 0u, 0u, 0u};
690 uint8 red[1 * 4] = {255u, 0u, 0u, 255u};
691 uint8 green[1 * 4] = {0u, 255u, 0u, 255u};
692 uint8 blue[1 * 4] = {0u, 0u, 255u, 255u};
693 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, transparent);
694 GLTestHelper::CheckPixels(1, 1, 1, 1, 0, red);
695 GLTestHelper::CheckPixels(1, 0, 1, 1, 0, green);
696 GLTestHelper::CheckPixels(0, 1, 1, 1, 0, blue);
697 EXPECT_TRUE(GL_NO_ERROR == glGetError());
700 } // namespace gpu