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