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_gles.h"
17 
18 #include <render/namespace.h>
19 
20 #include "device/device.h"
21 #include "gles/device_gles.h"
22 #include "gles/gl_functions.h"
23 #include "perf/gpu_query.h"
24 #include "util/log.h"
25 
RENDER_BEGIN_NAMESPACE()26 RENDER_BEGIN_NAMESPACE()
27 GpuQueryGLES::GpuQueryGLES(Device& device, const GpuQueryDesc& desc) : desc_(desc)
28 {
29     PLUGIN_ASSERT(static_cast<DeviceGLES&>(device).IsActive());
30     BASE_NS::vector<GLuint> queries(device.GetCommandBufferingCount() + 1);
31     glGenQueries(static_cast<GLsizei>(queries.size()), queries.data());
32     plats_.reserve(queries.size());
33     for (const auto q : queries) {
34         plats_.emplace_back().queryObject = q;
35     }
36 }
37 
~GpuQueryGLES()38 GpuQueryGLES::~GpuQueryGLES()
39 {
40     for (const auto& plat : plats_) {
41         glDeleteQueries(1, &plat.queryObject);
42     }
43 }
44 
NextQueryIndex()45 void GpuQueryGLES::NextQueryIndex()
46 {
47     queryIndex_ = (queryIndex_ + 1) % (static_cast<uint32_t>(plats_.size()));
48 }
49 
GetDesc() const50 const GpuQueryDesc& GpuQueryGLES::GetDesc() const
51 {
52     return desc_;
53 }
54 
GetPlatformData() const55 const GpuQueryPlatformData& GpuQueryGLES::GetPlatformData() const
56 {
57     PLUGIN_ASSERT(queryIndex_ < plats_.size());
58     return plats_[queryIndex_];
59 }
60 
CreateGpuQueryGLES(Device & device,const GpuQueryDesc & desc)61 BASE_NS::unique_ptr<GpuQuery> CreateGpuQueryGLES(Device& device, const GpuQueryDesc& desc)
62 {
63     return BASE_NS::make_unique<GpuQueryGLES>(device, desc);
64 }
65 RENDER_END_NAMESPACE()
66