1 /* 2 * Copyright (C) 2023 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()24RENDER_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()33RenderFrameSyncGLES::~RenderFrameSyncGLES() 34 { 35 for (auto& ref : frameFences_) { 36 GLsync fence = (GLsync)ref.aFence; 37 if (fence) { 38 glDeleteSync(fence); 39 } 40 } 41 } BeginFrame()42void 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) % (uint32_t)frameFences_.size(); 47 } 48 WaitForFrameFence()49void RenderFrameSyncGLES::WaitForFrameFence() 50 { 51 if (frameFences_[bufferingIndex_].aFence) { 52 GLsync fence = (GLsync)(frameFences_[bufferingIndex_].aFence); 53 glClientWaitSync(fence, GL_SYNC_FLUSH_COMMANDS_BIT, UINT64_MAX); 54 glDeleteSync(fence); 55 frameFences_[bufferingIndex_].aFence = nullptr; 56 } else { 57 // no-fence, no wait. 58 } 59 } 60 GetFrameFence()61const LowLevelFenceGLES& RenderFrameSyncGLES::GetFrameFence() 62 { 63 return frameFences_[bufferingIndex_]; 64 } 65 RENDER_END_NAMESPACE() 66