We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / gpu / command_buffer / tests / gl_copy_texture_CHROMIUM_unittest.cc
blobff15241233dd3e030c5f1491cc940d2124efda3b
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);
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);
86 EXPECT_TRUE(glGetError() == GL_NO_ERROR);
88 // Check the FB is still bound.
89 GLint value = 0;
90 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &value);
91 GLuint fb_id = value;
92 EXPECT_EQ(framebuffer_id_, fb_id);
94 // Check that FB is complete.
95 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
96 glCheckFramebufferStatus(GL_FRAMEBUFFER));
98 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, pixels);
99 EXPECT_TRUE(GL_NO_ERROR == glGetError());
102 TEST_P(GLCopyTextureCHROMIUMTest, ImmutableTexture) {
103 if (!GLTestHelper::HasExtension("GL_EXT_texture_storage")) {
104 LOG(INFO) << "GL_EXT_texture_storage not supported. Skipping test...";
105 return;
107 CopyType copy_type = GetParam();
109 uint8 pixels[1 * 4] = {255u, 0u, 0u, 255u};
111 glBindTexture(GL_TEXTURE_2D, textures_[0]);
112 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8_OES, 1, 1);
113 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
114 pixels);
116 glBindTexture(GL_TEXTURE_2D, textures_[1]);
117 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8_OES, 1, 1);
118 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
119 textures_[1], 0);
120 EXPECT_TRUE(glGetError() == GL_NO_ERROR);
122 if (copy_type == TexImage) {
123 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
124 GL_UNSIGNED_BYTE);
125 EXPECT_TRUE(glGetError() == GL_INVALID_OPERATION);
126 } else {
127 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
128 EXPECT_TRUE(glGetError() == GL_NO_ERROR);
130 // Check the FB is still bound.
131 GLint value = 0;
132 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &value);
133 GLuint fb_id = value;
134 EXPECT_EQ(framebuffer_id_, fb_id);
136 // Check that FB is complete.
137 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
138 glCheckFramebufferStatus(GL_FRAMEBUFFER));
140 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, pixels);
141 EXPECT_TRUE(GL_NO_ERROR == glGetError());
145 TEST_P(GLCopyTextureCHROMIUMTest, InternalFormat) {
146 CopyType copy_type = GetParam();
147 GLint src_formats[] = {GL_ALPHA, GL_RGB, GL_RGBA,
148 GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_BGRA_EXT};
149 GLint dest_formats[] = {GL_RGB, GL_RGBA};
151 for (size_t src_index = 0; src_index < arraysize(src_formats); src_index++) {
152 for (size_t dest_index = 0; dest_index < arraysize(dest_formats);
153 dest_index++) {
154 glBindTexture(GL_TEXTURE_2D, textures_[0]);
155 glTexImage2D(GL_TEXTURE_2D, 0, src_formats[src_index], 1, 1, 0,
156 src_formats[src_index], GL_UNSIGNED_BYTE, nullptr);
157 EXPECT_TRUE(GL_NO_ERROR == glGetError());
159 if (copy_type == TexImage) {
160 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1],
161 dest_formats[dest_index], GL_UNSIGNED_BYTE);
162 } else {
163 glBindTexture(GL_TEXTURE_2D, textures_[1]);
164 glTexImage2D(GL_TEXTURE_2D, 0, dest_formats[dest_index], 1, 1, 0,
165 dest_formats[dest_index], GL_UNSIGNED_BYTE, nullptr);
166 EXPECT_TRUE(GL_NO_ERROR == glGetError());
168 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0,
172 EXPECT_TRUE(GL_NO_ERROR == glGetError()) << "src_index:" << src_index
173 << " dest_index:" << dest_index;
178 TEST_P(GLCopyTextureCHROMIUMTest, InternalFormatNotSupported) {
179 CopyType copy_type = GetParam();
180 glBindTexture(GL_TEXTURE_2D, textures_[0]);
181 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
182 nullptr);
183 EXPECT_TRUE(GL_NO_ERROR == glGetError());
185 // Check unsupported format reports error.
186 GLint unsupported_dest_formats[] = {GL_ALPHA, GL_LUMINANCE,
187 GL_LUMINANCE_ALPHA};
188 for (size_t dest_index = 0; dest_index < arraysize(unsupported_dest_formats);
189 dest_index++) {
190 if (copy_type == TexImage) {
191 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1],
192 unsupported_dest_formats[dest_index],
193 GL_UNSIGNED_BYTE);
194 } else {
195 glBindTexture(GL_TEXTURE_2D, textures_[1]);
196 glTexImage2D(GL_TEXTURE_2D, 0, unsupported_dest_formats[dest_index], 1, 1,
197 0, unsupported_dest_formats[dest_index], GL_UNSIGNED_BYTE,
198 nullptr);
199 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
201 EXPECT_TRUE(GL_INVALID_OPERATION == glGetError())
202 << "dest_index:" << dest_index;
206 // Test to ensure that the destination texture is redefined if the properties
207 // are different.
208 TEST_F(GLCopyTextureCHROMIUMTest, RedefineDestinationTexture) {
209 uint8 pixels[4 * 4] = {255u, 0u, 0u, 255u, 255u, 0u, 0u, 255u,
210 255u, 0u, 0u, 255u, 255u, 0u, 0u, 255u};
212 glBindTexture(GL_TEXTURE_2D, textures_[0]);
213 glTexImage2D(
214 GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
216 glBindTexture(GL_TEXTURE_2D, textures_[1]);
217 glTexImage2D(GL_TEXTURE_2D,
219 GL_BGRA_EXT,
223 GL_BGRA_EXT,
224 GL_UNSIGNED_BYTE,
225 pixels);
226 EXPECT_TRUE(GL_NO_ERROR == glGetError());
228 // GL_INVALID_OPERATION due to "intrinsic format" != "internal format".
229 glTexSubImage2D(
230 GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
231 EXPECT_TRUE(GL_INVALID_OPERATION == glGetError());
232 // GL_INVALID_VALUE due to bad dimensions.
233 glTexSubImage2D(
234 GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
235 EXPECT_TRUE(GL_INVALID_VALUE == glGetError());
237 // If the dest texture has different properties, glCopyTextureCHROMIUM()
238 // redefines them.
239 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
240 GL_UNSIGNED_BYTE);
241 EXPECT_TRUE(GL_NO_ERROR == glGetError());
243 // glTexSubImage2D() succeeds because textures_[1] is redefined into 2x2
244 // dimension and GL_RGBA format.
245 glBindTexture(GL_TEXTURE_2D, textures_[1]);
246 glTexSubImage2D(
247 GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
248 EXPECT_TRUE(GL_NO_ERROR == glGetError());
250 // Check the FB is still bound.
251 GLint value = 0;
252 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &value);
253 GLuint fb_id = value;
254 EXPECT_EQ(framebuffer_id_, fb_id);
256 // Check that FB is complete.
257 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
258 glCheckFramebufferStatus(GL_FRAMEBUFFER));
260 GLTestHelper::CheckPixels(1, 1, 1, 1, 0, &pixels[12]);
261 EXPECT_TRUE(GL_NO_ERROR == glGetError());
264 // Test that the extension respects the flip-y pixel storage setting.
265 TEST_P(GLCopyTextureCHROMIUMTest, FlipY) {
266 CopyType copy_type = GetParam();
267 uint8 pixels[2][2][4];
268 for (int x = 0; x < 2; ++x) {
269 for (int y = 0; y < 2; ++y) {
270 pixels[y][x][0] = x + y;
271 pixels[y][x][1] = x + y;
272 pixels[y][x][2] = x + y;
273 pixels[y][x][3] = 255u;
277 glBindTexture(GL_TEXTURE_2D, textures_[0]);
278 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
279 pixels);
281 glPixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, GL_TRUE);
283 if (copy_type == TexImage) {
284 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
285 GL_UNSIGNED_BYTE);
286 } else {
287 glBindTexture(GL_TEXTURE_2D, textures_[1]);
288 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
289 nullptr);
290 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
292 EXPECT_TRUE(GL_NO_ERROR == glGetError());
294 uint8 copied_pixels[2][2][4] = {{{0}}};
295 glReadPixels(0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, copied_pixels);
296 for (int x = 0; x < 2; ++x) {
297 for (int y = 0; y < 2; ++y) {
298 EXPECT_EQ(pixels[1-y][x][0], copied_pixels[y][x][0]);
299 EXPECT_EQ(pixels[1-y][x][1], copied_pixels[y][x][1]);
300 EXPECT_EQ(pixels[1-y][x][2], copied_pixels[y][x][2]);
301 EXPECT_EQ(pixels[1-y][x][3], copied_pixels[y][x][3]);
305 EXPECT_TRUE(GL_NO_ERROR == glGetError());
308 // Test that the extension respects the GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM
309 // storage setting.
310 TEST_P(GLCopyTextureCHROMIUMTest, PremultiplyAlpha) {
311 CopyType copy_type = GetParam();
312 uint8 pixels[1 * 4] = { 2, 2, 2, 128 };
314 glBindTexture(GL_TEXTURE_2D, textures_[0]);
315 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
316 pixels);
318 glPixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, GL_TRUE);
319 if (copy_type == TexImage) {
320 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
321 GL_UNSIGNED_BYTE);
322 } else {
323 glBindTexture(GL_TEXTURE_2D, textures_[1]);
324 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
325 nullptr);
326 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
328 EXPECT_TRUE(GL_NO_ERROR == glGetError());
330 uint8 copied_pixels[1 * 4] = {0};
331 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, copied_pixels);
332 EXPECT_EQ(1u, copied_pixels[0]);
333 EXPECT_EQ(1u, copied_pixels[1]);
334 EXPECT_EQ(1u, copied_pixels[2]);
335 EXPECT_EQ(128u, copied_pixels[3]);
337 EXPECT_TRUE(GL_NO_ERROR == glGetError());
340 // Test that the extension respects the GL_UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM
341 // storage setting.
342 TEST_P(GLCopyTextureCHROMIUMTest, UnpremultiplyAlpha) {
343 CopyType copy_type = GetParam();
344 uint8 pixels[1 * 4] = { 16, 16, 16, 128 };
346 glBindTexture(GL_TEXTURE_2D, textures_[0]);
347 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
348 pixels);
350 glPixelStorei(GL_UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM, GL_TRUE);
351 if (copy_type == TexImage) {
352 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
353 GL_UNSIGNED_BYTE);
354 } else {
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);
358 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
360 EXPECT_TRUE(GL_NO_ERROR == glGetError());
362 uint8 copied_pixels[1 * 4] = {0};
363 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, copied_pixels);
364 EXPECT_EQ(32u, copied_pixels[0]);
365 EXPECT_EQ(32u, copied_pixels[1]);
366 EXPECT_EQ(32u, copied_pixels[2]);
367 EXPECT_EQ(128u, copied_pixels[3]);
369 EXPECT_TRUE(GL_NO_ERROR == glGetError());
372 TEST_P(GLCopyTextureCHROMIUMTest, FlipYAndPremultiplyAlpha) {
373 CopyType copy_type = GetParam();
374 uint8 pixels[2][2][4];
375 for (int x = 0; x < 2; ++x) {
376 for (int y = 0; y < 2; ++y) {
377 uint8 color = 16 * x + 16 * y;
378 pixels[y][x][0] = color;
379 pixels[y][x][1] = color;
380 pixels[y][x][2] = color;
381 pixels[y][x][3] = 128u;
385 glBindTexture(GL_TEXTURE_2D, textures_[0]);
386 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
387 pixels);
389 glPixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, GL_TRUE);
390 glPixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, GL_TRUE);
391 if (copy_type == TexImage) {
392 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
393 GL_UNSIGNED_BYTE);
394 } else {
395 glBindTexture(GL_TEXTURE_2D, textures_[1]);
396 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
397 nullptr);
398 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
400 EXPECT_TRUE(GL_NO_ERROR == glGetError());
402 uint8 copied_pixels[2][2][4] = {{{0}}};
403 glReadPixels(0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, copied_pixels);
404 for (int x = 0; x < 2; ++x) {
405 for (int y = 0; y < 2; ++y) {
406 EXPECT_EQ(pixels[1-y][x][0] / 2, copied_pixels[y][x][0]);
407 EXPECT_EQ(pixels[1-y][x][1] / 2, copied_pixels[y][x][1]);
408 EXPECT_EQ(pixels[1-y][x][2] / 2, copied_pixels[y][x][2]);
409 EXPECT_EQ(pixels[1-y][x][3], copied_pixels[y][x][3]);
413 EXPECT_TRUE(GL_NO_ERROR == glGetError());
416 TEST_P(GLCopyTextureCHROMIUMTest, FlipYAndUnpremultiplyAlpha) {
417 CopyType copy_type = GetParam();
418 uint8 pixels[2][2][4];
419 for (int x = 0; x < 2; ++x) {
420 for (int y = 0; y < 2; ++y) {
421 uint8 color = 16 * x + 16 * y;
422 pixels[y][x][0] = color;
423 pixels[y][x][1] = color;
424 pixels[y][x][2] = color;
425 pixels[y][x][3] = 128u;
429 glBindTexture(GL_TEXTURE_2D, textures_[0]);
430 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
431 pixels);
433 glPixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, GL_TRUE);
434 glPixelStorei(GL_UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM, GL_TRUE);
435 if (copy_type == TexImage) {
436 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
437 GL_UNSIGNED_BYTE);
438 } else {
439 glBindTexture(GL_TEXTURE_2D, textures_[1]);
440 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
441 nullptr);
442 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
444 EXPECT_TRUE(GL_NO_ERROR == glGetError());
446 uint8 copied_pixels[2][2][4] = {{{0}}};
447 glReadPixels(0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, copied_pixels);
448 for (int x = 0; x < 2; ++x) {
449 for (int y = 0; y < 2; ++y) {
450 EXPECT_EQ(pixels[1-y][x][0] * 2, copied_pixels[y][x][0]);
451 EXPECT_EQ(pixels[1-y][x][1] * 2, copied_pixels[y][x][1]);
452 EXPECT_EQ(pixels[1-y][x][2] * 2, copied_pixels[y][x][2]);
453 EXPECT_EQ(pixels[1-y][x][3], copied_pixels[y][x][3]);
457 EXPECT_TRUE(GL_NO_ERROR == glGetError());
460 namespace {
462 void glEnableDisable(GLint param, GLboolean value) {
463 if (value)
464 glEnable(param);
465 else
466 glDisable(param);
469 } // unnamed namespace
471 // Validate that some basic GL state is not touched upon execution of
472 // the extension.
473 TEST_P(GLCopyTextureCHROMIUMTest, BasicStatePreservation) {
474 CopyType copy_type = GetParam();
475 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u };
477 glBindFramebuffer(GL_FRAMEBUFFER, 0);
479 glBindTexture(GL_TEXTURE_2D, textures_[0]);
480 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
481 pixels);
483 if (copy_type == TexSubImage) {
484 glBindTexture(GL_TEXTURE_2D, textures_[1]);
485 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
486 nullptr);
489 GLboolean reference_settings[2] = { GL_TRUE, GL_FALSE };
490 for (int x = 0; x < 2; ++x) {
491 GLboolean setting = reference_settings[x];
492 glEnableDisable(GL_DEPTH_TEST, setting);
493 glEnableDisable(GL_SCISSOR_TEST, setting);
494 glEnableDisable(GL_STENCIL_TEST, setting);
495 glEnableDisable(GL_CULL_FACE, setting);
496 glEnableDisable(GL_BLEND, setting);
497 glColorMask(setting, setting, setting, setting);
498 glDepthMask(setting);
500 glActiveTexture(GL_TEXTURE1 + x);
502 if (copy_type == TexImage) {
503 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
504 GL_UNSIGNED_BYTE);
505 } else {
506 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
508 EXPECT_TRUE(GL_NO_ERROR == glGetError());
510 EXPECT_EQ(setting, glIsEnabled(GL_DEPTH_TEST));
511 EXPECT_EQ(setting, glIsEnabled(GL_SCISSOR_TEST));
512 EXPECT_EQ(setting, glIsEnabled(GL_STENCIL_TEST));
513 EXPECT_EQ(setting, glIsEnabled(GL_CULL_FACE));
514 EXPECT_EQ(setting, glIsEnabled(GL_BLEND));
516 GLboolean bool_array[4] = { GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE };
517 glGetBooleanv(GL_DEPTH_WRITEMASK, bool_array);
518 EXPECT_EQ(setting, bool_array[0]);
520 bool_array[0] = GL_FALSE;
521 glGetBooleanv(GL_COLOR_WRITEMASK, bool_array);
522 EXPECT_EQ(setting, bool_array[0]);
523 EXPECT_EQ(setting, bool_array[1]);
524 EXPECT_EQ(setting, bool_array[2]);
525 EXPECT_EQ(setting, bool_array[3]);
527 GLint active_texture = 0;
528 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
529 EXPECT_EQ(GL_TEXTURE1 + x, active_texture);
532 EXPECT_TRUE(GL_NO_ERROR == glGetError());
535 // Verify that invocation of the extension does not modify the bound
536 // texture state.
537 TEST_P(GLCopyTextureCHROMIUMTest, TextureStatePreserved) {
538 CopyType copy_type = GetParam();
539 // Setup the texture used for the extension invocation.
540 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u };
541 glBindTexture(GL_TEXTURE_2D, textures_[0]);
542 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
543 pixels);
545 if (copy_type == TexSubImage) {
546 glBindTexture(GL_TEXTURE_2D, textures_[1]);
547 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
548 nullptr);
551 GLuint texture_ids[2];
552 glGenTextures(2, texture_ids);
554 glActiveTexture(GL_TEXTURE0);
555 glBindTexture(GL_TEXTURE_2D, texture_ids[0]);
557 glActiveTexture(GL_TEXTURE1);
558 glBindTexture(GL_TEXTURE_2D, texture_ids[1]);
560 if (copy_type == TexImage) {
561 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
562 GL_UNSIGNED_BYTE);
563 } else {
564 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
566 EXPECT_TRUE(GL_NO_ERROR == glGetError());
568 GLint active_texture = 0;
569 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
570 EXPECT_EQ(GL_TEXTURE1, active_texture);
572 GLint bound_texture = 0;
573 glGetIntegerv(GL_TEXTURE_BINDING_2D, &bound_texture);
574 EXPECT_EQ(texture_ids[1], static_cast<GLuint>(bound_texture));
575 glBindTexture(GL_TEXTURE_2D, 0);
577 bound_texture = 0;
578 glActiveTexture(GL_TEXTURE0);
579 glGetIntegerv(GL_TEXTURE_BINDING_2D, &bound_texture);
580 EXPECT_EQ(texture_ids[0], static_cast<GLuint>(bound_texture));
581 glBindTexture(GL_TEXTURE_2D, 0);
583 glDeleteTextures(2, texture_ids);
585 EXPECT_TRUE(GL_NO_ERROR == glGetError());
588 // Verify that invocation of the extension does not perturb the currently
589 // bound FBO state.
590 TEST_P(GLCopyTextureCHROMIUMTest, FBOStatePreserved) {
591 CopyType copy_type = GetParam();
592 // Setup the texture used for the extension invocation.
593 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u };
594 glBindTexture(GL_TEXTURE_2D, textures_[0]);
595 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
596 pixels);
598 if (copy_type == TexSubImage) {
599 glBindTexture(GL_TEXTURE_2D, textures_[1]);
600 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
601 nullptr);
604 GLuint texture_id;
605 glGenTextures(1, &texture_id);
606 glBindTexture(GL_TEXTURE_2D, texture_id);
607 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
610 GLuint renderbuffer_id;
611 glGenRenderbuffers(1, &renderbuffer_id);
612 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer_id);
613 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 1, 1);
615 GLuint framebuffer_id;
616 glGenFramebuffers(1, &framebuffer_id);
617 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
618 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
619 texture_id, 0);
620 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
621 GL_RENDERBUFFER, renderbuffer_id);
622 EXPECT_TRUE(
623 GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER));
625 // Test that we can write to the bound framebuffer
626 uint8 expected_color[4] = { 255u, 255u, 0, 255u };
627 glClearColor(1.0, 1.0, 0, 1.0);
628 glClear(GL_COLOR_BUFFER_BIT);
629 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color);
631 if (copy_type == TexImage) {
632 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
633 GL_UNSIGNED_BYTE);
634 } else {
635 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
637 EXPECT_TRUE(GL_NO_ERROR == glGetError());
639 EXPECT_TRUE(glIsFramebuffer(framebuffer_id));
641 // Ensure that reading from the framebuffer produces correct pixels.
642 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color);
644 uint8 expected_color2[4] = { 255u, 0, 255u, 255u };
645 glClearColor(1.0, 0, 1.0, 1.0);
646 glClear(GL_COLOR_BUFFER_BIT);
647 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color2);
649 GLint bound_fbo = 0;
650 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &bound_fbo);
651 EXPECT_EQ(framebuffer_id, static_cast<GLuint>(bound_fbo));
653 GLint fbo_params = 0;
654 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
655 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
656 &fbo_params);
657 EXPECT_EQ(GL_TEXTURE, fbo_params);
659 fbo_params = 0;
660 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
661 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
662 &fbo_params);
663 EXPECT_EQ(texture_id, static_cast<GLuint>(fbo_params));
665 fbo_params = 0;
666 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
667 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
668 &fbo_params);
669 EXPECT_EQ(GL_RENDERBUFFER, fbo_params);
671 fbo_params = 0;
672 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
673 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
674 &fbo_params);
675 EXPECT_EQ(renderbuffer_id, static_cast<GLuint>(fbo_params));
677 glDeleteRenderbuffers(1, &renderbuffer_id);
678 glDeleteTextures(1, &texture_id);
679 glDeleteFramebuffers(1, &framebuffer_id);
681 EXPECT_TRUE(GL_NO_ERROR == glGetError());
684 TEST_P(GLCopyTextureCHROMIUMTest, ProgramStatePreservation) {
685 CopyType copy_type = GetParam();
686 // unbind the one created in setup.
687 glBindFramebuffer(GL_FRAMEBUFFER, 0);
688 glBindTexture(GL_TEXTURE_2D, 0);
690 GLManager gl2;
691 GLManager::Options options;
692 options.size = gfx::Size(16, 16);
693 options.share_group_manager = &gl_;
694 gl2.Initialize(options);
695 gl_.MakeCurrent();
697 static const char* v_shader_str =
698 "attribute vec4 g_Position;\n"
699 "void main()\n"
700 "{\n"
701 " gl_Position = g_Position;\n"
702 "}\n";
703 static const char* f_shader_str =
704 "precision mediump float;\n"
705 "void main()\n"
706 "{\n"
707 " gl_FragColor = vec4(0,1,0,1);\n"
708 "}\n";
710 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
711 glUseProgram(program);
712 GLuint position_loc = glGetAttribLocation(program, "g_Position");
713 glFlush();
715 // Delete program from other context.
716 gl2.MakeCurrent();
717 glDeleteProgram(program);
718 EXPECT_TRUE(GL_NO_ERROR == glGetError());
719 glFlush();
721 // Program should still be usable on this context.
722 gl_.MakeCurrent();
724 GLTestHelper::SetupUnitQuad(position_loc);
726 // test using program before
727 uint8 expected[] = { 0, 255, 0, 255, };
728 uint8 zero[] = { 0, 0, 0, 0, };
729 glClear(GL_COLOR_BUFFER_BIT);
730 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, zero));
731 glDrawArrays(GL_TRIANGLES, 0, 6);
732 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected));
734 // Call copyTextureCHROMIUM
735 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u };
736 glBindTexture(GL_TEXTURE_2D, textures_[0]);
737 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
738 pixels);
739 if (copy_type == TexImage) {
740 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
741 GL_UNSIGNED_BYTE);
742 } else {
743 glBindTexture(GL_TEXTURE_2D, textures_[1]);
744 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
745 nullptr);
746 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
749 // test using program after
750 glClear(GL_COLOR_BUFFER_BIT);
751 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, zero));
752 glDrawArrays(GL_TRIANGLES, 0, 6);
753 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected));
755 EXPECT_TRUE(GL_NO_ERROR == glGetError());
757 gl2.MakeCurrent();
758 gl2.Destroy();
759 gl_.MakeCurrent();
762 // Test that glCopyTextureCHROMIUM doesn't leak uninitialized textures.
763 TEST_P(GLCopyTextureCHROMIUMTest, UninitializedSource) {
764 CopyType copy_type = GetParam();
765 const GLsizei kWidth = 64, kHeight = 64;
766 glBindTexture(GL_TEXTURE_2D, textures_[0]);
767 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kWidth, kHeight, 0, GL_RGBA,
768 GL_UNSIGNED_BYTE, nullptr);
770 if (copy_type == TexImage) {
771 glCopyTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], GL_RGBA,
772 GL_UNSIGNED_BYTE);
773 } else {
774 glBindTexture(GL_TEXTURE_2D, textures_[1]);
775 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kWidth, kHeight, 0, GL_RGBA,
776 GL_UNSIGNED_BYTE, nullptr);
777 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 0, 0);
779 EXPECT_TRUE(GL_NO_ERROR == glGetError());
781 uint8 pixels[kHeight][kWidth][4] = {{{1}}};
782 glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
783 for (int x = 0; x < kWidth; ++x) {
784 for (int y = 0; y < kHeight; ++y) {
785 EXPECT_EQ(0, pixels[y][x][0]);
786 EXPECT_EQ(0, pixels[y][x][1]);
787 EXPECT_EQ(0, pixels[y][x][2]);
788 EXPECT_EQ(0, pixels[y][x][3]);
792 EXPECT_TRUE(GL_NO_ERROR == glGetError());
795 TEST_F(GLCopyTextureCHROMIUMTest, CopySubTextureDimension) {
796 glBindTexture(GL_TEXTURE_2D, textures_[0]);
797 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
798 nullptr);
800 glBindTexture(GL_TEXTURE_2D, textures_[1]);
801 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 3, 0, GL_RGBA, GL_UNSIGNED_BYTE,
802 nullptr);
804 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 1, 1);
805 EXPECT_TRUE(GL_NO_ERROR == glGetError());
807 // xoffset < 0
808 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], -1, 1);
809 EXPECT_TRUE(glGetError() == GL_INVALID_VALUE);
811 // xoffset + source_width > dest_width
812 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 2, 2);
813 EXPECT_TRUE(glGetError() == GL_INVALID_VALUE);
816 TEST_F(GLCopyTextureCHROMIUMTest, CopySubTextureOffset) {
817 uint8 red[1 * 4] = {255u, 0u, 0u, 255u};
818 glBindTexture(GL_TEXTURE_2D, textures_[0]);
819 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
820 red);
822 uint8 transparent_pixel[4 * 4] = {
823 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u};
824 glBindTexture(GL_TEXTURE_2D, textures_[1]);
825 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
826 transparent_pixel);
828 glCopySubTextureCHROMIUM(GL_TEXTURE_2D, textures_[0], textures_[1], 1, 1);
829 EXPECT_TRUE(glGetError() == GL_NO_ERROR);
831 // Check the FB is still bound.
832 GLint value = 0;
833 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &value);
834 GLuint fb_id = value;
835 EXPECT_EQ(framebuffer_id_, fb_id);
837 // Check that FB is complete.
838 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
839 glCheckFramebufferStatus(GL_FRAMEBUFFER));
841 uint8 transparent[1 * 4] = {0u, 0u, 0u, 0u};
842 GLTestHelper::CheckPixels(0, 0, 1, 1, 0, transparent);
843 GLTestHelper::CheckPixels(1, 1, 1, 1, 0, red);
844 EXPECT_TRUE(GL_NO_ERROR == glGetError());
847 } // namespace gpu