2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/graphics/gles/egl-graphics.h>
22 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/internal/system/common/configuration-manager.h>
25 #include <dali/internal/system/common/environment-options.h>
26 #include <dali/internal/window-system/common/display-utils.h> // For Utils::MakeUnique
34 EglGraphics::EglGraphics(EnvironmentOptions& environmentOptions)
35 : mMultiSamplingLevel(0)
37 mDepthBufferRequired = static_cast<Integration::DepthBufferAvailable>(environmentOptions.DepthBufferRequired());
38 mStencilBufferRequired = static_cast<Integration::StencilBufferAvailable>(environmentOptions.StencilBufferRequired());
39 mPartialUpdateRequired = static_cast<Integration::PartialUpdateAvailable>(environmentOptions.PartialUpdateRequired());
40 mMultiSamplingLevel = environmentOptions.GetMultiSamplingLevel();
42 if(environmentOptions.GetGlesCallTime() > 0)
44 mGLES = Utils::MakeUnique<GlProxyImplementation>(environmentOptions);
48 mGLES.reset(new GlImplementation());
51 mGraphicsController.InitializeGLES(*mGLES.get());
54 EglGraphics::~EglGraphics()
58 void EglGraphics::SetGlesVersion(const int32_t glesVersion)
60 if(mEglImplementation)
62 mEglImplementation->SetGlesVersion(glesVersion);
65 mGLES->SetGlesVersion(glesVersion);
67 mGraphicsController.SetGLESVersion(static_cast<Graphics::GLES::GLESVersion>(glesVersion));
70 void EglGraphics::SetIsSurfacelessContextSupported(const bool isSupported)
72 mGLES->SetIsSurfacelessContextSupported(isSupported);
75 void EglGraphics::ActivateResourceContext()
77 if(mEglImplementation && mEglImplementation->IsSurfacelessContextSupported())
79 // Make the shared surfaceless context as current before rendering
80 mEglImplementation->MakeContextCurrent(EGL_NO_SURFACE, mEglImplementation->GetContext());
83 mGraphicsController.ActivateResourceContext();
86 void EglGraphics::ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface)
90 surface->InitializeGraphics();
91 surface->MakeContextCurrent();
94 mGraphicsController.ActivateSurfaceContext(surface);
97 void EglGraphics::PostRender()
99 ActivateResourceContext();
101 if(mGraphicsController.GetCurrentContext())
103 mGraphicsController.GetCurrentContext()->InvalidateDepthStencilBuffers();
106 mGraphicsController.PostRender();
109 void EglGraphics::SetFirstFrameAfterResume()
111 if(mEglImplementation)
113 mEglImplementation->SetFirstFrameAfterResume();
117 void EglGraphics::Initialize()
121 // Sync and context helper require EGL to be initialized first (can't execute in the constructor)
122 mGraphicsController.Initialize(*mEglSync.get(), *mEglContextHelper.get(), *this);
125 void EglGraphics::Initialize(bool depth, bool stencil, bool partialRendering, int msaa)
127 mDepthBufferRequired = static_cast<Integration::DepthBufferAvailable>(depth);
128 mStencilBufferRequired = static_cast<Integration::StencilBufferAvailable>(stencil);
129 mPartialUpdateRequired = static_cast<Integration::PartialUpdateAvailable>(partialRendering);
130 mMultiSamplingLevel = msaa;
135 void EglGraphics::EglInitialize()
137 mEglSync = Utils::MakeUnique<EglSyncImplementation>();
138 mEglContextHelper = Utils::MakeUnique<EglContextHelperImplementation>();
139 mEglImplementation = Utils::MakeUnique<EglImplementation>(mMultiSamplingLevel, mDepthBufferRequired, mStencilBufferRequired, mPartialUpdateRequired);
140 mEglImageExtensions = Utils::MakeUnique<EglImageExtensions>(mEglImplementation.get());
142 mEglSync->Initialize(mEglImplementation.get()); // The sync impl needs the EglDisplay
143 mEglContextHelper->Initialize(mEglImplementation.get()); // The context helper impl needs the EglContext
146 void EglGraphics::ConfigureSurface(Dali::RenderSurfaceInterface* surface)
148 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
150 // Try to use OpenGL es 3.0
151 // ChooseConfig returns false here when the device only support gles 2.0.
152 // Because eglChooseConfig with gles 3.0 setting fails when the device only support gles 2.0 and Our default setting is gles 3.0.
153 if(!mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32))
155 // Retry to use OpenGL es 2.0
157 mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32);
160 // Check whether surfaceless context is supported
161 bool isSurfacelessContextSupported = mEglImplementation->IsSurfacelessContextSupported();
162 SetIsSurfacelessContextSupported(isSurfacelessContextSupported);
164 RenderSurfaceInterface* currentSurface = nullptr;
165 if(isSurfacelessContextSupported)
167 // Create a surfaceless OpenGL context for shared resources
168 mEglImplementation->CreateContext();
169 ActivateResourceContext();
173 currentSurface = surface;
176 ActivateSurfaceContext(currentSurface);
180 mGLES->ContextCreated();
181 SetGlesVersion(mGLES->GetGlesVersion());
184 void EglGraphics::Shutdown()
186 if(mEglImplementation)
188 // Shutdown controller
189 mGraphicsController.Shutdown();
192 mEglImplementation->TerminateGles();
196 void EglGraphics::Destroy()
198 mGraphicsController.Destroy();
201 GlImplementation& EglGraphics::GetGlesInterface()
206 Integration::GlAbstraction& EglGraphics::GetGlAbstraction() const
208 DALI_ASSERT_DEBUG(mGLES && "GLImplementation not created");
212 EglImplementation& EglGraphics::GetEglImplementation() const
214 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
215 return *mEglImplementation;
218 EglInterface& EglGraphics::GetEglInterface() const
220 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
221 EglInterface* eglInterface = mEglImplementation.get();
222 return *eglInterface;
225 EglSyncImplementation& EglGraphics::GetSyncImplementation()
227 DALI_ASSERT_DEBUG(mEglSync && "EglSyncImplementation not created");
231 EglContextHelperImplementation& EglGraphics::GetContextHelperImplementation()
233 DALI_ASSERT_DEBUG(mEglContextHelper && "EglContextHelperImplementation not created");
234 return *mEglContextHelper;
237 EglImageExtensions* EglGraphics::GetImageExtensions()
239 DALI_ASSERT_DEBUG(mEglImageExtensions && "EglImageExtensions not created");
240 return mEglImageExtensions.get();
243 Graphics::Controller& EglGraphics::GetController()
245 return mGraphicsController;
248 void EglGraphics::CacheConfigurations(ConfigurationManager& configurationManager)
250 mGLES->SetIsAdvancedBlendEquationSupported(configurationManager.IsAdvancedBlendEquationSupported());
251 mGLES->SetIsMultisampledRenderToTextureSupported(configurationManager.IsMultisampledRenderToTextureSupported());
252 mGLES->SetShadingLanguageVersion(configurationManager.GetShadingLanguageVersion());
255 void EglGraphics::FrameStart()
257 mGraphicsController.FrameStart();
260 void EglGraphics::LogMemoryPools()
262 std::size_t graphicsCapacity = mGraphicsController.GetCapacity();
263 DALI_LOG_RELEASE_INFO(
265 " GraphicsController Capacity: %lu\n",
269 } // namespace Adaptor
270 } // namespace Internal