2 * Copyright (c) 2023 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::SetIsSurfacelessContextSupported(const bool isSupported)
60 mGLES->SetIsSurfacelessContextSupported(isSupported);
63 void EglGraphics::ActivateResourceContext()
65 if(mEglImplementation && mEglImplementation->IsSurfacelessContextSupported())
67 // Make the shared surfaceless context as current before rendering
68 mEglImplementation->MakeContextCurrent(EGL_NO_SURFACE, mEglImplementation->GetContext());
71 mGraphicsController.ActivateResourceContext();
74 void EglGraphics::ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface)
78 surface->InitializeGraphics();
79 surface->MakeContextCurrent();
82 mGraphicsController.ActivateSurfaceContext(surface);
85 void EglGraphics::PostRender()
87 ActivateResourceContext();
89 if(mGraphicsController.GetCurrentContext())
91 mGraphicsController.GetCurrentContext()->InvalidateDepthStencilBuffers();
94 mGraphicsController.PostRender();
97 void EglGraphics::SetFirstFrameAfterResume()
99 if(mEglImplementation)
101 mEglImplementation->SetFirstFrameAfterResume();
105 void EglGraphics::Initialize()
109 // Sync and context helper require EGL to be initialized first (can't execute in the constructor)
110 mGraphicsController.Initialize(*mEglSync.get(), *mEglContextHelper.get(), *this);
113 void EglGraphics::Initialize(bool depth, bool stencil, bool partialRendering, int msaa)
115 mDepthBufferRequired = static_cast<Integration::DepthBufferAvailable>(depth);
116 mStencilBufferRequired = static_cast<Integration::StencilBufferAvailable>(stencil);
117 mPartialUpdateRequired = static_cast<Integration::PartialUpdateAvailable>(partialRendering);
118 mMultiSamplingLevel = msaa;
123 void EglGraphics::EglInitialize()
125 mEglSync = Utils::MakeUnique<EglSyncImplementation>();
126 mEglContextHelper = Utils::MakeUnique<EglContextHelperImplementation>();
127 mEglImplementation = Utils::MakeUnique<EglImplementation>(mMultiSamplingLevel, mDepthBufferRequired, mStencilBufferRequired, mPartialUpdateRequired);
128 mEglImageExtensions = Utils::MakeUnique<EglImageExtensions>(mEglImplementation.get());
130 mEglSync->Initialize(mEglImplementation.get()); // The sync impl needs the EglDisplay
131 mEglContextHelper->Initialize(mEglImplementation.get()); // The context helper impl needs the EglContext
134 void EglGraphics::ConfigureSurface(Dali::RenderSurfaceInterface* surface)
136 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
138 // Try to use OpenGL es 3.0
139 // ChooseConfig returns false here when the device only support gles 2.0.
140 // Because eglChooseConfig with gles 3.0 setting fails when the device only support gles 2.0 and Our default setting is gles 3.0.
141 if(!mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32))
143 // Retry to use OpenGL es 2.0
144 mEglImplementation->SetGlesVersion(20);
146 // Mark gles that we will use gles 2.0 version.
147 // After this call, we will not change mGLES version anymore.
148 mGLES->SetGlesVersion(20);
150 mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32);
153 // Check whether surfaceless context is supported
154 bool isSurfacelessContextSupported = mEglImplementation->IsSurfacelessContextSupported();
155 SetIsSurfacelessContextSupported(isSurfacelessContextSupported);
157 RenderSurfaceInterface* currentSurface = nullptr;
158 if(isSurfacelessContextSupported)
160 // Create a surfaceless OpenGL context for shared resources
161 mEglImplementation->CreateContext();
162 ActivateResourceContext();
166 currentSurface = surface;
169 ActivateSurfaceContext(currentSurface);
173 mGLES->ContextCreated(); // After this call, we can know exact gles version.
174 auto glesVersion = mGLES->GetGlesVersion();
176 // Set more detail GLES version to egl and graphics controller.
177 // Note. usually we don't need EGL client's minor version. So don't need to choose config one more time.
178 mEglImplementation->SetGlesVersion(glesVersion);
179 mGraphicsController.SetGLESVersion(static_cast<Graphics::GLES::GLESVersion>(glesVersion));
182 void EglGraphics::Shutdown()
184 if(mEglImplementation)
186 // Shutdown controller
187 mGraphicsController.Shutdown();
190 mEglImplementation->TerminateGles();
194 void EglGraphics::Destroy()
196 mGraphicsController.Destroy();
199 GlImplementation& EglGraphics::GetGlesInterface()
204 Integration::GlAbstraction& EglGraphics::GetGlAbstraction() const
206 DALI_ASSERT_DEBUG(mGLES && "GLImplementation not created");
210 EglImplementation& EglGraphics::GetEglImplementation() const
212 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
213 return *mEglImplementation;
216 EglInterface& EglGraphics::GetEglInterface() const
218 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
219 EglInterface* eglInterface = mEglImplementation.get();
220 return *eglInterface;
223 EglSyncImplementation& EglGraphics::GetSyncImplementation()
225 DALI_ASSERT_DEBUG(mEglSync && "EglSyncImplementation not created");
229 EglContextHelperImplementation& EglGraphics::GetContextHelperImplementation()
231 DALI_ASSERT_DEBUG(mEglContextHelper && "EglContextHelperImplementation not created");
232 return *mEglContextHelper;
235 EglImageExtensions* EglGraphics::GetImageExtensions()
237 DALI_ASSERT_DEBUG(mEglImageExtensions && "EglImageExtensions not created");
238 return mEglImageExtensions.get();
241 Graphics::Controller& EglGraphics::GetController()
243 return mGraphicsController;
246 void EglGraphics::CacheConfigurations(ConfigurationManager& configurationManager)
248 mGLES->SetIsAdvancedBlendEquationSupported(configurationManager.IsAdvancedBlendEquationSupported());
249 mGLES->SetIsMultisampledRenderToTextureSupported(configurationManager.IsMultisampledRenderToTextureSupported());
250 mGLES->SetShadingLanguageVersion(configurationManager.GetShadingLanguageVersion());
253 void EglGraphics::FrameStart()
255 mGraphicsController.FrameStart();
258 void EglGraphics::LogMemoryPools()
260 std::size_t graphicsCapacity = mGraphicsController.GetCapacity();
261 DALI_LOG_RELEASE_INFO(
263 " GraphicsController Capacity: %lu\n",
267 } // namespace Adaptor
268 } // namespace Internal