2 * Copyright (c) 2024 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(), *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 mEglImplementation = Utils::MakeUnique<EglImplementation>(mMultiSamplingLevel, mDepthBufferRequired, mStencilBufferRequired, mPartialUpdateRequired);
127 mEglImageExtensions = Utils::MakeUnique<EglImageExtensions>(mEglImplementation.get());
129 mEglSync->Initialize(mEglImplementation.get()); // The sync impl needs the EglDisplay
132 void EglGraphics::ConfigureSurface(Dali::RenderSurfaceInterface* surface)
134 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
136 // Try to use OpenGL es 3.0
137 // ChooseConfig returns false here when the device only support gles 2.0.
138 // Because eglChooseConfig with gles 3.0 setting fails when the device only support gles 2.0 and Our default setting is gles 3.0.
139 if(!mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32))
141 // Retry to use OpenGL es 2.0
142 mEglImplementation->SetGlesVersion(20);
144 // Mark gles that we will use gles 2.0 version.
145 // After this call, we will not change mGLES version anymore.
146 mGLES->SetGlesVersion(20);
148 mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32);
151 // Check whether surfaceless context is supported
152 bool isSurfacelessContextSupported = mEglImplementation->IsSurfacelessContextSupported();
153 SetIsSurfacelessContextSupported(isSurfacelessContextSupported);
155 RenderSurfaceInterface* currentSurface = nullptr;
156 if(isSurfacelessContextSupported)
158 // Create a surfaceless OpenGL context for shared resources
159 mEglImplementation->CreateContext();
160 ActivateResourceContext();
164 currentSurface = surface;
167 ActivateSurfaceContext(currentSurface);
171 mGLES->ContextCreated(); // After this call, we can know exact gles version.
172 auto glesVersion = mGLES->GetGlesVersion();
174 // Set more detail GLES version to egl and graphics controller.
175 // Note. usually we don't need EGL client's minor version. So don't need to choose config one more time.
176 mEglImplementation->SetGlesVersion(glesVersion);
177 mGraphicsController.SetGLESVersion(static_cast<Graphics::GLES::GLESVersion>(glesVersion));
180 void EglGraphics::Shutdown()
182 if(mEglImplementation)
184 // Shutdown controller
185 mGraphicsController.Shutdown();
188 mEglImplementation->TerminateGles();
192 void EglGraphics::Destroy()
194 mGraphicsController.Destroy();
197 GlImplementation& EglGraphics::GetGlesInterface()
202 Integration::GlAbstraction& EglGraphics::GetGlAbstraction() const
204 DALI_ASSERT_DEBUG(mGLES && "GLImplementation not created");
208 EglImplementation& EglGraphics::GetEglImplementation() const
210 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
211 return *mEglImplementation;
214 EglInterface& EglGraphics::GetEglInterface() const
216 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
217 EglInterface* eglInterface = mEglImplementation.get();
218 return *eglInterface;
221 EglSyncImplementation& EglGraphics::GetSyncImplementation()
223 DALI_ASSERT_DEBUG(mEglSync && "EglSyncImplementation not created");
227 EglImageExtensions* EglGraphics::GetImageExtensions()
229 DALI_ASSERT_DEBUG(mEglImageExtensions && "EglImageExtensions not created");
230 return mEglImageExtensions.get();
233 Graphics::Controller& EglGraphics::GetController()
235 return mGraphicsController;
238 void EglGraphics::CacheConfigurations(ConfigurationManager& configurationManager)
240 mGLES->SetIsAdvancedBlendEquationSupported(configurationManager.IsAdvancedBlendEquationSupported());
241 mGLES->SetIsMultisampledRenderToTextureSupported(configurationManager.IsMultisampledRenderToTextureSupported());
242 mGLES->SetShadingLanguageVersion(configurationManager.GetShadingLanguageVersion());
245 void EglGraphics::FrameStart()
247 mGraphicsController.FrameStart();
250 void EglGraphics::LogMemoryPools()
252 std::size_t graphicsCapacity = mGraphicsController.GetCapacity();
253 DALI_LOG_RELEASE_INFO(
255 " GraphicsController Capacity: %lu\n",
259 } // namespace Adaptor
260 } // namespace Internal