Skip to content

Commit

Permalink
Add VideoGeometrySetter Service for Cobalt
Browse files Browse the repository at this point in the history
StarboardRender needs to be informed with the video geometry information from
the display compositor. VideoGeometrySetter provides the IPC between
the StarobardRenderer and the display compositor so the video geometry
information can reach StarboardRenderer.

This is from https://chromium-review.googlesource.com/c/chromium/src/+/1799692.

b/391938746
  • Loading branch information
borongc committed Feb 6, 2025
1 parent 76256e5 commit 96701d6
Show file tree
Hide file tree
Showing 40 changed files with 540 additions and 210 deletions.
1 change: 1 addition & 0 deletions cobalt/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ if (!is_android) {

deps = [
"//cobalt/browser",
"//cobalt/gpu:gpu",
"//cobalt/renderer:renderer",
"//content/public/app",
"//content/shell:content_shell_app",
Expand Down
1 change: 1 addition & 0 deletions cobalt/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ shared_library("libcobalt_content_shell_content_view") {
deps = [
":content_shell_jni_headers",
"//cobalt/browser",
"//cobalt/gpu:gpu",
"//cobalt/renderer:renderer",

# TODO: what can be removed in the dependencies?
Expand Down
3 changes: 3 additions & 0 deletions cobalt/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ source_set("browser") {
"cobalt_browser_interface_binders.h",
"cobalt_content_browser_client.cc",
"cobalt_content_browser_client.h",
"cobalt_render_process_observer.cc",
"cobalt_render_process_observer.h",
"cobalt_web_contents_observer.cc",
"cobalt_web_contents_observer.h",
]
Expand All @@ -30,6 +32,7 @@ source_set("browser") {
":embed_polyfilled_javascript",
"//cobalt/browser/crash_annotator",
"//cobalt/browser/crash_annotator/public/mojom",
"//cobalt/media/service/mojom",
"//cobalt/user_agent",
"//components/js_injection/browser:browser",
"//content/public/browser",
Expand Down
24 changes: 23 additions & 1 deletion cobalt/browser/cobalt_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string>

#include "cobalt/browser/cobalt_browser_interface_binders.h"
#include "cobalt/media/service/mojom/video_geometry_setter.mojom.h"
#include "cobalt/user_agent/user_agent_platform_info.h"
#include "content/public/common/user_agent.h"
// TODO(b/390021478): Remove this include when CobaltBrowserMainParts stops
Expand Down Expand Up @@ -83,10 +84,17 @@ blink::UserAgentMetadata GetCobaltUserAgentMetadata() {
return metadata;
}

CobaltContentBrowserClient::CobaltContentBrowserClient() = default;
CobaltContentBrowserClient::CobaltContentBrowserClient() {
render_process_observer_.reset(new CobaltRenderProcessObserver());
};

CobaltContentBrowserClient::~CobaltContentBrowserClient() = default;

void CobaltContentBrowserClient::RenderProcessWillLaunch(
content::RenderProcessHost* host) {
render_process_observer_->Add(host);
}

std::unique_ptr<content::BrowserMainParts>
CobaltContentBrowserClient::CreateBrowserMainParts(
bool /* is_integration_test */) {
Expand Down Expand Up @@ -133,4 +141,18 @@ void CobaltContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
render_frame_host, map);
}

void CobaltContentBrowserClient::BindGpuHostReceiver(
mojo::GenericPendingReceiver receiver) {
if (auto r = receiver.As<media::mojom::VideoGeometrySetter>()) {
for (auto renderer_process_id :
render_process_observer_->GetRenderProcessIDs()) {
content::RenderProcessHost* host =
content::RenderProcessHost::FromID(renderer_process_id);
if (host) {
host->BindReceiver(std::move(r));
}
}
}
}

} // namespace cobalt
6 changes: 6 additions & 0 deletions cobalt/browser/cobalt_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
#ifndef COBALT_BROWSER_COBALT_CONTENT_BROWSER_CLIENT_H_
#define COBALT_BROWSER_COBALT_CONTENT_BROWSER_CLIENT_H_

#include "cobalt/browser/cobalt_render_process_observer.h"
#include "cobalt/browser/cobalt_web_contents_observer.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/shell/browser/shell_content_browser_client.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"

namespace content {
class BrowserMainParts;
Expand All @@ -44,6 +47,7 @@ class CobaltContentBrowserClient : public content::ShellContentBrowserClient {
// ShellContentBrowserClient overrides.
std::unique_ptr<content::BrowserMainParts> CreateBrowserMainParts(
bool is_integration_test) override;
void RenderProcessWillLaunch(content::RenderProcessHost* host) override;
std::string GetUserAgent() override;
std::string GetFullUserAgent() override;
std::string GetReducedUserAgent() override;
Expand All @@ -55,9 +59,11 @@ class CobaltContentBrowserClient : public content::ShellContentBrowserClient {
content::RenderFrameHost* render_frame_host,
mojo::BinderMapWithContext<content::RenderFrameHost*>* binder_map)
override;
void BindGpuHostReceiver(mojo::GenericPendingReceiver receiver) override;

private:
std::unique_ptr<CobaltWebContentsObserver> web_contents_observer_;
std::unique_ptr<CobaltRenderProcessObserver> render_process_observer_;
};

} // namespace cobalt
Expand Down
70 changes: 70 additions & 0 deletions cobalt/browser/cobalt_render_process_observer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/browser/cobalt_render_process_observer.h"

#include "base/logging.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"

namespace cobalt {

CobaltRenderProcessObserver::CobaltRenderProcessObserver() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
LOG(INFO) << "CobaltRenderProcessObserver constructed.";
}

CobaltRenderProcessObserver::~CobaltRenderProcessObserver() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
LOG(INFO) << "Destructing CobaltRenderProcessObserver.";
for (auto renderer_process_id : renderers_) {
content::RenderProcessHost* process_host =
content::RenderProcessHost::FromID(renderer_process_id);
if (process_host) {
process_host->RemoveObserver(this);
}
}
renderers_.clear();
};

void CobaltRenderProcessObserver::Add(
content::RenderProcessHost* process_host) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
std::unordered_set<int>::const_iterator it =
renderers_.find(process_host->GetID());
if (it == renderers_.end()) {
base::AutoLock auto_lock(lock_);
process_host->AddObserver(this);
renderers_.insert(process_host->GetID());
}
}

const std::unordered_set<int>&
CobaltRenderProcessObserver::GetRenderProcessIDs() {
return renderers_;
}

void CobaltRenderProcessObserver::RenderProcessExited(
content::RenderProcessHost* process_host,
const content::ChildProcessTerminationInfo& info) {
std::unordered_set<int>::const_iterator it =
renderers_.find(process_host->GetID());
if (it != renderers_.end()) {
base::AutoLock auto_lock(lock_);
process_host->RemoveObserver(this);
renderers_.erase(process_host->GetID());
}
}

} // namespace cobalt
55 changes: 55 additions & 0 deletions cobalt/browser/cobalt_render_process_observer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef COBALT_BROWSER_COBALT_RENDER_PROCESS_OBSERVER_H_
#define COBALT_BROWSER_COBALT_RENDER_PROCESS_OBSERVER_H_

