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/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
33 EglGraphics::EglGraphics(EnvironmentOptions& environmentOptions)
34 : mMultiSamplingLevel(0)
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();
41 if(environmentOptions.GetGlesCallTime() > 0)
43 mGLES = Utils::MakeUnique<GlProxyImplementation>(environmentOptions);
47 mGLES.reset(new GlImplementation());
50 mGraphicsController.InitializeGLES(*mGLES.get());
53 EglGraphics::~EglGraphics()
57 void EglGraphics::SetGlesVersion(const int32_t glesVersion)
59 if(mEglImplementation)
61 mEglImplementation->SetGlesVersion(glesVersion);
64 mGLES->SetGlesVersion(glesVersion);
66 mGraphicsController.SetGLESVersion(static_cast<Graphics::GLES::GLESVersion>(glesVersion));
69 void EglGraphics::SetIsSurfacelessContextSupported(const bool isSupported)
71 mGLES->SetIsSurfacelessContextSupported(isSupported);
74 void EglGraphics::ActivateResourceContext()
76 if(mEglImplementation && mEglImplementation->IsSurfacelessContextSupported())
78 // Make the shared surfaceless context as current before rendering
79 mEglImplementation->MakeContextCurrent(EGL_NO_SURFACE, mEglImplementation->GetContext());
82 mGraphicsController.ActivateResourceContext();
85 void EglGraphics::ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface)
89 surface->InitializeGraphics();
90 surface->MakeContextCurrent();
93 mGraphicsController.ActivateSurfaceContext(surface);
96 void EglGraphics::PostRender()
98 ActivateResourceContext();
100 if(mGraphicsController.GetCurrentContext())
102 mGraphicsController.GetCurrentContext()->InvalidateDepthStencilBuffers();
105 mGraphicsController.PostRender();
108 void EglGraphics::SetFirstFrameAfterResume()
110 if(mEglImplementation)
112 mEglImplementation->SetFirstFrameAfterResume();
116 void EglGraphics::Initialize()
120 // Sync and context helper require EGL to be initialized first (can't execute in the constructor)
121 mGraphicsController.Initialize(*mEglSync.get(), *mEglContextHelper.get(), *this);
124 void EglGraphics::Initialize(bool depth, bool stencil, bool partialRendering, int msaa)
126 mDepthBufferRequired = static_cast<Integration::DepthBufferAvailable>(depth);
127 mStencilBufferRequired = static_cast<Integration::StencilBufferAvailable>(stencil);
128 mPartialUpdateRequired = static_cast<Integration::PartialUpdateAvailable>(partialRendering);
129 mMultiSamplingLevel = msaa;
134 void EglGraphics::EglInitialize()
136 mEglSync = Utils::MakeUnique<EglSyncImplementation>();
137 mEglContextHelper = Utils::MakeUnique<EglContextHelperImplementation>();
138 mEglImplementation = Utils::MakeUnique<EglImplementation>(mMultiSamplingLevel, mDepthBufferRequired, mStencilBufferRequired, mPartialUpdateRequired);
139 mEglImageExtensions = Utils::MakeUnique<EglImageExtensions>(mEglImplementation.get());
141 mEglSync->Initialize(mEglImplementation.get()); // The sync impl needs the EglDisplay
142 mEglContextHelper->Initialize(mEglImplementation.get()); // The context helper impl needs the EglContext
145 void EglGraphics::ConfigureSurface(Dali::RenderSurfaceInterface* surface)
147 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
149 // Try to use OpenGL es 3.0
150 // ChooseConfig returns false here when the device only support gles 2.0.
151 // Because eglChooseConfig with gles 3.0 setting fails when the device only support gles 2.0 and Our default setting is gles 3.0.
152 if(!mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32))
154 // Retry to use OpenGL es 2.0
156 mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32);
159 // Check whether surfaceless context is supported
160 bool isSurfacelessContextSupported = mEglImplementation->IsSurfacelessContextSupported();
161 SetIsSurfacelessContextSupported(isSurfacelessContextSupported);
163 RenderSurfaceInterface* currentSurface = nullptr;
164 if(isSurfacelessContextSupported)
166 // Create a surfaceless OpenGL context for shared resources
167 mEglImplementation->CreateContext();
168 ActivateResourceContext();
172 currentSurface = surface;
175 ActivateSurfaceContext(currentSurface);
179 mGLES->ContextCreated();
180 SetGlesVersion(mGLES->GetGlesVersion());
183 void EglGraphics::Shutdown()
185 if(mEglImplementation)
187 // Shutdown controller
188 mGraphicsController.Shutdown();
191 mEglImplementation->TerminateGles();
195 void EglGraphics::Destroy()
197 mGraphicsController.Destroy();
200 GlImplementation& EglGraphics::GetGlesInterface()
205 Integration::GlAbstraction& EglGraphics::GetGlAbstraction() const
207 DALI_ASSERT_DEBUG(mGLES && "GLImplementation not created");
211 EglImplementation& EglGraphics::GetEglImplementation() const
213 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
214 return *mEglImplementation;
217 EglInterface& EglGraphics::GetEglInterface() const
219 DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
220 EglInterface* eglInterface = mEglImplementation.get();
221 return *eglInterface;
224 EglSyncImplementation& EglGraphics::GetSyncImplementation()
226 DALI_ASSERT_DEBUG(mEglSync && "EglSyncImplementation not created");
230 EglContextHelperImplementation& EglGraphics::GetContextHelperImplementation()
232 DALI_ASSERT_DEBUG(mEglContextHelper && "EglContextHelperImplementation not created");
233 return *mEglContextHelper;
236 EglImageExtensions* EglGraphics::GetImageExtensions()
238 DALI_ASSERT_DEBUG(mEglImageExtensions && "EglImageExtensions not created");
239 return mEglImageExtensions.get();
242 Graphics::Controller& EglGraphics::GetController()
244 return mGraphicsController;
247 void EglGraphics::CacheConfigurations(ConfigurationManager& configurationManager)
249 mGLES->SetIsAdvancedBlendEquationSupported(configurationManager.IsAdvancedBlendEquationSupported());
250 mGLES->SetShadingLanguageVersion(configurationManager.GetShadingLanguageVersion());
253 } // namespace Adaptor
254 } // namespace Internal