2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali/internal/graphics/gles-impl/gles-sync-pool.h>
21 #include <dali/graphics-api/graphics-sync-object-create-info.h>
24 #include <dali/internal/graphics/gles-impl/egl-graphics-controller.h>
25 #include <dali/internal/graphics/gles/egl-sync-implementation.h>
27 #if defined(DEBUG_ENABLED)
28 extern Debug::Filter* gLogSyncFilter;
31 namespace Dali::Graphics::GLES
33 AgingSyncObject::AgingSyncObject(Graphics::EglGraphicsController& controller, const Context* writeContext, bool _egl)
34 : controller(controller),
35 writeContext(writeContext),
40 eglSyncObject = static_cast<Internal::Adaptor::EglSyncObject*>(controller.GetEglSyncImplementation().CreateSyncObject());
41 DALI_LOG_INFO(gLogSyncFilter, Debug::Verbose, "AgingSyncObject::cons; EGL::CreateSyncObject: %p\n", eglSyncObject);
45 auto gl = controller.GetGL();
48 glSyncObject = gl->FenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
53 AgingSyncObject::~AgingSyncObject()
55 if(!controller.IsShuttingDown())
59 DALI_LOG_INFO(gLogSyncFilter, Debug::Verbose, "AgingSyncObject::dstr; EGL::DestroySyncObject: %p\n", eglSyncObject);
60 controller.GetEglSyncImplementation().DestroySyncObject(eglSyncObject);
64 auto gl = controller.GetGL();
65 if(gl && glSyncObject != nullptr)
67 gl->DeleteSync(glSyncObject);
73 bool AgingSyncObject::ClientWait()
80 DALI_LOG_INFO(gLogSyncFilter, Debug::Verbose, "AgingSyncObject::ClientWait(); EGL::ClientWaitSync\n");
81 eglSyncObject->ClientWait();
89 DALI_LOG_INFO(gLogSyncFilter, Debug::Verbose, "AgingSyncObject::ClientWait(); glClientWaitSync 1ms\n");
90 const GLuint64 TIMEOUT = 1000000; //1ms!
91 auto gl = controller.GetGL();
92 GLenum result = gl->ClientWaitSync(glSyncObject, GL_SYNC_FLUSH_COMMANDS_BIT, TIMEOUT);
94 synced = (result == GL_ALREADY_SIGNALED || result == GL_CONDITION_SATISFIED);
97 DALI_LOG_INFO(gLogSyncFilter, Debug::Verbose, "AgingSyncObject::ClientWait(); Result: %s\n", synced ? "Synced" : "NOT SYNCED");
101 void AgingSyncObject::Wait()
107 DALI_LOG_INFO(gLogSyncFilter, Debug::Verbose, "AgingSyncObject::Wait(); EGL::WaitSync\n");
108 eglSyncObject->Wait();
115 DALI_LOG_INFO(gLogSyncFilter, Debug::Verbose, "AgingSyncObject::Wait(); glWaitSync\n");
116 auto gl = controller.GetGL();
117 gl->WaitSync(glSyncObject, 0, 0ull);
122 SyncPool::~SyncPool() = default;
124 AgingSyncObject* SyncPool::AllocateSyncObject(const Context* writeContext, SyncPool::SyncContext syncContext)
126 std::unique_ptr<AgingSyncObject> syncObject = std::make_unique<AgingSyncObject>(mController, writeContext, (syncContext == SyncContext::EGL));
127 mSyncObjects.push_back(std::move(syncObject));
128 return mSyncObjects.back().get();
131 void SyncPool::Wait(AgingSyncObject* syncPoolObject)
133 if(syncPoolObject != nullptr)
135 syncPoolObject->syncing = true;
136 syncPoolObject->Wait();
140 bool SyncPool::ClientWait(AgingSyncObject* syncPoolObject)
144 return syncPoolObject->ClientWait();
149 void SyncPool::FreeSyncObject(AgingSyncObject* agingSyncObject)
151 auto iter = std::find_if(mSyncObjects.begin(), mSyncObjects.end(), [&agingSyncObject](AgingSyncPtrRef agingSyncPtr) { return agingSyncPtr.get() == agingSyncObject; });
152 if(iter != mSyncObjects.end())
159 * Age sync objects. Call at the end of each frame.
160 * When a sync object is older than 2 frames, delete it.
162 void SyncPool::AgeSyncObjects()
164 DALI_LOG_INFO(gLogSyncFilter, Debug::Verbose, "AgeSyncObjects: count: %d\n", mSyncObjects.size());
166 if(!mSyncObjects.empty())
168 // Age the remaining sync objects.
169 for(auto& agingSyncObject : mSyncObjects)
171 if(agingSyncObject != nullptr && (agingSyncObject->glSyncObject != 0 || agingSyncObject->eglSyncObject != nullptr))
173 if(agingSyncObject->age > 0)
175 agingSyncObject->age--;
179 agingSyncObject.reset();
184 // Move any old sync objects to the end of the list, and then remove them all.
185 mSyncObjects.erase(std::remove_if(mSyncObjects.begin(), mSyncObjects.end(), [&](std::unique_ptr<AgingSyncObject>& agingSyncObject) { return agingSyncObject == nullptr; }),
188 DALI_LOG_INFO(gLogSyncFilter, Debug::Verbose, "AgeSyncObjects: count after erase: %d\n", mSyncObjects.size());
191 } // namespace Dali::Graphics::GLES