|
| 1 | +// Copyright 2016 The Chromium Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "gpu/ipc/service/pass_through_image_transport_surface.h" |
| 6 | + |
| 7 | +#include <utility> |
| 8 | + |
| 9 | +#include "base/functional/bind.h" |
| 10 | +#include "base/functional/callback_helpers.h" |
| 11 | +#include "base/metrics/histogram_macros.h" |
| 12 | +#include "base/metrics/histogram_macros_local.h" |
| 13 | +#include "base/time/time.h" |
| 14 | +#include "build/build_config.h" |
| 15 | +#include "gpu/command_buffer/common/swap_buffers_complete_params.h" |
| 16 | +#include "ui/gfx/vsync_provider.h" |
| 17 | +#include "ui/gl/gl_context.h" |
| 18 | +#include "ui/gl/gl_features.h" |
| 19 | + |
| 20 | +namespace gpu { |
| 21 | + |
| 22 | +namespace { |
| 23 | +// Number of swap generations before vsync is reenabled after we've stopped |
| 24 | +// doing multiple swaps per frame. |
| 25 | +const int kMultiWindowSwapEnableVSyncDelay = 60; |
| 26 | + |
| 27 | +int g_current_swap_generation_ = 0; |
| 28 | +int g_num_swaps_in_current_swap_generation_ = 0; |
| 29 | +int g_last_multi_window_swap_generation_ = 0; |
| 30 | + |
| 31 | +} // anonymous namespace |
| 32 | + |
| 33 | +PassThroughImageTransportSurface::PassThroughImageTransportSurface( |
| 34 | + base::WeakPtr<ImageTransportSurfaceDelegate> delegate, |
| 35 | + gl::GLSurface* surface, |
| 36 | + bool override_vsync_for_multi_window_swap) |
| 37 | + : GLSurfaceAdapter(surface), |
| 38 | + is_gpu_vsync_disabled_(!features::UseGpuVsync()), |
| 39 | + is_multi_window_swap_vsync_override_enabled_( |
| 40 | + override_vsync_for_multi_window_swap), |
| 41 | + delegate_(delegate) {} |
| 42 | + |
| 43 | +PassThroughImageTransportSurface::~PassThroughImageTransportSurface() = default; |
| 44 | + |
| 45 | +bool PassThroughImageTransportSurface::Initialize(gl::GLSurfaceFormat format) { |
| 46 | + // The surface is assumed to have already been initialized. |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +gfx::SwapResult PassThroughImageTransportSurface::SwapBuffers( |
| 51 | + PresentationCallback callback, |
| 52 | + gfx::FrameData data) { |
| 53 | + StartSwapBuffers(); |
| 54 | + gfx::SwapResult result = |
| 55 | + gl::GLSurfaceAdapter::SwapBuffers(std::move(callback), data); |
| 56 | + return result; |
| 57 | +} |
| 58 | + |
| 59 | +void PassThroughImageTransportSurface::SwapBuffersAsync( |
| 60 | + SwapCompletionCallback completion_callback, |
| 61 | + PresentationCallback presentation_callback, |
| 62 | + gfx::FrameData data) { |
| 63 | + StartSwapBuffers(); |
| 64 | + |
| 65 | + gl::GLSurfaceAdapter::SwapBuffersAsync( |
| 66 | + std::move(completion_callback), std::move(presentation_callback), data); |
| 67 | +} |
| 68 | + |
| 69 | +gfx::SwapResult PassThroughImageTransportSurface::SwapBuffersWithBounds( |
| 70 | + const std::vector<gfx::Rect>& rects, |
| 71 | + PresentationCallback callback, |
| 72 | + gfx::FrameData data) { |
| 73 | + StartSwapBuffers(); |
| 74 | + gfx::SwapResult result = gl::GLSurfaceAdapter::SwapBuffersWithBounds( |
| 75 | + rects, std::move(callback), data); |
| 76 | + return result; |
| 77 | +} |
| 78 | + |
| 79 | +gfx::SwapResult PassThroughImageTransportSurface::PostSubBuffer( |
| 80 | + int x, |
| 81 | + int y, |
| 82 | + int width, |
| 83 | + int height, |
| 84 | + PresentationCallback callback, |
| 85 | + gfx::FrameData data) { |
| 86 | + StartSwapBuffers(); |
| 87 | + gfx::SwapResult result = gl::GLSurfaceAdapter::PostSubBuffer( |
| 88 | + x, y, width, height, std::move(callback), data); |
| 89 | + |
| 90 | + return result; |
| 91 | +} |
| 92 | + |
| 93 | +void PassThroughImageTransportSurface::PostSubBufferAsync( |
| 94 | + int x, |
| 95 | + int y, |
| 96 | + int width, |
| 97 | + int height, |
| 98 | + SwapCompletionCallback completion_callback, |
| 99 | + PresentationCallback presentation_callback, |
| 100 | + gfx::FrameData data) { |
| 101 | + StartSwapBuffers(); |
| 102 | + gl::GLSurfaceAdapter::PostSubBufferAsync(x, y, width, height, |
| 103 | + |
| 104 | + std::move(completion_callback), |
| 105 | + |
| 106 | + std::move(presentation_callback), |
| 107 | + data); |
| 108 | +} |
| 109 | + |
| 110 | +void PassThroughImageTransportSurface::SetVSyncEnabled(bool enabled) { |
| 111 | + if (vsync_enabled_ == enabled) |
| 112 | + return; |
| 113 | + vsync_enabled_ = enabled; |
| 114 | + GLSurfaceAdapter::SetVSyncEnabled(enabled); |
| 115 | +} |
| 116 | + |
| 117 | +void PassThroughImageTransportSurface::TrackMultiSurfaceSwap() { |
| 118 | + // This code is a simple way of enforcing that we only vsync if one surface |
| 119 | + // is swapping per frame. This provides single window cases a stable refresh |
| 120 | + // while allowing multi-window cases to not slow down due to multiple syncs |
| 121 | + // on a single thread. A better way to fix this problem would be to have |
| 122 | + // each surface present on its own thread. |
| 123 | + if (g_current_swap_generation_ == swap_generation_) { |
| 124 | + // No other surface has swapped since we swapped last time. |
| 125 | + if (g_num_swaps_in_current_swap_generation_ > 1) |
| 126 | + g_last_multi_window_swap_generation_ = g_current_swap_generation_; |
| 127 | + g_num_swaps_in_current_swap_generation_ = 0; |
| 128 | + g_current_swap_generation_++; |
| 129 | + } |
| 130 | + |
| 131 | + swap_generation_ = g_current_swap_generation_; |
| 132 | + g_num_swaps_in_current_swap_generation_++; |
| 133 | + |
| 134 | + multiple_surfaces_swapped_ = |
| 135 | + (g_num_swaps_in_current_swap_generation_ > 1) || |
| 136 | + (g_current_swap_generation_ - g_last_multi_window_swap_generation_ < |
| 137 | + kMultiWindowSwapEnableVSyncDelay); |
| 138 | +} |
| 139 | + |
| 140 | +void PassThroughImageTransportSurface::UpdateVSyncEnabled() { |
| 141 | + if (is_gpu_vsync_disabled_) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + if (is_multi_window_swap_vsync_override_enabled_) { |
| 146 | + SetVSyncEnabled(!multiple_surfaces_swapped_); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +void PassThroughImageTransportSurface::StartSwapBuffers() { |
| 151 | + TrackMultiSurfaceSwap(); |
| 152 | + UpdateVSyncEnabled(); |
| 153 | +} |
| 154 | + |
| 155 | +} // namespace gpu |
0 commit comments