File: openxr_device_provider.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (102 lines) | stat: -rw-r--r-- 4,135 bytes parent folder | download | duplicates (6)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/webxr/android/openxr_device_provider.h"

#include <utility>

#include "base/android/jni_android.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/system/sys_info.h"
#include "base/task/single_thread_task_runner.h"
#include "components/viz/common/gpu/context_provider.h"
#include "components/webxr/android/openxr_platform_helper_android.h"
#include "components/webxr/android/xr_session_coordinator.h"
#include "components/webxr/mailbox_to_surface_bridge_impl.h"
#include "components/webxr/xr_test_hook_wrapper.h"
#include "content/public/browser/android/compositor.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "device/vr/android/xr_image_transport_base.h"
#include "device/vr/openxr/context_provider_callbacks.h"
#include "device/vr/openxr/openxr_api_wrapper.h"
#include "device/vr/openxr/openxr_device.h"
#include "device/vr/openxr/openxr_platform.h"
#include "gpu/command_buffer/client/shared_memory_limits.h"
#include "gpu/command_buffer/common/context_creation_attribs.h"
#include "third_party/openxr/src/include/openxr/openxr.h"

namespace webxr {

// static
void OpenXrDeviceProvider::SetTestHook(
    mojo::PendingRemote<device_test::mojom::XRTestHook> hook_info) {
  static base::NoDestructor<std::unique_ptr<XRTestHookWrapper>> wrapper;
  if (wrapper->get() != nullptr) {
    device::OpenXrApiWrapper::SetTestHook(nullptr);
  }
  *wrapper = hook_info
                 ? std::make_unique<XRTestHookWrapper>(std::move(hook_info))
                 : nullptr;
  device::OpenXrApiWrapper::SetTestHook(wrapper->get());
}

OpenXrDeviceProvider::OpenXrDeviceProvider() = default;

OpenXrDeviceProvider::~OpenXrDeviceProvider() {
  // Must make sure that the OpenXrPlatformHelper outlives the OpenXrDevice.
  openxr_device_.reset();
  openxr_platform_helper_.reset();
}

void OpenXrDeviceProvider::Initialize(
    device::VRDeviceProviderClient* client,
    content::WebContents* initializing_web_contents) {
  CHECK(!initialized_);

  openxr_platform_helper_ = std::make_unique<OpenXrPlatformHelperAndroid>();

  if (openxr_platform_helper_->EnsureInitialized() &&
      openxr_platform_helper_->CheckHardwareSupport(
          initializing_web_contents)) {
    DVLOG(2) << __func__ << ": OpenXr is supported, creating device";
    // Unretained is safe since we own the device this callback is being
    // passed to and we ensure that it does not outlive us. The device is
    // expected to wind down any threads that it spins up as well (for e.g.
    // rendering), so this destruction is also safe. The OpenXrDevice passes
    // this off to different render loops as it creates them, so we can't just
    // use a WeakPtr of ourselves here, since it would technically end up
    // dereferenced on different threads (albeit all children).
    openxr_device_ = std::make_unique<device::OpenXrDevice>(
        base::BindRepeating(&OpenXrDeviceProvider::CreateContextProviderAsync,
                            base::Unretained(this)),
        openxr_platform_helper_.get());

    client->AddRuntime(openxr_device_->GetId(), openxr_device_->GetDeviceData(),
                       openxr_device_->BindXRRuntime());
  }

  initialized_ = true;
  client->OnProviderInitialized();
}

bool OpenXrDeviceProvider::Initialized() {
  return initialized_;
}

void OpenXrDeviceProvider::CreateContextProviderAsync(
    VizContextProviderCallback viz_context_provider_callback) {
  content::GetUIThreadTaskRunner({})->PostTask(
      FROM_HERE, base::BindOnce(
                     [](content::Compositor::ContextProviderCallback callback) {
                       content::Compositor::CreateContextProvider(
                           gpu::SharedMemoryLimits::ForMailboxContext(),
                           std::move(callback));
                     },
                     std::move(viz_context_provider_callback)));
}

}  // namespace webxr