[go: nahoru, domu]

blob: c45682361cc7807b5e190abe0e519067d2c05284 [file] [log] [blame]
junweid4ed5a812023-04-19 03:19:261// Copyright 2023 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "services/webnn/webnn_context_provider_impl.h"
6
7#include <memory>
8#include <utility>
9
10#include "mojo/public/cpp/bindings/self_owned_receiver.h"
Shiyi Zouff06a312023-09-15 05:18:0511#include "services/webnn/error.h"
mingmingtasdafe5ad42024-01-05 07:09:5512#include "services/webnn/public/mojom/webnn_error.mojom.h"
junweid4ed5a812023-04-19 03:19:2613#include "services/webnn/webnn_context_impl.h"
14
Ningxin Huc8a5ff62023-08-04 03:53:2815#if BUILDFLAG(IS_WIN)
16#include "services/webnn/dml/adapter.h"
17#include "services/webnn/dml/context_impl.h"
18#endif
19
sushraja189e93e2024-01-31 22:39:3920#if BUILDFLAG(IS_MAC)
21#include "services/webnn/coreml/context_impl.h"
22#endif
23
junweid4ed5a812023-04-19 03:19:2624namespace webnn {
25
26namespace {
27
junwei40fca562023-08-16 05:39:1128WebNNContextProviderImpl::BackendForTesting* g_backend_for_testing = nullptr;
29
junweid4ed5a812023-04-19 03:19:2630using webnn::mojom::CreateContextOptionsPtr;
31using webnn::mojom::WebNNContextProvider;
32
33} // namespace
34
35WebNNContextProviderImpl::WebNNContextProviderImpl() = default;
36
37WebNNContextProviderImpl::~WebNNContextProviderImpl() = default;
38
39// static
40void WebNNContextProviderImpl::Create(
41 mojo::PendingReceiver<WebNNContextProvider> receiver) {
42 mojo::MakeSelfOwnedReceiver<WebNNContextProvider>(
43 std::make_unique<WebNNContextProviderImpl>(), std::move(receiver));
44}
45
junwei3a4fa102023-05-30 03:37:2046void WebNNContextProviderImpl::OnConnectionError(WebNNContextImpl* impl) {
47 auto it =
48 base::ranges::find(impls_, impl, &std::unique_ptr<WebNNContextImpl>::get);
49 CHECK(it != impls_.end());
50 impls_.erase(it);
51}
52
junwei40fca562023-08-16 05:39:1153// static
54void WebNNContextProviderImpl::SetBackendForTesting(
55 BackendForTesting* backend_for_testing) {
56 g_backend_for_testing = backend_for_testing;
57}
58
junweid4ed5a812023-04-19 03:19:2659void WebNNContextProviderImpl::CreateWebNNContext(
60 CreateContextOptionsPtr options,
61 WebNNContextProvider::CreateWebNNContextCallback callback) {
junwei40fca562023-08-16 05:39:1162 if (g_backend_for_testing) {
63 g_backend_for_testing->CreateWebNNContext(impls_, this, std::move(options),
64 std::move(callback));
65 return;
66 }
67
junweid4ed5a812023-04-19 03:19:2668#if BUILDFLAG(IS_WIN)
Ningxin Huc8a5ff62023-08-04 03:53:2869 // Get the default `Adapter` instance which is created for the adapter queried
70 // from ANGLE. At the current stage, all `ContextImpl` share this instance.
71 //
72 // TODO(crbug.com/1469755): Support getting `Adapter` instance based on
73 // `options`.
Bryan Bernhart6e27fa162023-08-16 18:05:2774 constexpr DML_FEATURE_LEVEL kMinDMLFeatureLevelForWebNN =
75 DML_FEATURE_LEVEL_4_0;
mingmingtasda2425402023-11-28 03:56:3876 auto adapter_creation_result =
Bryan Bernhart6e27fa162023-08-16 18:05:2777 dml::Adapter::GetInstance(kMinDMLFeatureLevelForWebNN);
mingmingtasda2425402023-11-28 03:56:3878 if (!adapter_creation_result.has_value()) {
79 std::move(callback).Run(mojom::CreateContextResult::NewError(
80 std::move(adapter_creation_result.error())));
Ningxin Huc8a5ff62023-08-04 03:53:2881 return;
82 }
junweid4ed5a812023-04-19 03:19:2683 // The remote sent to the renderer.
84 mojo::PendingRemote<mojom::WebNNContext> blink_remote;
85 // The receiver bound to WebNNContextImpl.
Ningxin Huc8a5ff62023-08-04 03:53:2886 impls_.push_back(base::WrapUnique<WebNNContextImpl>(new dml::ContextImpl(
mingmingtasda2425402023-11-28 03:56:3887 std::move(adapter_creation_result.value()),
88 blink_remote.InitWithNewPipeAndPassReceiver(), this)));
Shiyi Zouff06a312023-09-15 05:18:0589 std::move(callback).Run(
90 mojom::CreateContextResult::NewContextRemote(std::move(blink_remote)));
sushraja189e93e2024-01-31 22:39:3991#elif BUILDFLAG(IS_MAC)
92 if (__builtin_available(macOS 13, *)) {
93 // The remote sent to the renderer.
94 mojo::PendingRemote<mojom::WebNNContext> blink_remote;
95 // The receiver bound to WebNNContextImpl.
96 impls_.push_back(base::WrapUnique<WebNNContextImpl>(new coreml::ContextImpl(
97 blink_remote.InitWithNewPipeAndPassReceiver(), this)));
98 std::move(callback).Run(
99 mojom::CreateContextResult::NewContextRemote(std::move(blink_remote)));
100 } else {
101 std::move(callback).Run(ToError<mojom::CreateContextResult>(
102 mojom::Error::Code::kNotSupportedError,
103 "WebNN Service is not supported on this platform."));
104 DLOG(ERROR) << "WebNN Service is not supported on this platform.";
105 }
junweid4ed5a812023-04-19 03:19:26106#else
107 // TODO(crbug.com/1273291): Supporting WebNN Service on the platform.
Shiyi Zouff06a312023-09-15 05:18:05108 std::move(callback).Run(ToError<mojom::CreateContextResult>(
109 mojom::Error::Code::kNotSupportedError,
110 "WebNN Service is not supported on this platform."));
111 DLOG(ERROR) << "WebNN Service is not supported on this platform.";
junweid4ed5a812023-04-19 03:19:26112#endif
113}
114
115} // namespace webnn