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