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 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/service/shared_image/shared_image_factory.h"
#include <inttypes.h>
#include <memory>
#include "base/containers/contains.h"
#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_split.h"
#include "base/trace_event/memory_dump_manager.h"
#include "build/build_config.h"
#include "build/chromecast_buildflags.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "gpu/command_buffer/common/shared_image_trace_utils.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/gles2_cmd_decoder.h"
#include "gpu/command_buffer/service/service_utils.h"
#include "gpu/command_buffer/service/shared_context_state.h"
#include "gpu/command_buffer/service/shared_image/compound_image_backing.h"
#include "gpu/command_buffer/service/shared_image/egl_image_backing_factory.h"
#include "gpu/command_buffer/service/shared_image/gl_texture_image_backing_factory.h"
#include "gpu/command_buffer/service/shared_image/raw_draw_image_backing_factory.h"
#include "gpu/command_buffer/service/shared_image/shared_image_backing.h"
#include "gpu/command_buffer/service/shared_image/shared_image_format_service_utils.h"
#include "gpu/command_buffer/service/shared_image/shared_image_manager.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "gpu/command_buffer/service/shared_image/shared_memory_image_backing_factory.h"
#include "gpu/command_buffer/service/shared_image/wrapped_sk_image_backing_factory.h"
#include "gpu/config/gpu_finch_features.h"
#include "gpu/config/gpu_preferences.h"
#include "gpu/ipc/common/gpu_memory_buffer_impl_shared_memory.h"
#include "ui/base/ozone_buildflags.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gfx/gpu_memory_buffer_handle.h"
#include "ui/gl/gl_display.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_switches.h"
#include "ui/gl/gl_utils.h"
#include "ui/gl/gl_version_info.h"
#include "ui/gl/trace_util.h"
#if BUILDFLAG(ENABLE_VULKAN)
#include "components/viz/common/gpu/vulkan_context_provider.h"
#include "gpu/command_buffer/service/shared_image/angle_vulkan_image_backing_factory.h"
#include "gpu/vulkan/vulkan_device_queue.h"
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_WIN)
#include "gpu/command_buffer/service/shared_image/external_vk_image_backing_factory.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include <vulkan/vulkan_android.h>
#endif
#endif // BUILDFLAG(ENABLE_VULKAN)
#if BUILDFLAG(IS_OZONE)
#include "gpu/command_buffer/service/shared_image/ozone_image_backing_factory.h"
#include "ui/ozone/public/gl_ozone.h"
#include "ui/ozone/public/ozone_platform.h"
#include "ui/ozone/public/surface_factory_ozone.h"
#endif
#if BUILDFLAG(IS_APPLE)
#include "gpu/command_buffer/service/shared_image/iosurface_image_backing_factory.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "gpu/command_buffer/service/dxgi_shared_handle_manager.h"
#include "gpu/command_buffer/service/shared_image/d3d_image_backing_factory.h"
#include "gpu/command_buffer/service/shared_image/dcomp_image_backing_factory.h"
#include "ui/gl/direct_composition_support.h"
#include "ui/gl/gl_angle_util_win.h"
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_FUCHSIA)
#include <lib/zx/channel.h>
#include "gpu/vulkan/vulkan_device_queue.h"
#include "gpu/vulkan/vulkan_implementation.h"
#endif // BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_ANDROID)
#include "base/android/android_hardware_buffer_compat.h"
#include "gpu/command_buffer/service/shared_image/ahardwarebuffer_image_backing_factory.h"
#endif // BUILDFLAG(IS_ANDROID)
namespace gpu {
namespace {
const char* GmbTypeToString(gfx::GpuMemoryBufferType type) {
switch (type) {
case gfx::EMPTY_BUFFER:
return "empty";
case gfx::SHARED_MEMORY_BUFFER:
return "shared_memory";
#if BUILDFLAG(IS_APPLE)
case gfx::IO_SURFACE_BUFFER:
return "platform";
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
case gfx::NATIVE_PIXMAP:
return "platform";
#endif
#if BUILDFLAG(IS_WIN)
case gfx::DXGI_SHARED_HANDLE:
return "platform";
#endif
#if BUILDFLAG(IS_ANDROID)
case gfx::ANDROID_HARDWARE_BUFFER:
return "platform";
#endif
}
NOTREACHED();
}
gfx::GpuMemoryBufferType GetNativeBufferType() {
#if BUILDFLAG(IS_APPLE)
return gfx::GpuMemoryBufferType::IO_SURFACE_BUFFER;
#elif BUILDFLAG(IS_ANDROID)
return gfx::GpuMemoryBufferType::ANDROID_HARDWARE_BUFFER;
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
return gfx::GpuMemoryBufferType::NATIVE_PIXMAP;
#elif BUILDFLAG(IS_WIN)
return gfx::GpuMemoryBufferType::DXGI_SHARED_HANDLE;
#else
return gfx::GpuMemoryBufferType::EMPTY_BUFFER;
#endif
}
bool WillGetGmbConfigFromGpu() {
#if BUILDFLAG(IS_OZONE)
// Ozone/X11 requires gpu initialization to be done before it can determine
// what formats gmb can use. This limitation comes from the requirement to
// have GLX bindings initialized. The buffer formats will be passed through
// gpu extra info.
return ui::OzonePlatform::GetInstance()
->GetPlatformProperties()
.fetch_buffer_formats_for_gmb_on_gpu;
#else
return false;
#endif
}
} // namespace
SharedImageFactory::SharedImageFactory(
const GpuPreferences& gpu_preferences,
const GpuDriverBugWorkarounds& workarounds,
const GpuFeatureInfo& gpu_feature_info,
SharedContextState* context_state,
SharedImageManager* shared_image_manager,
scoped_refptr<gpu::MemoryTracker> memory_tracker,
bool is_for_display_compositor)
: shared_image_manager_(shared_image_manager),
context_state_(context_state),
memory_type_tracker_(
std::make_unique<MemoryTypeTracker>(std::move(memory_tracker))),
is_for_display_compositor_(is_for_display_compositor),
gr_context_type_(context_state_ ? context_state_->gr_context_type()
: GrContextType::kNone),
gpu_preferences_(gpu_preferences),
#if BUILDFLAG(IS_MAC)
texture_target_for_io_surfaces_(GetTextureTargetForIOSurfaces()),
#endif
workarounds_(workarounds) {
auto shared_memory_backing_factory =
std::make_unique<SharedMemoryImageBackingFactory>();
factories_.push_back(std::move(shared_memory_backing_factory));
// if GL is disabled, it only needs SharedMemoryImageBackingFactory.
if (gl::GetGLImplementation() == gl::kGLImplementationDisabled) {
return;
}
CHECK(context_state_);
scoped_refptr<gles2::FeatureInfo> feature_info =
context_state_->feature_info();
if (!feature_info) {
// For some unit tests like SharedImageFactoryTest, |shared_context_state_|
// could be nullptr.
bool use_passthrough = gpu_preferences.use_passthrough_cmd_decoder;
feature_info = new gles2::FeatureInfo(workarounds_, gpu_feature_info);
feature_info->Initialize(ContextType::CONTEXT_TYPE_OPENGLES2,
use_passthrough, gles2::DisallowedFeatures());
}
// Skia specific factories can't be used without a Skia context.
if (gr_context_type_ != GrContextType::kNone) {
auto wrapped_sk_image_factory =
std::make_unique<WrappedSkImageBackingFactory>(context_state_);
factories_.push_back(std::move(wrapped_sk_image_factory));
if (features::IsUsingRawDraw()) {
auto factory = std::make_unique<RawDrawImageBackingFactory>();
factories_.push_back(std::move(factory));
}
}
bool use_gl =
!is_for_display_compositor_ || gr_context_type_ == GrContextType::kGL;
if (use_gl) {
// On Windows readback is slower with GLTextureImageBacking than
// D3DImageBacking so prefer D3DImageBacking for software GMBs.
bool supports_cpu_upload = !BUILDFLAG(IS_WIN);
auto gl_texture_backing_factory =
std::make_unique<GLTextureImageBackingFactory>(
gpu_preferences_, workarounds_, feature_info.get(),
context_state_->progress_reporter(), supports_cpu_upload);
factories_.push_back(std::move(gl_texture_backing_factory));
}
#if BUILDFLAG(IS_WIN)
if (gl::DirectCompositionSupported()) {
factories_.push_back(
std::make_unique<DCompImageBackingFactory>(context_state_));
}
if (IsD3DSharedImageSupported()) {
auto d3d_factory = std::make_unique<D3DImageBackingFactory>(
context_state_->GetD3D11Device(),
shared_image_manager_->dxgi_shared_handle_manager(),
context_state_->GetGLFormatCaps(), workarounds_);
d3d_backing_factory_ = d3d_factory.get();
factories_.push_back(std::move(d3d_factory));
}
{
auto gl_texture_backing_factory =
std::make_unique<GLTextureImageBackingFactory>(
gpu_preferences_, workarounds_, feature_info.get(),
context_state_->progress_reporter(),
/*supports_cpu_upload=*/true);
factories_.push_back(std::move(gl_texture_backing_factory));
}
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(ENABLE_VULKAN)
// If Chrome and ANGLE are sharing the same vulkan device queue, AngleVulkan
// backing will be used for interop.
if ((gr_context_type_ == GrContextType::kVulkan) &&
(base::FeatureList::IsEnabled(features::kVulkanFromANGLE))) {
auto factory = std::make_unique<AngleVulkanImageBackingFactory>(
gpu_preferences_, workarounds_, context_state_);
factories_.push_back(std::move(factory));
}
#if BUILDFLAG(IS_WIN)
if (gr_context_type_ == GrContextType::kVulkan) {
auto external_vk_image_factory =
std::make_unique<ExternalVkImageBackingFactory>(context_state_);
factories_.push_back(std::move(external_vk_image_factory));
}
#endif // BUILDFLAG(IS_WIN)
#endif // BUILDFLAG(ENABLE_VULKAN)
// Create EGLImageBackingFactory if egl images are supported. Note that the
// factory creation is kept here to preserve the current preference of factory
// to be used.
auto* egl_display = gl::GetDefaultDisplayEGL();
if (use_gl && egl_display && egl_display->ext->b_EGL_KHR_image_base &&
egl_display->ext->b_EGL_KHR_gl_texture_2D_image &&
egl_display->ext->b_EGL_KHR_fence_sync &&
gl::g_current_gl_driver->ext.b_GL_OES_EGL_image) {
auto egl_backing_factory = std::make_unique<EGLImageBackingFactory>(
gpu_preferences_, workarounds_, feature_info.get());
factories_.push_back(std::move(egl_backing_factory));
}
#if BUILDFLAG(IS_ANDROID)
bool is_ahb_supported = true;
if (gr_context_type_ == GrContextType::kVulkan) {
const auto& enabled_extensions = context_state_->vk_context_provider()
->GetDeviceQueue()
->enabled_extensions();
is_ahb_supported = gfx::HasExtension(
enabled_extensions,
VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME);
}
if (is_ahb_supported) {
auto ahb_factory = std::make_unique<AHardwareBufferImageBackingFactory>(
feature_info.get(), gpu_preferences_,
context_state_->vk_context_provider());
factories_.push_back(std::move(ahb_factory));
}
#elif BUILDFLAG(IS_OZONE)
// For all Ozone platforms - Desktop Linux, ChromeOS, Fuchsia, CastOS.
if (ui::OzonePlatform::GetInstance()
->GetPlatformRuntimeProperties()
.supports_native_pixmaps) {
auto ozone_factory = std::make_unique<OzoneImageBackingFactory>(
context_state_, workarounds_);
factories_.push_back(std::move(ozone_factory));
}
#if BUILDFLAG(ENABLE_VULKAN) && (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_FUCHSIA))
if (gr_context_type_ == GrContextType::kVulkan) {
auto external_vk_image_factory =
std::make_unique<ExternalVkImageBackingFactory>(context_state_);
factories_.push_back(std::move(external_vk_image_factory));
}
#endif // BUILDFLAG(ENABLE_VULKAN) && (BUILDFLAG(IS_LINUX) ||
// BUILDFLAG(IS_FUCHSIA))
#endif // BUILDFLAG(IS_OZONE)
#if BUILDFLAG(IS_APPLE)
{
auto iosurface_backing_factory =
std::make_unique<IOSurfaceImageBackingFactory>(
gr_context_type_, context_state_->GetMaxTextureSize(),
feature_info.get(), context_state_->progress_reporter(),
#if BUILDFLAG(IS_MAC)
texture_target_for_io_surfaces_
#else
GL_TEXTURE_2D
#endif
);
factories_.push_back(std::move(iosurface_backing_factory));
}
#endif
}
SharedImageFactory::~SharedImageFactory() {
DCHECK(shared_images_.empty());
}
bool SharedImageFactory::CreateSharedImage(
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
gpu::SurfaceHandle surface_handle,
SharedImageUsageSet usage,
std::string debug_label,
std::optional<SharedImagePoolId> pool_id) {
auto* factory = GetFactoryByUsage(usage, format, size,
/*pixel_data=*/{}, gfx::EMPTY_BUFFER);
if (!factory) {
LogGetFactoryFailed(usage, format, gfx::EMPTY_BUFFER, size, debug_label);
return false;
}
auto backing = factory->CreateSharedImage(
mailbox, format, surface_handle, size, color_space, surface_origin,
alpha_type, SharedImageUsageSet(usage), std::move(debug_label),
IsSharedBetweenThreads(usage));
DVLOG_IF(1, !!backing) << "CreateSharedImage[" << backing->GetName()
<< "] size=" << size.ToString()
<< " usage=" << CreateLabelForSharedImageUsage(usage)
<< " format=" << format.ToString();
return RegisterBacking(std::move(backing), std::move(pool_id));
}
bool SharedImageFactory::IsNativeBufferSupported(gfx::BufferFormat format,
gfx::BufferUsage usage) {
// Note that we are initializing the |supported_gmb_configurations_| here to
// make sure gpu service have already initialized and required metadata like
// supported buffer configurations have already been sent from browser
// process to GPU process for wayland.
if (!supported_gmb_configurations_inited_) {
supported_gmb_configurations_inited_ = true;
if (WillGetGmbConfigFromGpu()) {
#if BUILDFLAG(IS_OZONE_X11)
for (const auto& config : gpu_extra_info_.gpu_memory_buffer_support_x11) {
supported_gmb_configurations_.emplace(config);
}
#endif // BUILDFLAG(IS_OZONE_X11)
} else {
supported_gmb_configurations_ =
gpu::GpuMemoryBufferSupport::GetNativeGpuMemoryBufferConfigurations();
}
}
return base::Contains(supported_gmb_configurations_,
gfx::BufferUsageAndFormat(usage, format));
}
bool SharedImageFactory::CreateSharedImage(const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SurfaceHandle surface_handle,
SharedImageUsageSet usage,
std::string debug_label,
gfx::BufferUsage buffer_usage) {
if (!viz::HasEquivalentBufferFormat(format)) {
// Client GMB code still operates on BufferFormat so the SharedImageFormat
// received here must have an equivalent BufferFormat.
LOG(ERROR) << "Invalid format " << format.ToString();
return false;
}
auto buffer_format = ToBufferFormat(format);
auto native_buffer_supported =
IsNativeBufferSupported(buffer_format, buffer_usage);
std::unique_ptr<SharedImageBacking> backing;
if (native_buffer_supported) {
auto* factory = GetFactoryByUsage(usage, format, size,
/*pixel_data=*/{}, GetNativeBufferType());
if (!factory) {
LogGetFactoryFailed(usage, format, GetNativeBufferType(), size,
debug_label);
return false;
}
backing = factory->CreateSharedImage(
mailbox, format, surface_handle, size, color_space, surface_origin,
alpha_type, SharedImageUsageSet(usage), debug_label,
IsSharedBetweenThreads(usage), buffer_usage);
if (backing) {
DVLOG(1) << "CreateSharedImageBackedByBuffer[" << backing->GetName()
<< "] size=" << size.ToString()
<< " usage=" << CreateLabelForSharedImageUsage(usage)
<< " format=" << format.ToString();
}
} else {
// If native buffers are not supported, try to create shared memory based
// backings.
if (gpu::GpuMemoryBufferImplSharedMemory::IsUsageSupported(buffer_usage) &&
gpu::GpuMemoryBufferImplSharedMemory::IsSizeValidForFormat(
size, buffer_format)) {
// Clear the external sampler prefs for shared memory case if it is set.
// https://issues.chromium.org/339546249.
if (format.PrefersExternalSampler()) {
format.ClearPrefersExternalSampler();
}
auto* factory =
GetFactoryByUsage(usage, format, size,
/*pixel_data=*/{}, gfx::SHARED_MEMORY_BUFFER);
bool use_compound = false;
if (!factory && !IsSharedBetweenThreads(usage)) {
// Check if CompoundImageBacking can hold shared memory buffer plus
// another GPU backing type to satisfy requirements.
if (CompoundImageBacking::IsValidSharedMemoryBufferFormat(size,
format)) {
factory =
GetFactoryByUsage(CompoundImageBacking::GetGpuSharedImageUsage(
SharedImageUsageSet(usage)),
format, size,
/*pixel_data=*/{}, gfx::EMPTY_BUFFER);
use_compound = factory != nullptr;
}
}
if (!factory) {
LogGetFactoryFailed(usage, format, gfx::SHARED_MEMORY_BUFFER, size,
debug_label);
return false;
}
if (use_compound) {
backing = CompoundImageBacking::CreateSharedMemory(
factory, mailbox, format, size, color_space, surface_origin,
alpha_type, usage, debug_label, buffer_usage);
} else {
backing = factory->CreateSharedImage(
mailbox, format, surface_handle, size, color_space, surface_origin,
alpha_type, SharedImageUsageSet(usage), debug_label,
IsSharedBetweenThreads(usage), buffer_usage);
}
if (backing) {
DVLOG(1) << "CreateSharedImageBackedByBuffer[" << backing->GetName()
<< "] size=" << size.ToString()
<< " usage=" << CreateLabelForSharedImageUsage(usage)
<< " format=" << format.ToString();
}
}
}
return RegisterBacking(std::move(backing));
}
bool SharedImageFactory::CreateSharedImage(const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
std::string debug_label,
base::span<const uint8_t> data) {
if (!format.is_single_plane()) {
// Pixel upload path only supports single-planar formats.
LOG(ERROR) << "Invalid format " << format.ToString();
return false;
}
SharedImageBackingFactory* const factory =
GetFactoryByUsage(usage, format, size, data, gfx::EMPTY_BUFFER);
if (!factory) {
LogGetFactoryFailed(usage, format, gfx::EMPTY_BUFFER, size, debug_label);
return false;
}
auto backing = factory->CreateSharedImage(
mailbox, format, size, color_space, surface_origin, alpha_type,
SharedImageUsageSet(usage), std::move(debug_label),
IsSharedBetweenThreads(usage), data);
if (backing) {
DVLOG(1) << "CreateSharedImagePixels[" << backing->GetName()
<< "] with pixels size=" << size.ToString()
<< " usage=" << CreateLabelForSharedImageUsage(usage)
<< " format=" << format.ToString();
}
return RegisterBacking(std::move(backing));
}
bool SharedImageFactory::CreateSharedImage(
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
std::string debug_label,
gfx::GpuMemoryBufferHandle buffer_handle,
std::optional<SharedImagePoolId> pool_id) {
gfx::GpuMemoryBufferType gmb_type = buffer_handle.type;
bool use_compound = false;
SharedImageBackingFactory* factory =
GetFactoryByUsage(usage, format, size, {}, gmb_type);
if (!factory && gmb_type == gfx::SHARED_MEMORY_BUFFER &&
!IsSharedBetweenThreads(usage)) {
// Check if CompoundImageBacking can hold shared memory buffer plus
// another GPU backing type to satisfy requirements.
if (CompoundImageBacking::IsValidSharedMemoryBufferFormat(size, format)) {
factory =
GetFactoryByUsage(CompoundImageBacking::GetGpuSharedImageUsage(
SharedImageUsageSet(usage)),
format, size, /*pixel_data=*/{}, gfx::EMPTY_BUFFER);
use_compound = factory != nullptr;
}
}
if (!factory) {
LogGetFactoryFailed(usage, format, gmb_type, size, debug_label);
return false;
}
std::unique_ptr<SharedImageBacking> backing;
if (use_compound) {
backing = CompoundImageBacking::CreateSharedMemory(
factory, mailbox, std::move(buffer_handle), format, size, color_space,
surface_origin, alpha_type, usage, std::move(debug_label));
} else {
backing = factory->CreateSharedImage(
mailbox, format, size, color_space, surface_origin, alpha_type, usage,
std::move(debug_label), /*is_thread_safe=*/false,
std::move(buffer_handle));
}
if (backing) {
DVLOG(1) << "CreateSharedImageWithBuffer[" << backing->GetName()
<< "] size=" << size.ToString()
<< " usage=" << CreateLabelForSharedImageUsage(usage)
<< " format=" << format.ToString()
<< " gmb_type=" << GmbTypeToString(gmb_type);
}
return RegisterBacking(std::move(backing), std::move(pool_id));
}
bool SharedImageFactory::UpdateSharedImage(const Mailbox& mailbox) {
return UpdateSharedImage(mailbox, nullptr);
}
bool SharedImageFactory::UpdateSharedImage(
const Mailbox& mailbox,
std::unique_ptr<gfx::GpuFence> in_fence) {
auto* shared_image = GetFactoryRef(mailbox);
if (!shared_image) {
LOG(ERROR) << "UpdateSharedImage: Could not find shared image mailbox";
return false;
}
shared_image->Update(std::move(in_fence));
return true;
}
bool SharedImageFactory::DestroySharedImage(const Mailbox& mailbox) {
auto it = shared_images_.find(mailbox);
if (it == shared_images_.end()) {
LOG(ERROR) << "DestroySharedImage: Could not find shared image mailbox";
return false;
}
shared_images_.erase(it);
return true;
}
bool SharedImageFactory::SetSharedImagePurgeable(const Mailbox& mailbox,
bool purgeable) {
auto* shared_image = GetFactoryRef(mailbox);
if (!shared_image) {
LOG(ERROR)
<< "SetSharedImagePurgeable: Could not find shared image mailbox";
return false;
}
shared_image->SetPurgeable(purgeable);
return true;
}
void SharedImageFactory::DestroyAllSharedImages(bool have_context) {
if (!have_context) {
for (auto& [_, shared_image] : shared_images_) {
shared_image->OnContextLost();
}
}
shared_images_.clear();
}
#if BUILDFLAG(IS_WIN)
bool SharedImageFactory::IsD3DSharedImageSupported() const {
if (!context_state_) {
return false;
}
return D3DImageBackingFactory::IsD3DSharedImageSupported(
context_state_->GetD3D11Device().Get(), gpu_preferences_);
}
bool SharedImageFactory::CreateSwapChain(const Mailbox& front_buffer_mailbox,
const Mailbox& back_buffer_mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
gpu::SharedImageUsageSet usage) {
if (!D3DImageBackingFactory::IsSwapChainSupported(gpu_preferences_)) {
return false;
}
auto backings = d3d_backing_factory_->CreateSwapChain(
front_buffer_mailbox, back_buffer_mailbox, format, size, color_space,
surface_origin, alpha_type, usage);
return RegisterBacking(std::move(backings.front_buffer)) &&
RegisterBacking(std::move(backings.back_buffer));
}
bool SharedImageFactory::PresentSwapChain(const Mailbox& mailbox) {
if (!D3DImageBackingFactory::IsSwapChainSupported(gpu_preferences_)) {
return false;
}
auto* shared_image = GetFactoryRef(mailbox);
if (!shared_image) {
DLOG(ERROR) << "PresentSwapChain: Could not find shared image mailbox";
return false;
}
shared_image->PresentSwapChain();
return true;
}
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_FUCHSIA)
void SharedImageFactory::RegisterSysmemBufferCollection(
zx::eventpair service_handle,
zx::channel sysmem_token,
const viz::SharedImageFormat& format,
gfx::BufferUsage usage,
bool register_with_image_pipe) {
auto* vulkan_context_provider = context_state_->vk_context_provider();
VkDevice device =
vulkan_context_provider->GetDeviceQueue()->GetVulkanDevice();
DCHECK(device != VK_NULL_HANDLE);
auto buffer_format = ToBufferFormat(format);
vulkan_context_provider->GetVulkanImplementation()
->RegisterSysmemBufferCollection(
device, std::move(service_handle), std::move(sysmem_token),
buffer_format, usage, gfx::Size(), 0, register_with_image_pipe);
}
#endif // BUILDFLAG(IS_FUCHSIA)
bool SharedImageFactory::CopyToGpuMemoryBuffer(const Mailbox& mailbox) {
auto* shared_image = GetFactoryRef(mailbox);
if (!shared_image) {
DLOG(ERROR) << "CopyToGpuMemoryBuffer: Could not find shared image mailbox";
return false;
}
return shared_image->CopyToGpuMemoryBuffer();
}
#if BUILDFLAG(IS_WIN)
bool SharedImageFactory::CopyToGpuMemoryBufferAsync(
const Mailbox& mailbox,
base::OnceCallback<void(bool)> callback) {
auto* shared_image = GetFactoryRef(mailbox);
if (!shared_image) {
DLOG(ERROR)
<< "CopyToGpuMemoryBufferAsync: Could not find shared image mailbox";
return false;
}
shared_image->CopyToGpuMemoryBufferAsync(std::move(callback));
return true;
}
#endif
bool SharedImageFactory::GetGpuMemoryBufferHandleInfo(
const Mailbox& mailbox,
gfx::GpuMemoryBufferHandle& handle,
viz::SharedImageFormat& format,
gfx::Size& size,
gfx::BufferUsage& buffer_usage) {
auto* shared_image = GetFactoryRef(mailbox);
if (!shared_image) {
LOG(ERROR)
<< "GetGpuMemoryBufferHandleInfo: Could not find shared image mailbox";
return false;
}
shared_image->GetGpuMemoryBufferHandleInfo(handle, format, size,
buffer_usage);
return true;
}
bool SharedImageFactory::CreateSharedImagePool(
const SharedImagePoolId& pool_id,
mojo::PendingRemote<mojom::SharedImagePoolClientInterface> client_remote) {
auto it = shared_image_pool_map_.find(pool_id);
// Ensure that there is no pool already corresponding to the |pool_id|.
if (it != shared_image_pool_map_.end()) {
return false;
}
auto pool = std::make_unique<SharedImagePoolService>(
pool_id, std::move(client_remote), this);
shared_image_pool_map_.emplace(pool_id, std::move(pool));
return true;
}
bool SharedImageFactory::DestroySharedImagePool(
const SharedImagePoolId& pool_id) {
// Ensure that there is a pool corresponding to the |pool_id|.
return shared_image_pool_map_.erase(pool_id);
}
void SharedImageFactory::RegisterSharedImageBackingFactoryForTesting(
SharedImageBackingFactory* factory) {
backing_factory_for_testing_ = factory;
}
gpu::SharedImageCapabilities SharedImageFactory::MakeCapabilities() {
gpu::SharedImageCapabilities shared_image_caps;
shared_image_caps.supports_scanout_shared_images =
shared_image_manager_->SupportsScanoutImages();
#if BUILDFLAG(IS_WIN)
// Scanout for software video frames is supported on Windows except on D3D9.
shared_image_caps.supports_scanout_shared_images_for_software_video_frames =
gl::QueryD3D11DeviceObjectFromANGLE();
#endif
const bool is_angle_metal =
gl::GetGLImplementation() == gl::kGLImplementationEGLANGLE &&
gl::GetANGLEImplementation() == gl::ANGLEImplementation::kMetal;
const bool is_skia_graphite =
gr_context_type_ == GrContextType::kGraphiteDawn ||
gr_context_type_ == GrContextType::kGraphiteMetal;
shared_image_caps.supports_luminance_shared_images =
!is_angle_metal && !is_skia_graphite;
shared_image_caps.supports_r16_shared_images =
is_angle_metal || is_skia_graphite;
shared_image_caps.supports_native_nv12_mappable_shared_images =
IsNativeBufferSupported(gfx::BufferFormat::YUV_420_BIPLANAR,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE);
shared_image_caps.disable_r8_shared_images =
workarounds_.r8_egl_images_broken;
shared_image_caps.disable_webgpu_shared_images =
workarounds_.disable_webgpu_shared_images;
if (!context_state_) {
shared_image_caps.is_r16f_supported = false;
} else if (is_skia_graphite || gr_context_type_ == GrContextType::kVulkan) {
// R16F is always supported with Dawn and Vulkan contexts.
shared_image_caps.is_r16f_supported = true;
} else if (gr_context_type_ == GrContextType::kGL) {
CHECK(context_state_->gr_context());
// With Skia GL, R16F is supported only with GLES 3.0 and above.
shared_image_caps.is_r16f_supported =
context_state_->feature_info()->gl_version_info().IsAtLeastGLES(3, 0) &&
context_state_->gr_context()->colorTypeSupportedAsImage(
kA16_float_SkColorType);
}
const bool display_compositor_on_another_thread =
shared_image_manager_->display_context_on_another_thread();
if (!context_state_) {
shared_image_caps.disable_one_component_textures = false;
} else if (context_state_->GrContextIsGL()) {
shared_image_caps.disable_one_component_textures =
display_compositor_on_another_thread &&
workarounds_.avoid_one_component_egl_images;
} else if (context_state_->GrContextIsVulkan() ||
context_state_->IsGraphiteDawnVulkan()) {
// Vulkan currently doesn't support single-component cross-thread shared
// images for WebView.
const bool is_drdc =
features::IsDrDcEnabled() && !workarounds_.disable_drdc;
shared_image_caps.disable_one_component_textures =
display_compositor_on_another_thread && !is_drdc;
}
#if BUILDFLAG(IS_MAC)
shared_image_caps.texture_target_for_io_surfaces =
texture_target_for_io_surfaces_;
#endif
#if BUILDFLAG(IS_WIN)
shared_image_caps.shared_image_d3d = IsD3DSharedImageSupported();
shared_image_caps.shared_image_swap_chain =
shared_image_caps.shared_image_d3d &&
D3DImageBackingFactory::IsSwapChainSupported(gpu_preferences_);
#endif // BUILDFLAG(IS_WIN)
return shared_image_caps;
}
bool SharedImageFactory::HasSharedImage(const Mailbox& mailbox) const {
return shared_images_.contains(mailbox);
}
void SharedImageFactory::SetGpuExtraInfo(
const gfx::GpuExtraInfo& gpu_extra_info) {
gpu_extra_info_ = gpu_extra_info;
}
bool SharedImageFactory::IsSharedBetweenThreads(
gpu::SharedImageUsageSet usage) {
// Ignore for mipmap usage.
usage.RemoveAll(SHARED_IMAGE_USAGE_MIPMAP);
// Raw Draw backings will be write accessed on the GPU main thread, and
// be read accessed on the compositor thread.
if (usage.Has(SHARED_IMAGE_USAGE_RAW_DRAW)) {
return true;
}
// DISPLAY is for gpu composition and SCANOUT for overlays.
constexpr gpu::SharedImageUsageSet kDisplayCompositorUsage =
SHARED_IMAGE_USAGE_DISPLAY_READ | SHARED_IMAGE_USAGE_DISPLAY_WRITE |
SHARED_IMAGE_USAGE_SCANOUT;
// Image is used on display compositor gpu thread if it's used by display
// compositor and if display compositor runs on a separate thread. Image is
// used by display compositor if it has kDisplayCompositorUsage or is being
// created by display compositor.
const bool used_by_display_compositor_gpu_thread =
(usage.HasAny(kDisplayCompositorUsage) || is_for_display_compositor_) &&
shared_image_manager_->display_context_on_another_thread();
// If it has usage other than kDisplayCompositorUsage OR if it is not created
// by display compositor, it means that it is used by the gpu main thread.
const bool used_by_main_gpu_thread =
!kDisplayCompositorUsage.HasAll(usage) || !is_for_display_compositor_;
return used_by_display_compositor_gpu_thread && used_by_main_gpu_thread;
}
SharedImageBackingFactory* SharedImageFactory::GetFactoryByUsage(
gpu::SharedImageUsageSet usage,
viz::SharedImageFormat format,
const gfx::Size& size,
base::span<const uint8_t> pixel_data,
gfx::GpuMemoryBufferType gmb_type) {
if (backing_factory_for_testing_)
return backing_factory_for_testing_;
bool share_between_threads = IsSharedBetweenThreads(usage);
for (auto& factory : factories_) {
if (factory->CanCreateSharedImage(SharedImageUsageSet(usage), format, size,
share_between_threads, gmb_type,
gr_context_type_, pixel_data)) {
return factory.get();
}
}
return nullptr;
}
void SharedImageFactory::LogGetFactoryFailed(gpu::SharedImageUsageSet usage,
viz::SharedImageFormat format,
gfx::GpuMemoryBufferType gmb_type,
const gfx::Size& size,
const std::string& debug_label) {
LOG(ERROR) << "Could not find SharedImageBackingFactory with params: usage: "
<< CreateLabelForSharedImageUsage(usage)
<< ", format: " << format.ToString()
<< ", share_between_threads: " << IsSharedBetweenThreads(usage)
<< ", gmb_type: " << GmbTypeToString(gmb_type)
<< ", size: " << size.ToString()
<< ", debug_label: " << debug_label;
std::string new_debug_label = debug_label;
// Get the debug label with Process Id for filtering crash reports by label as
// key.
if (debug_label.find("_Pid") != std::string::npos) {
auto parts = base::RSplitStringOnce(debug_label, '_');
new_debug_label = parts->first;
}
#if BUILDFLAG(IS_ANDROID)
// TODO(crbug.com/423037052): Handle offscreen canvas case for WebView where
// we fail to find a shared image factory.
// Suppress crashes due to this client for now.
if (new_debug_label == "CanvasResourceRasterGmb") {
return;
}
#endif
SCOPED_CRASH_KEY_STRING32("SIFactory", "DebugLabel", new_debug_label);
SCOPED_CRASH_KEY_STRING64("SIFactory", "Format", format.ToString());
SCOPED_CRASH_KEY_NUMBER("SIFactory", "Usage", usage);
SCOPED_CRASH_KEY_STRING64("SIFactory", "GMBType", GmbTypeToString(gmb_type));
SCOPED_CRASH_KEY_STRING64("SIFactory", "Size", size.ToString());
SCOPED_CRASH_KEY_BOOL("SIFactory", "SharedBwThreads",
IsSharedBetweenThreads(usage));
// DumpWithoutCrashing to get crash reports for failure to find a shared image
// backing factory.
// base::debug::DumpWithoutCrashing();
}
bool SharedImageFactory::RegisterBacking(
std::unique_ptr<SharedImageBacking> backing,
std::optional<SharedImagePoolId> pool_id) {
if (!backing) {
LOG(ERROR) << "CreateSharedImage: could not create backing.";
return false;
}
std::unique_ptr<SharedImageRepresentationFactoryRef> shared_image =
shared_image_manager_->Register(std::move(backing),
memory_type_tracker_.get());
if (!shared_image) {
LOG(ERROR) << "CreateSharedImage: could not register backing.";
return false;
}
shared_image->RegisterImageFactory(this);
if (pool_id) {
shared_image->SetSharedImagePoolId(pool_id.value());
}
gpu::Mailbox mailbox = shared_image->mailbox();
shared_images_.emplace(std::move(mailbox), std::move(shared_image));
return true;
}
bool SharedImageFactory::AddSecondaryReference(const gpu::Mailbox& mailbox) {
if (shared_images_.contains(mailbox)) {
LOG(ERROR) << "AddSecondaryReference: Can't have more than one reference.";
return false;
}
std::unique_ptr<SharedImageRepresentationFactoryRef> shared_image =
shared_image_manager_->AddSecondaryReference(mailbox,
memory_type_tracker_.get());
if (!shared_image) {
return false;
}
shared_images_.emplace(mailbox, std::move(shared_image));
return true;
}
SharedImageUsageSet SharedImageFactory::GetUsageForMailbox(
const Mailbox& mailbox) {
auto* shared_image = GetFactoryRef(mailbox);
return shared_image ? shared_image->usage() : SharedImageUsageSet();
}
SharedImageRepresentationFactoryRef* SharedImageFactory::GetFactoryRef(
const gpu::Mailbox& mailbox) const {
auto it = shared_images_.find(mailbox);
return it != shared_images_.end() ? it->second.get() : nullptr;
}
SharedImageRepresentationFactory::SharedImageRepresentationFactory(
SharedImageManager* manager,
scoped_refptr<gpu::MemoryTracker> memory_tracker)
: manager_(manager),
memory_type_tracker_(
std::make_unique<MemoryTypeTracker>(std::move(memory_tracker))) {}
SharedImageRepresentationFactory::~SharedImageRepresentationFactory() {
DCHECK_EQ(0u, memory_type_tracker_->GetMemRepresented());
}
std::unique_ptr<GLTextureImageRepresentation>
SharedImageRepresentationFactory::ProduceGLTexture(const Mailbox& mailbox) {
return manager_->ProduceGLTexture(mailbox, memory_type_tracker_.get());
}
std::unique_ptr<GLTexturePassthroughImageRepresentation>
SharedImageRepresentationFactory::ProduceGLTexturePassthrough(
const Mailbox& mailbox) {
return manager_->ProduceGLTexturePassthrough(mailbox,
memory_type_tracker_.get());
}
std::unique_ptr<SkiaImageRepresentation>
SharedImageRepresentationFactory::ProduceSkia(
const Mailbox& mailbox,
scoped_refptr<SharedContextState> context_state) {
return manager_->ProduceSkia(mailbox, memory_type_tracker_.get(),
context_state);
}
std::unique_ptr<DawnImageRepresentation>
SharedImageRepresentationFactory::ProduceDawn(
const Mailbox& mailbox,
const wgpu::Device& device,
wgpu::BackendType backend_type,
std::vector<wgpu::TextureFormat> view_formats,
scoped_refptr<SharedContextState> context_state) {
return manager_->ProduceDawn(mailbox, memory_type_tracker_.get(), device,
backend_type, std::move(view_formats),
context_state);
}
std::unique_ptr<DawnBufferRepresentation>
SharedImageRepresentationFactory::ProduceDawnBuffer(
const Mailbox& mailbox,
const wgpu::Device& device,
wgpu::BackendType backend_type) {
return manager_->ProduceDawnBuffer(mailbox, memory_type_tracker_.get(),
device, backend_type);
}
std::unique_ptr<OverlayImageRepresentation>
SharedImageRepresentationFactory::ProduceOverlay(const gpu::Mailbox& mailbox) {
return manager_->ProduceOverlay(mailbox, memory_type_tracker_.get());
}
std::unique_ptr<MemoryImageRepresentation>
SharedImageRepresentationFactory::ProduceMemory(const gpu::Mailbox& mailbox) {
return manager_->ProduceMemory(mailbox, memory_type_tracker_.get());
}
std::unique_ptr<RasterImageRepresentation>
SharedImageRepresentationFactory::ProduceRaster(const Mailbox& mailbox) {
return manager_->ProduceRaster(mailbox, memory_type_tracker_.get());
}
#if BUILDFLAG(IS_ANDROID)
std::unique_ptr<LegacyOverlayImageRepresentation>
SharedImageRepresentationFactory::ProduceLegacyOverlay(
const gpu::Mailbox& mailbox) {
return manager_->ProduceLegacyOverlay(mailbox, memory_type_tracker_.get());
}
#endif
#if BUILDFLAG(ENABLE_VULKAN) && BUILDFLAG(IS_OZONE)
std::unique_ptr<VulkanImageRepresentation>
SharedImageRepresentationFactory::ProduceVulkan(
const gpu::Mailbox& mailbox,
gpu::VulkanDeviceQueue* vulkan_device_queue,
gpu::VulkanImplementation& vulkan_impl,
bool needs_detiling) {
return manager_->ProduceVulkan(mailbox, memory_type_tracker_.get(),
vulkan_device_queue, vulkan_impl,
needs_detiling);
}
#endif
} // namespace gpu
|