3f00195a9b4284f908f3f49b73bbfdd28d0bdb7d
[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::SetFirstFrameAfterResume()
97 {
98   if(mEglImplementation)
99   {
100     mEglImplementation->SetFirstFrameAfterResume();
101   }
102 }
103
104 void EglGraphics::Initialize()
105 {
106   EglInitialize();
107
108   // Sync and context helper require EGL to be initialized first (can't execute in the constructor)
109   mGraphicsController.Initialize(*mEglSync.get(), *mEglContextHelper.get(), *this);
110 }
111
112 void EglGraphics::Initialize(bool depth, bool stencil, bool partialRendering, int msaa)
113 {
114   mDepthBufferRequired   = static_cast<Integration::DepthBufferAvailable>(depth);
115   mStencilBufferRequired = static_cast<Integration::StencilBufferAvailable>(stencil);
116   mPartialUpdateRequired = static_cast<Integration::PartialUpdateAvailable>(partialRendering);
117   mMultiSamplingLevel    = msaa;
118
119   EglInitialize();
120 }
121
122 void EglGraphics::EglInitialize()
123 {
124   mEglSync            = Utils::MakeUnique<EglSyncImplementation>();
125   mEglContextHelper   = Utils::MakeUnique<EglContextHelperImplementation>();
126   mEglImplementation  = Utils::MakeUnique<EglImplementation>(mMultiSamplingLevel, mDepthBufferRequired, mStencilBufferRequired, mPartialUpdateRequired);
127   mEglImageExtensions = Utils::MakeUnique<EglImageExtensions>(mEglImplementation.get());
128
129   mEglSync->Initialize(mEglImplementation.get());          // The sync impl needs the EglDisplay
130   mEglContextHelper->Initialize(mEglImplementation.get()); // The context helper impl needs the EglContext
131 }
132
133 void EglGraphics::ConfigureSurface(Dali::RenderSurfaceInterface* surface)
134 {
135   DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
136
137   // Try to use OpenGL es 3.0
138   // ChooseConfig returns false here when the device only support gles 2.0.
139   // Because eglChooseConfig with gles 3.0 setting fails when the device only support gles 2.0 and Our default setting is gles 3.0.
140   if(!mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32))
141   {
142     // Retry to use OpenGL es 2.0
143     SetGlesVersion(20);
144     mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32);
145   }
146
147   // Check whether surfaceless context is supported
148   bool isSurfacelessContextSupported = mEglImplementation->IsSurfacelessContextSupported();
149   SetIsSurfacelessContextSupported(isSurfacelessContextSupported);
150
151   RenderSurfaceInterface* currentSurface = nullptr;
152   if(isSurfacelessContextSupported)
153   {
154     // Create a surfaceless OpenGL context for shared resources
155     mEglImplementation->CreateContext();
156     ActivateResourceContext();
157   }
158   else
159   {
160     currentSurface = surface;
161     if(currentSurface)
162     {
163       ActivateSurfaceContext(currentSurface);
164     }
165   }
166
167   mGLES->ContextCreated();
168   SetGlesVersion(mGLES->GetGlesVersion());
169 }
170
171 void EglGraphics::Shutdown()
172 {
173   if(mEglImplementation)
174   {
175     // Shutdown controller
176     mGraphicsController.Shutdown();
177
178     // Terminate GLES
179     mEglImplementation->TerminateGles();
180   }
181 }
182
183 void EglGraphics::Destroy()
184 {
185   mGraphicsController.Destroy();
186 }
187
188 GlImplementation& EglGraphics::GetGlesInterface()
189 {
190   return *mGLES;
191 }
192
193 Integration::GlAbstraction& EglGraphics::GetGlAbstraction() const
194 {
195   DALI_ASSERT_DEBUG(mGLES && "GLImplementation not created");
196   return *mGLES;
197 }
198
199 EglImplementation& EglGraphics::GetEglImplementation() const
200 {
201   DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
202   return *mEglImplementation;
203 }
204
205 EglInterface& EglGraphics::GetEglInterface() const
206 {
207   DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
208   EglInterface* eglInterface = mEglImplementation.get();
209   return *eglInterface;
210 }
211
212 EglSyncImplementation& EglGraphics::GetSyncImplementation()
213 {
214   DALI_ASSERT_DEBUG(mEglSync && "EglSyncImplementation not created");
215   return *mEglSync;
216 }
217
218 EglContextHelperImplementation& EglGraphics::GetContextHelperImplementation()
219 {
220   DALI_ASSERT_DEBUG(mEglContextHelper && "EglContextHelperImplementation not created");
221   return *mEglContextHelper;
222 }
223
224 EglImageExtensions* EglGraphics::GetImageExtensions()
225 {
226   DALI_ASSERT_DEBUG(mEglImageExtensions && "EglImageExtensions not created");
227   return mEglImageExtensions.get();
228 }
229
230 Graphics::Controller& EglGraphics::GetController()
231 {
232   return mGraphicsController;
233 }
234
235 void EglGraphics::CacheConfigurations(ConfigurationManager& configurationManager)
236 {
237   mGLES->SetIsAdvancedBlendEquationSupported(configurationManager.IsAdvancedBlendEquationSupported());
238   mGLES->SetShadingLanguageVersion(configurationManager.GetShadingLanguageVersion());
239 }
240
241 } // namespace Adaptor
242 } // namespace Internal
243 } // namespace Dali