#include <unordered_set>

#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_observer.h"

namespace cobalt {

class CobaltRenderProcessObserver : public content::RenderProcessHostObserver {
public:
CobaltRenderProcessObserver();

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

~CobaltRenderProcessObserver() override;

void Add(content::RenderProcessHost* process_host);

const std::unordered_set<int>& GetRenderProcessIDs();

private:
// content::RenderProcessHostObserver implementation
void RenderProcessExited(
content::RenderProcessHost* host,
const content::ChildProcessTerminationInfo& info) override;

mutable base::Lock lock_;
std::unordered_set<int> renderers_;

base::WeakPtrFactory<CobaltRenderProcessObserver> weak_factory_{this};
};

} // namespace cobalt

#endif // COBALT_BROWSER_COBALT_RENDER_PROCESS_OBSERVER_H_
6 changes: 6 additions & 0 deletions cobalt/cobalt_main_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "base/process/current_process.h"
#include "base/trace_event/trace_log.h"
#include "cobalt/browser/cobalt_content_browser_client.h"
#include "cobalt/gpu/cobalt_content_gpu_client.h"
#include "cobalt/renderer/cobalt_content_renderer_client.h"
#include "content/common/content_constants_internal.h"
#include "content/public/browser/render_frame_host.h"
Expand All @@ -34,6 +35,11 @@ CobaltMainDelegate::CreateContentBrowserClient() {
return browser_client_.get();
}

