Skip to content

Commit

Permalink
Revert "Remove PassThroughImageTransport and GLSurfaceAdapter"
Browse files Browse the repository at this point in the history
Reason for revert: see #101

This reverts commit f8d4ce8.
  • Loading branch information
zakharvoit committed May 9, 2024
1 parent 82450f8 commit 09ae537
Show file tree
Hide file tree
Showing 6 changed files with 544 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gpu/ipc/service/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ component("service") {
"image_decode_accelerator_worker.h",
"image_transport_surface.h",
"image_transport_surface_delegate.h",
"pass_through_image_transport_surface.cc",
"pass_through_image_transport_surface.h",
"raster_command_buffer_stub.cc",
"raster_command_buffer_stub.h",
"shared_image_stub.cc",
Expand Down
1 change: 1 addition & 0 deletions gpu/ipc/service/image_transport_surface_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "gpu/ipc/service/image_transport_surface.h"

#include "gpu/ipc/service/image_transport_surface_overlay_mac.h"
#include "gpu/ipc/service/pass_through_image_transport_surface.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gl/buildflags.h"
#include "ui/gl/gl_surface_stub.h"
Expand Down
155 changes: 155 additions & 0 deletions gpu/ipc/service/pass_through_image_transport_surface.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright 2016 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/ipc/service/pass_through_image_transport_surface.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/histogram_macros_local.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "gpu/command_buffer/common/swap_buffers_complete_params.h"
#include "ui/gfx/vsync_provider.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_features.h"

namespace gpu {

namespace {
// Number of swap generations before vsync is reenabled after we've stopped
// doing multiple swaps per frame.
const int kMultiWindowSwapEnableVSyncDelay = 60;

int g_current_swap_generation_ = 0;
int g_num_swaps_in_current_swap_generation_ = 0;
int g_last_multi_window_swap_generation_ = 0;

} // anonymous namespace

PassThroughImageTransportSurface::PassThroughImageTransportSurface(
base::WeakPtr<ImageTransportSurfaceDelegate> delegate,
gl::GLSurface* surface,
bool override_vsync_for_multi_window_swap)
: GLSurfaceAdapter(surface),
is_gpu_vsync_disabled_(!features::UseGpuVsync()),
is_multi_window_swap_vsync_override_enabled_(
override_vsync_for_multi_window_swap),
delegate_(delegate) {}

PassThroughImageTransportSurface::~PassThroughImageTransportSurface() = default;

bool PassThroughImageTransportSurface::Initialize(gl::GLSurfaceFormat format) {
// The surface is assumed to have already been initialized.
return true;
}

gfx::SwapResult PassThroughImageTransportSurface::SwapBuffers(
PresentationCallback callback,
gfx::FrameData data) {
StartSwapBuffers();
gfx::SwapResult result =
gl::GLSurfaceAdapter::SwapBuffers(std::move(callback), data);
return result;
}

void PassThroughImageTransportSurface::SwapBuffersAsync(
SwapCompletionCallback completion_callback,
PresentationCallback presentation_callback,
gfx::FrameData data) {
StartSwapBuffers();

gl::GLSurfaceAdapter::SwapBuffersAsync(
std::move(completion_callback), std::move(presentation_callback), data);
}

gfx::SwapResult PassThroughImageTransportSurface::SwapBuffersWithBounds(
const std::vector<gfx::Rect>& rects,
PresentationCallback callback,
gfx::FrameData data) {
StartSwapBuffers();
gfx::SwapResult result = gl::GLSurfaceAdapter::SwapBuffersWithBounds(
rects, std::move(callback), data);
return result;
}

gfx::SwapResult PassThroughImageTransportSurface::PostSubBuffer(
int x,
int y,
int width,
int height,
PresentationCallback callback,
gfx::FrameData data) {
StartSwapBuffers();
gfx::SwapResult result = gl::GLSurfaceAdapter::PostSubBuffer(
x, y, width, height, std::move(callback), data);

return result;
}

void PassThroughImageTransportSurface::PostSubBufferAsync(
int x,
int y,
int width,
int height,
SwapCompletionCallback completion_callback,
PresentationCallback presentation_callback,
gfx::FrameData data) {
StartSwapBuffers();
gl::GLSurfaceAdapter::PostSubBufferAsync(x, y, width, height,

std::move(completion_callback),

std::move(presentation_callback),
data);
}

void PassThroughImageTransportSurface::SetVSyncEnabled(bool enabled) {
if (vsync_enabled_ == enabled)
return;
vsync_enabled_ = enabled;
GLSurfaceAdapter::SetVSyncEnabled(enabled);
}

void PassThroughImageTransportSurface::TrackMultiSurfaceSwap() {
// This code is a simple way of enforcing that we only vsync if one surface
// is swapping per frame. This provides single window cases a stable refresh
// while allowing multi-window cases to not slow down due to multiple syncs
// on a single thread. A better way to fix this problem would be to have
// each surface present on its own thread.
if (g_current_swap_generation_ == swap_generation_) {
// No other surface has swapped since we swapped last time.
if (g_num_swaps_in_current_swap_generation_ > 1)
g_last_multi_window_swap_generation_ = g_current_swap_generation_;
g_num_swaps_in_current_swap_generation_ = 0;
g_current_swap_generation_++;
}

swap_generation_ = g_current_swap_generation_;
g_num_swaps_in_current_swap_generation_++;

multiple_surfaces_swapped_ =
(g_num_swaps_in_current_swap_generation_ > 1) ||
(g_current_swap_generation_ - g_last_multi_window_swap_generation_ <
kMultiWindowSwapEnableVSyncDelay);
}

void PassThroughImageTransportSurface::UpdateVSyncEnabled() {
if (is_gpu_vsync_disabled_) {
return;
}

if (is_multi_window_swap_vsync_override_enabled_) {
SetVSyncEnabled(!multiple_surfaces_swapped_);
}
}

void PassThroughImageTransportSurface::StartSwapBuffers() {
TrackMultiSurfaceSwap();
UpdateVSyncEnabled();
}

} // namespace gpu
78 changes: 78 additions & 0 deletions gpu/ipc/service/pass_through_image_transport_surface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_IPC_SERVICE_PASS_THROUGH_IMAGE_TRANSPORT_SURFACE_H_
#define GPU_IPC_SERVICE_PASS_THROUGH_IMAGE_TRANSPORT_SURFACE_H_

#include <stdint.h>

#include <memory>
#include <vector>

#include "base/containers/queue.h"
#include "base/memory/weak_ptr.h"
#include "gpu/ipc/service/image_transport_surface.h"
#include "gpu/ipc/service/image_transport_surface_delegate.h"
#include "ui/gl/gl_surface.h"

namespace gpu {

// An implementation of ImageTransportSurface that implements GLSurface through
// GLSurfaceAdapter, thereby forwarding GLSurface methods through to it.
class PassThroughImageTransportSurface : public gl::GLSurfaceAdapter {
public:
PassThroughImageTransportSurface(
base::WeakPtr<ImageTransportSurfaceDelegate> delegate,
gl::GLSurface* surface,
bool override_vsync_for_multi_window_swap);

PassThroughImageTransportSurface(const PassThroughImageTransportSurface&) =
delete;
PassThroughImageTransportSurface& operator=(
const PassThroughImageTransportSurface&) = delete;

// GLSurface implementation.
bool Initialize(gl::GLSurfaceFormat format) override;
gfx::SwapResult SwapBuffers(PresentationCallback callback,
gfx::FrameData data) override;
void SwapBuffersAsync(SwapCompletionCallback completion_callback,
PresentationCallback presentation_callback,
gfx::FrameData data) override;
gfx::SwapResult SwapBuffersWithBounds(const std::vector<gfx::Rect>& rects,
PresentationCallback callback,
gfx::FrameData data) override;
gfx::SwapResult PostSubBuffer(int x,
int y,
int width,
int height,
PresentationCallback callback,
gfx::FrameData data) override;
void PostSubBufferAsync(int x,
int y,
int width,
int height,
SwapCompletionCallback completion_callback,
PresentationCallback presentation_callback,
gfx::FrameData data) override;
void SetVSyncEnabled(bool enabled) override;

private:
~PassThroughImageTransportSurface() override;

void TrackMultiSurfaceSwap();
void UpdateVSyncEnabled();

void StartSwapBuffers();

const bool is_gpu_vsync_disabled_;
const bool is_multi_window_swap_vsync_override_enabled_;
base::WeakPtr<ImageTransportSurfaceDelegate> delegate_;
int swap_generation_ = 0;
bool vsync_enabled_ = true;
bool multiple_surfaces_swapped_ = false;
};

} // namespace gpu

#endif // GPU_IPC_SERVICE_PASS_THROUGH_IMAGE_TRANSPORT_SURFACE_H_
Loading

0 comments on commit 09ae537

Please sign in to comment.