[dali_2.0.42] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles / egl-sync-implementation.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/graphics/gles/egl-sync-implementation.h>
20
21 // EXTERNAL INCLUDES
22
23 #ifdef _ARCH_ARM_
24
25 #include <EGL/eglext.h>
26 #include <GLES2/gl2.h>
27 #include <GLES2/gl2ext.h>
28
29 #endif
30
31 #include <dali/integration-api/debug.h>
32
33 // INTERNAL INCLUDES
34 #include <dali/internal/graphics/gles/egl-implementation.h>
35
36 #ifdef _ARCH_ARM_
37
38 // function pointers
39 static PFNEGLCREATESYNCKHRPROC     eglCreateSyncKHR     = NULL;
40 static PFNEGLCLIENTWAITSYNCKHRPROC eglClientWaitSyncKHR = NULL;
41 static PFNEGLDESTROYSYNCKHRPROC    eglDestroySyncKHR    = NULL;
42
43 #endif
44
45 namespace Dali
46 {
47 namespace Internal
48 {
49 namespace Adaptor
50 {
51 #ifdef _ARCH_ARM_
52
53 EglSyncObject::EglSyncObject(EglImplementation& eglSyncImpl)
54 : mEglSync(NULL),
55   mEglImplementation(eglSyncImpl)
56 {
57   EGLDisplay display = mEglImplementation.GetDisplay();
58   mEglSync           = eglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, NULL);
59   if(mEglSync == EGL_NO_SYNC_KHR)
60   {
61     DALI_LOG_ERROR("eglCreateSyncKHR failed %#0.4x\n", eglGetError());
62     mEglSync = NULL;
63   }
64 }
65
66 EglSyncObject::~EglSyncObject()
67 {
68   if(mEglSync != NULL)
69   {
70     eglDestroySyncKHR(mEglImplementation.GetDisplay(), mEglSync);
71     EGLint error = eglGetError();
72     if(EGL_SUCCESS != error)
73     {
74       DALI_LOG_ERROR("eglDestroySyncKHR failed %#0.4x\n", error);
75     }
76   }
77 }
78
79 bool EglSyncObject::IsSynced()
80 {
81   bool synced = false;
82
83   if(mEglSync != NULL)
84   {
85     EGLint result = eglClientWaitSyncKHR(mEglImplementation.GetDisplay(), mEglSync, 0, 0ull);
86     EGLint error  = eglGetError();
87     if(EGL_SUCCESS != error)
88     {
89       DALI_LOG_ERROR("eglClientWaitSyncKHR failed %#0.4x\n", error);
90     }
91     else if(result == EGL_CONDITION_SATISFIED_KHR)
92     {
93       synced = true;
94     }
95   }
96
97   return synced;
98 }
99
100 EglSyncImplementation::EglSyncImplementation()
101 : mEglImplementation(NULL),
102   mSyncInitialized(false),
103   mSyncInitializeFailed(false)
104 {
105 }
106
107 EglSyncImplementation::~EglSyncImplementation()
108 {
109 }
110
111 void EglSyncImplementation::Initialize(EglImplementation* eglImpl)
112 {
113   mEglImplementation = eglImpl;
114 }
115
116 Integration::GraphicsSyncAbstraction::SyncObject* EglSyncImplementation::CreateSyncObject()
117 {
118   DALI_ASSERT_ALWAYS(mEglImplementation && "Sync Implementation not initialized");
119   if(mSyncInitialized == false)
120   {
121     InitializeEglSync();
122   }
123
124   auto* syncObject = new EglSyncObject(*mEglImplementation);
125   mSyncObjects.PushBack(syncObject);
126   return syncObject;
127 }
128
129 void EglSyncImplementation::DestroySyncObject(Integration::GraphicsSyncAbstraction::SyncObject* syncObject)
130 {
131   DALI_ASSERT_ALWAYS(mEglImplementation && "Sync Implementation not initialized");
132
133   if(mSyncInitialized == false)
134   {
135     InitializeEglSync();
136   }
137
138   for(SyncIter iter = mSyncObjects.Begin(), end = mSyncObjects.End(); iter != end; ++iter)
139   {
140     if(*iter == syncObject)
141     {
142       mSyncObjects.Erase(iter);
143       break;
144     }
145   }
146   delete static_cast<EglSyncObject*>(syncObject);
147 }
148
149 void EglSyncImplementation::InitializeEglSync()
150 {
151   if(!mSyncInitializeFailed)
152   {
153     eglCreateSyncKHR     = reinterpret_cast<PFNEGLCREATESYNCKHRPROC>(eglGetProcAddress("eglCreateSyncKHR"));
154     eglClientWaitSyncKHR = reinterpret_cast<PFNEGLCLIENTWAITSYNCKHRPROC>(eglGetProcAddress("eglClientWaitSyncKHR"));
155     eglDestroySyncKHR    = reinterpret_cast<PFNEGLDESTROYSYNCKHRPROC>(eglGetProcAddress("eglDestroySyncKHR"));
156   }
157
158   if(eglCreateSyncKHR && eglClientWaitSyncKHR && eglDestroySyncKHR)
159   {
160     mSyncInitialized = true;
161   }
162   else
163   {
164     mSyncInitializeFailed = true;
165   }
166 }
167
168 #else
169
170 EglSyncObject::EglSyncObject(EglImplementation& eglImpl)
171 : mPollCounter(3),
172   mEglImplementation(eglImpl)
173 {
174 }
175
176 EglSyncObject::~EglSyncObject()
177 {
178 }
179
180 bool EglSyncObject::IsSynced()
181 {
182   if(mPollCounter <= 0)
183   {
184     return true;
185   }
186   --mPollCounter;
187   return false;
188 }
189
190 EglSyncImplementation::EglSyncImplementation()
191 : mEglImplementation(NULL),
192   mSyncInitialized(false),
193   mSyncInitializeFailed(false)
194 {
195 }
196
197 EglSyncImplementation::~EglSyncImplementation()
198 {
199 }
200
201 void EglSyncImplementation::Initialize(EglImplementation* eglImpl)
202 {
203   mEglImplementation = eglImpl;
204 }
205
206 Integration::GraphicsSyncAbstraction::SyncObject* EglSyncImplementation::CreateSyncObject()
207 {
208   DALI_ASSERT_ALWAYS(mEglImplementation && "Sync Implementation not initialized");
209   return new EglSyncObject(*mEglImplementation);
210 }
211
212 void EglSyncImplementation::DestroySyncObject(Integration::GraphicsSyncAbstraction::SyncObject* syncObject)
213 {
214   DALI_ASSERT_ALWAYS(mEglImplementation && "Sync Implementation not initialized");
215   delete static_cast<EglSyncObject*>(syncObject);
216 }
217
218 void EglSyncImplementation::InitializeEglSync()
219 {
220 }
221
222 #endif
223
224 } // namespace Adaptor
225 } // namespace Internal
226 } // namespace Dali