forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompositor_base.cc
672 lines (576 loc) · 24.7 KB
/
compositor_base.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/vr/windows/compositor_base.h"
#include "base/functional/bind.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "base/types/cxx23_to_underlying.h"
#include "build/build_config.h"
#include "components/viz/common/gpu/context_provider.h"
#include "ui/gfx/geometry/angle_conversions.h"
#include "ui/gfx/geometry/transform.h"
#if BUILDFLAG(IS_WIN)
#include "device/vr/windows/d3d11_texture_helper.h"
#endif
namespace {
// Number of frames to use for sliding averages for pose timings,
// as used for estimating prediction times.
constexpr unsigned kSlidingAverageSize = 5;
device::mojom::XRRenderInfoPtr GetRenderInfo(
const device::mojom::XRFrameData& frame_data) {
device::mojom::XRRenderInfoPtr result = device::mojom::XRRenderInfo::New();
result->frame_id = frame_data.frame_id;
result->mojo_from_viewer = frame_data.mojo_from_viewer.Clone();
for (size_t i = 0; i < frame_data.views.size(); i++) {
result->views.push_back(frame_data.views[i]->Clone());
}
return result;
}
} // namespace
namespace device {
mojom::XRFrameDataPtr XRDeviceAbstraction::GetNextFrameData() {
return nullptr;
}
void XRDeviceAbstraction::OnSessionStart() {}
void XRDeviceAbstraction::HandleDeviceLost() {}
bool XRDeviceAbstraction::HasSessionEnded() {
return false;
}
void XRDeviceAbstraction::OnLayerBoundsChanged(const gfx::Size& frame_size) {}
device::mojom::XREnvironmentBlendMode
XRDeviceAbstraction::GetEnvironmentBlendMode(
device::mojom::XRSessionMode session_mode) {
return device::mojom::XREnvironmentBlendMode::kOpaque;
}
device::mojom::XRInteractionMode XRDeviceAbstraction::GetInteractionMode(
device::mojom::XRSessionMode session_mode) {
return device::mojom::XRInteractionMode::kWorldSpace;
}
bool XRDeviceAbstraction::CanEnableAntiAliasing() const {
return true;
}
XRCompositorCommon::OutstandingFrame::OutstandingFrame() = default;
XRCompositorCommon::OutstandingFrame::~OutstandingFrame() = default;
XRCompositorCommon::XRCompositorCommon()
: XRThread("WindowsXRCompositor"),
main_thread_task_runner_(
base::SingleThreadTaskRunner::GetCurrentDefault()),
webxr_js_time_(kSlidingAverageSize),
webxr_gpu_time_(kSlidingAverageSize) {
DCHECK(main_thread_task_runner_);
}
XRCompositorCommon::~XRCompositorCommon() {
// Since we derive from base::Thread, all derived classes must call Stop() in
// their destructor so the thread gets torn down before any members that may
// be in use on the thread get torn down.
}
void XRCompositorCommon::ClearPendingFrame() {
DVLOG(3) << __func__;
// Notify the derived class first so it can clear its pending frame before
// potentially starting a new frame with delayed_get_frame_data_callback_.
ClearPendingFrameInternal();
pending_frame_.reset();
// Send frame data to outstanding requests.
if (delayed_get_frame_data_callback_ &&
(webxr_visible_ || on_webxr_submitted_)) {
// If WebXR is not visible, but the browser wants to know when it submits a
// frame, we allow the renderer to receive poses.
std::move(delayed_get_frame_data_callback_).Run();
}
}
bool XRCompositorCommon::IsUsingSharedImages() const {
return false;
}
void XRCompositorCommon::SubmitFrameMissing(int16_t frame_index,
const gpu::SyncToken& sync_token) {
DVLOG(3) << __func__ << " frame_index=" << frame_index;
TRACE_EVENT_INSTANT0("xr", "SubmitFrameMissing", TRACE_EVENT_SCOPE_THREAD);
if (pending_frame_) {
// WebXR for this frame is hidden.
pending_frame_->waiting_for_webxr_ = false;
}
webxr_has_pose_ = false;
#if (BUILDFLAG(IS_ANDROID))
// We haven't finished the rendering path on Android yet so are just calling
// this to ensure that the page stays un-stuck while we aren't actually
// rendering anything.
// TODO(alcooper): Clean this up.
pending_frame_->webxr_submitted_ = true;
#endif
MaybeCompositeAndSubmit();
}
void XRCompositorCommon::SubmitFrame(int16_t frame_index,
const gpu::MailboxHolder& mailbox,
base::TimeDelta time_waited) {
DVLOG(3) << __func__ << " frame_index=" << frame_index;
NOTREACHED();
}
void XRCompositorCommon::SubmitFrameDrawnIntoTexture(
int16_t frame_index,
const gpu::SyncToken& sync_token,
base::TimeDelta time_waited) {
DVLOG(3) << __func__ << " frame_index=" << frame_index;
NOTREACHED();
}
bool XRCompositorCommon::MarkFrameSubmitted(int16_t frame_index) {
DVLOG(3) << __func__;
webxr_has_pose_ = false;
// Tell the browser that WebXR has submitted a frame.
if (on_webxr_submitted_) {
std::move(on_webxr_submitted_).Run();
}
if (!pending_frame_ ||
pending_frame_->render_info_->frame_id != frame_index) {
// We weren't expecting a submitted frame. This can happen if WebXR was
// hidden by an overlay for some time.
if (submit_client_) {
submit_client_->OnSubmitFrameTransferred(false);
submit_client_->OnSubmitFrameRendered();
TRACE_EVENT1("xr", "SubmitFrameTransferred", "success", false);
}
return false;
}
pending_frame_->waiting_for_webxr_ = false;
pending_frame_->webxr_submitted_ = true;
pending_frame_->submit_frame_time_ = base::TimeTicks::Now();
return true;
}
#if BUILDFLAG(IS_WIN)
void XRCompositorCommon::SubmitFrameWithTextureHandle(
int16_t frame_index,
mojo::PlatformHandle texture_handle,
const gpu::SyncToken& sync_token) {
DVLOG(3) << __func__ << " frame_index=" << frame_index;
TRACE_EVENT1("xr", "SubmitFrameWithTextureHandle", "frameIndex", frame_index);
if (!MarkFrameSubmitted(frame_index)) {
return;
}
base::win::ScopedHandle scoped_handle = texture_handle.is_valid()
? texture_handle.TakeHandle()
: base::win::ScopedHandle();
texture_helper_.SetSourceTexture(std::move(scoped_handle), sync_token,
left_webxr_bounds_, right_webxr_bounds_);
// Regardless of success - try to composite what we have.
MaybeCompositeAndSubmit();
}
#endif
void XRCompositorCommon::CleanUp() {
DVLOG(1) << __func__;
submit_client_.reset();
webxr_has_pose_ = false;
presentation_receiver_.reset();
frame_data_receiver_.reset();
overlay_receiver_.reset();
StopRuntime();
}
void XRCompositorCommon::RequestOverlay(
mojo::PendingReceiver<mojom::ImmersiveOverlay> receiver) {
overlay_receiver_.reset();
overlay_receiver_.Bind(std::move(receiver));
// WebXR is visible and overlay hidden by default until the overlay overrides
// this.
SetOverlayAndWebXRVisibility(false, true);
}
void XRCompositorCommon::UpdateLayerBounds(int16_t frame_id,
const gfx::RectF& left_bounds,
const gfx::RectF& right_bounds,
const gfx::Size& source_size) {
// Bounds are updated instantly, rather than waiting for frame_id. This works
// since blink always passes the current frame_id when updating the bounds.
// Ignoring the frame_id keeps the logic simpler, so this can more easily
// merge with vr_shell_gl eventually.
left_webxr_bounds_ = left_bounds;
right_webxr_bounds_ = right_bounds;
// Swap top/bottom to account for differences between D3D and GL coordinates.
left_webxr_bounds_.set_y(
1 - (left_webxr_bounds_.y() + left_webxr_bounds_.height()));
right_webxr_bounds_.set_y(
1 - (right_webxr_bounds_.y() + right_webxr_bounds_.height()));
source_size_ = source_size;
OnLayerBoundsChanged(source_size_);
}
void XRCompositorCommon::RequestSession(
base::RepeatingCallback<void(mojom::XRVisibilityState)>
on_visibility_state_changed,
mojom::XRRuntimeSessionOptionsPtr options,
RequestSessionCallback callback) {
webxr_has_pose_ = false;
presentation_receiver_.reset();
frame_data_receiver_.reset();
EnableSupportedFeatures(options->required_features,
options->optional_features);
// Call the subclass's StartRuntime method. Upon completion, StartRuntime will
// call the callback passed to its first parameter, start_runtime_callback.
// XRCompositorCommon::StartRuntimeFinish. We setup BindOnce such that all of
// the parameters give to us here in XRCompositorCommon::RequestSession are
// passed through to StartRuntimeFinish so that it can finish the job.
StartRuntime(base::BindOnce(&XRCompositorCommon::StartRuntimeFinish,
base::Unretained(this),
std::move(on_visibility_state_changed),
std::move(options), std::move(callback)));
}
void XRCompositorCommon::StartRuntimeFinish(
base::RepeatingCallback<void(mojom::XRVisibilityState)>
on_visibility_state_changed,
mojom::XRRuntimeSessionOptionsPtr options,
RequestSessionCallback callback,
bool success) {
if (!success) {
TRACE_EVENT_INSTANT0("xr", "Failed to start runtime",
TRACE_EVENT_SCOPE_THREAD);
main_thread_task_runner_->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), false, nullptr));
return;
}
on_visibility_state_changed_ = std::move(on_visibility_state_changed);
// Queue up a notification to the requester of the current visibility state,
// so that it can be initialized to the right value.
if (on_visibility_state_changed_) {
main_thread_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(on_visibility_state_changed_, visibility_state_));
}
device::mojom::XRPresentationTransportOptionsPtr transport_options =
device::mojom::XRPresentationTransportOptions::New();
if (IsUsingSharedImages()) {
transport_options->transport_method =
device::mojom::XRPresentationTransportMethod::DRAW_INTO_TEXTURE_MAILBOX;
} else {
#if BUILDFLAG(IS_WIN)
transport_options->transport_method =
device::mojom::XRPresentationTransportMethod::SUBMIT_AS_TEXTURE_HANDLE;
#else
transport_options->transport_method =
device::mojom::XRPresentationTransportMethod::SUBMIT_AS_MAILBOX_HOLDER;
#endif
}
// Only set boolean options that we need. Default is false, and we should be
// able to safely ignore ones that our implementation doesn't care about.
transport_options->wait_for_transfer_notification = true;
OnSessionStart();
auto submit_frame_sink = device::mojom::XRPresentationConnection::New();
submit_frame_sink->provider =
presentation_receiver_.BindNewPipeAndPassRemote();
submit_frame_sink->client_receiver =
submit_client_.BindNewPipeAndPassReceiver();
submit_frame_sink->transport_options = std::move(transport_options);
auto session = device::mojom::XRSession::New();
session->data_provider = frame_data_receiver_.BindNewPipeAndPassRemote();
session->submit_frame_sink = std::move(submit_frame_sink);
session->enabled_features.insert(session->enabled_features.end(),
enabled_features_.begin(),
enabled_features_.end());
session->device_config = device::mojom::XRSessionDeviceConfig::New();
session->device_config->enable_anti_aliasing = CanEnableAntiAliasing();
session->device_config->views = GetDefaultViews();
session->enviroment_blend_mode = GetEnvironmentBlendMode(options->mode);
session->interaction_mode = GetInteractionMode(options->mode);
main_thread_task_runner_->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), true, std::move(session)));
is_presenting_ = true;
#if BUILDFLAG(IS_WIN)
texture_helper_.SetSourceAndOverlayVisible(webxr_visible_, overlay_visible_);
#endif
}
void XRCompositorCommon::ExitPresent(ExitXrPresentReason reason) {
DVLOG(1) << __func__ << " reason=" << base::to_underlying(reason);
TRACE_EVENT_INSTANT1("xr", "ExitPresent", TRACE_EVENT_SCOPE_THREAD, "reason",
base::to_underlying(reason));
if (!is_presenting_)
return;
is_presenting_ = false;
webxr_has_pose_ = false;
presentation_receiver_.reset();
frame_data_receiver_.reset();
submit_client_.reset();
pending_frame_.reset();
delayed_get_frame_data_callback_.Reset();
// Reset webxr_visible_ for subsequent presentations.
webxr_visible_ = true;
// Kill outstanding overlays:
overlay_visible_ = false;
overlay_receiver_.reset();
#if BUILDFLAG(IS_WIN)
texture_helper_.SetSourceAndOverlayVisible(false, false);
#endif
// Don't call StopRuntime until this thread has finished the rest of the work.
// This is to prevent the OpenXrApiWrapper from being deleted before its
// cleanup work has finished.
task_runner()->PostTask(FROM_HERE,
base::BindOnce(&XRCompositorCommon::StopRuntime,
weak_ptr_factory_.GetWeakPtr()));
}
void XRCompositorCommon::SetVisibilityState(
mojom::XRVisibilityState visibility_state) {
if (visibility_state_ != visibility_state) {
visibility_state_ = visibility_state;
if (on_visibility_state_changed_) {
main_thread_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(on_visibility_state_changed_, visibility_state));
}
}
}
const mojom::VRStageParametersPtr&
XRCompositorCommon::GetCurrentStageParameters() const {
return current_stage_parameters_;
}
void XRCompositorCommon::SetStageParameters(
mojom::VRStageParametersPtr stage_parameters) {
// If the stage parameters are identical no need to update them.
if ((!current_stage_parameters_ && !stage_parameters) ||
(current_stage_parameters_ && stage_parameters &&
current_stage_parameters_.Equals(stage_parameters))) {
return;
}
// If they have changed, increment the ID and save the new parameters.
stage_parameters_id_++;
current_stage_parameters_ = std::move(stage_parameters);
}
void XRCompositorCommon::Init() {}
void XRCompositorCommon::StartPendingFrame() {
DVLOG(3) << __func__ << " pending_frame_=" << pending_frame_.has_value();
if (!pending_frame_) {
pending_frame_.emplace();
pending_frame_->waiting_for_webxr_ = webxr_visible_;
pending_frame_->waiting_for_overlay_ = overlay_visible_;
pending_frame_->frame_data_ = GetNextFrameData();
// GetNextFrameData() should never return null:
DCHECK(pending_frame_->frame_data_);
pending_frame_->render_info_ = GetRenderInfo(*pending_frame_->frame_data_);
}
}
void XRCompositorCommon::GetFrameData(
mojom::XRFrameDataRequestOptionsPtr options,
mojom::XRFrameDataProvider::GetFrameDataCallback callback) {
TRACE_EVENT0("xr", "GetFrameData");
if (HasSessionEnded()) {
ExitPresent(ExitXrPresentReason::kGetFrameAfterSessionEnded);
return;
}
// HasSessionEnded() may do some work that alters the state of
// `is_presenting_`, in which case we'll get the ExitPresent log to know that
// we've ignored this request; but otherwise, we should log the rest of the
// state after that.
DVLOG(3) << __func__ << " is_presenting_=" << is_presenting_
<< " webxr_visible=" << webxr_visible_
<< " on_webxr_submitted_=" << !!on_webxr_submitted_
<< " webxr_has_pose_=" << webxr_has_pose_;
if (!is_presenting_) {
return;
}
// If we've already given out a pose for the current frame, or aren't visible,
// delay giving out a pose until the next frame we are visible.
// However, if we aren't visible and the browser is waiting to learn that
// WebXR has submitted a frame, we can give out a pose as though we are
// visible.
if ((!webxr_visible_ && !on_webxr_submitted_) || webxr_has_pose_) {
// There should only be one outstanding GetFrameData call at a time. We
// shouldn't get new ones until this resolves or presentation ends/restarts.
if (delayed_get_frame_data_callback_) {
frame_data_receiver_.ReportBadMessage(
"Multiple outstanding GetFrameData calls");
return;
}
delayed_get_frame_data_callback_ = base::BindOnce(
&XRCompositorCommon::GetFrameData, base::Unretained(this),
std::move(options), std::move(callback));
return;
}
StartPendingFrame();
webxr_has_pose_ = true;
pending_frame_->webxr_has_pose_ = true;
pending_frame_->sent_frame_data_time_ = base::TimeTicks::Now();
// TODO(https://crbug.com/1218135): The lack of frame_data_ here indicates
// that we probably should have deferred this call, but it matches the
// behavior from before the stage parameters were updated in this function and
// avoids a crash. Likely the deferral above should check if we're awaiting
// either the webxr or overlay submit.
if (pending_frame_->frame_data_) {
// If the stage parameters have been updated since the last frame that was
// sent, send the updated values.
pending_frame_->frame_data_->stage_parameters_id = stage_parameters_id_;
if (options->stage_parameters_id != stage_parameters_id_) {
pending_frame_->frame_data_->stage_parameters =
current_stage_parameters_.Clone();
}
} else {
TRACE_EVENT0("xr", "GetFrameData Missing FrameData");
}
// Yield here to let the event queue process pending mojo messages,
// specifically the next gamepad callback request that's likely to
// have been sent during WaitGetPoses.
task_runner()->PostTask(
FROM_HERE, base::BindOnce(&XRCompositorCommon::SendFrameData,
base::Unretained(this), std::move(callback),
std::move(pending_frame_->frame_data_)));
next_frame_id_ += 1;
if (next_frame_id_ < 0) {
next_frame_id_ = 0;
}
}
void XRCompositorCommon::SendFrameData(
XRFrameDataProvider::GetFrameDataCallback callback,
mojom::XRFrameDataPtr frame_data) {
DVLOG(3) << __func__;
TRACE_EVENT0("xr", "SendFrameData");
// This method represents a call from the renderer process. If our visibility
// state is hidden, we should avoid handing "sensitive" information, like the
// pose back up to the renderer. Note that this check is done here as other
// methods (RequestNextOverlayPose) represent a call from the browser process,
// which should receive the pose.
bool is_visible =
(visibility_state_ != device::mojom::XRVisibilityState::HIDDEN);
// We have posted a message to allow other calls to get through, and now state
// may have changed. WebXR may not be presenting any more, or may be hidden.
std::move(callback).Run(is_presenting_ && is_visible &&
(webxr_visible_ || on_webxr_submitted_)
? std::move(frame_data)
: mojom::XRFrameData::New());
}
void XRCompositorCommon::GetEnvironmentIntegrationProvider(
mojo::PendingAssociatedReceiver<
device::mojom::XREnvironmentIntegrationProvider> environment_provider) {
// Environment integration is not supported. This call should not
// be made on this device.
mojo::ReportBadMessage("Environment integration is not supported.");
}
void XRCompositorCommon::SubmitOverlayTexture(
int16_t frame_id,
mojo::PlatformHandle texture_handle,
const gpu::SyncToken& sync_token,
const gfx::RectF& left_bounds,
const gfx::RectF& right_bounds,
SubmitOverlayTextureCallback overlay_submit_callback) {
TRACE_EVENT_INSTANT0("xr", "SubmitOverlay", TRACE_EVENT_SCOPE_THREAD);
DCHECK(overlay_visible_);
overlay_submit_callback_ = std::move(overlay_submit_callback);
if (!pending_frame_) {
// We may stop presenting while there is a pending SubmitOverlayTexture
// outstanding. If we get an overlay submit we weren't expecting, just
// ignore it.
DCHECK(!is_presenting_);
std::move(overlay_submit_callback_).Run(false);
return;
}
pending_frame_->waiting_for_overlay_ = false;
#if BUILDFLAG(IS_WIN)
texture_helper_.SetOverlayTexture(texture_handle.TakeHandle(), sync_token,
left_bounds, right_bounds);
pending_frame_->overlay_submitted_ = true;
// Regardless of success - try to composite what we have.
MaybeCompositeAndSubmit();
#endif
}
void XRCompositorCommon::RequestNextOverlayPose(
RequestNextOverlayPoseCallback callback) {
DVLOG(3) << __func__;
// We will only request poses while the overlay is visible.
DCHECK(overlay_visible_);
TRACE_EVENT_INSTANT0("xr", "RequestOverlayPose", TRACE_EVENT_SCOPE_THREAD);
// Ensure we have a pending frame.
StartPendingFrame();
pending_frame_->overlay_has_pose_ = true;
std::move(callback).Run(pending_frame_->render_info_->Clone());
}
void XRCompositorCommon::SetOverlayAndWebXRVisibility(bool overlay_visible,
bool webxr_visible) {
DVLOG(1) << __func__ << " overlay_visible=" << overlay_visible
<< " webxr_visible=" << webxr_visible;
TRACE_EVENT_INSTANT2("xr", "SetOverlayAndWebXRVisibility",
TRACE_EVENT_SCOPE_THREAD, "overlay", overlay_visible,
"webxr", webxr_visible);
// Update state.
webxr_visible_ = webxr_visible;
overlay_visible_ = overlay_visible;
if (pending_frame_) {
pending_frame_->waiting_for_webxr_ =
pending_frame_->waiting_for_webxr_ && webxr_visible;
pending_frame_->waiting_for_overlay_ =
pending_frame_->waiting_for_overlay_ && overlay_visible;
}
// Update texture helper.
#if BUILDFLAG(IS_WIN)
texture_helper_.SetSourceAndOverlayVisible(webxr_visible, overlay_visible);
#endif
// Maybe composite and submit if we have a pending that is now valid to
// submit.
MaybeCompositeAndSubmit();
}
void XRCompositorCommon::RequestNotificationOnWebXrSubmitted(
RequestNotificationOnWebXrSubmittedCallback callback) {
on_webxr_submitted_ = std::move(callback);
}
void XRCompositorCommon::MaybeCompositeAndSubmit() {
DVLOG(3) << __func__;
if (!pending_frame_) {
// There is no outstanding frame, nor frame to composite, but there may be
// pending GetFrameData calls, so ClearPendingFrame() to respond to them.
ClearPendingFrame();
return;
}
// Check if we have obtained all layers (overlay and webxr) that we need.
if (pending_frame_->waiting_for_webxr_ ||
pending_frame_->waiting_for_overlay_) {
DVLOG(3) << __func__ << "Waiting for additional layers, waiting_for_webxr_="
<< pending_frame_->waiting_for_webxr_
<< " waiting_for_overlay=" << pending_frame_->waiting_for_overlay_;
// Haven't received submits from all layers.
return;
}
// TODO(https://crbug.com/1454950): Unify OpenXr Rendering paths.
#if BUILDFLAG(IS_WIN)
bool copy_successful = false;
bool has_webxr_content = pending_frame_->webxr_submitted_ && webxr_visible_;
bool has_overlay_content =
pending_frame_->overlay_submitted_ && overlay_visible_;
bool can_submit = has_webxr_content || has_overlay_content;
// Tell texture helper to composite, then grab the output texture, and submit.
// If we submitted, set up the next frame, and send outstanding pose requests.
if (can_submit) {
copy_successful = texture_helper_.UpdateBackbufferSizes() &&
texture_helper_.CompositeToBackBuffer();
} else {
texture_helper_.CleanupNoSubmit();
}
#elif BUILDFLAG(IS_ANDROID)
bool copy_successful = true;
#endif
// A copy can only be succesful if we actually tried to submit.
if (copy_successful) {
pending_frame_->frame_ready_time_ = base::TimeTicks::Now();
if (!SubmitCompositedFrame()) {
ExitPresent(ExitXrPresentReason::kSubmitFrameFailed);
// ExitPresent() clears pending_frame_, so return here to avoid
// accessing it below.
return;
}
}
if (pending_frame_->webxr_submitted_ && copy_successful) {
// We've submitted a frame.
webxr_js_time_.AddSample(pending_frame_->submit_frame_time_ -
pending_frame_->sent_frame_data_time_);
webxr_gpu_time_.AddSample(pending_frame_->frame_ready_time_ -
pending_frame_->submit_frame_time_);
TRACE_EVENT_INSTANT2(
"gpu", "WebXR frame time (ms)", TRACE_EVENT_SCOPE_THREAD, "javascript",
webxr_js_time_.GetAverage().InMillisecondsF(), "rendering",
webxr_gpu_time_.GetAverage().InMillisecondsF());
fps_meter_.AddFrame(base::TimeTicks::Now());
TRACE_COUNTER1("gpu", "WebXR FPS", fps_meter_.GetFPS());
}
if (pending_frame_->webxr_submitted_ && submit_client_) {
// Tell WebVR that we are done with the texture (if we got a texture)
submit_client_->OnSubmitFrameTransferred(copy_successful);
submit_client_->OnSubmitFrameRendered();
TRACE_EVENT1("xr", "SubmitFrameTransferred", "success", copy_successful);
}
if (pending_frame_->overlay_submitted_ && overlay_submit_callback_) {
// Tell the browser/overlay that we are done with its texture so it can be
// reused.
std::move(overlay_submit_callback_).Run(copy_successful);
}
ClearPendingFrame();
}
} // namespace device