Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: ui/gl/test/gl_image_test_template.h

Issue 1354483004: Re-land: ui: Add GLImage unit test framework. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gl/test/gl_image_test_support.cc ('k') | ui/gl/test/gl_surface_test_support.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 // This file defines tests that implementations of GLImage should pass in order
6 // to be conformant.
7
8 #ifndef UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_
9 #define UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_
10
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/stringize_macros.h"
14 #include "base/strings/stringprintf.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/gfx/buffer_format_util.h"
17 #include "ui/gfx/buffer_types.h"
18 #include "ui/gl/gl_bindings.h"
19 #include "ui/gl/gl_context.h"
20 #include "ui/gl/gl_image.h"
21 #include "ui/gl/gl_implementation.h"
22 #include "ui/gl/gl_surface.h"
23 #include "ui/gl/test/gl_image_test_support.h"
24 #include "ui/gl/test/gl_test_helper.h"
25
26 namespace gfx {
27
28 template <typename GLImageTestDelegate>
29 class GLImageTest : public testing::Test {
30 protected:
31 // Overridden from testing::Test:
32 void SetUp() override {
33 GLImageTestSupport::InitializeGL();
34 surface_ = GLSurface::CreateOffscreenGLSurface(Size());
35 context_ = GLContext::CreateGLContext(nullptr, surface_.get(),
36 PreferIntegratedGpu);
37 context_->MakeCurrent(surface_.get());
38 }
39 void TearDown() override {
40 context_->ReleaseCurrent(surface_.get());
41 context_ = nullptr;
42 surface_ = nullptr;
43 GLImageTestSupport::CleanupGL();
44 }
45
46 protected:
47 scoped_refptr<GLSurface> surface_;
48 scoped_refptr<GLContext> context_;
49 GLImageTestDelegate delegate_;
50 };
51
52 TYPED_TEST_CASE_P(GLImageTest);
53
54 // Copy image to texture. Support is optional. Texels should be updated if
55 // supported, and left unchanged if not.
56 TYPED_TEST_P(GLImageTest, CopyTexSubImage) {
57 const Size image_size(256, 256);
58 const uint8_t image_color[] = {0, 0xff, 0, 0xff};
59 const uint8_t texture_color[] = {0, 0, 0xff, 0xff};
60
61 GLuint framebuffer =
62 GLTestHelper::SetupFramebuffer(image_size.width(), image_size.height());
63 ASSERT_TRUE(framebuffer);
64 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer);
65 glViewport(0, 0, image_size.width(), image_size.height());
66
67 // Create a solid color green image of preferred format. This must succeed
68 // in order for a GLImage to be conformant.
69 scoped_refptr<GLImage> image = this->delegate_.CreateSolidColorImage(
70 image_size, GLImageTestSupport::GetPreferredInternalFormat(),
71 GLImageTestSupport::GetPreferredBufferFormat(), image_color);
72 ASSERT_TRUE(image);
73
74 // Create a solid color blue texture of the same size as |image|.
75 GLuint texture = GLTestHelper::CreateTexture(GL_TEXTURE_2D);
76 scoped_ptr<uint8_t[]> pixels(new uint8_t[BufferSizeForBufferFormat(
77 image_size, GLImageTestSupport::GetPreferredBufferFormat())]);
78 GLImageTestSupport::SetBufferDataToColor(
79 image_size.width(), image_size.height(),
80 static_cast<int>(RowSizeForBufferFormat(
81 image_size.width(), GLImageTestSupport::GetPreferredBufferFormat(),
82 0)),
83 GLImageTestSupport::GetPreferredBufferFormat(), texture_color,
84 pixels.get());
85 // Note: This test assume that |image| can be used with GL_TEXTURE_2D but
86 // that might not be the case for some GLImage implementations.
87 glBindTexture(GL_TEXTURE_2D, texture);
88 glTexImage2D(GL_TEXTURE_2D, 0,
89 GLImageTestSupport::GetPreferredInternalFormat(),
90 image_size.width(), image_size.height(), 0,
91 GLImageTestSupport::GetPreferredInternalFormat(),
92 GL_UNSIGNED_BYTE, pixels.get());
93
94 // Attempt to copy |image| to |texture|.
95 // Returns true on success, false on failure.
96 bool rv = image->CopyTexSubImage(GL_TEXTURE_2D, Point(), Rect(image_size));
97
98 // clang-format off
99 const char kVertexShader[] = STRINGIZE(
100 attribute vec2 a_position;
101 attribute vec2 a_texCoord;
102 varying vec2 v_texCoord;
103 void main() {
104 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0);
105 v_texCoord = a_texCoord;
106 }
107 );
108 const char kFragmentShader[] = STRINGIZE(
109 uniform sampler2D a_texture;
110 varying vec2 v_texCoord;
111 void main() {
112 gl_FragColor = texture2D(a_texture, v_texCoord);
113 }
114 );
115 const char kShaderFloatPrecision[] = STRINGIZE(
116 precision mediump float;
117 );
118 // clang-format on
119
120 GLuint vertex_shader =
121 GLTestHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader);
122 bool is_gles = GetGLImplementation() == kGLImplementationEGLGLES2;
123 GLuint fragment_shader = GLTestHelper::LoadShader(
124 GL_FRAGMENT_SHADER,
125 base::StringPrintf("%s%s", is_gles ? kShaderFloatPrecision : "",
126 kFragmentShader)
127 .c_str());
128 GLuint program = GLTestHelper::SetupProgram(vertex_shader, fragment_shader);
129 EXPECT_NE(program, 0u);
130 glUseProgram(program);
131
132 GLint sampler_location = glGetUniformLocation(program, "a_texture");
133 ASSERT_NE(sampler_location, -1);
134 glUniform1i(sampler_location, 0);
135
136 // clang-format off
137 static GLfloat vertices[] = {
138 -1.f, -1.f, 0.f, 0.f,
139 1.f, -1.f, 1.f, 0.f,
140 -1.f, 1.f, 0.f, 1.f,
141 1.f, 1.f, 1.f, 1.f
142 };
143 // clang-format on
144
145 GLuint vertex_buffer;
146 glGenBuffersARB(1, &vertex_buffer);
147 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
148 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
149 GLint position_location = glGetAttribLocation(program, "a_position");
150 ASSERT_NE(position_location, -1);
151 glEnableVertexAttribArray(position_location);
152 glVertexAttribPointer(position_location, 2, GL_FLOAT, GL_FALSE,
153 sizeof(GLfloat) * 4, 0);
154 GLint tex_coord_location = glGetAttribLocation(program, "a_texCoord");
155 EXPECT_NE(tex_coord_location, -1);
156 glEnableVertexAttribArray(tex_coord_location);
157 glVertexAttribPointer(tex_coord_location, 2, GL_FLOAT, GL_FALSE,
158 sizeof(GLfloat) * 4,
159 reinterpret_cast<void*>(sizeof(GLfloat) * 2));
160
161 // Draw |texture| to viewport and read back pixels to check expectations.
162 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
163 GLTestHelper::CheckPixels(0, 0, image_size.width(), image_size.height(),
164 rv ? image_color : texture_color);
165
166 // Clean up.
167 glDeleteProgram(program);
168 glDeleteShader(vertex_shader);
169 glDeleteShader(fragment_shader);
170 glDeleteBuffersARB(1, &vertex_buffer);
171 glDeleteTextures(1, &texture);
172 glDeleteFramebuffersEXT(1, &framebuffer);
173 image->Destroy(true);
174 }
175
176 // The GLImageTest test case verifies behaviour that is expected from a
177 // GLImage in order to be conformant.
178 REGISTER_TYPED_TEST_CASE_P(GLImageTest, CopyTexSubImage);
179
180 } // namespace gfx
181
182 #endif // UI_GL_TEST_GL_IMAGE_TEST_TEMPLATE_H_
OLDNEW
« no previous file with comments | « ui/gl/test/gl_image_test_support.cc ('k') | ui/gl/test/gl_surface_test_support.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698