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