Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / command_buffer / tests / gl_texture_storage_unittest.cc
blob8c1162ff48c9d16933526569e599b7fdc8448485
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/gl2ext.h>
8 #include "gpu/command_buffer/tests/gl_manager.h"
9 #include "gpu/command_buffer/tests/gl_test_utils.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace gpu {
15 class TextureStorageTest : public testing::Test {
16 protected:
17 static const GLsizei kResolution = 64;
18 void SetUp() override {
19 GLManager::Options options;
20 options.size = gfx::Size(kResolution, kResolution);
21 gl_.Initialize(options);
22 gl_.MakeCurrent();
24 glGenTextures(1, &tex_);
25 glBindTexture(GL_TEXTURE_2D, tex_);
27 glGenFramebuffers(1, &fbo_);
28 glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
29 glFramebufferTexture2D(GL_FRAMEBUFFER,
30 GL_COLOR_ATTACHMENT0,
31 GL_TEXTURE_2D,
32 tex_,
33 0);
35 const GLubyte* extensions = glGetString(GL_EXTENSIONS);
36 extension_available_ = strstr(reinterpret_cast<const char*>(
37 extensions), "GL_EXT_texture_storage");
40 void TearDown() override { gl_.Destroy(); }
42 GLManager gl_;
43 GLuint tex_;
44 GLuint fbo_;
45 bool extension_available_;
48 TEST_F(TextureStorageTest, CorrectPixels) {
49 if (!extension_available_)
50 return;
52 glTexStorage2DEXT(GL_TEXTURE_2D, 2, GL_RGBA8_OES, 2, 2);
54 // Mesa dirvers crash without rebinding to FBO. It's why
55 // DISABLE_TEXTURE_STORAGE workaround is introduced. crbug.com/521904
56 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
57 tex_, 0);
59 uint8 source_pixels[16] = {
60 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4
62 glTexSubImage2D(GL_TEXTURE_2D,
64 0, 0,
65 2, 2,
66 GL_RGBA, GL_UNSIGNED_BYTE,
67 source_pixels);
68 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 2, 2, 0, source_pixels));
71 TEST_F(TextureStorageTest, IsImmutable) {
72 if (!extension_available_)
73 return;
75 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8_OES, 4, 4);
77 GLint param = 0;
78 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_IMMUTABLE_FORMAT_EXT, &param);
79 EXPECT_TRUE(param);
82 TEST_F(TextureStorageTest, OneLevel) {
83 if (!extension_available_)
84 return;
86 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8_OES, 4, 4);
88 uint8 source_pixels[64] = { 0 };
90 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
91 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 4, 4,
92 GL_RGBA, GL_UNSIGNED_BYTE, source_pixels);
93 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
94 glTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, 2, 2,
95 GL_RGBA, GL_UNSIGNED_BYTE, source_pixels);
96 EXPECT_EQ(static_cast<GLenum>(GL_INVALID_OPERATION), glGetError());
99 TEST_F(TextureStorageTest, MultipleLevels) {
100 if (!extension_available_)
101 return;
103 glTexStorage2DEXT(GL_TEXTURE_2D, 2, GL_RGBA8_OES, 2, 2);
105 uint8 source_pixels[16] = { 0 };
107 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
108 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 2, 2,
109 GL_RGBA, GL_UNSIGNED_BYTE, source_pixels);
110 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
111 glTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, 1, 1,
112 GL_RGBA, GL_UNSIGNED_BYTE, source_pixels);
113 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
114 glTexSubImage2D(GL_TEXTURE_2D, 2, 0, 0, 1, 1,
115 GL_RGBA, GL_UNSIGNED_BYTE, source_pixels);
116 EXPECT_EQ(static_cast<GLenum>(GL_INVALID_OPERATION), glGetError());
119 TEST_F(TextureStorageTest, BadTarget) {
120 if (!extension_available_)
121 return;
123 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
124 glTexStorage2DEXT(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 1, GL_RGBA8_OES, 4, 4);
125 EXPECT_EQ(static_cast<GLenum>(GL_INVALID_ENUM), glGetError());
128 TEST_F(TextureStorageTest, InvalidId) {
129 if (!extension_available_)
130 return;
132 glDeleteTextures(1, &tex_);
133 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
134 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8_OES, 4, 4);
135 EXPECT_EQ(static_cast<GLenum>(GL_INVALID_OPERATION), glGetError());
138 TEST_F(TextureStorageTest, CannotRedefine) {
139 if (!extension_available_)
140 return;
142 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8_OES, 4, 4);
144 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
145 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8_OES, 4, 4);
146 EXPECT_EQ(static_cast<GLenum>(GL_INVALID_OPERATION), glGetError());
148 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
149 glTexImage2D(GL_TEXTURE_2D,
151 GL_RGBA,
152 4, 4,
154 GL_RGBA,
155 GL_UNSIGNED_BYTE,
156 NULL);
157 EXPECT_EQ(static_cast<GLenum>(GL_INVALID_OPERATION), glGetError());
160 } // namespace gpu