Skip to content

Commit

Permalink
gpu: win: DComp layer SharedImageBackings
Browse files Browse the repository at this point in the history
The CL introduces two new SharedImageBackings (IDCompositionSurface- and IDXGISwapChain-based) and a factory to create them. It does not introduce any users of these backings.

The new DCompImageBackingFactory is inserted at a higher priority than the D3DImageBackingFactory, so it gets the first-change opportunity to back SI.

To draw to the DCompSurfaceImageBacking, we continue the design used by DirectCompositionChildSurfaceWin of binding the DComp surface to a pbuffer and GLFB0 (this is opaque to the SI caller and non-interleaved drawing is a requirement of using this backing). The DXGISwapChainImageBacking binds the back buffer to a texture, similar to how D3DImageBacking does.

Note, this change does not preserve:
- DirectCompositionVerifyDrawOffset behavior (added in https://chromium-review.googlesource.com/c/chromium/src/+/3554067), because it is no longer needed due to (https://chromium-review.googlesource.com/c/angle/angle/+/3827081).
- Ability to nest DComp surface draws (via SuspendDraw and ResumeDraw)

Change-Id: I9d2b45552e4fdda7c67d417251b1a1ec99ba8670
Bug: 1378741
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3863757
Commit-Queue: Rafael Cintron <rafael.cintron@microsoft.com>
Reviewed-by: Rafael Cintron <rafael.cintron@microsoft.com>
Reviewed-by: Sunny Sachanandani <sunnyps@chromium.org>
Reviewed-by: Vasiliy Telezhnikov <vasilyt@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1067712}
  • Loading branch information
tangm-msft authored and Chromium LUCI CQ committed Nov 4, 2022
1 parent 9f028e6 commit 455b94a
Show file tree
Hide file tree
Showing 25 changed files with 1,588 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ bool ImageContextImpl::BeginAccessIfNecessaryForSharedImage(

if (!(representation->usage() & gpu::SHARED_IMAGE_USAGE_DISPLAY_READ)) {
DLOG(ERROR) << "Failed to fulfill the promise texture - SharedImage "
"was not created with display usage.";
"was not created with DISPLAY_READ usage.";
return false;
}

Expand Down
1 change: 1 addition & 0 deletions gpu/command_buffer/common/shared_image_usage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ std::string CreateLabelForSharedImageUsage(uint32_t usage) {
"RasterDelegatedCompositing"},
{SHARED_IMAGE_USAGE_HIGH_PERFORMANCE_GPU, "HighPerformanceGpu"},
{SHARED_IMAGE_USAGE_CPU_UPLOAD, "CpuUpload"},
{SHARED_IMAGE_USAGE_SCANOUT_DCOMP_SURFACE, "ScanoutDCompSurface"},
};

std::string label;
Expand Down
9 changes: 7 additions & 2 deletions gpu/command_buffer/common/shared_image_usage.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,20 @@ enum SharedImageUsage : uint32_t {
SHARED_IMAGE_USAGE_RASTER_DELEGATED_COMPOSITING = 1 << 16,
// Image will be created on the high performance GPU if supported.
SHARED_IMAGE_USAGE_HIGH_PERFORMANCE_GPU = 1 << 17,
// Windows only: image will be backed by a DComp surface. A swap chain is
// preferred when an image is opaque and expected to update frequently and
// independently of other overlays. This flag is incompatible with
// DISPLAY_READ.
SHARED_IMAGE_USAGE_SCANOUT_DCOMP_SURFACE = 1 << 18,

// Start service side only usage flags after this entry. They must be larger
// than `LAST_CLIENT_USAGE`.
LAST_CLIENT_USAGE = SHARED_IMAGE_USAGE_HIGH_PERFORMANCE_GPU,
LAST_CLIENT_USAGE = SHARED_IMAGE_USAGE_SCANOUT_DCOMP_SURFACE,

// Image will have pixels uploaded from CPU. The backing must implement
// `UploadFromMemory()` if it supports this usage. Clients should specify
// SHARED_IMAGE_USAGE_CPU_WRITE if they need to write pixels to the image.
SHARED_IMAGE_USAGE_CPU_UPLOAD = 1 << 18,
SHARED_IMAGE_USAGE_CPU_UPLOAD = 1 << 19,
};

