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 "dawn_native/Adapter.h" 19 #include "dawn_native/BackendConnection.h" 20 #include "dawn_native/Extensions.h" 21 #include "dawn_native/RefCounted.h" 22 #include "dawn_native/Toggles.h" 23 #include "dawn_native/dawn_platform.h" 24 25 #include <array> 26 #include <memory> 27 #include <unordered_map> 28 #include <vector> 29 30 namespace dawn_native { 31 32 class Surface; 33 34 // This is called InstanceBase for consistency across the frontend, even if the backends don't 35 // specialize this class. 36 class InstanceBase final : public RefCounted { 37 public: 38 static InstanceBase* Create(const InstanceDescriptor* descriptor = nullptr); 39 40 void DiscoverDefaultAdapters(); 41 bool DiscoverAdapters(const AdapterDiscoveryOptionsBase* options); 42 43 const std::vector<std::unique_ptr<AdapterBase>>& GetAdapters() const; 44 45 // Used to handle error that happen up to device creation. 46 bool ConsumedError(MaybeError maybeError); 47 48 // Used to query the details of a toggle. Return nullptr if toggleName is not a valid name 49 // of a toggle supported in Dawn. 50 const ToggleInfo* GetToggleInfo(const char* toggleName); 51 Toggle ToggleNameToEnum(const char* toggleName); 52 53 // Used to query the details of an extension. Return nullptr if extensionName is not a valid 54 // name of an extension supported in Dawn. 55 const ExtensionInfo* GetExtensionInfo(const char* extensionName); 56 Extension ExtensionNameToEnum(const char* extensionName); 57 ExtensionsSet ExtensionNamesToExtensionsSet( 58 const std::vector<const char*>& requiredExtensions); 59 60 void EnableBackendValidation(bool enableBackendValidation); 61 bool IsBackendValidationEnabled() const; 62 63 void EnableBeginCaptureOnStartup(bool beginCaptureOnStartup); 64 bool IsBeginCaptureOnStartupEnabled() const; 65 66 void SetPlatform(dawn_platform::Platform* platform); 67 dawn_platform::Platform* GetPlatform() const; 68 69 // Dawn API 70 Surface* CreateSurface(const SurfaceDescriptor* descriptor); 71 72 private: 73 InstanceBase() = default; 74 ~InstanceBase() = default; 75 76 InstanceBase(const InstanceBase& other) = delete; 77 InstanceBase& operator=(const InstanceBase& other) = delete; 78 79 bool Initialize(const InstanceDescriptor* descriptor); 80 81 // Lazily creates connections to all backends that have been compiled. 82 void EnsureBackendConnections(); 83 84 // Finds the BackendConnection for `type` or returns an error. 85 ResultOrError<BackendConnection*> FindBackend(wgpu::BackendType type); 86 87 MaybeError DiscoverAdaptersInternal(const AdapterDiscoveryOptionsBase* options); 88 89 bool mBackendsConnected = false; 90 bool mDiscoveredDefaultAdapters = false; 91 92 bool mEnableBackendValidation = false; 93 bool mBeginCaptureOnStartup = false; 94 95 dawn_platform::Platform* mPlatform = nullptr; 96 97 std::vector<std::unique_ptr<BackendConnection>> mBackends; 98 std::vector<std::unique_ptr<AdapterBase>> mAdapters; 99 100 ExtensionsInfo mExtensionsInfo; 101 TogglesInfo mTogglesInfo; 102 }; 103 104 } // namespace dawn_native 105 106 #endif // DAWNNATIVE_INSTANCE_H_ 107