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 "render_frame_sync_gles.h"
17
18 #include <render/namespace.h>
19
20 #include "gles/device_gles.h"
21 #include "gles/gl_functions.h"
22 #include "util/log.h"
23
RENDER_BEGIN_NAMESPACE()24 RENDER_BEGIN_NAMESPACE()
25 RenderFrameSyncGLES::RenderFrameSyncGLES(Device& device) : RenderFrameSync()
26 {
27 frameFences_.resize(device.GetCommandBufferingCount());
28 for (auto& ref : frameFences_) {
29 ref.aFence = nullptr;
30 }
31 }
32
~RenderFrameSyncGLES()33 RenderFrameSyncGLES::~RenderFrameSyncGLES()
34 {
35 for (auto& ref : frameFences_) {
36 GLsync fence = (GLsync)ref.aFence;
37 if (fence) {
38 glDeleteSync(fence);
39 }
40 }
41 }
BeginFrame()42 void RenderFrameSyncGLES::BeginFrame()
43 {
44 PLUGIN_ASSERT(frameFences_[bufferingIndex_].aFence == nullptr);
45 frameFences_[bufferingIndex_].aFence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
46 bufferingIndex_ = (bufferingIndex_ + 1) % static_cast<uint32_t>(frameFences_.size());
47 }
48
WaitForFrameFence()49 void RenderFrameSyncGLES::WaitForFrameFence()
50 {
51 if (frameFences_[bufferingIndex_].aFence) {
52 GLsync fence = (GLsync)(frameFences_[bufferingIndex_].aFence);
53 const GLenum result = glClientWaitSync(fence, GL_SYNC_FLUSH_COMMANDS_BIT, UINT64_MAX);
54 if ((result != GL_ALREADY_SIGNALED) && (result != GL_CONDITION_SATISFIED)) {
55 PLUGIN_LOG_E("glClientWaitSync returned %x", result);
56 }
57 glDeleteSync(fence);
58 frameFences_[bufferingIndex_].aFence = nullptr;
59 } else {
60 // no-fence, no wait.
61 }
62 }
63
GetFrameFence()64 const LowLevelFenceGLES& RenderFrameSyncGLES::GetFrameFence()
65 {
66 return frameFences_[bufferingIndex_];
67 }
68 RENDER_END_NAMESPACE()
69