Skip to content

Commit f19998d

Browse files
authored
Reland "Add VideoGeometrySetter Service for Cobalt (#4810)" (#4921)
This is a reland of 10d4851 This CL fixed DCHECK failed caused by the PR (youtube/cobalt#4810) and list the fixes below (b/397413189): - Removed `DCHECK(content::RenderThread::IsMainThread());` in CobaltContentRendererClient due to CobaltContentRendererClient is not in Render Thread. - Moved the order of `DCHECK(video_geometry_change_subcriber_remote_);` after `video_geometry_setter_service_->GetVideoGeometryChangeSubscriber` in StarboardRenderer. - Changed `gfx::ToNearestRect()` to `gfx::ToEnclosingRect()` in StarboardRenderer. Original change's description: > Add VideoGeometrySetter Service for Cobalt (#4810) > > 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/cobalt#4385, because it is unnecessary > with this PR. > > b/391938746
1 parent cfc7c1c commit f19998d

40 files changed

+635
-223
lines changed

cobalt/BUILD.gn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ if (!is_android) {
4747
deps = [
4848
"//cobalt/browser",
4949
"//cobalt/browser:switches",
50-
"//cobalt/renderer:renderer",
50+
"//cobalt/gpu",
51+
"//cobalt/renderer",
5152
"//content/public/app",
5253
"//content/shell:content_shell_app",
5354
"//content/shell:content_shell_lib",

cobalt/android/BUILD.gn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ shared_library("libcobalt_content_shell_content_view") {
272272
deps = [
273273
":content_shell_jni_headers",
274274
"//cobalt/browser",
275-
"//cobalt/renderer:renderer",
275+
"//cobalt/gpu",
276+
"//cobalt/renderer",
276277

277278
# TODO: what can be removed in the dependencies?
278279
"//components/crash/content/browser",

cobalt/browser/BUILD.gn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ source_set("browser") {
2424
"cobalt_browser_interface_binders.h",
2525
"cobalt_content_browser_client.cc",
2626
"cobalt_content_browser_client.h",
27+
"cobalt_single_render_process_observer.cc",
28+
"cobalt_single_render_process_observer.h",
2729
"cobalt_web_contents_observer.cc",
2830
"cobalt_web_contents_observer.h",
2931
]
@@ -42,6 +44,7 @@ source_set("browser") {
4244
"//cobalt/browser/h5vcc_runtime/public/mojom",
4345
"//cobalt/browser/h5vcc_system",
4446
"//cobalt/browser/h5vcc_system/public/mojom",
47+
"//cobalt/media/service/mojom",
4548
"//cobalt/user_agent",
4649
"//components/js_injection/browser:browser",
4750
"//content/public/browser",

cobalt/browser/cobalt_content_browser_client.cc

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717
#include <string>
1818

1919
#include "cobalt/browser/cobalt_browser_interface_binders.h"
20+
#include "cobalt/browser/cobalt_web_contents_observer.h"
21+
#include "cobalt/media/service/mojom/video_geometry_setter.mojom.h"
2022
#include "cobalt/user_agent/user_agent_platform_info.h"
23+
#include "content/public/browser/browser_thread.h"
24+
#include "content/public/browser/render_process_host.h"
25+
#include "content/public/browser/web_contents.h"
2126
#include "content/public/common/user_agent.h"
2227
// TODO(b/390021478): Remove this include when CobaltBrowserMainParts stops
2328
// being a ShellBrowserMainParts.
2429
#include "content/shell/browser/shell_browser_main_parts.h"
2530
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
2631

27-
#include "base/logging.h"
28-
2932
#if BUILDFLAG(IS_ANDROIDTV)
3033
#include "cobalt/browser/android/mojo/cobalt_interface_registrar_android.h"
3134
#endif
@@ -100,54 +103,86 @@ blink::UserAgentMetadata GetCobaltUserAgentMetadata() {
100103
return metadata;
101104
}
102105

103-
CobaltContentBrowserClient::CobaltContentBrowserClient() = default;
106+
CobaltContentBrowserClient::CobaltContentBrowserClient() {
107+
DETACH_FROM_THREAD(thread_checker_);
108+
}
104109

105110
CobaltContentBrowserClient::~CobaltContentBrowserClient() = default;
106111

112+
void CobaltContentBrowserClient::RenderProcessWillLaunch(
113+
content::RenderProcessHost* host) {
114+
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
115+
single_render_process_observer_.UpdateRenderProcessHost(host);
116+
}
117+
107118
std::unique_ptr<content::BrowserMainParts>
108119
CobaltContentBrowserClient::CreateBrowserMainParts(
109120
bool /* is_integration_test */) {
121+
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
110122
auto browser_main_parts = std::make_unique<CobaltBrowserMainParts>();
111123
set_browser_main_parts(browser_main_parts.get());
112124
return browser_main_parts;
113125
}
114126

115127
std::string CobaltContentBrowserClient::GetUserAgent() {
128+
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
116129
return GetCobaltUserAgent();
117130
}
118131

119132
std::string CobaltContentBrowserClient::GetFullUserAgent() {
133+
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
120134
return GetCobaltUserAgent();
121135
}
122136

123137
std::string CobaltContentBrowserClient::GetReducedUserAgent() {
138+
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
124139
return GetCobaltUserAgent();
125140
}
126141

127142
blink::UserAgentMetadata CobaltContentBrowserClient::GetUserAgentMetadata() {
143+
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
128144
return GetCobaltUserAgentMetadata();
129145
}
130146

131147
void CobaltContentBrowserClient::OverrideWebkitPrefs(
132148
content::WebContents* web_contents,
133149
blink::web_pref::WebPreferences* prefs) {
150+
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
134151
#if !defined(COBALT_IS_RELEASE_BUILD)
135152
// Allow creating a ws: connection on a https: page to allow current
136153
// testing set up. See b/377410179.
137154
prefs->allow_running_insecure_content = true;
138155
#endif // !defined(COBALT_IS_RELEASE_BUILD)
139156
content::ShellContentBrowserClient::OverrideWebkitPrefs(web_contents, prefs);
140157
}
158+
141159
void CobaltContentBrowserClient::OnWebContentsCreated(
142160
content::WebContents* web_contents) {
161+
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
143162
web_contents_observer_.reset(new CobaltWebContentsObserver(web_contents));
144163
}
164+
145165
void CobaltContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
146166
content::RenderFrameHost* render_frame_host,
147167
mojo::BinderMapWithContext<content::RenderFrameHost*>* map) {
168+
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
148169
PopulateCobaltFrameBinders(render_frame_host, map);
149170
ShellContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
150171
render_frame_host, map);
151172
}
152173

174+
void CobaltContentBrowserClient::BindGpuHostReceiver(
175+
mojo::GenericPendingReceiver receiver) {
176+
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
177+
if (auto r = receiver.As<media::mojom::VideoGeometrySetter>()) {
178+
const auto renderer_process_id =
179+
single_render_process_observer_.renderer_id();
180+
content::RenderProcessHost* host =
181+
content::RenderProcessHost::FromID(renderer_process_id);
182+
if (host) {
183+
host->BindReceiver(std::move(r));
184+
}
185+
}
186+
}
187+
153188
} // namespace cobalt

cobalt/browser/cobalt_content_browser_client.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
#ifndef COBALT_BROWSER_COBALT_CONTENT_BROWSER_CLIENT_H_
1616
#define COBALT_BROWSER_COBALT_CONTENT_BROWSER_CLIENT_H_
1717

18-
#include "cobalt/browser/cobalt_web_contents_observer.h"
19-
#include "content/public/browser/web_contents.h"
18+
#include "base/threading/thread_checker.h"
19+
#include "cobalt/browser/cobalt_single_render_process_observer.h"
2020
#include "content/shell/browser/shell_content_browser_client.h"
21+
#include "mojo/public/cpp/bindings/pending_receiver.h"
2122

2223
namespace content {
2324
class BrowserMainParts;
2425
class RenderFrameHost;
26+
class RenderProcessHost;
27+
class WebContents;
2528
} // namespace content
2629

2730
namespace mojo {
@@ -31,6 +34,8 @@ class BinderMapWithContext;
3134

3235
namespace cobalt {
3336

37+
class CobaltWebContentsObserver;
38+
3439
// This class allows Cobalt to inject specific logic in the business of the
3540
// browser (i.e. of Content), for example for startup or to override the UA.
3641
// TODO(b/390021478): In time CobaltContentBrowserClient should derive and
@@ -39,11 +44,17 @@ namespace cobalt {
3944
class CobaltContentBrowserClient : public content::ShellContentBrowserClient {
4045
public:
4146
CobaltContentBrowserClient();
47+
48+
CobaltContentBrowserClient(const CobaltContentBrowserClient&) = delete;
49+
CobaltContentBrowserClient& operator=(const CobaltContentBrowserClient&) =
50+
delete;
51+
4252
~CobaltContentBrowserClient() override;
4353

4454
// ShellContentBrowserClient overrides.
4555
std::unique_ptr<content::BrowserMainParts> CreateBrowserMainParts(
4656
bool is_integration_test) override;
57+
void RenderProcessWillLaunch(content::RenderProcessHost* host) override;
4758
std::string GetUserAgent() override;
4859
std::string GetFullUserAgent() override;
4960
std::string GetReducedUserAgent() override;
@@ -55,9 +66,13 @@ class CobaltContentBrowserClient : public content::ShellContentBrowserClient {
5566
content::RenderFrameHost* render_frame_host,
5667
mojo::BinderMapWithContext<content::RenderFrameHost*>* binder_map)
5768
override;
69+
void BindGpuHostReceiver(mojo::GenericPendingReceiver receiver) override;
5870

5971
private:
6072
std::unique_ptr<CobaltWebContentsObserver> web_contents_observer_;
73+
CobaltSingleRenderProcessObserver single_render_process_observer_;
74+
75+
THREAD_CHECKER(thread_checker_);
6176
};
6277

6378
} // namespace cobalt
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "cobalt/browser/cobalt_single_render_process_observer.h"
16+
17+
#include "content/public/browser/browser_thread.h"
18+
#include "content/public/browser/render_process_host.h"
19+
#include "content/public/browser/render_process_host_observer.h"
20+
21+
namespace cobalt {
22+
23+
CobaltSingleRenderProcessObserver::CobaltSingleRenderProcessObserver() =
24+
default;
25+
CobaltSingleRenderProcessObserver::~CobaltSingleRenderProcessObserver() =
26+
default;
27+
28+
void CobaltSingleRenderProcessObserver::UpdateRenderProcessHost(
29+
content::RenderProcessHost* host) {
30+
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
31+
DCHECK_EQ(renderer_id_, content::kInvalidChildProcessUniqueId)
32+
<< "Cobalt should only have one renderer.";
33+
renderer_id_ = host->GetID();
34+
process_observation_.Reset();
35+
if (auto* rph = content::RenderProcessHost::FromID(renderer_id_)) {
36+
process_observation_.Observe(rph);
37+
}
38+
}
39+
40+
void CobaltSingleRenderProcessObserver::RenderProcessExited(
41+
content::RenderProcessHost* host,
42+
const content::ChildProcessTerminationInfo& info) {
43+
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44+
NOTIMPLEMENTED()
45+
<< "CobaltSingleRenderProcessObserver only supports single process.";
46+
}
47+
48+
void CobaltSingleRenderProcessObserver::InProcessRendererExiting(
49+
content::RenderProcessHost* host) {
50+
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
51+
DCHECK_EQ(host->GetID(), renderer_id_)
52+
<< "Cobalt should only have one renderer.";
53+
process_observation_.Reset();
54+
}
55+
56+
} // namespace cobalt
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef COBALT_BROWSER_COBALT_SINGLE_RENDER_PROCESS_OBSERVER_H_
16+
#define COBALT_BROWSER_COBALT_SINGLE_RENDER_PROCESS_OBSERVER_H_
17+
18+
#include "base/memory/weak_ptr.h"
19+
#include "base/scoped_observation.h"
20+
#include "content/public/browser/render_process_host_observer.h"
21+
#include "content/public/common/content_constants.h"
22+
23+
namespace content {
24+
class RenderProcessHost;
25+
class RenderProcessHostObserver;
26+
} // namespace content
27+
28+
namespace cobalt {
29+
30+
// This class keeps track of a Renderer ID during its lifetime. It must
31+
// be used on the UI thread, and can observe one such Renderer.
32+
class CobaltSingleRenderProcessObserver
33+
: public content::RenderProcessHostObserver {
34+
public:
35+
CobaltSingleRenderProcessObserver();
36+
37+
CobaltSingleRenderProcessObserver(const CobaltSingleRenderProcessObserver&) =
38+
delete;
39+
CobaltSingleRenderProcessObserver& operator=(
40+
const CobaltSingleRenderProcessObserver&) = delete;
41+
42+
~CobaltSingleRenderProcessObserver() override;
43+
44+
void UpdateRenderProcessHost(content::RenderProcessHost* host);
45+
int renderer_id() const { return renderer_id_; }
46+
47+
// content::RenderProcessHostObserver implementation
48+
void RenderProcessExited(
49+
content::RenderProcessHost* host,
50+
const content::ChildProcessTerminationInfo& info) override;
51+
void InProcessRendererExiting(content::RenderProcessHost* host) override;
52+
53+
private:
54+
int renderer_id_ = content::kInvalidChildProcessUniqueId;
55+
base::ScopedObservation<content::RenderProcessHost,
56+
content::RenderProcessHostObserver>
57+
process_observation_{this};
58+
};
59+
60+
} // namespace cobalt
61+
62+
#endif // COBALT_BROWSER_COBALT_SINGLE_RENDER_PROCESS_OBSERVER_H_

cobalt/cobalt_main_delegate.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "base/process/current_process.h"
1818
#include "base/trace_event/trace_log.h"
1919
#include "cobalt/browser/cobalt_content_browser_client.h"
20+
#include "cobalt/gpu/cobalt_content_gpu_client.h"
2021
#include "cobalt/renderer/cobalt_content_renderer_client.h"
2122
#include "content/common/content_constants_internal.h"
2223
#include "content/public/browser/render_frame_host.h"
@@ -34,6 +35,11 @@ CobaltMainDelegate::CreateContentBrowserClient() {
3435
return browser_client_.get();
3536
}
3637

38+
content::ContentGpuClient* CobaltMainDelegate::CreateContentGpuClient() {
39+
gpu_client_ = std::make_unique<CobaltContentGpuClient>();
40+
return gpu_client_.get();
41+
}
42+
3743
content::ContentRendererClient*
3844
CobaltMainDelegate::CreateContentRendererClient() {
3945
renderer_client_ = std::make_unique<CobaltContentRendererClient>();

cobalt/cobalt_main_delegate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define COBALT_COBALT_MAIN_DELEGATE_H_
1717

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

3233
// ContentMainDelegate implementation:
3334
content::ContentBrowserClient* CreateContentBrowserClient() override;
35+
content::ContentGpuClient* CreateContentGpuClient() override;
3436
content::ContentRendererClient* CreateContentRendererClient() override;
3537
absl::optional<int> PostEarlyInitialization(InvokedIn invoked_in) override;
3638

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

4951
private:
5052
std::unique_ptr<content::BrowserMainRunner> main_runner_;
53+
std::unique_ptr<CobaltContentGpuClient> gpu_client_;
5154
std::unique_ptr<CobaltContentRendererClient> renderer_client_;
5255
};
5356

cobalt/gpu/BUILD.gn

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2025 The Cobalt Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
source_set("gpu") {
16+
sources = [
17+
"cobalt_content_gpu_client.cc",
18+
"cobalt_content_gpu_client.h",
19+
]
20+
21+
deps = [
22+
"//base",
23+
"//cobalt/media/service/mojom",
24+
"//components/viz/common",
25+
"//components/viz/service",
26+
"//content/public/child",
27+
"//content/public/gpu",
28+
]
29+
}

0 commit comments

Comments
 (0)