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

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: Add clean up code 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
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
Daniele Castagna 2015/09/30 19:17:35 Nit: why did you break this line so early? It seem
reveman 2015/10/01 04:01:36 Cut-and-paste from task_runner_test_template.h. Us
6 // pass in order 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/stringprintf.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gfx/buffer_format_util.h"
16 #include "ui/gfx/buffer_types.h"
17 #include "ui/gl/gl_bindings.h"
18 #include "ui/gl/gl_context.h"
19 #include "ui/gl/gl_image.h"
20 #include "ui/gl/gl_implementation.h"
21 #include "ui/gl/gl_surface.h"
22 #include "ui/gl/test/gl_image_test_support.h"
23 #include "ui/gl/test/gl_test_helper.h"
24
25 #define SHADER(Src) #Src
Daniele Castagna 2015/09/30 19:17:36 FYI: This is defined exactly like this already in
reveman 2015/10/01 04:01:36 Instead using STRINGIZE from base/ in latest patch
26
27 namespace gfx {
28
29 template <typename GLImageTestDelegate>
30 class GLImageTest : public testing::Test {
31 protected:
32 // Overridden from testing::Test:
33 void SetUp() override {
34 GLImageTestSupport::InitializeGL();
35 surface_ = GLSurface::CreateOffscreenGLSurface(Size());
36 context_ = GLContext::CreateGLContext(nullptr, surface_.get(),
37 PreferIntegratedGpu);
38 context_->MakeCurrent(surface_.get());
39 }
40 void TearDown() override {
41 context_->ReleaseCurrent(surface_.get());
42 context_ = nullptr;
43 surface_ = nullptr;
44 GLImageTestSupport::CleanupGL();
45 }
46
47 protected:
48 scoped_refptr<GLSurface> surface_;
49 scoped_refptr<GLContext> context_;
50 GLImageTestDelegate delegate_;
51 };
52
53 TYPED_TEST_CASE_P(GLImageTest);
54
55 // Copy image to texture. Support is optional. Texels should be updated if
56 // supported, and left unchanged if not.
57 TYPED_TEST_P(GLImageTest, CopyTexSubImage) {
58 const Size image_size(256, 256);
59 const uint8_t image_color[] = {0, 0xff, 0, 0xff};
60 const uint8_t texture_color[] = {0, 0, 0xff, 0xff};
61
62 GLuint framebuffer =
63 GLTestHelper::SetupFramebuffer(image_size.width(), image_size.height());
64 ASSERT_TRUE(framebuffer);
65 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer);
66 glViewport(0, 0, image_size.width(), image_size.height());
67
68 // Create a solid color green image of preferred format. This must succeed
69 // in order for a GLImage to be conformant.
70 scoped_refptr<GLImage> image = this->delegate_.CreateSolidColorImage(
71 image_size, GLImageTestSupport::GetPreferredInternalFormat(),
72 GLImageTestSupport::GetPreferredBufferFormat(), image_color);
73 ASSERT_TRUE(image);
74
75 // Create a solid color blue texture of the same size as |image|.
76 GLuint texture = GLTestHelper::CreateTexture(GL_TEXTURE_2D);
77 scoped_ptr<uint8_t[]> pixels(new uint8_t[BufferSizeForBufferFormat(
Daniele Castagna 2015/09/30 19:17:35 std::vector<uint8_t> ?
reveman 2015/10/01 04:01:36 I prefer scoped_ptr<uint8_t[]> when std::vector do
78 image_size, GLImageTestSupport::GetPreferredBufferFormat())]);
79 GLImageTestSupport::SetBufferDataToColor(
80 image_size.width(), image_size.height(),
81 static_cast<int>(RowSizeForBufferFormat(
82 image_size.width(), GLImageTestSupport::GetPreferredBufferFormat(),
83 0)),
84 GLImageTestSupport::GetPreferredBufferFormat(), texture_color,
85 pixels.get());
86 // Note: This test assume that |image| can be used with GL_TEXTURE_2D but
87 // that might not be the case for some GLImage implementations.
88 glBindTexture(GL_TEXTURE_2D, texture);
89 glTexImage2D(GL_TEXTURE_2D, 0,
90 GLImageTestSupport::GetPreferredInternalFormat(),
91 image_size.width(), image_size.height(), 0,
92 GLImageTestSupport::GetPreferredInternalFormat(),
93 GL_UNSIGNED_BYTE, pixels.get());
94
95 // Attempt to copy |image| to |texture|.
96 // Returns true on success, false on failure.
97 bool rv = image->CopyTexSubImage(GL_TEXTURE_2D, Point(), Rect(image_size));
98
99 // clang-format off
100 const char kVertexShader[] = SHADER(
Daniele Castagna 2015/09/30 19:17:35 It'd be nice if the following code to draw a quad
reveman 2015/10/01 04:01:36 Yes, the plan is to move it as soon as we add anot
101 attribute vec2 a_position;
102 attribute vec2 a_texCoord;
103 varying vec2 v_texCoord;
104 void main() {
105 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0);
106 v_texCoord = a_texCoord;
107 }
108 );
109 const char kFragmentShader[] = SHADER(
110 uniform sampler2D a_texture;
111 varying vec2 v_texCoord;
112 void main() {
113 gl_FragColor = texture2D(a_texture, v_texCoord);
114 }
115 );
116 const char kShaderFloatPrecision[] = SHADER(
117 precision mediump float;
118 );
119 // clang-format on
120
121 GLuint vertex_shader =
122 GLTestHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader);
123 bool is_gles = GetGLImplementation() == kGLImplementationEGLGLES2;
124 GLuint fragment_shader = GLTestHelper::LoadShader(
125 GL_FRAGMENT_SHADER,
126 base::StringPrintf("%s%s", is_gles ? kShaderFloatPrecision : "",
127 kFragmentShader)
128 .c_str());
129 GLuint program = GLTestHelper::SetupProgram(vertex_shader, fragment_shader);
130 EXPECT_NE(program, 0u);
131 glUseProgram(program);
132
133 GLint sampler_location = glGetUniformLocation(program, "a_texture");
134 ASSERT_NE(sampler_location, -1);
135 glUniform1i(sampler_location, 0);
136
137 // clang-format off
138 static GLfloat vertices[] = {
139 -1.f, -1.f, 0.f, 0.f,
140 1.f, -1.f, 1.f, 0.f,
141 -1.f, 1.f, 0.f, 1.f,
142 1.f, 1.f, 1.f, 1.f
143 };
144 // clang-format on
145
146 GLuint vertex_buffer;
147 glGenBuffersARB(1, &vertex_buffer);
148 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
149 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
150 GLint position_location = glGetAttribLocation(program, "a_position");
151 ASSERT_NE(position_location, -1);
152 glEnableVertexAttribArray(position_location);
153 glVertexAttribPointer(position_location, 2, GL_FLOAT, GL_FALSE,
154 sizeof(GLfloat) * 4, 0);
155 GLint tex_coord_location = glGetAttribLocation(program, "a_texCoord");
156 EXPECT_NE(tex_coord_location, -1);
157 glEnableVertexAttribArray(tex_coord_location);
158 glVertexAttribPointer(tex_coord_location, 2, GL_FLOAT, GL_FALSE,
159 sizeof(GLfloat) * 4,
160 reinterpret_cast<void*>(sizeof(GLfloat) * 2));
161
162 // Draw |texture| to viewport and read back pixels to check expectations.
163 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
164 GLTestHelper::CheckPixels(0, 0, image_size.width(), image_size.height(),
165 rv ? image_color : texture_color);
166
167 // Clean up.
168 glDeleteProgram(program);
169 glDeleteShader(vertex_shader);
170 glDeleteShader(fragment_shader);
171 glDeleteBuffersARB(1, &vertex_buffer);
172 glDeleteTextures(1, &texture);
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

Powered by Google App Engine
This is Rietveld 408576698