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 18 #include "common/RefCounted.h" 19 #include "common/ityp_bitset.h" 20 #include "dawn_native/Adapter.h" 21 #include "dawn_native/BackendConnection.h" 22 #include "dawn_native/Features.h" 23 #include "dawn_native/Toggles.h" 24 #include "dawn_native/dawn_platform.h" 25 26 #include <array> 27 #include <memory> 28 #include <unordered_map> 29 #include <vector> 30 31 namespace dawn_platform { 32 class Platform; 33 } // namespace dawn_platform 34 35 namespace dawn_native { 36 37 class Surface; 38 class XlibXcbFunctions; 39 40 using BackendsBitset = ityp::bitset<wgpu::BackendType, kEnumCount<wgpu::BackendType>>; 41 42 // This is called InstanceBase for consistency across the frontend, even if the backends don't 43 // specialize this class. 44 class InstanceBase final : public RefCounted { 45 public: 46 static InstanceBase* Create(const InstanceDescriptor* descriptor = nullptr); 47 48 void DiscoverDefaultAdapters(); 49 bool DiscoverAdapters(const AdapterDiscoveryOptionsBase* options); 50 51 const std::vector<std::unique_ptr<AdapterBase>>& GetAdapters() const; 52 53 // Used to handle error that happen up to device creation. 54 bool ConsumedError(MaybeError maybeError); 55 56 // Used to query the details of a toggle. Return nullptr if toggleName is not a valid name 57 // of a toggle supported in Dawn. 58 const ToggleInfo* GetToggleInfo(const char* toggleName); 59 Toggle ToggleNameToEnum(const char* toggleName); 60 61 // Used to query the details of an feature. Return nullptr if featureName is not a valid 62 // name of an feature supported in Dawn. 63 const FeatureInfo* GetFeatureInfo(const char* featureName); 64 Feature FeatureNameToEnum(const char* featureName); 65 FeaturesSet FeatureNamesToFeaturesSet(const std::vector<const char*>& requiredFeatures); 66 67 bool IsBackendValidationEnabled() const; 68 void SetBackendValidationLevel(BackendValidationLevel level); 69 BackendValidationLevel GetBackendValidationLevel() const; 70 71 void EnableBeginCaptureOnStartup(bool beginCaptureOnStartup); 72 bool IsBeginCaptureOnStartupEnabled() const; 73 74 void SetPlatform(dawn_platform::Platform* platform); 75 dawn_platform::Platform* GetPlatform(); 76 77 // Get backend-independent libraries that need to be loaded dynamically. 78 const XlibXcbFunctions* GetOrCreateXlibXcbFunctions(); 79 80 // Dawn API 81 Surface* APICreateSurface(const SurfaceDescriptor* descriptor); 82 83 private: 84 InstanceBase() = default; 85 ~InstanceBase() = default; 86 87 InstanceBase(const InstanceBase& other) = delete; 88 InstanceBase& operator=(const InstanceBase& other) = delete; 89 90 bool Initialize(const InstanceDescriptor* descriptor); 91 92 // Lazily creates connections to all backends that have been compiled. 93 void EnsureBackendConnection(wgpu::BackendType backendType); 94 95 MaybeError DiscoverAdaptersInternal(const AdapterDiscoveryOptionsBase* options); 96 97 BackendsBitset mBackendsConnected; 98 99 bool mDiscoveredDefaultAdapters = false; 100 101 bool mBeginCaptureOnStartup = false; 102 BackendValidationLevel mBackendValidationLevel = BackendValidationLevel::Disabled; 103 104 dawn_platform::Platform* mPlatform = nullptr; 105 std::unique_ptr<dawn_platform::Platform> mDefaultPlatform; 106 107 std::vector<std::unique_ptr<BackendConnection>> mBackends; 108 std::vector<std::unique_ptr<AdapterBase>> mAdapters; 109 110 FeaturesInfo mFeaturesInfo; 111 TogglesInfo mTogglesInfo; 112 113 #if defined(DAWN_USE_X11) 114 std::unique_ptr<XlibXcbFunctions> mXlibXcbFunctions; 115 #endif // defined(DAWN_USE_X11) 116 }; 117 118 } // namespace dawn_native 119 120 #endif // DAWNNATIVE_INSTANCE_H_ 121