62563e12b02bda526081bf82bf8634b0c9902743
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles / egl-graphics.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-graphics.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
23 #include <dali/internal/system/common/configuration-manager.h>
24 #include <dali/internal/system/common/environment-options.h>
25 #include <dali/internal/window-system/common/display-utils.h> // For Utils::MakeUnique
26
27 namespace Dali
28 {
29 namespace Internal
30 {
31 namespace Adaptor
32 {
33 EglGraphics::EglGraphics(EnvironmentOptions& environmentOptions)
34 : mMultiSamplingLevel(0)
35 {
36   mDepthBufferRequired   = static_cast<Integration::DepthBufferAvailable>(environmentOptions.DepthBufferRequired());
37   mStencilBufferRequired = static_cast<Integration::StencilBufferAvailable>(environmentOptions.StencilBufferRequired());
38   mPartialUpdateRequired = static_cast<Integration::PartialUpdateAvailable>(environmentOptions.PartialUpdateRequired());
39   mMultiSamplingLevel    = environmentOptions.GetMultiSamplingLevel();
40
41   if(environmentOptions.GetGlesCallTime() > 0)
42   {
43     mGLES = Utils::MakeUnique<GlProxyImplementation>(environmentOptions);
44   }
45   else
46   {
47     mGLES.reset(new GlImplementation());
48   }
49
50   mGraphicsController.InitializeGLES(*mGLES.get());
51 }
52
53 EglGraphics::~EglGraphics()
54 {
55 }
56
57 void EglGraphics::SetGlesVersion(const int32_t glesVersion)
58 {
59   if(mEglImplementation)
60   {
61     mEglImplementation->SetGlesVersion(glesVersion);
62   }
63
64   mGLES->SetGlesVersion(glesVersion);
65
66   mGraphicsController.SetGLESVersion(static_cast<Graphics::GLES::GLESVersion>(glesVersion));
67 }
68
69 void EglGraphics::SetIsSurfacelessContextSupported(const bool isSupported)
70 {
71   mGLES->SetIsSurfacelessContextSupported(isSupported);
72 }
73
74 void EglGraphics::ActivateResourceContext()
75 {
76   if(mEglImplementation && mEglImplementation->IsSurfacelessContextSupported())
77   {
78     // Make the shared surfaceless context as current before rendering
79     mEglImplementation->MakeContextCurrent(EGL_NO_SURFACE, mEglImplementation->GetContext());
80   }
81
82   mGraphicsController.ActivateResourceContext();
83 }
84
85 void EglGraphics::ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface)
86 {
87   if(surface)
88   {
89     surface->InitializeGraphics();
90     surface->MakeContextCurrent();
91   }
92
93   mGraphicsController.ActivateSurfaceContext(surface);
94 }
95
96 void EglGraphics::PostRender()
97 {
98   ActivateResourceContext();
99
100   if(mGraphicsController.GetCurrentContext())
101   {
102     mGraphicsController.GetCurrentContext()->InvalidateDepthStencilBuffers();
103   }
104 }
105
106 void EglGraphics::SetFirstFrameAfterResume()
107 {
108   if(mEglImplementation)
109   {
110     mEglImplementation->SetFirstFrameAfterResume();
111   }
112 }
113
114 void EglGraphics::Initialize()
115 {
116   EglInitialize();
117
118   // Sync and context helper require EGL to be initialized first (can't execute in the constructor)
119   mGraphicsController.Initialize(*mEglSync.get(), *mEglContextHelper.get(), *this);
120 }
121
122 void EglGraphics::Initialize(bool depth, bool stencil, bool partialRendering, int msaa)
123 {
124   mDepthBufferRequired   = static_cast<Integration::DepthBufferAvailable>(depth);
125   mStencilBufferRequired = static_cast<Integration::StencilBufferAvailable>(stencil);
126   mPartialUpdateRequired = static_cast<Integration::PartialUpdateAvailable>(partialRendering);
127   mMultiSamplingLevel    = msaa;
128
129   EglInitialize();
130 }
131
132 void EglGraphics::EglInitialize()
133 {
134   mEglSync            = Utils::MakeUnique<EglSyncImplementation>();
135   mEglContextHelper   = Utils::MakeUnique<EglContextHelperImplementation>();
136   mEglImplementation  = Utils::MakeUnique<EglImplementation>(mMultiSamplingLevel, mDepthBufferRequired, mStencilBufferRequired, mPartialUpdateRequired);
137   mEglImageExtensions = Utils::MakeUnique<EglImageExtensions>(mEglImplementation.get());
138
139   mEglSync->Initialize(mEglImplementation.get());          // The sync impl needs the EglDisplay
140   mEglContextHelper->Initialize(mEglImplementation.get()); // The context helper impl needs the EglContext
141 }
142
143 void EglGraphics::ConfigureSurface(Dali::RenderSurfaceInterface* surface)
144 {
145   DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
146
147   // Try to use OpenGL es 3.0
148   // ChooseConfig returns false here when the device only support gles 2.0.
149   // Because eglChooseConfig with gles 3.0 setting fails when the device only support gles 2.0 and Our default setting is gles 3.0.
150   if(!mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32))
151   {
152     // Retry to use OpenGL es 2.0
153     SetGlesVersion(20);
154     mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32);
155   }
156
157   // Check whether surfaceless context is supported
158   bool isSurfacelessContextSupported = mEglImplementation->IsSurfacelessContextSupported();
159   SetIsSurfacelessContextSupported(isSurfacelessContextSupported);
160
161   RenderSurfaceInterface* currentSurface = nullptr;
162   if(isSurfacelessContextSupported)
163   {
164     // Create a surfaceless OpenGL context for shared resources
165     mEglImplementation->CreateContext();
166     ActivateResourceContext();
167   }
168   else
169   {
170     currentSurface = surface;
171     if(currentSurface)
172     {
173       ActivateSurfaceContext(currentSurface);
174     }
175   }
176
177   mGLES->ContextCreated();
178   SetGlesVersion(mGLES->GetGlesVersion());
179 }
180
181 void EglGraphics::Shutdown()
182 {
183   if(mEglImplementation)
184   {
185     // Shutdown controller
186     mGraphicsController.Shutdown();
187
188     // Terminate GLES
189     mEglImplementation->TerminateGles();
190   }
191 }
192
193 void EglGraphics::Destroy()
194 {
195   mGraphicsController.Destroy();
196 }
197
198 GlImplementation& EglGraphics::GetGlesInterface()
199 {
200   return *mGLES;
201 }
202
203 Integration::GlAbstraction& EglGraphics::GetGlAbstraction() const
204 {
205   DALI_ASSERT_DEBUG(mGLES && "GLImplementation not created");
206   return *mGLES;
207 }
208
209 EglImplementation& EglGraphics::GetEglImplementation() const
210 {
211   DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
212   return *mEglImplementation;
213 }
214
215 EglInterface& EglGraphics::GetEglInterface() const
216 {
217   DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
218   EglInterface* eglInterface = mEglImplementation.get();
219   return *eglInterface;
220 }
221
222 EglSyncImplementation& EglGraphics::GetSyncImplementation()
223 {
224   DALI_ASSERT_DEBUG(mEglSync && "EglSyncImplementation not created");
225   return *mEglSync;
226 }
227
228 EglContextHelperImplementation& EglGraphics::GetContextHelperImplementation()
229 {
230   DALI_ASSERT_DEBUG(mEglContextHelper && "EglContextHelperImplementation not created");
231   return *mEglContextHelper;
232 }
233
234 EglImageExtensions* EglGraphics::GetImageExtensions()
235 {
236   DALI_ASSERT_DEBUG(mEglImageExtensions && "EglImageExtensions not created");
237   return mEglImageExtensions.get();
238 }
239
240 Graphics::Controller& EglGraphics::GetController()
241 {
242   return mGraphicsController;
243 }
244
245 void EglGraphics::CacheConfigurations(ConfigurationManager& configurationManager)
246 {
247   mGLES->SetIsAdvancedBlendEquationSupported(configurationManager.IsAdvancedBlendEquationSupported());
248   mGLES->SetShadingLanguageVersion(configurationManager.GetShadingLanguageVersion());
249 }
250
251 } // namespace Adaptor
252 } // namespace Internal
253 } // namespace Dali