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
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gl/test/gl_test_support.h"
#include <array>
#include <vector>
#include "base/bit_cast.h"
#include "base/check_op.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "gpu/config/gpu_util.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gfx/half_float.h"
#include "ui/gl/init/gl_factory.h"
#include "ui/gl/test/gl_surface_test_support.h"
#if BUILDFLAG(IS_OZONE)
#include "base/run_loop.h"
#include "ui/base/ui_base_features.h"
#include "ui/ozone/public/ozone_platform.h"
#endif
namespace gl {
namespace {
template <typename T>
void rgb_to_yuv(uint8_t r, uint8_t g, uint8_t b, T* y, T* u, T* v) {
// These values are used in the transformation from YUV to RGB color values.
// They are taken from http://www.fourcc.org/fccyvrgb.php
*y = (0.257 * r) + (0.504 * g) + (0.098 * b) + 16;
*u = -(0.148 * r) - (0.291 * g) + (0.439 * b) + 128;
*v = (0.439 * r) - (0.368 * g) - (0.071 * b) + 128;
}
UNSAFE_BUFFER_USAGE base::span<uint8_t> ToSpan_uint8(uint8_t* data,
size_t start,
size_t size) {
return UNSAFE_BUFFERS(base::span<uint8_t>(data + start, size));
}
UNSAFE_BUFFER_USAGE base::span<uint16_t> ToSpan_uint16(uint8_t* data,
size_t start,
size_t size) {
uint16_t* pointer = reinterpret_cast<uint16_t*>(UNSAFE_BUFFERS(data + start));
return UNSAFE_BUFFERS(base::span<uint16_t>(pointer, size));
}
UNSAFE_BUFFER_USAGE base::span<uint32_t> ToSpan_uint32(uint8_t* data,
size_t start,
size_t size) {
uint32_t* pointer = reinterpret_cast<uint32_t*>(UNSAFE_BUFFERS(data + start));
return UNSAFE_BUFFERS(base::span<uint32_t>(pointer, size));
}
UNSAFE_BUFFER_USAGE base::span<uint64_t> ToSpan_uint64(uint8_t* data,
size_t start,
size_t size) {
uint64_t* pointer = reinterpret_cast<uint64_t*>(UNSAFE_BUFFERS(data + start));
return UNSAFE_BUFFERS(base::span<uint64_t>(pointer, size));
}
} // namespace
// static
GLDisplay* GLTestSupport::InitializeGL(
std::optional<GLImplementationParts> prefered_impl) {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
gpu::TrySetNonSoftwareDevicePreferenceForTesting(
gl::GpuPreference ::kDefault);
#endif
#if BUILDFLAG(IS_OZONE)
ui::OzonePlatform::InitParams params;
params.single_process = true;
ui::OzonePlatform::InitializeForGPU(params);
#endif
std::vector<GLImplementationParts> allowed_impls =
init::GetAllowedGLImplementations();
DCHECK(!allowed_impls.empty());
GLImplementationParts impl =
prefered_impl ? *prefered_impl : allowed_impls[0];
DCHECK(impl.IsAllowed(allowed_impls));
GLDisplay* display =
GLSurfaceTestSupport::InitializeOneOffImplementation(impl);
#if BUILDFLAG(IS_OZONE)
// Make sure all the tasks posted to the current task runner by the
// initialization functions are run before running the tests.
base::RunLoop().RunUntilIdle();
#endif
return display;
}
// static
void GLTestSupport::CleanupGL(GLDisplay* display) {
GLSurfaceTestSupport::ShutdownGL(display);
}
// static
void GLTestSupport::SetBufferDataToColor(int width,
int height,
int stride,
int plane,
gfx::BufferFormat format,
base::span<const uint8_t, 4> color,
uint8_t* data) {
switch (format) {
case gfx::BufferFormat::R_8:
DCHECK_EQ(0, plane);
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint8(data, y * stride, width));
std::ranges::fill(row, color[0]); // R
}
return;
case gfx::BufferFormat::RG_88:
DCHECK_EQ(0, plane);
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint16(data, y * stride, width));
std::ranges::fill(row, (color[1] << 8) | // G
color[0]); // R
}
return;
case gfx::BufferFormat::R_16:
DCHECK_EQ(0, plane);
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint16(data, y * stride, width));
std::ranges::fill(row, color[0] << 8); // R
}
return;
case gfx::BufferFormat::RG_1616:
DCHECK_EQ(0, plane);
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint32(data, y * stride, width));
std::ranges::fill(row, (color[1] << 24) | // G
(color[0] << 8)); // R
}
return;
case gfx::BufferFormat::RGBA_4444:
DCHECK_EQ(0, plane);
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint16(data, y * stride, width));
std::ranges::fill(row, ((color[3] & 0xf) << 4) | // A
(color[2] & 0xf) | // B
(color[1] << 12) | // G
((color[0] & 0xf) << 8)); // R
}
return;
case gfx::BufferFormat::BGR_565:
DCHECK_EQ(0, plane);
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint16(data, y * stride, width));
std::ranges::fill(row, ((color[0] >> 3) << 11) | // R
((color[1] >> 2) << 5) | // G
(color[2] >> 3)); // B
}
return;
case gfx::BufferFormat::RGBX_8888:
DCHECK_EQ(0, plane);
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint32(data, y * stride, width));
std::ranges::fill(row, (0xaa << 24) | // unused
(color[2] << 16) | // B
(color[1] << 8) | // G
color[0]); // R
}
return;
case gfx::BufferFormat::RGBA_8888:
DCHECK_EQ(0, plane);
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint32(data, y * stride, width));
std::ranges::fill(row, (color[3] << 24) | // A
(color[2] << 16) | // B
(color[1] << 8) | // G
color[0]); // R
}
return;
case gfx::BufferFormat::BGRX_8888:
DCHECK_EQ(0, plane);
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint32(data, y * stride, width));
std::ranges::fill(row, (0xaa << 24) | // unused
(color[0] << 16) | // R
(color[1] << 8) | // G
color[2]); // B
}
return;
case gfx::BufferFormat::BGRA_1010102: {
DCHECK_EQ(0, plane);
DCHECK_EQ(63, color[3] % 64) << "Alpha channel doesn't have enough "
"precision for the supplied value";
const uint8_t scaled_alpha = color[3] >> 6;
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint32(data, y * stride, width));
std::ranges::fill(
row,
(scaled_alpha << 30) | // A
(((color[0] << 2) | (color[0] >> 6)) << 20) | // R
(((color[1] << 2) | (color[1] >> 6)) << 10) | // G
((color[2] << 2) | (color[2] >> 6))); // B
}
return;
}
case gfx::BufferFormat::RGBA_1010102: {
DCHECK_EQ(0, plane);
DCHECK_EQ(63, color[3] % 64) << "Alpha channel doesn't have enough "
"precision for the supplied value";
const uint8_t scaled_alpha = color[3] >> 6;
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint32(data, y * stride, width));
std::ranges::fill(
row, (scaled_alpha << 30) | // A
(((color[2] << 2) | (color[2] >> 6)) << 20) | // B
(((color[1] << 2) | (color[1] >> 6)) << 10) | // G
((color[0] << 2) | (color[0] >> 6))); // R
}
return;
}
case gfx::BufferFormat::BGRA_8888:
DCHECK_EQ(0, plane);
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint32(data, y * stride, width));
std::ranges::fill(row, (color[3] << 24) | // A
(color[0] << 16) | // R
(color[1] << 8) | // G
color[2]); // B
}
return;
case gfx::BufferFormat::RGBA_F16: {
DCHECK_EQ(0, plane);
float float_color[4] = {
color[0] / 255.f, // R
color[1] / 255.f, // G
color[2] / 255.f, // B
color[3] / 255.f, // A
};
uint16_t half_float_color[4];
gfx::FloatToHalfFloat(float_color, half_float_color, 4);
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint64(data, y * stride, width));
std::ranges::fill(row, base::bit_cast<uint64_t>(half_float_color));
}
return;
}
case gfx::BufferFormat::YVU_420: {
DCHECK_LT(plane, 3);
DCHECK_EQ(0, height % 2);
DCHECK_EQ(0, width % 2);
std::array<uint8_t, 3> yvu = {};
rgb_to_yuv(color[0], color[1], color[2], &yvu[0], &yvu[2], &yvu[1]);
if (plane == 0) {
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint8(data, stride * y, width));
std::ranges::fill(row, yvu[0]);
}
} else {
for (int y = 0; y < height / 2; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint8(data, stride * y, width / 2));
std::ranges::fill(row, yvu[plane]);
}
}
return;
}
case gfx::BufferFormat::YUV_420_BIPLANAR: {
DCHECK_LT(plane, 2);
DCHECK_EQ(0, height % 2);
DCHECK_EQ(0, width % 2);
std::array<uint8_t, 3> yuv = {};
rgb_to_yuv(color[0], color[1], color[2], &yuv[0], &yuv[1], &yuv[2]);
if (plane == 0) {
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint8(data, stride * y, width));
std::ranges::fill(row, yuv[0]);
}
} else {
for (int y = 0; y < height / 2; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint16(data, stride * y, width / 2));
std::ranges::fill(row, (yuv[2] << 8) | yuv[1]);
}
}
return;
}
case gfx::BufferFormat::YUVA_420_TRIPLANAR: {
DCHECK_LT(plane, 3);
DCHECK_EQ(0, height % 2);
DCHECK_EQ(0, width % 2);
std::array<uint8_t, 4> yuv = {};
rgb_to_yuv(color[0], color[1], color[2], &yuv[0], &yuv[1], &yuv[2]);
yuv[3] = color[3];
if (plane == 0) {
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint8(data, stride * y, width));
std::ranges::fill(row, yuv[0]);
}
} else if (plane == 1) {
for (int y = 0; y < height / 2; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint16(data, stride * y, width / 2));
std::ranges::fill(row, (yuv[2] << 8) | yuv[1]);
}
} else {
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint8(data, stride * y, width));
std::ranges::fill(row, yuv[3]);
}
}
return;
}
case gfx::BufferFormat::P010: {
DCHECK_LT(plane, 3);
DCHECK_EQ(0, height % 2);
DCHECK_EQ(0, width % 2);
std::array<uint16_t, 3> yuv = {};
rgb_to_yuv(color[0], color[1], color[2], &yuv[0], &yuv[1], &yuv[2]);
if (plane == 0) {
for (int y = 0; y < height; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint16(data, y * stride, width));
std::ranges::fill(row, yuv[0] << 2);
}
} else {
for (int y = 0; y < height / 2; ++y) {
auto row = UNSAFE_TODO(ToSpan_uint32(data, y * stride, width / 2));
std::ranges::fill(row, (yuv[2] << 18) | (yuv[1] << 2));
}
}
return;
}
}
NOTREACHED();
}
} // namespace gl
|