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 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/xr/xr_frame_provider.h"
#include <utility>
#include "build/build_config.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/dom/frame_request_callback_collection.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/core/imagebitmap/image_bitmap.h"
#include "third_party/blink/renderer/core/loader/document_loader.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_device.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_texture.h"
#include "third_party/blink/renderer/modules/xr/xr_gpu_binding.h"
#include "third_party/blink/renderer/modules/xr/xr_gpu_projection_layer.h"
#include "third_party/blink/renderer/modules/xr/xr_gpu_swap_chain.h"
#include "third_party/blink/renderer/modules/xr/xr_graphics_binding.h"
#include "third_party/blink/renderer/modules/xr/xr_projection_layer.h"
#include "third_party/blink/renderer/modules/xr/xr_session.h"
#include "third_party/blink/renderer/modules/xr/xr_system.h"
#include "third_party/blink/renderer/modules/xr/xr_viewport.h"
#include "third_party/blink/renderer/modules/xr/xr_webgl_layer.h"
#include "third_party/blink/renderer/modules/xr/xr_webgl_layer_client.h"
#include "third_party/blink/renderer/modules/xr/xr_webgl_projection_layer.h"
#include "third_party/blink/renderer/platform/graphics/gpu/xr_frame_transport.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "ui/display/display.h"
#include "ui/gfx/geometry/transform.h"
namespace blink {
namespace {
class XRFrameProviderRequestCallback : public FrameCallback {
public:
explicit XRFrameProviderRequestCallback(XRFrameProvider* frame_provider)
: frame_provider_(frame_provider) {}
~XRFrameProviderRequestCallback() override = default;
void Invoke(double high_res_time_ms) override {
frame_provider_->OnNonImmersiveVSync(high_res_time_ms);
}
void Trace(Visitor* visitor) const override {
visitor->Trace(frame_provider_);
FrameCallback::Trace(visitor);
}
Member<XRFrameProvider> frame_provider_;
};
gfx::RectF NormalizeViewport(gfx::Rect viewport,
uint32_t width,
uint32_t height) {
return gfx::RectF(
static_cast<float>(viewport.x()) / width,
static_cast<float>(height - (viewport.y() + viewport.height())) / height,
static_cast<float>(viewport.width()) / width,
static_cast<float>(viewport.height()) / height);
}
} // namespace
XRFrameProvider::XRFrameProvider(XRSystem* xr)
: xr_(xr),
frame_transport_(MakeGarbageCollected<XRFrameTransport>(
xr->GetExecutionContext(),
xr->GetExecutionContext()->GetTaskRunner(
TaskType::kMiscPlatformAPI))),
immersive_data_provider_(xr->GetExecutionContext()),
immersive_presentation_provider_(xr->GetExecutionContext()),
last_has_focus_(xr->IsFrameFocused()) {}
void XRFrameProvider::AddImmersiveSessionObserver(
ImmersiveSessionObserver* observer) {
immersive_observers_.insert(observer);
}
void XRFrameProvider::OnSessionStarted(
XRSession* session,
device::mojom::blink::XRSessionPtr session_ptr) {
DCHECK(session);
if (session->immersive()) {
// Ensure we can only have one immersive session at a time.
DCHECK(!immersive_session_);
DCHECK(session_ptr->data_provider);
DCHECK(session_ptr->submit_frame_sink);
immersive_session_ = session;
for (auto& observer : immersive_observers_) {
observer->OnImmersiveSessionStart();
}
immersive_data_provider_.Bind(
std::move(session_ptr->data_provider),
xr_->GetExecutionContext()->GetTaskRunner(TaskType::kMiscPlatformAPI));
immersive_data_provider_.set_disconnect_handler(
WTF::BindOnce(&XRFrameProvider::OnProviderConnectionError,
WrapWeakPersistent(this), WrapWeakPersistent(session)));
immersive_presentation_provider_.Bind(
std::move(session_ptr->submit_frame_sink->provider),
xr_->GetExecutionContext()->GetTaskRunner(TaskType::kMiscPlatformAPI));
immersive_presentation_provider_.set_disconnect_handler(
WTF::BindOnce(&XRFrameProvider::OnProviderConnectionError,
WrapWeakPersistent(this), WrapWeakPersistent(session)));
frame_transport_->RegisterFrameRenderedCallback(WTF::BindRepeating(
&XRFrameProvider::OnRenderComplete, WrapWeakPersistent(this)));
frame_transport_->BindSubmitFrameClient(
std::move(session_ptr->submit_frame_sink->client_receiver));
frame_transport_->SetTransportOptions(
std::move(session_ptr->submit_frame_sink->transport_options));
frame_transport_->PresentChange();
last_frame_statistics_sent_time_ = base::TimeTicks::Now();
repeating_timer_.Start(FROM_HERE, base::Seconds(1),
WTF::BindRepeating(&XRFrameProvider::SendFrameData,
WrapWeakPersistent(this)));
} else {
// If a non-immersive session doesn't have a data provider, we don't
// need to store a reference to it.
if (!session_ptr->data_provider) {
return;
}
HeapMojoRemote<device::mojom::blink::XRFrameDataProvider> data_provider(
xr_->GetExecutionContext());
data_provider.Bind(
std::move(session_ptr->data_provider),
xr_->GetExecutionContext()->GetTaskRunner(TaskType::kMiscPlatformAPI));
data_provider.set_disconnect_handler(
WTF::BindOnce(&XRFrameProvider::OnProviderConnectionError,
WrapWeakPersistent(this), WrapWeakPersistent(session)));
non_immersive_data_providers_.insert(
session, WrapDisallowNew(std::move(data_provider)));
}
}
void XRFrameProvider::OnFocusChanged() {
bool focus = xr_->IsFrameFocused();
// If we are gaining focus, schedule a frame for magic window. This accounts
// for skipping RAFs in ProcessScheduledFrame. Only do this when there are
// magic window sessions but no immersive session. Note that immersive
// sessions don't stop scheduling RAFs when focus is lost, so there is no need
// to schedule immersive frames when focus is acquired.
if (focus && !last_has_focus_ && requesting_sessions_.size() > 0 &&
!immersive_session_) {
ScheduleNonImmersiveFrame(nullptr);
}
last_has_focus_ = focus;
}
// Ends the immersive session when the presentation or immersive data provider
// got disconnected.
void XRFrameProvider::OnProviderConnectionError(XRSession* session) {
DVLOG(2) << __func__;
// This will call into |OnSessionEnded|, unless it has already ended.
session->ForceEnd(XRSession::ShutdownPolicy::kImmediate);
}
void XRFrameProvider::OnSessionEnded(XRSession* session) {
DVLOG(2) << __func__;
if (session->immersive()) {
DCHECK(session == immersive_session_);
immersive_session_ = nullptr;
pending_immersive_vsync_ = false;
frame_id_ = -1;
immersive_presentation_provider_.reset();
immersive_data_provider_.reset();
first_immersive_frame_time_ = std::nullopt;
first_immersive_frame_time_delta_ = std::nullopt;
frame_transport_ = MakeGarbageCollected<XRFrameTransport>(
session->GetExecutionContext(),
session->GetExecutionContext()->GetTaskRunner(
TaskType::kMiscPlatformAPI));
repeating_timer_.Stop();
for (auto& observer : immersive_observers_) {
observer->OnImmersiveSessionEnd();
}
} else {
non_immersive_data_providers_.erase(session);
requesting_sessions_.erase(session);
}
}
void XRFrameProvider::RestartNonImmersiveFrameLoop() {
// When we no longer have an active immersive session schedule all the
// outstanding frames that were requested while the immersive session was
// active.
if (immersive_session_ || requesting_sessions_.size() == 0)
return;
for (auto& session : requesting_sessions_) {
RequestNonImmersiveFrameData(session.key.Get());
}
ScheduleNonImmersiveFrame(nullptr);
}
// Schedule a session to be notified when the next XR frame is available.
void XRFrameProvider::RequestFrame(XRSession* session) {
DVLOG(3) << __func__;
TRACE_EVENT0("gpu", "RequestFrame");
DCHECK(session);
auto options = device::mojom::blink::XRFrameDataRequestOptions::New();
options->include_lighting_estimation_data = session->LightEstimationEnabled();
options->stage_parameters_id = session->StageParametersId();
options->depth_active = session->IsDepthActive();
// Immersive frame logic.
if (session->immersive()) {
ScheduleImmersiveFrame(std::move(options));
return;
}
// Non-immersive frame logic.
// Duplicate frame requests are treated as a no-op.
if (requesting_sessions_.Contains(session)) {
DVLOG(2) << __func__ << ": session requested duplicate frame";
return;
}
requesting_sessions_.insert(session, nullptr);
// If there's an active immersive session save the request but suppress
// processing it until the immersive session is no longer active.
if (immersive_session_) {
return;
}
RequestNonImmersiveFrameData(session);
ScheduleNonImmersiveFrame(std::move(options));
}
void XRFrameProvider::ScheduleImmersiveFrame(
device::mojom::blink::XRFrameDataRequestOptionsPtr options) {
TRACE_EVENT0("gpu", "ScheduleImmersiveFrame");
if (pending_immersive_vsync_)
return;
pending_immersive_vsync_ = true;
frame_data_time_.StartTimer();
// `this` is an okay TRACE ID here, since we are only allowed one immersive
// session at a time.
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("xr", "RequestImmersiveFrame",
TRACE_ID_LOCAL(this));
immersive_data_provider_->GetFrameData(
std::move(options), WTF::BindOnce(&XRFrameProvider::OnImmersiveFrameData,
WrapWeakPersistent(this)));
}
void XRFrameProvider::ScheduleNonImmersiveFrame(
device::mojom::blink::XRFrameDataRequestOptionsPtr options) {
DVLOG(3) << __func__;
TRACE_EVENT0("gpu", "ScheduleNonImmersiveFrame");
DCHECK(!immersive_session_)
<< "Scheduling should be done via the exclusive session if present.";
if (pending_non_immersive_vsync_) {
DVLOG(3) << __func__ << ": non immersive vsync already pending";
return;
}
LocalDOMWindow* window = xr_->DomWindow();
if (!window)
return;
pending_non_immersive_vsync_ = true;
// Calls |OnNonImmersiveVSync|
window->document()->RequestAnimationFrame(
MakeGarbageCollected<XRFrameProviderRequestCallback>(this));
}
void XRFrameProvider::OnImmersiveFrameData(
device::mojom::blink::XRFrameDataPtr data) {
frame_data_time_.StopTimer();
TRACE_EVENT_NESTABLE_ASYNC_END0("xr", "RequestImmersiveFrame",
TRACE_ID_LOCAL(this));
TRACE_EVENT0("gpu", "OnImmersiveFrameData");
if (data.is_null()) {
DVLOG(2) << __func__ << ": no data, current frame_id=" << frame_id_;
} else {
DVLOG(2) << __func__
<< ": have data, frame_id=" << data->render_info->frame_id;
}
// We may have lost the immersive session since the last VSync request.
if (!immersive_session_) {
DVLOG(1) << __func__ << ": immersive session lost";
return;
}
if (!data) {
DVLOG(3) << __func__ << ": frame data not received, re-requesting frame";
// We have not received any frame data from the device. We could try to run
// an XR animation frame [1], but that may cause issues with APIs that
// receive state updates via XRFrameData (e.g. anchors, planes) for
// maintaining their current state - they would behave as if all entities
// lost tracking and got removed on the device. Let's behave as if we have
// not received anything from the device and request a new frame.
//
// [1] https://immersive-web.github.io/webxr/#xr-animation-frame)
pending_immersive_vsync_ = false;
RequestFrame(immersive_session_);
return;
}
LocalDOMWindow* window = xr_->DomWindow();
if (!window) {
DVLOG(3) << __func__ << ": unable to get local DOM window!";
return;
}
// Note: The |high_res_now_ms| is computed based on frame time returned from
// immersive frame and eventually passed in to requestAnimationFrame callback
// as `DOMHighResTimeStamp time`. This means that in case of immersive frames,
// the `now` is the same as `frameTime` in XR animation frame algorithm [1].
//
// [1] https://immersive-web.github.io/webxr/#xr-animation-frame
double high_res_now_ms = UpdateImmersiveFrameTime(window, *data);
frame_id_ = data->render_info->frame_id;
if (data->buffer_shared_image.has_value()) {
buffer_shared_image_ = gpu::ClientSharedImage::ImportUnowned(
std::move(data->buffer_shared_image.value()));
buffer_sync_token_ = data->buffer_sync_token.value();
}
if (data->camera_image_buffer_shared_image.has_value()) {
camera_image_shared_image_ = gpu::ClientSharedImage::ImportUnowned(
std::move(data->camera_image_buffer_shared_image.value()));
camera_image_sync_token_ = data->camera_image_buffer_sync_token.value();
}
pending_immersive_vsync_ = false;
for (auto& observer : immersive_observers_) {
observer->OnImmersiveFrame();
}
// Post a task to handle scheduled animations after the current
// execution context finishes, so that we yield to non-mojo tasks in
// between frames. Executing mojo tasks back to back within the same
// execution context caused extreme input delay due to processing
// multiple frames without yielding, see crbug.com/701444.
//
// Used kInternalMedia since 1) this is not spec-ed and 2) this is media
// related then tasks should not be throttled or frozen in background tabs.
window->GetTaskRunner(blink::TaskType::kInternalMedia)
->PostTask(
FROM_HERE,
WTF::BindOnce(&XRFrameProvider::ProcessScheduledFrame,
WrapWeakPersistent(this), std::move(data),
high_res_now_ms, ScheduledFrameType::kImmersive));
}
void XRFrameProvider::OnNonImmersiveVSync(double high_res_now_ms) {
TRACE_EVENT0("gpu", "OnNonImmersiveVSync");
DVLOG(2) << __func__;
pending_non_immersive_vsync_ = false;
// Suppress non-immersive vsyncs when there's an immersive session active.
if (immersive_session_)
return;
LocalDOMWindow* window = xr_->DomWindow();
if (!window)
return;
window->GetTaskRunner(blink::TaskType::kInternalMedia)
->PostTask(FROM_HERE,
WTF::BindOnce(&XRFrameProvider::ProcessScheduledFrame,
WrapWeakPersistent(this), nullptr,
high_res_now_ms, ScheduledFrameType::kInline));
}
void XRFrameProvider::OnNonImmersiveFrameData(
XRSession* session,
device::mojom::blink::XRFrameDataPtr frame_data) {
TRACE_EVENT0("gpu", "OnNonImmersiveFrameData");
DVLOG(2) << __func__;
// TODO(https://crbug.com/837834): add unit tests for this code path.
LocalDOMWindow* window = xr_->DomWindow();
if (!window)
return;
// Look up the request for this session. The session may have ended between
// when the request was sent and this callback, so skip it in that case.
auto request = requesting_sessions_.find(session);
if (request == requesting_sessions_.end()) {
DVLOG(3) << __func__
<< ": request corresponding to received frame data not found";
if (!session->ended()) {
DVLOG(2) << __func__
<< ": the session's frame data provider missed the vsync";
}
return;
}
if (frame_data) {
DVLOG(3) << __func__ << ": frame data for session stored";
request->value = std::move(frame_data);
} else {
// Unexpectedly didn't get frame data, and we don't have a timestamp.
// Try to request a regular animation frame to avoid getting stuck.
DVLOG(1) << __func__ << ": NO FRAME DATA!";
request->value = nullptr;
window->document()->RequestAnimationFrame(
MakeGarbageCollected<XRFrameProviderRequestCallback>(this));
}
}
void XRFrameProvider::RequestNonImmersiveFrameData(XRSession* session) {
DVLOG(3) << __func__;
DCHECK(session);
DCHECK(!session->immersive());
DCHECK(!immersive_session_);
// The requesting_sessions_ entry for this session must have already
// been created in |RequestFrame|.
auto request = requesting_sessions_.find(session);
CHECK(request != requesting_sessions_.end());
auto provider = non_immersive_data_providers_.find(session);
if (provider == non_immersive_data_providers_.end()) {
request->value = nullptr;
} else {
auto& data_provider = provider->value->Value();
auto options = device::mojom::blink::XRFrameDataRequestOptions::New();
options->include_lighting_estimation_data =
session->LightEstimationEnabled();
options->stage_parameters_id = session->StageParametersId();
data_provider->GetFrameData(
std::move(options),
WTF::BindOnce(&XRFrameProvider::OnNonImmersiveFrameData,
WrapWeakPersistent(this), WrapWeakPersistent(session)));
}
}
void XRFrameProvider::ProcessScheduledFrame(
device::mojom::blink::XRFrameDataPtr frame_data,
double high_res_now_ms,
ScheduledFrameType frame_type) {
DVLOG(2) << __func__ << ": frame_id_=" << frame_id_
<< ", high_res_now_ms=" << high_res_now_ms;
TRACE_EVENT2("gpu", "XRFrameProvider::ProcessScheduledFrame", "frame",
frame_id_, "timestamp", high_res_now_ms);
LocalDOMWindow* window = xr_->DomWindow();
if (!window)
return;
if (!xr_->IsFrameFocused() && !immersive_session_) {
return; // Not currently focused, so we won't expose poses (except to
// immersive sessions).
}
if (immersive_session_) {
if (frame_type != ScheduledFrameType::kImmersive) {
DVLOG(1)
<< __func__
<< " Attempted to process non-immersive scheduled frame as immersive";
return;
}
// Check if immersive session is still valid, it may have ended and be
// waiting for shutdown acknowledgement.
if (immersive_session_->ended()) {
return;
}
bool emulated_position = false;
if (frame_data && frame_data->render_info->mojo_from_viewer) {
DVLOG(3) << __func__ << ": pose available, emulated_position="
<< frame_data->render_info->mojo_from_viewer->emulated_position;
emulated_position =
frame_data->render_info->mojo_from_viewer->emulated_position;
} else {
DVLOG(2) << __func__ << ": emulating immersive frame position";
emulated_position = true;
}
immersive_session_->UpdatePresentationFrameState(
high_res_now_ms, std::move(frame_data), frame_id_, emulated_position);
// Check if immersive session is still set as any events dispatched to the
// page may have allowed a ForceEndSession to be triggered.
if (!immersive_session_ || immersive_session_->ended()) {
return;
}
#if DCHECK_IS_ON()
// Sanity check: if drawing into a shared buffer, the optional shared image
// must be present. Exception is the first immersive frame after a
// transition where the frame ID wasn't set yet. In that case, drawing can
// proceed, but the result will be discarded in SubmitWebGLLayer().
if (frame_transport_->DrawingIntoSharedBuffer() && frame_id_ >= 0) {
DCHECK(buffer_shared_image_);
}
#endif
// Run immersive_session_->OnFrame() in a posted task to ensure that
// createAnchor promises get a chance to run - the presentation frame state
// is already updated.
window->GetTaskRunner(blink::TaskType::kInternalMedia)
->PostTask(FROM_HERE,
WTF::BindOnce(&XRSession::OnFrame,
WrapWeakPersistent(immersive_session_.Get()),
high_res_now_ms, buffer_shared_image_,
buffer_sync_token_, camera_image_shared_image_,
camera_image_sync_token_));
} else {
// In the process of fulfilling the frame requests for each session they are
// extremely likely to request another frame. Work off of a separate list
// from the requests to prevent infinite loops.
decltype(requesting_sessions_) processing_sessions;
DVLOG(3) << __func__ << ": clearing requesting_sessions_";
swap(requesting_sessions_, processing_sessions);
// Inform sessions with a pending request of the new frame
for (auto& request : processing_sessions) {
XRSession* session = request.key.Get();
// If the session was terminated between requesting and now, we shouldn't
// process anything further.
if (session->ended()) {
continue;
}
session->UpdatePresentationFrameState(
high_res_now_ms, std::move(request.value), frame_id_,
true /* Non-immersive positions are always emulated */);
// If any events dispatched to the page caused this session to end, we
// should stop processing.
if (session->ended()) {
continue;
}
// Run session->OnFrame() in a posted task to ensure that createAnchor
// promises get a chance to run - the presentation frame state is already
// updated.
// Note that rather than call session->OnFrame() directly, we dispatch to
// a helper method who can determine if the state requirements are still
// met that would allow the frame to be served.
window->GetTaskRunner(blink::TaskType::kInternalMedia)
->PostTask(
FROM_HERE,
WTF::BindOnce(&XRFrameProvider::OnPreDispatchInlineFrame,
WrapWeakPersistent(this),
WrapWeakPersistent(session), high_res_now_ms));
}
}
}
void XRFrameProvider::OnPreDispatchInlineFrame(XRSession* session,
double timestamp) {
// Do nothing if the session was cleaned up or ended before we were schedueld.
if (!session || session->ended())
return;
// If we have an immersive session, we shouldn't serve frames to the inline
// session; however, we need to ensure that we don't stall out its frame loop,
// so add a new frame request to get served after the immersive session exits.
if (immersive_session_) {
RequestFrame(session);
return;
}
// If we still have the session and don't have an immersive session, then we
// should serve the frame.
session->OnFrame(timestamp, nullptr, gpu::SyncToken(), nullptr,
gpu::SyncToken());
}
double XRFrameProvider::UpdateImmersiveFrameTime(
LocalDOMWindow* window,
const device::mojom::blink::XRFrameData& data) {
DVLOG(3) << __func__;
// `data.time_delta` is in unspecified base. Because of that, we capture the
// `time_delta` of the first frame we see (this gives us device_t_0), along
// with the time we saw it (renderer_t_0). That allows us to translate from
// device time to renderer time, so when we see frame N, we perform:
// renderer_t_N = renderer_t_0 + (device_t_N - device_t_0)
if (!first_immersive_frame_time_) {
DCHECK(!first_immersive_frame_time_delta_);
// This is the first time we got a frame data from an immersive session.
// Let's capture device_t_0 and renderer_t_0.
first_immersive_frame_time_ = base::TimeTicks::Now();
first_immersive_frame_time_delta_ = data.time_delta;
}
// (device_t_N - device_t_0) is:
base::TimeDelta current_frame_time_from_first_frame =
data.time_delta - *first_immersive_frame_time_delta_;
// renderer_t_N is then:
base::TimeTicks current_frame_time =
*first_immersive_frame_time_ + current_frame_time_from_first_frame;
double high_res_now_ms =
window->document()
->Loader()
->GetTiming()
.MonotonicTimeToZeroBasedDocumentTime(current_frame_time)
.InMillisecondsF();
return high_res_now_ms;
}
void XRFrameProvider::SubmitWebGLLayer(XRWebGLLayerClient* layer_client,
bool was_changed) {
CHECK(layer_client);
CHECK(immersive_session_);
const XRLayer* layer = layer_client->layer();
CHECK(layer);
CHECK_EQ(layer->session(), immersive_session_);
CHECK_EQ(layer->session()->GraphicsApi(), XRGraphicsBinding::Api::kWebGL);
if (!immersive_presentation_provider_.is_bound())
return;
TRACE_EVENT1("gpu", "XRFrameProvider::SubmitWebGLLayer", "frame", frame_id_);
DVLOG(3) << __func__ << ": frame=" << frame_id_;
WebGLRenderingContextBase* webgl_context = layer_client->context();
if (frame_id_ < 0) {
// There is no valid frame_id_, and the browser side is not currently
// expecting a frame to be submitted. That can happen for the first
// immersive frame if the animation loop submits without a preceding
// immersive GetFrameData response, in that case frame_id_ is -1 (see
// https://crbug.com/855722).
return;
}
if (!was_changed) {
// Just tell the device side that there was no submitted frame instead of
// executing the implicit end-of-frame submit.
frame_transport_->FrameSubmitMissing(immersive_presentation_provider_.get(),
webgl_context->ContextGL(), frame_id_);
dropped_frames_++;
return;
}
frame_transport_->FramePreImage(webgl_context->ContextGL());
if (frame_transport_->DrawingIntoSharedBuffer()) {
// Image is written to shared buffer already. Just submit with a
// placeholder.
scoped_refptr<Image> image_ref;
DVLOG(3) << __func__ << ": FrameSubmit for SharedBuffer mode";
bool succeeded = frame_transport_->FrameSubmit(
immersive_presentation_provider_.get(), webgl_context->ContextGL(),
webgl_context->SharedImageInterface(), webgl_context,
std::move(image_ref), frame_id_);
succeeded ? num_frames_++ : dropped_frames_++;
if (succeeded) {
submit_frame_time_.StartTimer();
}
return;
}
scoped_refptr<StaticBitmapImage> image_ref =
layer_client->TransferToStaticBitmapImage();
if (!image_ref)
return;
// Hardware-accelerated rendering should always be texture backed. Ensure this
// is the case, don't attempt to render if using an unexpected drawing path.
if (!image_ref->IsTextureBacked()) {
NOTREACHED() << "WebXR requires hardware-accelerated rendering to texture";
}
bool succeeded = frame_transport_->FrameSubmit(
immersive_presentation_provider_.get(), webgl_context->ContextGL(),
webgl_context->SharedImageInterface(), webgl_context,
std::move(image_ref), frame_id_);
succeeded ? num_frames_++ : dropped_frames_++;
if (succeeded) {
submit_frame_time_.StartTimer();
}
// Reset our frame id, since anything we'd want to do (resizing/etc) can
// no-longer happen to this frame.
frame_id_ = -1;
}
// TODO(bajones): This only works because we're restricted to a single layer at
// the moment. Will need an overhaul when we get more robust layering support.
void XRFrameProvider::UpdateWebGLLayerViewports(XRWebGLLayer* layer) {
DCHECK(layer->session() == immersive_session_);
DCHECK(immersive_presentation_provider_.is_bound());
XRViewport* left =
layer->GetViewportForEye(device::mojom::blink::XREye::kLeft);
XRViewport* right =
layer->GetViewportForEye(device::mojom::blink::XREye::kRight);
float width = layer->framebufferWidth();
float height = layer->framebufferHeight();
// We may only have one eye view, i.e. in smartphone immersive AR mode.
// Use all-zero bounds for unused views.
gfx::RectF left_coords =
left ? gfx::RectF(
static_cast<float>(left->x()) / width,
static_cast<float>(height - (left->y() + left->height())) /
height,
static_cast<float>(left->width()) / width,
static_cast<float>(left->height()) / height)
: gfx::RectF();
gfx::RectF right_coords =
right ? gfx::RectF(
static_cast<float>(right->x()) / width,
static_cast<float>(height - (right->y() + right->height())) /
height,
static_cast<float>(right->width()) / width,
static_cast<float>(right->height()) / height)
: gfx::RectF();
immersive_presentation_provider_->UpdateLayerBounds(
frame_id_, left_coords, right_coords, gfx::Size(width, height));
}
void XRFrameProvider::SubmitWebGPULayer(XRGPUProjectionLayer* layer,
bool was_queried) {
CHECK(layer);
CHECK(immersive_session_);
CHECK_EQ(layer->session(), immersive_session_);
CHECK_EQ(layer->session()->GraphicsApi(), XRGraphicsBinding::Api::kWebGPU);
if (!immersive_presentation_provider_.is_bound()) {
return;
}
TRACE_EVENT1("gpu", "XRFrameProvider::SubmitWebGPULayer", "frame", frame_id_);
DVLOG(3) << __func__ << ": frame=" << frame_id_;
XRGPUBinding* webgpu_binding = static_cast<XRGPUBinding*>(layer->binding());
GPUDevice* device = webgpu_binding->device();
if (frame_id_ < 0) {
// There is no valid frame_id_, and the browser side is not currently
// expecting a frame to be submitted. That can happen for the first
// immersive frame if the animation loop submits without a preceding
// immersive GetFrameData response, in that case frame_id_ is -1 (see
// https://crbug.com/855722).
return;
}
if (!was_queried) {
// Just tell the device side that there was no submitted frame instead of
// executing the implicit end-of-frame submit.
frame_transport_->FrameSubmitMissingWebGPU(
immersive_presentation_provider_.get(), device->GetDawnControlClient(),
frame_id_);
dropped_frames_++;
return;
}
frame_transport_->FramePreImageWebGPU(device->GetDawnControlClient());
if (!frame_transport_->DrawingIntoSharedBuffer()) {
NOTREACHED()
<< "WebXR/WebGPU bindings only supports the DRAW_INTO_TEXTURE_MAILBOX "
<< "XRPresentationTransportMethod at this time.";
}
DVLOG(3) << __func__ << ": FrameSubmitWebGPU for SharedBuffer mode";
bool succeeded = frame_transport_->FrameSubmitWebGPU(
immersive_presentation_provider_.get(), device->GetDawnControlClient(),
device->GetHandle(), frame_id_);
succeeded ? num_frames_++ : dropped_frames_++;
if (succeeded) {
submit_frame_time_.StartTimer();
}
// Reset our frame id, since anything we'd want to do (resizing/etc) can
// no-longer happen to this frame.
frame_id_ = -1;
}
// TODO(bajones): This only works because we're restricted to a single layer at
// the moment. Will need an overhaul when we get more robust layering support.
void XRFrameProvider::UpdateLayerViewports(XRProjectionLayer* layer) {
DCHECK(layer->session() == immersive_session_);
DCHECK(immersive_presentation_provider_.is_bound());
XRGraphicsBinding* binding = layer->binding();
// TODO(crbug.com/359418629): Currently we have no way to submit texture
// arrays to the compositor, so any array textures produced by the page will
// be copied to a side-by-side texture prior to submission. That does mean
// that we need to adjust the viewports from those reported to the page,
// however, by altering the texture width here...
float width = layer->textureWidth() * layer->textureArrayLength();
float height = layer->textureHeight();
gfx::RectF left_coords;
gfx::RectF right_coords;
if (immersive_session_->StereoscopicViews()) {
XRViewData* left_view =
immersive_session_->ViewDataForEye(device::mojom::blink::XREye::kLeft);
XRViewData* right_view =
immersive_session_->ViewDataForEye(device::mojom::blink::XREye::kRight);
gfx::Rect left = binding->GetViewportForView(layer, left_view);
gfx::Rect right = binding->GetViewportForView(layer, right_view);
// (continued from prior comment) ...and offsetting the viewports here.
if (layer->textureArrayLength() > 1) {
right.set_x(right.x() + layer->textureWidth());
}
left_coords = NormalizeViewport(left, width, height);
right_coords = NormalizeViewport(right, width, height);
} else {
XRViewData* mono_view =
immersive_session_->ViewDataForEye(device::mojom::blink::XREye::kNone);
gfx::Rect viewport = binding->GetViewportForView(layer, mono_view);
left_coords = NormalizeViewport(viewport, width, height);
// Non-stereo modes (i.e. smartphone immersive AR mode)
// use the default all-zero bounds for right view.
}
immersive_presentation_provider_->UpdateLayerBounds(
frame_id_, left_coords, right_coords, gfx::Size(width, height));
}
void XRFrameProvider::Dispose() {
DVLOG(2) << __func__;
immersive_presentation_provider_.reset();
immersive_data_provider_.reset();
if (immersive_session_)
immersive_session_->ForceEnd(XRSession::ShutdownPolicy::kImmediate);
// TODO(bajones): Do something for outstanding frame requests?
}
void XRFrameProvider::SendFrameData() {
if (!immersive_session_) {
return;
}
device::mojom::blink::XrFrameStatisticsPtr xr_frame_stat =
device::mojom::blink::XrFrameStatistics::New();
xr_frame_stat->trace_id = immersive_session_->GetTraceId();
base::TimeTicks now = base::TimeTicks::Now();
xr_frame_stat->duration = now - last_frame_statistics_sent_time_;
last_frame_statistics_sent_time_ = now;
xr_frame_stat->num_frames = num_frames_;
xr_frame_stat->dropped_frames = dropped_frames_;
num_frames_ = 0;
dropped_frames_ = 0;
xr_frame_stat->frame_data_time = frame_data_time_.TakeAverageMicroseconds();
xr_frame_stat->page_animation_frame_time =
immersive_session()->TakeAnimationFrameTimerAverage();
xr_frame_stat->submit_frame_time =
submit_frame_time_.TakeAverageMicroseconds();
if (xr_->GetWebXrInternalsRendererListener()) {
xr_->GetWebXrInternalsRendererListener()->OnFrameData(
std::move(xr_frame_stat));
}
}
void XRFrameProvider::OnRenderComplete() {
submit_frame_time_.StopTimer();
}
bool XRFrameProvider::DrawingIntoSharedBuffer() const {
return frame_transport_->DrawingIntoSharedBuffer();
}
void XRFrameProvider::Trace(Visitor* visitor) const {
visitor->Trace(xr_);
visitor->Trace(frame_transport_);
visitor->Trace(immersive_session_);
visitor->Trace(immersive_data_provider_);
visitor->Trace(immersive_presentation_provider_);
visitor->Trace(non_immersive_data_providers_);
visitor->Trace(requesting_sessions_);
visitor->Trace(immersive_observers_);
}
} // namespace blink
|