1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "gpu/command_buffer/tests/gl_test_utils.h"
#include <GLES2/gl2extchromium.h>
#include <stdint.h>
#include <stdio.h>
#include <array>
#include <memory>
#include <string>
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/containers/heap_array.h"
#include "base/logging.h"
#include "build/build_config.h"
#include "gpu/command_buffer/common/gles2_cmd_utils.h"
#include "gpu/config/gpu_driver_bug_workarounds.h"
#include "gpu/config/gpu_info_collector.h"
#include "gpu/config/gpu_preferences.h"
#include "gpu/config/gpu_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gl/gl_utils.h"
#include "ui/gl/gl_version_info.h"
#include "ui/gl/init/gl_factory.h"
namespace gpu {
// GCC requires these declarations, but MSVC requires they not be present.
#ifndef COMPILER_MSVC
const uint8_t GLTestHelper::kCheckClearValue;
#endif
gl::GLDisplay* GLTestHelper::InitializeGL(gl::GLImplementation gl_impl) {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
gpu::TrySetNonSoftwareDevicePreferenceForTesting(gl::GpuPreference::kDefault);
#endif
gl::GLDisplay* display = nullptr;
if (gl_impl == gl::GLImplementation::kGLImplementationNone) {
display = gl::init::InitializeGLNoExtensionsOneOff(
/*init_bindings=*/true,
/*gpu_preference=*/gl::GpuPreference::kDefault);
} else {
if (!gl::init::InitializeStaticGLBindingsImplementation(
gl::GLImplementationParts(gl_impl))) {
return nullptr;
}
display = gl::init::InitializeGLOneOffPlatformImplementation(
/*disable_gl_drawing=*/false,
/*init_extensions=*/false,
/*gpu_preference=*/gl::GpuPreference::kDefault);
}
if (!display)
return nullptr;
gpu::GPUInfo gpu_info;
gpu::CollectGraphicsInfoForTesting(&gpu_info);
gpu::GLManager::g_gpu_feature_info = gpu::ComputeGpuFeatureInfo(
gpu_info, gpu::GpuPreferences(), base::CommandLine::ForCurrentProcess(),
nullptr // needs_more_info
);
gl::init::SetDisabledExtensionsPlatform(
gpu::GLManager::g_gpu_feature_info.disabled_extensions);
if (!gl::init::InitializeExtensionSettingsOneOffPlatform(display))
return nullptr;
return display;
}
gl::GLDisplay* GLTestHelper::InitializeGLDefault() {
return GLTestHelper::InitializeGL(
gl::GLImplementation::kGLImplementationNone);
}
bool GLTestHelper::HasExtension(const char* extension) {
// Pad with an extra space to ensure that |extension| is not a substring of
// another extension.
std::string extensions =
std::string(reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS))) +
" ";
std::string extension_padded = std::string(extension) + " ";
return base::Contains(extensions, extension_padded);
}
bool GLTestHelper::CheckGLError(const char* msg, int line) {
bool success = true;
GLenum error = GL_NO_ERROR;
while ((error = glGetError()) != GL_NO_ERROR) {
success = false;
EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), error)
<< "GL ERROR in " << msg << " at line " << line << " : " << error;
}
return success;
}
GLuint GLTestHelper::CompileShader(GLenum type, const char* shaderSrc) {
GLuint shader = glCreateShader(type);
// Load the shader source
glShaderSource(shader, 1, &shaderSrc, nullptr);
// Compile the shader
glCompileShader(shader);
return shader;
}
GLuint GLTestHelper::LoadShader(GLenum type, const char* shaderSrc) {
GLuint shader = CompileShader(type, shaderSrc);
// Check the compile status
GLint value = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &value);
if (value == 0) {
char buffer[1024];
GLsizei length = 0;
glGetShaderInfoLog(shader, sizeof(buffer), &length, buffer);
std::string log(buffer, length);
EXPECT_EQ(1, value) << "Error compiling shader: " << log;
glDeleteShader(shader);
shader = 0;
}
return shader;
}
GLuint GLTestHelper::LinkProgram(
GLuint vertex_shader, GLuint fragment_shader) {
// Create the program object
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
// Link the program
glLinkProgram(program);
return program;
}
GLuint GLTestHelper::SetupProgram(
GLuint vertex_shader, GLuint fragment_shader) {
GLuint program = LinkProgram(vertex_shader, fragment_shader);
// Check the link status
GLint linked = 0;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (linked == 0) {
char buffer[1024];
GLsizei length = 0;
glGetProgramInfoLog(program, sizeof(buffer), &length, buffer);
std::string log(buffer, length);
EXPECT_EQ(1, linked) << "Error linking program: " << log;
glDeleteProgram(program);
program = 0;
}
return program;
}
GLuint GLTestHelper::LoadProgram(
const char* vertex_shader_source,
const char* fragment_shader_source) {
GLuint vertex_shader = LoadShader(
GL_VERTEX_SHADER, vertex_shader_source);
GLuint fragment_shader = LoadShader(
GL_FRAGMENT_SHADER, fragment_shader_source);
if (!vertex_shader || !fragment_shader) {
return 0;
}
return SetupProgram(vertex_shader, fragment_shader);
}
GLuint GLTestHelper::SetupUnitQuad(GLint position_location) {
GLuint vbo = 0;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
static const float vertices[] = {
1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(position_location);
glVertexAttribPointer(position_location, 2, GL_FLOAT, GL_FALSE, 0, 0);
return vbo;
}
std::vector<GLuint> GLTestHelper::SetupIndexedUnitQuad(
GLint position_location) {
GLuint array_buffer = SetupUnitQuad(position_location);
static const uint8_t indices[] = {0, 1, 2, 3, 4, 5};
GLuint index_buffer = 0;
glGenBuffers(1, &index_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6, indices, GL_STATIC_DRAW);
std::vector<GLuint> buffers(2);
buffers[0] = array_buffer;
buffers[1] = index_buffer;
return buffers;
}
GLuint GLTestHelper::SetupColorsForUnitQuad(
GLint location, const GLfloat color[4], GLenum usage) {
GLuint vbo = 0;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
std::array<GLfloat, 6 * 4> vertices;
for (int ii = 0; ii < 6; ++ii) {
for (int jj = 0; jj < 4; ++jj) {
vertices[ii * 4 + jj] = color[jj];
}
}
glBufferData(GL_ARRAY_BUFFER,
(vertices.size() * sizeof(decltype(vertices)::value_type)),
vertices.data(), usage);
glEnableVertexAttribArray(location);
glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE, 0, 0);
return vbo;
}
bool GLTestHelper::CheckPixels(GLint x,
GLint y,
GLsizei width,
GLsizei height,
GLint tolerance,
const uint8_t* color,
const uint8_t* mask) {
std::vector<uint8_t> colors(width * height * 4);
for (int i = 0; i < width * height * 4; i += 4)
memcpy(&colors[i], color, 4);
return CheckPixels(x, y, width, height, tolerance, colors, mask);
}
bool GLTestHelper::CheckPixels(GLint x,
GLint y,
GLsizei width,
GLsizei height,
GLint tolerance,
const std::vector<uint8_t>& expected,
const uint8_t* mask) {
GLsizei size = width * height * 4;
std::vector<uint8_t> pixels(size, kCheckClearValue);
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
int bad_count = 0;
for (GLint yy = 0; yy < height; ++yy) {
for (GLint xx = 0; xx < width; ++xx) {
int offset = yy * width * 4 + xx * 4;
for (int jj = 0; jj < 4; ++jj) {
uint8_t actual = pixels[offset + jj];
uint8_t expected_component = expected[offset + jj];
int diff = actual - expected_component;
diff = diff < 0 ? -diff: diff;
if ((!mask || mask[jj]) && diff > tolerance) {
EXPECT_EQ(static_cast<int>(expected_component),
static_cast<int>(actual))
<< " at " << (xx + x) << ", " << (yy + y) << " channel " << jj;
++bad_count;
// Exit early just so we don't spam the log but we print enough
// to hopefully make it easy to diagnose the issue.
if (bad_count > 16) {
return false;
}
}
}
}
}
return bad_count == 0;
}
namespace {
void Set16BitValue(uint8_t dest[2], uint16_t value) {
dest[0] = value & 0xFFu;
dest[1] = value >> 8;
}
void Set32BitValue(uint8_t dest[4], uint32_t value) {
dest[0] = (value >> 0) & 0xFFu;
dest[1] = (value >> 8) & 0xFFu;
dest[2] = (value >> 16) & 0xFFu;
dest[3] = (value >> 24) & 0xFFu;
}
struct BitmapHeaderFile {
uint8_t magic[2];
uint8_t size[4];
uint8_t reserved[4];
uint8_t offset[4];
};
struct BitmapInfoHeader{
uint8_t size[4];
uint8_t width[4];
uint8_t height[4];
uint8_t planes[2];
uint8_t bit_count[2];
uint8_t compression[4];
uint8_t size_image[4];
uint8_t x_pels_per_meter[4];
uint8_t y_pels_per_meter[4];
uint8_t clr_used[4];
uint8_t clr_important[4];
};
} // namespace
bool GLTestHelper::SaveBackbufferAsBMP(
const char* filename, int width, int height) {
FILE* fp = fopen(filename, "wb");
EXPECT_TRUE(fp != nullptr);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
int num_pixels = width * height;
int size = num_pixels * 4;
auto data = base::HeapArray<uint8_t>::WithSize(size);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data.data());
// RGBA to BGRA
for (int ii = 0; ii < num_pixels; ++ii) {
int offset = ii * 4;
uint8_t t = data[offset + 0];
data[offset + 0] = data[offset + 2];
data[offset + 2] = t;
}
BitmapHeaderFile bhf;
BitmapInfoHeader bih;
bhf.magic[0] = 'B';
bhf.magic[1] = 'M';
Set32BitValue(bhf.size, 0);
Set32BitValue(bhf.reserved, 0);
Set32BitValue(bhf.offset, sizeof(bhf) + sizeof(bih));
Set32BitValue(bih.size, sizeof(bih));
Set32BitValue(bih.width, width);
Set32BitValue(bih.height, height);
Set16BitValue(bih.planes, 1);
Set16BitValue(bih.bit_count, 32);
Set32BitValue(bih.compression, 0);
Set32BitValue(bih.x_pels_per_meter, 0);
Set32BitValue(bih.y_pels_per_meter, 0);
Set32BitValue(bih.clr_used, 0);
Set32BitValue(bih.clr_important, 0);
fwrite(&bhf, sizeof(bhf), 1, fp);
fwrite(&bih, sizeof(bih), 1, fp);
fwrite(data.data(), size, 1, fp);
fclose(fp);
return true;
}
void GLTestHelper::DrawTextureQuad(const GLenum texture_target,
const char* vertex_src,
const char* fragment_src,
const char* position_name,
const char* sampler_name,
const char* face_name) {
GLuint program = GLTestHelper::LoadProgram(vertex_src, fragment_src);
EXPECT_NE(program, 0u);
glUseProgram(program);
GLint position_loc = glGetAttribLocation(program, position_name);
GLint sampler_location = glGetUniformLocation(program, sampler_name);
ASSERT_NE(position_loc, -1);
ASSERT_NE(sampler_location, -1);
GLint face_loc = -1;
if (gpu::gles2::GLES2Util::GLFaceTargetToTextureTarget(texture_target) ==
GL_TEXTURE_CUBE_MAP) {
ASSERT_NE(face_name, nullptr);
face_loc = glGetUniformLocation(program, face_name);
ASSERT_NE(face_loc, -1);
glUniform1i(face_loc, texture_target);
}
GLuint vertex_buffer = GLTestHelper::SetupUnitQuad(position_loc);
ASSERT_NE(vertex_buffer, 0u);
glActiveTexture(GL_TEXTURE0);
glUniform1i(sampler_location, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDeleteProgram(program);
glDeleteBuffers(1, &vertex_buffer);
}
GpuCommandBufferTestEGL::GpuCommandBufferTestEGL() : gl_reinitialized_(false) {}
GpuCommandBufferTestEGL::~GpuCommandBufferTestEGL() {}
bool GpuCommandBufferTestEGL::InitializeEGL(int width, int height) {
gl::GLImplementation current_impl = gl::GetGLImplementation();
if (!(current_impl == gl::kGLImplementationEGLGLES2 ||
current_impl == gl::kGLImplementationEGLANGLE)) {
gpu::GPUInfo gpu_info;
gpu::CollectContextGraphicsInfo(&gpu_info);
gpu::CollectBasicGraphicsInfo(base::CommandLine::ForCurrentProcess(),
&gpu_info);
// See crbug.com/822716, the ATI proprietary driver has eglGetProcAddress
// but eglInitialize crashes with x11.
if (base::Contains(gpu_info.gl_vendor, "ATI Technologies Inc.")) {
LOG(INFO) << "Skip test, ATI proprietary driver crashes with egl/x11";
return false;
}
// The native EGL driver is not supported with the passthrough command
// decoder, in that case use ANGLE
gl::GLImplementationParts new_impl(gl::kGLImplementationEGLGLES2);
if (gpu_info.passthrough_cmd_decoder)
new_impl = gl::GLImplementationParts(gl::kGLImplementationEGLANGLE);
const auto allowed_impls = gl::init::GetAllowedGLImplementations();
if (!new_impl.IsAllowed(allowed_impls)) {
LOG(INFO) << "Skip test, no EGL implementation is available";
return false;
}
gl_reinitialized_ = true;
gl::init::ShutdownGL(gl_display_, false /* due_to_fallback */);
gl_display_ = GLTestHelper::InitializeGL(new_impl.gl);
if (!gl_display_) {
LOG(INFO) << "Skip test, failed to initialize EGL";
return false;
}
}
current_impl = gl::GetGLImplementation();
DCHECK(current_impl == gl::kGLImplementationEGLGLES2 ||
current_impl == gl::kGLImplementationEGLANGLE);
// Make the GL context current now to get all extensions.
GLManager::Options options;
options.size = gfx::Size(width, height);
gl_.Initialize(options);
gl_.MakeCurrent();
gl_extensions_ =
gfx::MakeExtensionSet(gl::GetGLExtensionsFromCurrentContext());
gl::GLVersionInfo gl_version_info(
reinterpret_cast<const char*>(glGetString(GL_VERSION)),
reinterpret_cast<const char*>(glGetString(GL_RENDERER)), gl_extensions_);
bool result = gl::init::GetGLWindowSystemBindingInfo(
gl_version_info, &window_system_binding_info_);
DCHECK(result);
egl_extensions_ =
gfx::MakeExtensionSet(window_system_binding_info_.extensions);
return true;
}
void GpuCommandBufferTestEGL::RestoreGLDefault() {
gl_.Destroy();
if (gl_reinitialized_) {
gl::init::ShutdownGL(gl_display_, false /* due_to_fallback */);
gl_display_ = GLTestHelper::InitializeGLDefault();
}
gl_reinitialized_ = false;
gl_extensions_.clear();
egl_extensions_.clear();
window_system_binding_info_ = gl::GLWindowSystemBindingInfo();
}
} // namespace gpu
|