Using custom graphics deleter in test harness
[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     mEglImplementation->TerminateGles();
162   }
163 }
164
165 void EglGraphics::Destroy()
166 {
167 }
168
169 GlImplementation& EglGraphics::GetGlesInterface()
170 {
171   return *mGLES;
172 }
173
174 Integration::GlAbstraction& EglGraphics::GetGlAbstraction() const
175 {
176   DALI_ASSERT_DEBUG(mGLES && "GLImplementation not created");
177   return *mGLES;
178 }
179
180 EglImplementation& EglGraphics::GetEglImplementation() const
181 {
182   DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
183   return *mEglImplementation;
184 }
185
186 EglInterface& EglGraphics::GetEglInterface() const
187 {
188   DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
189   EglInterface* eglInterface = mEglImplementation.get();
190   return *eglInterface;
191 }
192
193 EglSyncImplementation& EglGraphics::GetSyncImplementation()
194 {
195   DALI_ASSERT_DEBUG(mEglSync && "EglSyncImplementation not created");
196   return *mEglSync;
197 }
198
199 EglContextHelperImplementation& EglGraphics::GetContextHelperImplementation()
200 {
201   DALI_ASSERT_DEBUG(mEglContextHelper && "EglContextHelperImplementation not created");
202   return *mEglContextHelper;
203 }
204
205 EglImageExtensions* EglGraphics::GetImageExtensions()
206 {
207   DALI_ASSERT_DEBUG(mEglImageExtensions && "EglImageExtensions not created");
208   return mEglImageExtensions.get();
209 }
210
211 Graphics::Controller& EglGraphics::GetController()
212 {
213   return mGraphicsController;
214 }
215
216 void EglGraphics::CacheConfigurations(ConfigurationManager& configurationManager)
217 {
218   mGLES->SetIsAdvancedBlendEquationSupported(configurationManager.IsAdvancedBlendEquationSupported());
219   mGLES->SetShadingLanguageVersion(configurationManager.GetShadingLanguageVersion());
220 }
221
222 } // namespace Adaptor
223 } // namespace Internal
224 } // namespace Dali