2 * Copyright (c) 2022 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>
26 namespace Dali::Graphics::GLES
28 AgingSyncObject::AgingSyncObject(Graphics::EglGraphicsController& controller, const Context* writeContext)
29 : controller(controller),
30 writeContext(writeContext)
32 auto gl = controller.GetGL();
35 glSyncObject = gl->FenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
39 AgingSyncObject::~AgingSyncObject()
41 auto gl = controller.GetGL();
42 if(gl && glSyncObject != nullptr)
44 gl->DeleteSync(glSyncObject);
48 SyncPool::~SyncPool() = default;
50 AgingSyncObject* SyncPool::AllocateSyncObject(const Context* writeContext)
52 std::unique_ptr<AgingSyncObject> syncObject = std::make_unique<AgingSyncObject>(mController, writeContext);
53 mSyncObjects.push_back(std::move(syncObject));
54 return mSyncObjects.back().get();
57 void SyncPool::Wait(AgingSyncObject* syncPoolObject)
59 auto gl = mController.GetGL();
60 if(gl && syncPoolObject->glSyncObject != nullptr)
62 syncPoolObject->syncing = true;
63 gl->WaitSync(syncPoolObject->glSyncObject, 0, GL_TIMEOUT_IGNORED);
67 void SyncPool::FreeSyncObject(AgingSyncObject* agingSyncObject)
69 auto iter = std::find_if(mSyncObjects.begin(), mSyncObjects.end(), [&agingSyncObject](AgingSyncPtrRef agingSyncPtr) { return agingSyncPtr.get() == agingSyncObject; });
70 if(iter != mSyncObjects.end())
77 * Age sync objects. Call at the end of each frame.
78 * When a sync object is older than 2 frames, delete it.
80 void SyncPool::AgeSyncObjects()
82 if(!mSyncObjects.empty())
84 // Age the remaining sync objects.
85 for(auto& agingSyncObject : mSyncObjects)
87 if(agingSyncObject != nullptr && agingSyncObject->glSyncObject != 0)
89 if(agingSyncObject->age > 0)
91 agingSyncObject->age--;
95 agingSyncObject.reset();
100 // Move any old sync objects to the end of the list, and then remove them all.
101 mSyncObjects.erase(std::remove_if(mSyncObjects.begin(), mSyncObjects.end(), [&](std::unique_ptr<AgingSyncObject>& agingSyncObject) { return agingSyncObject == nullptr; }),
105 } // namespace Dali::Graphics::GLES