Skip to content

Commit

Permalink
Add VideoGeometrySetter Service for Cobalt (youtube#4810)
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 refers to
https://chromium-review.googlesource.com/c/chromium/src/+/1799692 with
the following modifications:
- VideoGeometrySetterService is in Renderer thread, and it is exposed to
ContentBrowserClient.
- ContentBrowserClient binds VideoGeometrySetterService after receiving
mojo::PendingRemote\<cobalt::media::mojom::VideoGeometrySetter\> from
compositing thread (viz) to Renderer thread.
- ContentRendererClient creates a custom StarboardRendererFactory, which
allows to bind |overlay_plane_id| for each StarboardRenderer with
VideoGeometrySetterService.

This CL also cleans up the old implementations for setting video bounds:
youtube#4385, because it is unnecessary
with this PR.

b/391938746
  • Loading branch information
borongc authored Feb 15, 2025
1 parent afdc78d commit 10d4851
Show file tree
Hide file tree
Showing 40 changed files with 609 additions and 223 deletions.
3 changes: 2 additions & 1 deletion cobalt/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ if (!is_android) {
deps = [
"//cobalt/browser",
"//cobalt/browser:switches",
"//cobalt/renderer:renderer",
"//cobalt/gpu",
"//cobalt/renderer",
"//content/public/app",
"//content/shell:content_shell_app",
"//content/shell:content_shell_lib",
Expand Down
3 changes: 2 additions & 1 deletion cobalt/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ shared_library("libcobalt_content_shell_content_view") {
deps = [
":content_shell_jni_headers",
"//cobalt/browser",
"//cobalt/renderer:renderer",
"//cobalt/gpu",
"//cobalt/renderer",

# TODO: what can be removed in the dependencies?
"//components/crash/content/browser",
Expand Down
3 changes: 3 additions & 0 deletions cobalt/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ source_set("browser") {
"cobalt_browser_interface_binders.h",
"cobalt_content_browser_client.cc",
"cobalt_content_browser_client.h",
"cobalt_single_render_process_observer.cc",
"cobalt_single_render_process_observer.h",
"cobalt_web_contents_observer.cc",
"cobalt_web_contents_observer.h",
]
Expand All @@ -42,6 +44,7 @@ source_set("browser") {
"//cobalt/browser/h5vcc_runtime/public/mojom",
"//cobalt/browser/h5vcc_system",
"//cobalt/browser/h5vcc_system/public/mojom",
"//cobalt/media/service/mojom",
"//cobalt/user_agent",
"//components/js_injection/browser:browser",
"//content/public/browser",
Expand Down
29 changes: 26 additions & 3 deletions cobalt/browser/cobalt_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
#include <string>

#include "cobalt/browser/cobalt_browser_interface_binders.h"
#include "cobalt/browser/cobalt_web_contents_observer.h"
#include "cobalt/media/service/mojom/video_geometry_setter.mojom.h"
#include "cobalt/user_agent/user_agent_platform_info.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/user_agent.h"
// TODO(b/390021478): Remove this include when CobaltBrowserMainParts stops
// being a ShellBrowserMainParts.
#include "content/shell/browser/shell_browser_main_parts.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"

#include "base/logging.h"

#if BUILDFLAG(IS_ANDROIDTV)
#include "cobalt/browser/android/mojo/cobalt_interface_registrar_android.h"
#endif
Expand Down Expand Up @@ -101,9 +104,13 @@ blink::UserAgentMetadata GetCobaltUserAgentMetadata() {
}

CobaltContentBrowserClient::CobaltContentBrowserClient() = default;

CobaltContentBrowserClient::~CobaltContentBrowserClient() = default;

void CobaltContentBrowserClient::RenderProcessWillLaunch(
content::RenderProcessHost* host) {
single_render_process_observer_.UpdateRenderProcessHost(host);
}

std::unique_ptr<content::BrowserMainParts>
CobaltContentBrowserClient::CreateBrowserMainParts(
bool /* is_integration_test */) {
Expand Down Expand Up @@ -138,10 +145,12 @@ void CobaltContentBrowserClient::OverrideWebkitPrefs(
#endif // !defined(COBALT_IS_RELEASE_BUILD)
content::ShellContentBrowserClient::OverrideWebkitPrefs(web_contents, prefs);
}

void CobaltContentBrowserClient::OnWebContentsCreated(
content::WebContents* web_contents) {
web_contents_observer_.reset(new CobaltWebContentsObserver(web_contents));
}

void CobaltContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
content::RenderFrameHost* render_frame_host,
mojo::BinderMapWithContext<content::RenderFrameHost*>* map) {
Expand All @@ -150,4 +159,18 @@ void CobaltContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
render_frame_host, map);
}

void CobaltContentBrowserClient::BindGpuHostReceiver(
mojo::GenericPendingReceiver receiver) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (auto r = receiver.As<media::mojom::VideoGeometrySetter>()) {
const auto renderer_process_id =
single_render_process_observer_.renderer_id();
content::RenderProcessHost* host =
content::RenderProcessHost::FromID(renderer_process_id);
if (host) {
host->BindReceiver(std::move(r));
}
}
}

} // namespace cobalt
16 changes: 14 additions & 2 deletions cobalt/browser/cobalt_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#ifndef COBALT_BROWSER_COBALT_CONTENT_BROWSER_CLIENT_H_
#define COBALT_BROWSER_COBALT_CONTENT_BROWSER_CLIENT_H_

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

namespace content {
class BrowserMainParts;
class RenderFrameHost;
class RenderProcessHost;
class WebContents;
} // namespace content

namespace mojo {
Expand All @@ -31,6 +33,8 @@ class BinderMapWithContext;

namespace cobalt {

class CobaltWebContentsObserver;

// This class allows Cobalt to inject specific logic in the business of the
// browser (i.e. of Content), for example for startup or to override the UA.
// TODO(b/390021478): In time CobaltContentBrowserClient should derive and
Expand All @@ -39,11 +43,17 @@ namespace cobalt {
class CobaltContentBrowserClient : public content::ShellContentBrowserClient {
public:
CobaltContentBrowserClient();

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

~CobaltContentBrowserClient() override;

// 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 +65,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_;
CobaltSingleRenderProcessObserver single_render_process_observer_;
};

} // namespace cobalt
Expand Down
56 changes: 56 additions & 0 deletions cobalt/browser/cobalt_single_render_process_observer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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_single_render_process_observer.h"

#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_observer.h"

namespace cobalt {

CobaltSingleRenderProcessObserver::CobaltSingleRenderProcessObserver() =
default;
CobaltSingleRenderProcessObserver::~CobaltSingleRenderProcessObserver() =
default;

void CobaltSingleRenderProcessObserver::UpdateRenderProcessHost(
content::RenderProcessHost* host) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK_EQ(renderer_id_, content::kInvalidChildProcessUniqueId)
<< "Cobalt should only have one renderer.";
renderer_id_ = host->GetID();
process_observation_.Reset();
if (auto* rph = content::RenderProcessHost::FromID(renderer_id_)) {
process_observation_.Observe(rph);
}
}

void CobaltSingleRenderProcessObserver::RenderProcessExited(
content::RenderProcessHost* host,
const content::ChildProcessTerminationInfo& info) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
NOTIMPLEMENTED()
<< "CobaltSingleRenderProcessObserver only supports single process.";
}

void CobaltSingleRenderProcessObserver::InProcessRendererExiting(
content::RenderProcessHost* host) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK_EQ(host->GetID(), renderer_id_)
<< "Cobalt should only have one renderer.";
process_observation_.Reset();
}

} // namespace cobalt
62 changes: 62 additions & 0 deletions cobalt/browser/cobalt_single_render_process_observer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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_SINGLE_RENDER_PROCESS_OBSERVER_H_
#define COBALT_BROWSER_COBALT_SINGLE_RENDER_PROCESS_OBSERVER_H_

#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "content/public/browser/render_process_host_observer.h"
#include "content/public/common/content_constants.h"

namespace content {
class RenderProcessHost;
class RenderProcessHostObserver;
} // namespace content

namespace cobalt {

// This class keeps track of a Renderer ID during its lifetime. It must
// be used on the UI thread, and can observe one such Renderer.
class CobaltSingleRenderProcessObserver
: public content::RenderProcessHostObserver {
public:
CobaltSingleRenderProcessObserver();

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

~CobaltSingleRenderProcessObserver() override;

void UpdateRenderProcessHost(content::RenderProcessHost* host);
int renderer_id() const { return renderer_id_; }

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

private:
int renderer_id_ = content::kInvalidChildProcessUniqueId;
base::ScopedObservation<content::RenderProcessHost,
content::RenderProcessHostObserver>
process_observation_{this};
};

} // namespace cobalt

#endif // COBALT_BROWSER_COBALT_SINGLE_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_ = std::make_unique<CobaltContentGpuClient>();
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",
]
}
Loading

0 comments on commit 10d4851

Please sign in to comment.