1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 
16 #include "gpu_query_manager.h"
17 
18 #include <cstdint>
19 
20 #include <base/containers/string.h>
21 #include <base/containers/string_view.h>
22 #include <render/namespace.h>
23 
24 #include "device/gpu_resource_handle_util.h"
25 #include "perf/gpu_query.h"
26 #include "util/log.h"
27 
28 using namespace BASE_NS;
29 
RENDER_BEGIN_NAMESPACE()30 RENDER_BEGIN_NAMESPACE()
31 EngineResourceHandle GpuQueryManager::Create(const string_view name, unique_ptr<GpuQuery> gpuQuery)
32 {
33     PLUGIN_ASSERT(nameToHandle_.count(name) == 0 && "not an unique gpu query name");
34 
35     resources_.push_back(move(gpuQuery));
36     const uint32_t index = static_cast<uint32_t>(resources_.size()) - 1;
37 
38     const EngineResourceHandle handle =
39         RenderHandleUtil::CreateEngineResourceHandle(RenderHandleType::UNDEFINED, index, 0);
40     nameToHandle_[name] = handle;
41     return handle;
42 }
43 
GetGpuHandle(const string_view name) const44 EngineResourceHandle GpuQueryManager::GetGpuHandle(const string_view name) const
45 {
46     PLUGIN_ASSERT(nameToHandle_.count(name) > 0 && "gpu query name not found");
47 
48     const auto iter = nameToHandle_.find(name);
49     if (iter != nameToHandle_.cend()) {
50         return iter->second;
51     } else {
52         return {};
53     }
54 }
55 
Get(const EngineResourceHandle handle) const56 GpuQuery* GpuQueryManager::Get(const EngineResourceHandle handle) const
57 {
58     const uint32_t index = RenderHandleUtil::GetIndexPart(handle);
59     PLUGIN_ASSERT(index < static_cast<uint32_t>(resources_.size()) && "gpu query name not found");
60 
61     if (index < static_cast<uint32_t>(resources_.size())) {
62         return resources_[index].get();
63     } else {
64         return nullptr;
65     }
66 }
67 RENDER_END_NAMESPACE()
68