Skip to content

Commit

Permalink
[android] Notify ProcessVisibilityTracker in the GPU process
Browse files Browse the repository at this point in the history
This change:
- adds GpuServiceImpl::SetVisibilityChangedCallback() method
to register a callback;
- updates GpuServiceImpl::OnBackgrounded/OnForegrounded to run
the callback on the main thread;
- registers the callback in it GpuChildThread::OnGpuServiceConnection
for an out-of-process GPU service.

This will only work on Android since OnBackgrounded/OnForegrounded is
only called there.

Bug: 1177542
Change-Id: Ia11ff91726f986ad46519a3299fd7e7c12f93dc5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2717340
Reviewed-by: Kenneth Russell <kbr@chromium.org>
Reviewed-by: Eric Seckler <eseckler@chromium.org>
Commit-Queue: Oksana Zhuravlova <oksamyt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#859110}
  • Loading branch information
Oksana Zhuravlova authored and Chromium LUCI CQ committed Mar 2, 2021
1 parent bacc9ba commit 0465118
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
19 changes: 19 additions & 0 deletions components/viz/service/gl/gpu_service_impl.cc
Expand Up @@ -624,6 +624,12 @@ void GpuServiceImpl::FlushPreInitializeLogMessages(mojom::GpuHost* gpu_host) {
GetLogMessageManager()->FlushMessages(gpu_host);
}

void GpuServiceImpl::SetVisibilityChangedCallback(
VisibilityChangedCallback callback) {
DCHECK(main_runner_->BelongsToCurrentThread());
visibility_changed_callback_ = std::move(callback);
}

void GpuServiceImpl::RecordLogMessage(int severity,
const std::string& header,
const std::string& message) {
Expand Down Expand Up @@ -1095,11 +1101,24 @@ void GpuServiceImpl::OnBackgrounded() {

void GpuServiceImpl::OnBackgroundedOnMainThread() {
gpu_channel_manager_->OnApplicationBackgrounded();

if (visibility_changed_callback_)
visibility_changed_callback_.Run(false);
}

void GpuServiceImpl::OnForegrounded() {
DCHECK(io_runner_->BelongsToCurrentThread());
if (watchdog_thread_)
watchdog_thread_->OnForegrounded();

main_runner_->PostTask(
FROM_HERE,
base::BindOnce(&GpuServiceImpl::OnForegroundedOnMainThread, weak_ptr_));
}

void GpuServiceImpl::OnForegroundedOnMainThread() {
if (visibility_changed_callback_)
visibility_changed_callback_.Run(true);
}

#if !defined(OS_ANDROID)
Expand Down
7 changes: 7 additions & 0 deletions components/viz/service/gl/gpu_service_impl.h
Expand Up @@ -317,6 +317,10 @@ class VIZ_SERVICE_EXPORT GpuServiceImpl : public gpu::GpuChannelManagerDelegate,
DawnContextProvider* dawn_context_provider() { return nullptr; }
#endif

using VisibilityChangedCallback =
base::RepeatingCallback<void(bool /*visible*/)>;
void SetVisibilityChangedCallback(VisibilityChangedCallback);

private:
void RecordLogMessage(int severity,
const std::string& header,
Expand All @@ -337,6 +341,7 @@ class VIZ_SERVICE_EXPORT GpuServiceImpl : public gpu::GpuChannelManagerDelegate,
void RequestHDRStatusOnMainThread(RequestHDRStatusCallback callback);

void OnBackgroundedOnMainThread();
void OnForegroundedOnMainThread();

// Ensure that all peak memory tracking occurs on the main thread as all
// MemoryTracker are created on that thread. All requests made before
Expand Down Expand Up @@ -431,6 +436,8 @@ class VIZ_SERVICE_EXPORT GpuServiceImpl : public gpu::GpuChannelManagerDelegate,
// Display compositor contexts that don't have a corresponding GPU channel.
base::ObserverList<gpu::DisplayContext>::Unchecked display_contexts_;

VisibilityChangedCallback visibility_changed_callback_;

base::WeakPtr<GpuServiceImpl> weak_ptr_;
base::WeakPtrFactory<GpuServiceImpl> weak_ptr_factory_{this};

Expand Down
34 changes: 34 additions & 0 deletions components/viz/service/gl/gpu_service_impl_unittest.cc
Expand Up @@ -85,6 +85,8 @@ class GpuServiceTest : public testing::Test {
io_thread_.Stop();
}

base::Optional<bool> visible_;

private:
base::Thread io_thread_;
std::unique_ptr<GpuServiceImpl> gpu_service_;
Expand Down Expand Up @@ -153,4 +155,36 @@ TEST_F(GpuServiceTest, LoseAllContexts) {
gpu_service()->UnregisterDisplayContext(&display_context);
}

// Tests that the visibility callback gets called when visibility changes.
TEST_F(GpuServiceTest, VisibilityCallbackCalled) {
mojo::Remote<mojom::GpuService> gpu_service_remote;
gpu_service()->Bind(gpu_service_remote.BindNewPipeAndPassReceiver());

mojo::PendingRemote<mojom::GpuHost> gpu_host_proxy;
ignore_result(gpu_host_proxy.InitWithNewPipeAndPassReceiver());
gpu_service()->InitializeWithHost(
std::move(gpu_host_proxy), gpu::GpuProcessActivityFlags(),
gl::init::CreateOffscreenGLSurface(gfx::Size()),
/*sync_point_manager=*/nullptr, /*shared_image_manager=*/nullptr,
/*shutdown_event=*/nullptr);
gpu_service_remote.FlushForTesting();

gpu_service()->SetVisibilityChangedCallback(base::BindRepeating(
[](GpuServiceTest* test, bool visible) { test->visible_ = visible; },
base::Unretained(this)));
EXPECT_FALSE(visible_.has_value());

gpu_service_remote->OnForegrounded();
gpu_service_remote.FlushForTesting();

EXPECT_TRUE(visible_.has_value());
EXPECT_TRUE(*visible_);

gpu_service_remote->OnBackgrounded();
gpu_service_remote.FlushForTesting();

EXPECT_TRUE(visible_.has_value());
EXPECT_FALSE(*visible_);
}

} // namespace viz
9 changes: 9 additions & 0 deletions content/gpu/gpu_child_thread.cc
Expand Up @@ -20,6 +20,7 @@
#include "base/threading/thread_checker.h"
#include "build/build_config.h"
#include "content/child/child_process.h"
#include "content/common/process_visibility_tracker.h"
#include "content/gpu/browser_exposed_gpu_interfaces.h"
#include "content/gpu/gpu_service_factory.h"
#include "content/public/common/content_client.h"
Expand Down Expand Up @@ -188,6 +189,14 @@ void GpuChildThread::OnGpuServiceConnection(viz::GpuServiceImpl* gpu_service) {
overlay_factory_cb);
#endif

if (!IsInBrowserProcess()) {
gpu_service->SetVisibilityChangedCallback(
base::BindRepeating([](bool visible) {
ProcessVisibilityTracker::GetInstance()->OnProcessVisibilityChanged(
visible);
}));
}

// Only set once per process instance.
service_factory_.reset(new GpuServiceFactory(
gpu_service->gpu_preferences(),
Expand Down

0 comments on commit 0465118

Please sign in to comment.