Hash :
24ddc7a8
Author :
Date :
2018-06-11T14:56:34
WGL: Support unvirtualized contexts and unsafe multithreading. When using unvirtualized contexts, DisplayWGL still creates a Renderer for managing any internal GL resources such as emulated back buffers for DXGISwapChainWindowSurfaceWGL or D3DTextureSurfaceWGL but also creates a new Renderer for each GL context. All created contexts share resources. BUG=angleproject:2464 Change-Id: I945502514079368e062beef70bed49c61ed44403 Reviewed-on: https://chromium-review.googlesource.com/1097459 Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: Yuly Novikov <ynovikov@chromium.org>
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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// renderer_utils:
// Helper methods pertaining to most or all back-ends.
//
#include "libANGLE/renderer/renderer_utils.h"
#include "image_util/copyimage.h"
#include "image_util/imageformats.h"
#include "libANGLE/AttributeMap.h"
#include "libANGLE/Context.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/renderer/ContextImpl.h"
#include "libANGLE/renderer/Format.h"
#include <string.h>
#include "common/utilities.h"
namespace rx
{
namespace
{
typedef std::pair<gl::FormatType, ColorWriteFunction> FormatWriteFunctionPair;
typedef std::map<gl::FormatType, ColorWriteFunction> FormatWriteFunctionMap;
static inline void InsertFormatWriteFunctionMapping(FormatWriteFunctionMap *map,
GLenum format,
GLenum type,
ColorWriteFunction writeFunc)
{
map->insert(FormatWriteFunctionPair(gl::FormatType(format, type), writeFunc));
}
static FormatWriteFunctionMap BuildFormatWriteFunctionMap()
{
using namespace angle; // For image writing functions
FormatWriteFunctionMap map;
// clang-format off
// | Format | Type | Color write function |
InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_BYTE, WriteColor<R8G8B8A8S, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, WriteColor<R4G4B4A4, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, WriteColor<R5G5B5A1, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, WriteColor<R10G10B10A2, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_FLOAT, WriteColor<R32G32B32A32F, GLfloat>);
InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_HALF_FLOAT, WriteColor<R16G16B16A16F, GLfloat>);
InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_HALF_FLOAT_OES, WriteColor<R16G16B16A16F, GLfloat>);
InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT,
WriteColor<R16G16B16A16, GLfloat>);
InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_SHORT, WriteColor<R16G16B16A16S, GLfloat>);
InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_BYTE, WriteColor<R8G8B8A8S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16B16A16, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_SHORT, WriteColor<R16G16B16A16S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32B32A32, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_INT, WriteColor<R32G32B32A32S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, WriteColor<R10G10B10A2, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_BYTE, WriteColor<R8G8B8S, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, WriteColor<R5G6B5, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, WriteColor<R11G11B10F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, WriteColor<R9G9B9E5, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_FLOAT, WriteColor<R32G32B32F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_HALF_FLOAT, WriteColor<R16G16B16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_HALF_FLOAT_OES, WriteColor<R16G16B16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_SHORT,
WriteColor<R16G16B16, GLfloat>);
InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_SHORT, WriteColor<R16G16B16S, GLfloat>);
InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_BYTE, WriteColor<R8G8B8S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16B16, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_SHORT, WriteColor<R16G16B16S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32B32, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_INT, WriteColor<R32G32B32S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_RG, GL_UNSIGNED_BYTE, WriteColor<R8G8, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RG, GL_BYTE, WriteColor<R8G8S, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RG, GL_FLOAT, WriteColor<R32G32F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RG, GL_HALF_FLOAT, WriteColor<R16G16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RG, GL_HALF_FLOAT_OES, WriteColor<R16G16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RG, GL_UNSIGNED_SHORT, WriteColor<R16G16, GLfloat>);
InsertFormatWriteFunctionMapping(&map, GL_RG, GL_SHORT, WriteColor<R16G16S, GLfloat>);
InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_BYTE, WriteColor<R8G8S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_SHORT, WriteColor<R16G16S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_INT, WriteColor<R32G32S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_RED, GL_UNSIGNED_BYTE, WriteColor<R8, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RED, GL_BYTE, WriteColor<R8S, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RED, GL_FLOAT, WriteColor<R32F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RED, GL_HALF_FLOAT, WriteColor<R16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RED, GL_HALF_FLOAT_OES, WriteColor<R16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_RED, GL_UNSIGNED_SHORT, WriteColor<R16, GLfloat>);
InsertFormatWriteFunctionMapping(&map, GL_RED, GL_SHORT, WriteColor<R16S, GLfloat>);
InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_BYTE, WriteColor<R8S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_SHORT, WriteColor<R16S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_INT, WriteColor<R32, GLuint> );
InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_INT, WriteColor<R32S, GLint> );
InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, WriteColor<L8A8, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_UNSIGNED_BYTE, WriteColor<L8, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_UNSIGNED_BYTE, WriteColor<A8, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_FLOAT, WriteColor<L32A32F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_FLOAT, WriteColor<L32F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_FLOAT, WriteColor<A32F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT, WriteColor<L16A16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, WriteColor<L16A16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_HALF_FLOAT, WriteColor<L16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_HALF_FLOAT_OES, WriteColor<L16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_HALF_FLOAT, WriteColor<A16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_HALF_FLOAT_OES, WriteColor<A16F, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_BYTE, WriteColor<B8G8R8A8, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT, WriteColor<A4R4G4B4, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT, WriteColor<A1R5G5B5, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_SRGB_EXT, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLfloat> );
InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE, nullptr );
InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE, nullptr );
InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, GL_UNSIGNED_BYTE, nullptr );
InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, GL_UNSIGNED_BYTE, nullptr );
InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, nullptr );
InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr );
InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr );
InsertFormatWriteFunctionMapping(&map, GL_STENCIL, GL_UNSIGNED_BYTE, nullptr );
InsertFormatWriteFunctionMapping(&map, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr );
InsertFormatWriteFunctionMapping(&map, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, nullptr );
// clang-format on
return map;
}
void CopyColor(gl::ColorF *color)
{
// No-op
}
void PremultiplyAlpha(gl::ColorF *color)
{
color->red *= color->alpha;
color->green *= color->alpha;
color->blue *= color->alpha;
}
void UnmultiplyAlpha(gl::ColorF *color)
{
if (color->alpha != 0.0f)
{
float invAlpha = 1.0f / color->alpha;
color->red *= invAlpha;
color->green *= invAlpha;
color->blue *= invAlpha;
}
}
void ClipChannelsR(gl::ColorF *color)
{
color->green = 0.0f;
color->blue = 0.0f;
color->alpha = 1.0f;
}
void ClipChannelsRG(gl::ColorF *color)
{
color->blue = 0.0f;
color->alpha = 1.0f;
}
void ClipChannelsRGB(gl::ColorF *color)
{
color->alpha = 1.0f;
}
void ClipChannelsLuminance(gl::ColorF *color)
{
color->alpha = 1.0f;
}
void ClipChannelsAlpha(gl::ColorF *color)
{
color->red = 0.0f;
color->green = 0.0f;
color->blue = 0.0f;
}
void ClipChannelsNoOp(gl::ColorF *color)
{
}
void WriteUintColor(const gl::ColorF &color,
ColorWriteFunction colorWriteFunction,
uint8_t *destPixelData)
{
gl::ColorUI destColor(
static_cast<unsigned int>(color.red * 255), static_cast<unsigned int>(color.green * 255),
static_cast<unsigned int>(color.blue * 255), static_cast<unsigned int>(color.alpha * 255));
colorWriteFunction(reinterpret_cast<const uint8_t *>(&destColor), destPixelData);
}
void WriteFloatColor(const gl::ColorF &color,
ColorWriteFunction colorWriteFunction,
uint8_t *destPixelData)
{
colorWriteFunction(reinterpret_cast<const uint8_t *>(&color), destPixelData);
}
template <typename T, int cols, int rows>
bool TransposeExpandMatrix(T *target, const GLfloat *value)
{
constexpr int targetWidth = 4;
constexpr int targetHeight = rows;
constexpr int srcWidth = rows;
constexpr int srcHeight = cols;
constexpr int copyWidth = std::min(targetHeight, srcWidth);
constexpr int copyHeight = std::min(targetWidth, srcHeight);
T staging[targetWidth * targetHeight] = {0};
for (int x = 0; x < copyWidth; x++)
{
for (int y = 0; y < copyHeight; y++)
{
staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
}
}
if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
{
return false;
}
memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
return true;
}
template <typename T, int cols, int rows>
bool ExpandMatrix(T *target, const GLfloat *value)
{
constexpr int kTargetWidth = 4;
constexpr int kTargetHeight = rows;
constexpr int kSrcWidth = cols;
constexpr int kSrcHeight = rows;
constexpr int kCopyWidth = std::min(kTargetWidth, kSrcWidth);
constexpr int kCopyHeight = std::min(kTargetHeight, kSrcHeight);
T staging[kTargetWidth * kTargetHeight] = {0};
for (int y = 0; y < kCopyHeight; y++)
{
for (int x = 0; x < kCopyWidth; x++)
{
staging[y * kTargetWidth + x] = static_cast<T>(value[y * kSrcWidth + x]);
}
}
if (memcmp(target, staging, kTargetWidth * kTargetHeight * sizeof(T)) == 0)
{
return false;
}
memcpy(target, staging, kTargetWidth * kTargetHeight * sizeof(T));
return true;
}
} // anonymous namespace
PackPixelsParams::PackPixelsParams()
: format(GL_NONE), type(GL_NONE), outputPitch(0), packBuffer(nullptr), offset(0)
{
}
PackPixelsParams::PackPixelsParams(const gl::Rectangle &areaIn,
GLenum formatIn,
GLenum typeIn,
GLuint outputPitchIn,
const gl::PixelPackState &packIn,
gl::Buffer *packBufferIn,
ptrdiff_t offsetIn)
: area(areaIn),
format(formatIn),
type(typeIn),
outputPitch(outputPitchIn),
packBuffer(packBufferIn),
pack(),
offset(offsetIn)
{
pack.alignment = packIn.alignment;
pack.reverseRowOrder = packIn.reverseRowOrder;
}
void PackPixels(const PackPixelsParams ¶ms,
const angle::Format &sourceFormat,
int inputPitchIn,
const uint8_t *sourceIn,
uint8_t *destWithoutOffset)
{
uint8_t *destWithOffset = destWithoutOffset + params.offset;
const uint8_t *source = sourceIn;
int inputPitch = inputPitchIn;
if (params.pack.reverseRowOrder)
{
source += inputPitch * (params.area.height - 1);
inputPitch = -inputPitch;
}
const gl::InternalFormat &internalFormat =
gl::GetInternalFormatInfo(params.format, params.type);
if (sourceFormat.glInternalFormat == internalFormat.sizedInternalFormat)
{
// Direct copy possible
for (int y = 0; y < params.area.height; ++y)
{
memcpy(destWithOffset + y * params.outputPitch, source + y * inputPitch,
params.area.width * sourceFormat.pixelBytes);
}
return;
}
gl::FormatType formatType(params.format, params.type);
ColorCopyFunction fastCopyFunc =
GetFastCopyFunction(sourceFormat.fastCopyFunctions, formatType);
const auto &destFormatInfo = gl::GetInternalFormatInfo(formatType.format, formatType.type);
if (fastCopyFunc)
{
// Fast copy is possible through some special function
for (int y = 0; y < params.area.height; ++y)
{
for (int x = 0; x < params.area.width; ++x)
{
uint8_t *dest =
destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes;
const uint8_t *src = source + y * inputPitch + x * sourceFormat.pixelBytes;
fastCopyFunc(src, dest);
}
}
return;
}
ColorWriteFunction colorWriteFunction = GetColorWriteFunction(formatType);
// Maximum size of any Color<T> type used.
uint8_t temp[16];
static_assert(sizeof(temp) >= sizeof(gl::ColorF) && sizeof(temp) >= sizeof(gl::ColorUI) &&
sizeof(temp) >= sizeof(gl::ColorI),
"Unexpected size of gl::Color struct.");
const auto &colorReadFunction = sourceFormat.colorReadFunction;
for (int y = 0; y < params.area.height; ++y)
{
for (int x = 0; x < params.area.width; ++x)
{
uint8_t *dest = destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes;
const uint8_t *src = source + y * inputPitch + x * sourceFormat.pixelBytes;
// readFunc and writeFunc will be using the same type of color, CopyTexImage
// will not allow the copy otherwise.
colorReadFunction(src, temp);
colorWriteFunction(temp, dest);
}
}
}
ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType)
{
static const FormatWriteFunctionMap formatTypeMap = BuildFormatWriteFunctionMap();
auto iter = formatTypeMap.find(formatType);
ASSERT(iter != formatTypeMap.end());
if (iter != formatTypeMap.end())
{
return iter->second;
}
else
{
return nullptr;
}
}
ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions,
const gl::FormatType &formatType)
{
return fastCopyFunctions.get(formatType);
}
bool FastCopyFunctionMap::has(const gl::FormatType &formatType) const
{
return (get(formatType) != nullptr);
}
ColorCopyFunction FastCopyFunctionMap::get(const gl::FormatType &formatType) const
{
for (size_t index = 0; index < mSize; ++index)
{
if (mData[index].format == formatType.format && mData[index].type == formatType.type)
{
return mData[index].func;
}
}
return nullptr;
}
bool ShouldUseDebugLayers(const egl::AttributeMap &attribs)
{
EGLAttrib debugSetting =
attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE);
// Prefer to enable debug layers if compiling in Debug, and disabled in Release.
#if defined(ANGLE_ENABLE_ASSERTS)
return (debugSetting != EGL_FALSE);
#else
return (debugSetting == EGL_TRUE);
#endif // defined(ANGLE_ENABLE_ASSERTS)
}
bool ShouldUseVirtualizedContexts(const egl::AttributeMap &attribs, bool defaultValue)
{
EGLAttrib virtualizedContextRequest =
attribs.get(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE, EGL_DONT_CARE);
if (defaultValue)
{
return (virtualizedContextRequest != EGL_FALSE);
}
else
{
return (virtualizedContextRequest == EGL_TRUE);
}
}
void CopyImageCHROMIUM(const uint8_t *sourceData,
size_t sourceRowPitch,
size_t sourcePixelBytes,
ColorReadFunction colorReadFunction,
uint8_t *destData,
size_t destRowPitch,
size_t destPixelBytes,
ColorWriteFunction colorWriteFunction,
GLenum destUnsizedFormat,
GLenum destComponentType,
size_t width,
size_t height,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha)
{
using ConversionFunction = void (*)(gl::ColorF *);
ConversionFunction conversionFunction = CopyColor;
if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha)
{
if (unpackPremultiplyAlpha)
{
conversionFunction = PremultiplyAlpha;
}
else
{
conversionFunction = UnmultiplyAlpha;
}
}
auto clipChannelsFunction = ClipChannelsNoOp;
switch (destUnsizedFormat)
{
case GL_RED:
clipChannelsFunction = ClipChannelsR;
break;
case GL_RG:
clipChannelsFunction = ClipChannelsRG;
break;
case GL_RGB:
clipChannelsFunction = ClipChannelsRGB;
break;
case GL_LUMINANCE:
clipChannelsFunction = ClipChannelsLuminance;
break;
case GL_ALPHA:
clipChannelsFunction = ClipChannelsAlpha;
break;
}
auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor;
for (size_t y = 0; y < height; y++)
{
for (size_t x = 0; x < width; x++)
{
const uint8_t *sourcePixelData = sourceData + y * sourceRowPitch + x * sourcePixelBytes;
gl::ColorF sourceColor;
colorReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor));
conversionFunction(&sourceColor);
clipChannelsFunction(&sourceColor);
size_t destY = 0;
if (unpackFlipY)
{
destY += (height - 1);
destY -= y;
}
else
{
destY += y;
}
uint8_t *destPixelData = destData + destY * destRowPitch + x * destPixelBytes;
writeFunction(sourceColor, colorWriteFunction, destPixelData);
}
}
}
// IncompleteTextureSet implementation.
IncompleteTextureSet::IncompleteTextureSet()
{
}
IncompleteTextureSet::~IncompleteTextureSet()
{
}
void IncompleteTextureSet::onDestroy(const gl::Context *context)
{
// Clear incomplete textures.
for (auto &incompleteTexture : mIncompleteTextures)
{
if (incompleteTexture.get() != nullptr)
{
ANGLE_SWALLOW_ERR(incompleteTexture->onDestroy(context));
incompleteTexture.set(context, nullptr);
}
}
}
gl::Error IncompleteTextureSet::getIncompleteTexture(
const gl::Context *context,
gl::TextureType type,
MultisampleTextureInitializer *multisampleInitializer,
gl::Texture **textureOut)
{
*textureOut = mIncompleteTextures[type].get();
if (*textureOut != nullptr)
{
return gl::NoError();
}
ContextImpl *implFactory = context->getImplementation();
const GLubyte color[] = {0, 0, 0, 255};
const gl::Extents colorSize(1, 1, 1);
gl::PixelUnpackState unpack;
unpack.alignment = 1;
const gl::Box area(0, 0, 0, 1, 1, 1);
// If a texture is external use a 2D texture for the incomplete texture
gl::TextureType createType = (type == gl::TextureType::External) ? gl::TextureType::_2D : type;
gl::Texture *tex = new gl::Texture(implFactory, std::numeric_limits<GLuint>::max(), createType);
angle::UniqueObjectPointer<gl::Texture, gl::Context> t(tex, context);
if (createType == gl::TextureType::_2DMultisample)
{
ANGLE_TRY(t->setStorageMultisample(context, createType, 1, GL_RGBA8, colorSize, true));
}
else
{
ANGLE_TRY(t->setStorage(context, createType, 1, GL_RGBA8, colorSize));
}
if (type == gl::TextureType::CubeMap)
{
for (gl::TextureTarget face : gl::AllCubeFaceTextureTargets())
{
ANGLE_TRY(
t->setSubImage(context, unpack, face, 0, area, GL_RGBA, GL_UNSIGNED_BYTE, color));
}
}
else if (type == gl::TextureType::_2DMultisample)
{
// Call a specialized clear function to init a multisample texture.
ANGLE_TRY(multisampleInitializer->initializeMultisampleTextureToBlack(context, t.get()));
}
else
{
ANGLE_TRY(t->setSubImage(context, unpack, gl::NonCubeTextureTypeToTarget(createType), 0,
area, GL_RGBA, GL_UNSIGNED_BYTE, color));
}
ANGLE_TRY(t->syncState(context));
mIncompleteTextures[type].set(context, t.release());
*textureOut = mIncompleteTextures[type].get();
return gl::NoError();
}
#define ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(cols, rows) \
template bool SetFloatUniformMatrix<cols, rows>(unsigned int, unsigned int, GLsizei, \
GLboolean, const GLfloat *, uint8_t *)
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 2);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 3);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 4);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 3);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 2);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(2, 4);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 2);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(3, 4);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(4, 3);
#undef ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC
template <int cols, int rows>
bool SetFloatUniformMatrix(unsigned int arrayElementOffset,
unsigned int elementCount,
GLsizei countIn,
GLboolean transpose,
const GLfloat *value,
uint8_t *targetData)
{
unsigned int count =
std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
const unsigned int targetMatrixStride = (4 * rows);
GLfloat *target = reinterpret_cast<GLfloat *>(
targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
bool dirty = false;
for (unsigned int i = 0; i < count; i++)
{
if (transpose == GL_FALSE)
{
dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
}
else
{
dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
}
target += targetMatrixStride;
value += cols * rows;
}
return dirty;
}
template void GetMatrixUniform<GLint>(GLenum, GLint *, const GLint *, bool);
template void GetMatrixUniform<GLuint>(GLenum, GLuint *, const GLuint *, bool);
void GetMatrixUniform(GLenum type, GLfloat *dataOut, const GLfloat *source, bool transpose)
{
int columns = gl::VariableColumnCount(type);
int rows = gl::VariableRowCount(type);
for (GLint col = 0; col < columns; ++col)
{
for (GLint row = 0; row < rows; ++row)
{
GLfloat *outptr = dataOut + ((col * rows) + row);
const GLfloat *inptr =
transpose ? source + ((row * 4) + col) : source + ((col * 4) + row);
*outptr = *inptr;
}
}
}
template <typename NonFloatT>
void GetMatrixUniform(GLenum type, NonFloatT *dataOut, const NonFloatT *source, bool transpose)
{
UNREACHABLE();
}
} // namespace rx