Skip to content

Commit

Permalink
Enable allow-tearing for video overlays
Browse files Browse the repository at this point in the history
Chromium has the command-line option "disable-gpu-vsync" to control
presentation with vblank but it doesn't take effect when presenting
video with overlays. This CL enables this feature to reduce video
presenting latency.


Change-Id: I1e74937d15ff8a9f0355fea57a62f3431c27b9ff
Bug: 807406
Change-Id: I1e74937d15ff8a9f0355fea57a62f3431c27b9ff
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1905266
Commit-Queue: Richard Li <richard.li@intel.com>
Reviewed-by: Zhenyao Mo <zmo@chromium.org>
Reviewed-by: Sunny Sachanandani <sunnyps@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715569}
  • Loading branch information
RRRichardLi authored and Commit Bot committed Nov 15, 2019
1 parent 896db8d commit 53142d1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
21 changes: 11 additions & 10 deletions ui/gl/direct_composition_child_surface_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ namespace {
// is made current, then this surface will be suspended.
IDCompositionSurface* g_current_surface = nullptr;

bool AllowTearing() {
// Swap chain tearing is used only if vsync is disabled explicitly.
return base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableGpuVsync) &&
DirectCompositionSurfaceWin::IsSwapChainTearingSupported();
}
} // namespace

DirectCompositionChildSurfaceWin::DirectCompositionChildSurfaceWin() = default;
Expand Down Expand Up @@ -117,8 +111,11 @@ bool DirectCompositionChildSurfaceWin::ReleaseDrawTexture(bool will_discard) {
dcomp_surface_serial_++;
} else if (!will_discard) {
TRACE_EVENT0("gpu", "DirectCompositionChildSurfaceWin::PresentSwapChain");
UINT interval = first_swap_ || !vsync_enabled_ || AllowTearing() ? 0 : 1;
UINT flags = AllowTearing() ? DXGI_PRESENT_ALLOW_TEARING : 0;
const bool use_swap_chain_tearing =
DirectCompositionSurfaceWin::AllowTearing();
UINT interval =
first_swap_ || !vsync_enabled_ || use_swap_chain_tearing ? 0 : 1;
UINT flags = use_swap_chain_tearing ? DXGI_PRESENT_ALLOW_TEARING : 0;
DXGI_PRESENT_PARAMETERS params = {};
RECT dirty_rect = swap_rect_.ToRECT();
params.DirtyRectsCount = 1;
Expand Down Expand Up @@ -307,7 +304,9 @@ bool DirectCompositionChildSurfaceWin::SetDrawRectangle(
desc.AlphaMode = (has_alpha_ || enable_dc_layers_)
? DXGI_ALPHA_MODE_PREMULTIPLIED
: DXGI_ALPHA_MODE_IGNORE;
desc.Flags = AllowTearing() ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0;
desc.Flags = DirectCompositionSurfaceWin::AllowTearing()
? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING
: 0;
HRESULT hr = dxgi_factory->CreateSwapChainForComposition(
d3d11_device_.Get(), &desc, nullptr, &swap_chain_);
first_swap_ = true;
Expand Down Expand Up @@ -405,7 +404,9 @@ bool DirectCompositionChildSurfaceWin::Resize(const gfx::Size& size,
// ResizeBuffers can't change alpha blending mode.
if (swap_chain_ && resize_only) {
DXGI_FORMAT format = ColorSpaceUtils::GetDXGIFormat(color_space_);
UINT flags = AllowTearing() ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0;
UINT flags = DirectCompositionSurfaceWin::AllowTearing()
? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING
: 0;
HRESULT hr = swap_chain_->ResizeBuffers(2 /* BufferCount */, size.width(),
size.height(), format, flags);
UMA_HISTOGRAM_BOOLEAN("GPU.DirectComposition.SwapChainResizeResult",
Expand Down
8 changes: 8 additions & 0 deletions ui/gl/direct_composition_surface_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,14 @@ bool DirectCompositionSurfaceWin::IsSwapChainTearingSupported() {
return supported;
}

// static
bool DirectCompositionSurfaceWin::AllowTearing() {
// Swap chain tearing is used only if vsync is disabled explicitly.
return base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableGpuVsync) &&
DirectCompositionSurfaceWin::IsSwapChainTearingSupported();
}

bool DirectCompositionSurfaceWin::Initialize(GLSurfaceFormat format) {
d3d11_device_ = QueryD3D11DeviceObjectFromANGLE();
if (!d3d11_device_) {
Expand Down
2 changes: 2 additions & 0 deletions ui/gl/direct_composition_surface_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class GL_EXPORT DirectCompositionSurfaceWin : public GLSurfaceEGL,
// Returns true if swap chain tearing is supported.
static bool IsSwapChainTearingSupported();

static bool AllowTearing();

static void SetScaledOverlaysSupportedForTesting(bool value);

static void SetOverlayFormatUsedForTesting(DXGI_FORMAT format);
Expand Down
11 changes: 9 additions & 2 deletions ui/gl/swap_chain_presenter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -872,10 +872,13 @@ bool SwapChainPresenter::PresentToSwapChain(
DCHECK(SUCCEEDED(hr));
event.Wait();
}

const bool use_swap_chain_tearing =
DirectCompositionSurfaceWin::AllowTearing();
UINT flags = use_swap_chain_tearing ? DXGI_PRESENT_ALLOW_TEARING : 0;
UINT interval = use_swap_chain_tearing ? 0 : 1;
// Ignore DXGI_STATUS_OCCLUDED since that's not an error but only indicates
// that the window is occluded and we can stop rendering.
HRESULT hr = swap_chain_->Present(1, 0);
HRESULT hr = swap_chain_->Present(interval, flags);
if (FAILED(hr) && hr != DXGI_STATUS_OCCLUDED) {
DLOG(ERROR) << "Present failed with error 0x" << std::hex << hr;
return false;
Expand Down Expand Up @@ -1159,6 +1162,8 @@ bool SwapChainPresenter::ReallocateSwapChain(
desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
desc.Flags =
DXGI_SWAP_CHAIN_FLAG_YUV_VIDEO | DXGI_SWAP_CHAIN_FLAG_FULLSCREEN_VIDEO;
if (DirectCompositionSurfaceWin::AllowTearing())
desc.Flags |= DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
if (IsProtectedVideo(protected_video_type))
desc.Flags |= DXGI_SWAP_CHAIN_FLAG_DISPLAY_ONLY;
if (protected_video_type == gfx::ProtectedVideoType::kHardwareProtected)
Expand Down Expand Up @@ -1205,6 +1210,8 @@ bool SwapChainPresenter::ReallocateSwapChain(
desc.Flags |= DXGI_SWAP_CHAIN_FLAG_DISPLAY_ONLY;
if (protected_video_type == gfx::ProtectedVideoType::kHardwareProtected)
desc.Flags |= DXGI_SWAP_CHAIN_FLAG_HW_PROTECTED;
if (DirectCompositionSurfaceWin::AllowTearing())
desc.Flags |= DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;

HRESULT hr = media_factory->CreateSwapChainForCompositionSurfaceHandle(
d3d11_device_.Get(), swap_chain_handle_.Get(), &desc, nullptr,
Expand Down

0 comments on commit 53142d1

Please sign in to comment.