Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 1 | // 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 Cintron | 7e8385c | 2020-04-20 17:36:22 +0000 | [diff] [blame] | 18 | #include "common/RefCounted.h" |
Austin Eng | 2d0007c | 2021-12-02 18:12:57 +0000 | [diff] [blame] | 19 | #include "common/ityp_bitset.h" |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 20 | #include "dawn_native/Adapter.h" |
| 21 | #include "dawn_native/BackendConnection.h" |
François Beaufort | 3f689a4 | 2021-10-04 11:30:02 +0000 | [diff] [blame] | 22 | #include "dawn_native/Features.h" |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 23 | #include "dawn_native/Toggles.h" |
Corentin Wallez | 5fc2c82 | 2020-01-10 13:06:48 +0000 | [diff] [blame] | 24 | #include "dawn_native/dawn_platform.h" |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 25 | |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 26 | #include <array> |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 27 | #include <memory> |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 28 | #include <unordered_map> |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Corentin Wallez | 8bf8587 | 2022-01-11 02:10:53 +0000 | [diff] [blame] | 31 | namespace dawn::platform { |
Jiawei Shao | 5e1ca53 | 2021-06-04 05:12:06 +0000 | [diff] [blame] | 32 | class Platform; |
Corentin Wallez | 8bf8587 | 2022-01-11 02:10:53 +0000 | [diff] [blame] | 33 | } // namespace dawn::platform |
Jiawei Shao | 5e1ca53 | 2021-06-04 05:12:06 +0000 | [diff] [blame] | 34 | |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 35 | namespace dawn_native { |
| 36 | |
Corentin Wallez | 3a1746e | 2020-01-15 13:14:12 +0000 | [diff] [blame] | 37 | class Surface; |
Corentin Wallez | aa0e1be | 2021-02-11 08:26:38 +0000 | [diff] [blame] | 38 | class XlibXcbFunctions; |
Corentin Wallez | 3a1746e | 2020-01-15 13:14:12 +0000 | [diff] [blame] | 39 | |
Austin Eng | 2d0007c | 2021-12-02 18:12:57 +0000 | [diff] [blame] | 40 | using BackendsBitset = ityp::bitset<wgpu::BackendType, kEnumCount<wgpu::BackendType>>; |
| 41 | |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 42 | // This is called InstanceBase for consistency across the frontend, even if the backends don't |
| 43 | // specialize this class. |
Corentin Wallez | 5fc2c82 | 2020-01-10 13:06:48 +0000 | [diff] [blame] | 44 | class InstanceBase final : public RefCounted { |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 45 | public: |
Corentin Wallez | 5fc2c82 | 2020-01-10 13:06:48 +0000 | [diff] [blame] | 46 | static InstanceBase* Create(const InstanceDescriptor* descriptor = nullptr); |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 47 | |
Austin Eng | 5397f9f | 2021-12-14 23:22:46 +0000 | [diff] [blame] | 48 | void APIRequestAdapter(const RequestAdapterOptions* options, |
| 49 | WGPURequestAdapterCallback callback, |
| 50 | void* userdata); |
| 51 | |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 52 | void DiscoverDefaultAdapters(); |
Corentin Wallez | 90e594e | 2019-01-07 09:48:03 +0000 | [diff] [blame] | 53 | bool DiscoverAdapters(const AdapterDiscoveryOptionsBase* options); |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 54 | |
| 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 Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 60 | // 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 Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 63 | Toggle ToggleNameToEnum(const char* toggleName); |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 64 | |
François Beaufort | 3f689a4 | 2021-10-04 11:30:02 +0000 | [diff] [blame] | 65 | // 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 Eng | 2f218e2 | 2021-12-22 19:02:23 +0000 | [diff] [blame] | 67 | const FeatureInfo* GetFeatureInfo(wgpu::FeatureName feature); |
Jiawei Shao | 574b951 | 2019-08-02 00:06:38 +0000 | [diff] [blame] | 68 | |
Rafael Cintron | 4729b15 | 2019-06-21 02:09:05 +0000 | [diff] [blame] | 69 | bool IsBackendValidationEnabled() const; |
Brandon Jones | bdbf98a | 2021-02-04 19:32:12 +0000 | [diff] [blame] | 70 | void SetBackendValidationLevel(BackendValidationLevel level); |
| 71 | BackendValidationLevel GetBackendValidationLevel() const; |
Bryan Bernhart | 03cf7c3 | 2020-07-29 19:44:41 +0000 | [diff] [blame] | 72 | |
Rafael Cintron | 4729b15 | 2019-06-21 02:09:05 +0000 | [diff] [blame] | 73 | void EnableBeginCaptureOnStartup(bool beginCaptureOnStartup); |
| 74 | bool IsBeginCaptureOnStartupEnabled() const; |
Li Hao | 0195dbf | 2019-05-15 06:06:26 +0000 | [diff] [blame] | 75 | |
Corentin Wallez | 8bf8587 | 2022-01-11 02:10:53 +0000 | [diff] [blame] | 76 | void SetPlatform(dawn::platform::Platform* platform); |
| 77 | dawn::platform::Platform* GetPlatform(); |
Austin Eng | d4ce736 | 2019-08-13 19:00:34 +0000 | [diff] [blame] | 78 | |
Corentin Wallez | aa0e1be | 2021-02-11 08:26:38 +0000 | [diff] [blame] | 79 | // Get backend-independent libraries that need to be loaded dynamically. |
| 80 | const XlibXcbFunctions* GetOrCreateXlibXcbFunctions(); |
| 81 | |
Corentin Wallez | 3a1746e | 2020-01-15 13:14:12 +0000 | [diff] [blame] | 82 | // Dawn API |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 83 | Surface* APICreateSurface(const SurfaceDescriptor* descriptor); |
Corentin Wallez | 3a1746e | 2020-01-15 13:14:12 +0000 | [diff] [blame] | 84 | |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 85 | private: |
Corentin Wallez | 5fc2c82 | 2020-01-10 13:06:48 +0000 | [diff] [blame] | 86 | 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 Wallez | 90e594e | 2019-01-07 09:48:03 +0000 | [diff] [blame] | 94 | // Lazily creates connections to all backends that have been compiled. |
Austin Eng | 2d0007c | 2021-12-02 18:12:57 +0000 | [diff] [blame] | 95 | void EnsureBackendConnection(wgpu::BackendType backendType); |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 96 | |
Corentin Wallez | 90e594e | 2019-01-07 09:48:03 +0000 | [diff] [blame] | 97 | MaybeError DiscoverAdaptersInternal(const AdapterDiscoveryOptionsBase* options); |
| 98 | |
Austin Eng | 2d0007c | 2021-12-02 18:12:57 +0000 | [diff] [blame] | 99 | BackendsBitset mBackendsConnected; |
| 100 | |
Corentin Wallez | 9b8d2b0 | 2019-02-25 17:42:56 +0000 | [diff] [blame] | 101 | bool mDiscoveredDefaultAdapters = false; |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 102 | |
Rafael Cintron | 4729b15 | 2019-06-21 02:09:05 +0000 | [diff] [blame] | 103 | bool mBeginCaptureOnStartup = false; |
Brandon Jones | bdbf98a | 2021-02-04 19:32:12 +0000 | [diff] [blame] | 104 | BackendValidationLevel mBackendValidationLevel = BackendValidationLevel::Disabled; |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 105 | |
Corentin Wallez | 8bf8587 | 2022-01-11 02:10:53 +0000 | [diff] [blame] | 106 | dawn::platform::Platform* mPlatform = nullptr; |
| 107 | std::unique_ptr<dawn::platform::Platform> mDefaultPlatform; |
Austin Eng | d4ce736 | 2019-08-13 19:00:34 +0000 | [diff] [blame] | 108 | |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 109 | std::vector<std::unique_ptr<BackendConnection>> mBackends; |
| 110 | std::vector<std::unique_ptr<AdapterBase>> mAdapters; |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 111 | |
François Beaufort | 3f689a4 | 2021-10-04 11:30:02 +0000 | [diff] [blame] | 112 | FeaturesInfo mFeaturesInfo; |
Jiawei Shao | d08611b | 2019-07-30 23:58:52 +0000 | [diff] [blame] | 113 | TogglesInfo mTogglesInfo; |
Corentin Wallez | aa0e1be | 2021-02-11 08:26:38 +0000 | [diff] [blame] | 114 | |
| 115 | #if defined(DAWN_USE_X11) |
| 116 | std::unique_ptr<XlibXcbFunctions> mXlibXcbFunctions; |
| 117 | #endif // defined(DAWN_USE_X11) |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // namespace dawn_native |
| 121 | |
| 122 | #endif // DAWNNATIVE_INSTANCE_H_ |