content::ContentGpuClient* CobaltMainDelegate::CreateContentGpuClient() {
gpu_client_ = CobaltContentGpuClient::Create();
return gpu_client_.get();
}

content::ContentRendererClient*
CobaltMainDelegate::CreateContentRendererClient() {
renderer_client_ = std::make_unique<CobaltContentRendererClient>();
Expand Down
3 changes: 3 additions & 0 deletions cobalt/cobalt_main_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define COBALT_COBALT_MAIN_DELEGATE_H_

#include "build/build_config.h"
#include "cobalt/gpu/cobalt_content_gpu_client.h"
#include "cobalt/renderer/cobalt_content_renderer_client.h"
#include "content/public/browser/browser_main_runner.h"
#include "content/shell/app/shell_main_delegate.h"
Expand All @@ -31,6 +32,7 @@ class CobaltMainDelegate : public content::ShellMainDelegate {

// ContentMainDelegate implementation:
content::ContentBrowserClient* CreateContentBrowserClient() override;
content::ContentGpuClient* CreateContentGpuClient() override;
content::ContentRendererClient* CreateContentRendererClient() override;
absl::optional<int> PostEarlyInitialization(InvokedIn invoked_in) override;

Expand All @@ -48,6 +50,7 @@ class CobaltMainDelegate : public content::ShellMainDelegate {

private:
std::unique_ptr<content::BrowserMainRunner> main_runner_;
std::unique_ptr<CobaltContentGpuClient> gpu_client_;
std::unique_ptr<CobaltContentRendererClient> renderer_client_;
};

Expand Down
29 changes: 29 additions & 0 deletions cobalt/gpu/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2025 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

source_set("gpu") {
sources = [
"cobalt_content_gpu_client.cc",
"cobalt_content_gpu_client.h",
]

deps = [
"//base",
"//cobalt/media/service/mojom",
"//components/viz/common",
"//components/viz/service",
"//content/public/child",
"//content/public/gpu",
]
}
37 changes: 37 additions & 0 deletions cobalt/gpu/cobalt_content_gpu_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2025 The Cobalt Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cobalt/gpu/cobalt_content_gpu_client.h"

#include "base/memory/ptr_util.h"
#include "base/task/single_thread_task_runner.h"
#include "components/viz/service/display/starboard/overlay_strategy_underlay_starboard.h"
#include "content/public/child/child_thread.h"

namespace cobalt {

CobaltContentGpuClient::CobaltContentGpuClient() = default;
CobaltContentGpuClient::~CobaltContentGpuClient() = default;

// static
std::unique_ptr<CobaltContentGpuClient> CobaltContentGpuClient::Create() {
return base::WrapUnique(new CobaltContentGpuClient());
}

void CobaltContentGpuClient::PostCompositorThreadCreated(
base::SingleThreadTaskRunner* task_runner) {
DCHECK(task_runner);
mojo::PendingRemote<cobalt::media::mojom::VideoGeometrySetter>
video_geometry_setter;
content::ChildThread::Get()->BindHostReceiver(
video_geometry_setter.InitWithNewPipeAndPassReceiver());

task_runner->PostTask(
FROM_HERE,
base::BindOnce(
&viz::OverlayStrategyUnderlayStarboard::ConnectVideoGeometrySetter,
std::move(video_geometry_setter)));
}

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

#ifndef COBALT_GPU_COBALT_CONTENT_GPU_CLIENT_H_
#define COBALT_GPU_COBALT_CONTENT_GPU_CLIENT_H_

#include <memory>

#include "base/task/single_thread_task_runner.h"
#include "content/public/gpu/content_gpu_client.h"

namespace cobalt {

class CobaltContentGpuClient : public content::ContentGpuClient {
public:
static std::unique_ptr<CobaltContentGpuClient> Create();

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

~CobaltContentGpuClient() override;

// content::ContentGpuClient:
void PostCompositorThreadCreated(
base::SingleThreadTaskRunner* task_runner) override;

protected:
CobaltContentGpuClient();
};

} // namespace cobalt

#endif // COBALT_GPU_COBALT_CONTENT_GPU_CLIENT_H_
Loading

0 comments on commit 96701d6

Please sign in to comment.