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