// Returns true if usage is a valid client usage.
Expand Down
10 changes: 10 additions & 0 deletions gpu/command_buffer/service/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,16 @@ target(link_target_type, "gles2_sources") {
"shared_image/d3d_image_representation.h",
"shared_image/d3d_shared_fence.cc",
"shared_image/d3d_shared_fence.h",
"shared_image/dcomp_image_backing_factory.cc",
"shared_image/dcomp_image_backing_factory.h",
"shared_image/dcomp_surface_image_backing.cc",
"shared_image/dcomp_surface_image_backing.h",
"shared_image/dcomp_surface_image_representation.cc",
"shared_image/dcomp_surface_image_representation.h",
"shared_image/dxgi_swap_chain_image_backing.cc",
"shared_image/dxgi_swap_chain_image_backing.h",
"shared_image/dxgi_swap_chain_image_representation.cc",
"shared_image/dxgi_swap_chain_image_representation.h",
]
libs = [ "dxguid.lib" ]
}
Expand Down
8 changes: 4 additions & 4 deletions gpu/command_buffer/service/shared_context_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ bool SharedContextState::InitializeGrContext(
if (gr_context_type_ == GrContextType::kGL) {
DCHECK(context_->IsCurrent(nullptr));
constexpr bool use_version_es2 = false;
sk_sp<GrGLInterface> interface(gl::init::CreateGrGLInterface(
sk_sp<GrGLInterface> gr_gl_interface(gl::init::CreateGrGLInterface(
*context_->GetVersionInfo(), use_version_es2, progress_reporter));
if (!interface) {
if (!gr_gl_interface) {
LOG(ERROR) << "OOP raster support disabled: GrGLInterface creation "
"failed.";
return false;
Expand All @@ -310,7 +310,7 @@ bool SharedContextState::InitializeGrContext(
if (activity_flags && cache) {
// |activity_flags| is safe to capture here since it must outlive the
// this context state.
interface->fFunctions.fProgramBinary =
gr_gl_interface->fFunctions.fProgramBinary =
[activity_flags](GrGLuint program, GrGLenum binaryFormat,
void* binary, GrGLsizei length) {
GpuProcessActivityFlags::ScopedSetFlag scoped_set_flag(
Expand All @@ -336,7 +336,7 @@ bool SharedContextState::InitializeGrContext(
DCHECK(owned_gr_context_);
} else {
owned_gr_context_ =
GrDirectContext::MakeGL(std::move(interface), options);
GrDirectContext::MakeGL(std::move(gr_gl_interface), options);
}

gr_context_ = owned_gr_context_.get();
Expand Down
37 changes: 19 additions & 18 deletions gpu/command_buffer/service/shared_image/d3d_image_backing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,29 @@ gfx::Size PlaneSize(DXGI_FORMAT dxgi_format,
}
}

scoped_refptr<gles2::TexturePassthrough> CreateGLTexture(
void CopyPlane(const uint8_t* source_memory,
size_t source_stride,
uint8_t* dest_memory,
size_t dest_stride,
viz::SharedImageFormat format,
const gfx::Size& size) {
int row_bytes = size.width() * viz::BitsPerPixel(format) / 8;
libyuv::CopyPlane(source_memory, source_stride, dest_memory, dest_stride,
row_bytes, size.height());
}

} // namespace

// static
scoped_refptr<gles2::TexturePassthrough> D3DImageBacking::CreateGLTexture(
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
Microsoft::WRL::ComPtr<ID3D11Texture2D> d3d11_texture,
GLenum texture_target = GL_TEXTURE_2D,
unsigned array_slice = 0u,
unsigned plane_index = 0u,
Microsoft::WRL::ComPtr<IDXGISwapChain1> swap_chain = nullptr) {
GLenum texture_target,
unsigned array_slice,
unsigned plane_index,
Microsoft::WRL::ComPtr<IDXGISwapChain1> swap_chain) {
gl::GLApi* const api = gl::g_current_gl_context;
gl::ScopedRestoreTexture scoped_restore(api, texture_target);

Expand Down Expand Up @@ -165,19 +179,6 @@ scoped_refptr<gles2::TexturePassthrough> CreateGLTexture(
return texture;
}

void CopyPlane(const uint8_t* source_memory,
size_t source_stride,
uint8_t* dest_memory,
size_t dest_stride,
viz::SharedImageFormat format,
const gfx::Size& size) {
int row_bytes = size.width() * viz::BitsPerPixel(format) / 8;
libyuv::CopyPlane(source_memory, source_stride, dest_memory, dest_stride,
row_bytes, size.height());
}

} // namespace

#if BUILDFLAG(USE_DAWN)
D3DImageBacking::DawnExternalImageState::DawnExternalImageState() = default;
D3DImageBacking::DawnExternalImageState::~DawnExternalImageState() = default;
Expand Down
10 changes: 10 additions & 0 deletions gpu/command_buffer/service/shared_image/d3d_image_backing.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ class GPU_GLES2_EXPORT D3DImageBacking
return d3d11_texture_;
}

static scoped_refptr<gles2::TexturePassthrough> CreateGLTexture(
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
Microsoft::WRL::ComPtr<ID3D11Texture2D> d3d11_texture,
GLenum texture_target = GL_TEXTURE_2D,
unsigned array_slice = 0u,
unsigned plane_index = 0u,
Microsoft::WRL::ComPtr<IDXGISwapChain1> swap_chain = nullptr);

protected:
std::unique_ptr<GLTexturePassthroughImageRepresentation>
ProduceGLTexturePassthrough(SharedImageManager* manager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ bool D3DImageBackingFactory::IsSupported(uint32_t usage,
return false;
}

if (usage & SHARED_IMAGE_USAGE_SCANOUT_DCOMP_SURFACE) {
return false;
}

if (usage & SHARED_IMAGE_USAGE_CONCURRENT_READ_WRITE) {
return false;
}
Expand Down
153 changes: 153 additions & 0 deletions gpu/command_buffer/service/shared_image/dcomp_image_backing_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright 2022 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/dcomp_image_backing_factory.h"

#include <d3d11_1.h>

#include "components/viz/common/resources/resource_format_utils.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/shared_image/dcomp_surface_image_backing.h"
#include "gpu/command_buffer/service/shared_image/dxgi_swap_chain_image_backing.h"
#include "ui/gfx/color_space_win.h"
#include "ui/gl/direct_composition_child_surface_win.h"

namespace gpu {

namespace {

// Check if a format is supported by DXGI for DComp surfaces or swap chains.
// https://learn.microsoft.com/en-us/windows/win32/direct3ddxgi/converting-data-color-space
bool IsFormatSupportedForScanout(viz::SharedImageFormat format) {
if (format.is_multi_plane()) {
return false;
}

switch (format.resource_format()) {
case viz::ResourceFormat::RGBA_8888:
case viz::ResourceFormat::BGRA_8888:
case viz::ResourceFormat::RGBX_8888:
case viz::ResourceFormat::BGRX_8888:
case viz::ResourceFormat::RGBA_F16:
case viz::ResourceFormat::RGBA_1010102:
return true;

default:
return false;
}
}

} // namespace

DCompImageBackingFactory::DCompImageBackingFactory() = default;

DCompImageBackingFactory::~DCompImageBackingFactory() = default;

std::unique_ptr<SharedImageBacking> DCompImageBackingFactory::CreateSharedImage(
const Mailbox& mailbox,
viz::SharedImageFormat format,
SurfaceHandle surface_handle,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
uint32_t usage,
bool is_thread_safe) {
DCHECK(!is_thread_safe);

// DXGI only supports a handful of formats for scan-out, so we map the
// requested format to a supported compatible DXGI format.
DXGI_FORMAT internal_format = gfx::ColorSpaceWin::GetDXGIFormat(color_space);

if (usage & SHARED_IMAGE_USAGE_SCANOUT_DCOMP_SURFACE) {
DCHECK_NE(internal_format, DXGI_FORMAT_R10G10B10A2_UNORM);
return DCompSurfaceImageBacking::Create(mailbox, format, internal_format,
size, color_space, surface_origin,
alpha_type, usage);
} else {
return DXGISwapChainImageBacking::Create(mailbox, format, internal_format,
size, color_space, surface_origin,
alpha_type, usage);
}
}

std::unique_ptr<SharedImageBacking> DCompImageBackingFactory::CreateSharedImage(
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
uint32_t usage,
base::span<const uint8_t> pixel_data) {
NOTREACHED();
return nullptr;
}

std::unique_ptr<SharedImageBacking> DCompImageBackingFactory::CreateSharedImage(
const Mailbox& mailbox,
int client_id,
gfx::GpuMemoryBufferHandle handle,
gfx::BufferFormat format,
gfx::BufferPlane plane,
SurfaceHandle surface_handle,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
uint32_t usage) {
NOTREACHED();
return nullptr;
}

bool DCompImageBackingFactory::IsSupported(
uint32_t usage,
viz::SharedImageFormat format,
const gfx::Size& size,
bool thread_safe,
gfx::GpuMemoryBufferType gmb_type,
GrContextType gr_context_type,
base::span<const uint8_t> pixel_data) {
constexpr uint32_t kDXGISwapChainUsage = SHARED_IMAGE_USAGE_DISPLAY_READ |
SHARED_IMAGE_USAGE_DISPLAY_WRITE |
SHARED_IMAGE_USAGE_SCANOUT;
constexpr uint32_t kDCompSurfaceUsage =
SHARED_IMAGE_USAGE_DISPLAY_WRITE | SHARED_IMAGE_USAGE_SCANOUT |
SHARED_IMAGE_USAGE_SCANOUT_DCOMP_SURFACE;

bool is_usage_valid =
usage == kDXGISwapChainUsage || usage == kDCompSurfaceUsage;
if (!is_usage_valid) {
return false;
}

if (!IsFormatSupportedForScanout(format)) {
return false;
}

// IDCompositionDevice2::CreateSurface does not support rgb10. In cases where
// dc overlays are to be used for rgb10, the caller should use swap chains
// instead.
if (usage == kDCompSurfaceUsage &&
format.resource_format() == viz::ResourceFormat::RGBA_1010102) {
return false;
}

if (thread_safe) {
return false;
}

if (gmb_type != gfx::GpuMemoryBufferType::EMPTY_BUFFER) {
return false;
}

if (!pixel_data.empty()) {
return false;
}

return true;
}

} // namespace gpu

0 comments on commit 455b94a

Please sign in to comment.