blob: 9ff179986929acdfacebb054d4b8b381a9371e32 [file] [log] [blame]
Corentin Wallezac67fec2019-01-04 10:30:40 +00001// Copyright 2018 The Dawn Authors
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 DAWNNATIVE_INSTANCE_H_
16#define DAWNNATIVE_INSTANCE_H_
17
Rafael Cintron7e8385c2020-04-20 17:36:22 +000018#include "common/RefCounted.h"
Austin Eng2d0007c2021-12-02 18:12:57 +000019#include "common/ityp_bitset.h"
Corentin Wallezac67fec2019-01-04 10:30:40 +000020#include "dawn_native/Adapter.h"
21#include "dawn_native/BackendConnection.h"
François Beaufort3f689a42021-10-04 11:30:02 +000022#include "dawn_native/Features.h"
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000023#include "dawn_native/Toggles.h"
Corentin Wallez5fc2c822020-01-10 13:06:48 +000024#include "dawn_native/dawn_platform.h"
Corentin Wallezac67fec2019-01-04 10:30:40 +000025
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000026#include <array>
Corentin Wallezac67fec2019-01-04 10:30:40 +000027#include <memory>
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000028#include <unordered_map>
Corentin Wallezac67fec2019-01-04 10:30:40 +000029#include <vector>
30
Corentin Wallez8bf85872022-01-11 02:10:53 +000031namespace dawn::platform {
Jiawei Shao5e1ca532021-06-04 05:12:06 +000032 class Platform;
Corentin Wallez8bf85872022-01-11 02:10:53 +000033} // namespace dawn::platform
Jiawei Shao5e1ca532021-06-04 05:12:06 +000034
Corentin Wallezac67fec2019-01-04 10:30:40 +000035namespace dawn_native {
36
Corentin Wallez3a1746e2020-01-15 13:14:12 +000037 class Surface;
Corentin Wallezaa0e1be2021-02-11 08:26:38 +000038 class XlibXcbFunctions;
Corentin Wallez3a1746e2020-01-15 13:14:12 +000039
Austin Eng2d0007c2021-12-02 18:12:57 +000040 using BackendsBitset = ityp::bitset<wgpu::BackendType, kEnumCount<wgpu::BackendType>>;
41
Corentin Wallezac67fec2019-01-04 10:30:40 +000042 // This is called InstanceBase for consistency across the frontend, even if the backends don't
43 // specialize this class.
Corentin Wallez5fc2c822020-01-10 13:06:48 +000044 class InstanceBase final : public RefCounted {
Corentin Wallezac67fec2019-01-04 10:30:40 +000045 public:
Corentin Wallez5fc2c822020-01-10 13:06:48 +000046 static InstanceBase* Create(const InstanceDescriptor* descriptor = nullptr);
Corentin Wallezac67fec2019-01-04 10:30:40 +000047
Austin Eng5397f9f2021-12-14 23:22:46 +000048 void APIRequestAdapter(const RequestAdapterOptions* options,
49 WGPURequestAdapterCallback callback,
50 void* userdata);
51
Corentin Wallezac67fec2019-01-04 10:30:40 +000052 void DiscoverDefaultAdapters();
Corentin Wallez90e594e2019-01-07 09:48:03 +000053 bool DiscoverAdapters(const AdapterDiscoveryOptionsBase* options);
Corentin Wallezac67fec2019-01-04 10:30:40 +000054
55 const std::vector<std::unique_ptr<AdapterBase>>& GetAdapters() const;
56
57 // Used to handle error that happen up to device creation.
58 bool ConsumedError(MaybeError maybeError);
59
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000060 // Used to query the details of a toggle. Return nullptr if toggleName is not a valid name
61 // of a toggle supported in Dawn.
62 const ToggleInfo* GetToggleInfo(const char* toggleName);
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000063 Toggle ToggleNameToEnum(const char* toggleName);
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000064
François Beaufort3f689a42021-10-04 11:30:02 +000065 // Used to query the details of an feature. Return nullptr if featureName is not a valid
66 // name of an feature supported in Dawn.
Austin Eng2f218e22021-12-22 19:02:23 +000067 const FeatureInfo* GetFeatureInfo(wgpu::FeatureName feature);
Jiawei Shao574b9512019-08-02 00:06:38 +000068
Rafael Cintron4729b152019-06-21 02:09:05 +000069 bool IsBackendValidationEnabled() const;
Brandon Jonesbdbf98a2021-02-04 19:32:12 +000070 void SetBackendValidationLevel(BackendValidationLevel level);
71 BackendValidationLevel GetBackendValidationLevel() const;
Bryan Bernhart03cf7c32020-07-29 19:44:41 +000072
Rafael Cintron4729b152019-06-21 02:09:05 +000073 void EnableBeginCaptureOnStartup(bool beginCaptureOnStartup);
74 bool IsBeginCaptureOnStartupEnabled() const;
Li Hao0195dbf2019-05-15 06:06:26 +000075
Corentin Wallez8bf85872022-01-11 02:10:53 +000076 void SetPlatform(dawn::platform::Platform* platform);
77 dawn::platform::Platform* GetPlatform();
Austin Engd4ce7362019-08-13 19:00:34 +000078
Corentin Wallezaa0e1be2021-02-11 08:26:38 +000079 // Get backend-independent libraries that need to be loaded dynamically.
80 const XlibXcbFunctions* GetOrCreateXlibXcbFunctions();
81
Corentin Wallez3a1746e2020-01-15 13:14:12 +000082 // Dawn API
Corentin Wallez2ce4b902021-03-29 14:02:05 +000083 Surface* APICreateSurface(const SurfaceDescriptor* descriptor);
Corentin Wallez3a1746e2020-01-15 13:14:12 +000084
Corentin Wallezac67fec2019-01-04 10:30:40 +000085 private:
Corentin Wallez5fc2c822020-01-10 13:06:48 +000086 InstanceBase() = default;
87 ~InstanceBase() = default;
88
89 InstanceBase(const InstanceBase& other) = delete;
90 InstanceBase& operator=(const InstanceBase& other) = delete;
91
92 bool Initialize(const InstanceDescriptor* descriptor);
93
Corentin Wallez90e594e2019-01-07 09:48:03 +000094 // Lazily creates connections to all backends that have been compiled.
Austin Eng2d0007c2021-12-02 18:12:57 +000095 void EnsureBackendConnection(wgpu::BackendType backendType);
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000096
Corentin Wallez90e594e2019-01-07 09:48:03 +000097 MaybeError DiscoverAdaptersInternal(const AdapterDiscoveryOptionsBase* options);
98
Austin Eng2d0007c2021-12-02 18:12:57 +000099 BackendsBitset mBackendsConnected;
100
Corentin Wallez9b8d2b02019-02-25 17:42:56 +0000101 bool mDiscoveredDefaultAdapters = false;
Corentin Wallezac67fec2019-01-04 10:30:40 +0000102
Rafael Cintron4729b152019-06-21 02:09:05 +0000103 bool mBeginCaptureOnStartup = false;
Brandon Jonesbdbf98a2021-02-04 19:32:12 +0000104 BackendValidationLevel mBackendValidationLevel = BackendValidationLevel::Disabled;
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000105
Corentin Wallez8bf85872022-01-11 02:10:53 +0000106 dawn::platform::Platform* mPlatform = nullptr;
107 std::unique_ptr<dawn::platform::Platform> mDefaultPlatform;
Austin Engd4ce7362019-08-13 19:00:34 +0000108
Corentin Wallezac67fec2019-01-04 10:30:40 +0000109 std::vector<std::unique_ptr<BackendConnection>> mBackends;
110 std::vector<std::unique_ptr<AdapterBase>> mAdapters;
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000111
François Beaufort3f689a42021-10-04 11:30:02 +0000112 FeaturesInfo mFeaturesInfo;
Jiawei Shaod08611b2019-07-30 23:58:52 +0000113 TogglesInfo mTogglesInfo;
Corentin Wallezaa0e1be2021-02-11 08:26:38 +0000114
115#if defined(DAWN_USE_X11)
116 std::unique_ptr<XlibXcbFunctions> mXlibXcbFunctions;
117#endif // defined(DAWN_USE_X11)
Corentin Wallezac67fec2019-01-04 10:30:40 +0000118 };
119
120} // namespace dawn_native
121
122#endif // DAWNNATIVE_INSTANCE_H_