• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Toggles.h"
21 
22 #include <array>
23 #include <memory>
24 #include <unordered_map>
25 #include <vector>
26 
27 namespace dawn_native {
28 
29     // This is called InstanceBase for consistency across the frontend, even if the backends don't
30     // specialize this class.
31     class InstanceBase final {
32       public:
33         InstanceBase() = default;
34         ~InstanceBase() = default;
35 
36         InstanceBase(const InstanceBase& other) = delete;
37         InstanceBase& operator=(const InstanceBase& other) = delete;
38 
39         void DiscoverDefaultAdapters();
40         bool DiscoverAdapters(const AdapterDiscoveryOptionsBase* options);
41 
42         const std::vector<std::unique_ptr<AdapterBase>>& GetAdapters() const;
43 
44         // Used to handle error that happen up to device creation.
45         bool ConsumedError(MaybeError maybeError);
46 
47         // Used to query the details of a toggle. Return nullptr if toggleName is not a valid name
48         // of a toggle supported in Dawn.
49         const ToggleInfo* GetToggleInfo(const char* toggleName);
50 
51         Toggle ToggleNameToEnum(const char* toggleName);
52         const char* ToggleEnumToName(Toggle toggle);
53 
54         void EnableBackendValidation(bool enableBackendValidation);
55         bool IsBackendValidationEnabled() const;
56 
57         void EnableBeginCaptureOnStartup(bool beginCaptureOnStartup);
58         bool IsBeginCaptureOnStartupEnabled() const;
59 
60       private:
61         // Lazily creates connections to all backends that have been compiled.
62         void EnsureBackendConnections();
63 
64         // Finds the BackendConnection for `type` or returns an error.
65         ResultOrError<BackendConnection*> FindBackend(BackendType type);
66 
67         MaybeError DiscoverAdaptersInternal(const AdapterDiscoveryOptionsBase* options);
68 
69         void EnsureToggleNameToEnumMapInitialized();
70 
71         bool mBackendsConnected = false;
72         bool mDiscoveredDefaultAdapters = false;
73 
74         bool mToggleNameToEnumMapInitialized = false;
75         bool mEnableBackendValidation = false;
76         bool mBeginCaptureOnStartup = false;
77 
78         std::vector<std::unique_ptr<BackendConnection>> mBackends;
79         std::vector<std::unique_ptr<AdapterBase>> mAdapters;
80 
81         std::unordered_map<std::string, Toggle> mToggleNameToEnumMap;
82     };
83 
84 }  // namespace dawn_native
85 
86 #endif  // DAWNNATIVE_INSTANCE_H_
87