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