summaryrefslogtreecommitdiff
path: root/dali/internal/graphics/gles/egl-graphics.cpp
blob: 975b37f2bcd07b40d75858b1f41607f44ae8410c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// CLASS HEADER
#include <dali/internal/graphics/gles/egl-graphics.h>

// INTERNAL INCLUDES
#include <dali/integration-api/adaptor-framework/render-surface-interface.h>
#include <dali/integration-api/debug.h>
#include <dali/internal/graphics/common/egl-include.h>
#include <dali/internal/system/common/configuration-manager.h>
#include <dali/internal/system/common/environment-options.h>
#include <dali/internal/window-system/common/display-utils.h> // For Utils::MakeUnique
#include <dali/internal/window-system/common/window-base.h>

#include <X11/Xlib.h>

namespace Dali
{
namespace Internal
{
namespace Adaptor
{
EglGraphics::EglGraphics(
  EnvironmentOptions&                 environmentOptions,
  const Graphics::GraphicsCreateInfo& info,
  Integration::DepthBufferAvailable   depthBufferRequired,
  Integration::StencilBufferAvailable stencilBufferRequired,
  Integration::PartialUpdateAvailable partialUpdateRequired)
: GraphicsInterface(info, depthBufferRequired, stencilBufferRequired, partialUpdateRequired),
  mMultiSamplingLevel(info.multiSamplingLevel)
{
  if(environmentOptions.GetGlesCallTime() > 0)
  {
    mGLES = Utils::MakeUnique<GlProxyImplementation>(environmentOptions);
  }
  else
  {
    mGLES.reset(new GlImplementation());
  }

  mGraphicsController.InitializeGLES(*mGLES.get());
}

EglGraphics::~EglGraphics()
{
}

void EglGraphics::SetIsSurfacelessContextSupported(const bool isSupported)
{
  mGLES->SetIsSurfacelessContextSupported(isSupported);
}

void EglGraphics::ActivateResourceContext()
{
  if(mEglImplementation && mEglImplementation->IsSurfacelessContextSupported())
  {
    // Make the shared surfaceless context as current before rendering
    mEglImplementation->MakeContextCurrent(EGL_NO_SURFACE, mEglImplementation->GetContext());
  }

  mGraphicsController.ActivateResourceContext();
}

void EglGraphics::ActivateSurfaceContext(Dali::Integration::RenderSurfaceInterface* surface)
{
  if(surface)
  {
    surface->InitializeGraphics();
    surface->MakeContextCurrent();
  }

  mGraphicsController.ActivateSurfaceContext(surface);
}

void EglGraphics::MakeContextCurrent(Graphics::SurfaceId surfaceId)
{
  auto search = mSurfaceMap.find(surfaceId);
  DALI_ASSERT_DEBUG(search != mSurfaceMap.end());

  mEglImplementation->MakeContextCurrent(search->second.surface, search->second.context);
}

void EglGraphics::PostRender()
{
  ActivateResourceContext();

  if(mGraphicsController.GetCurrentContext())
  {
    mGraphicsController.GetCurrentContext()->InvalidateDepthStencilBuffers();
  }

  mGraphicsController.PostRender();
}

void EglGraphics::Pause()
{
}

void EglGraphics::Resume()
{
  if(mEglImplementation)
  {
    mEglImplementation->SetFirstFrameAfterResume();
  }
}

int EglGraphics::GetBufferAge(Graphics::SurfaceId surfaceId)
{
  auto search = mSurfaceMap.find(surfaceId);
  DALI_ASSERT_DEBUG(search != mSurfaceMap.end());

  return mEglImplementation->GetBufferAge(search->second.surface);
}

void EglGraphics::SetDamageRegion(Graphics::SurfaceId surfaceId, std::vector<Rect<int>>& damagedRegion)
{
  auto search = mSurfaceMap.find(surfaceId);
  DALI_ASSERT_DEBUG(search != mSurfaceMap.end());

  mEglImplementation->SetDamageRegion(search->second.surface, damagedRegion);
}

void EglGraphics::SwapBuffers(Graphics::SurfaceId surfaceId)
{
  auto search = mSurfaceMap.find(surfaceId);
  DALI_ASSERT_DEBUG(search != mSurfaceMap.end());

  mEglImplementation->SwapBuffers(search->second.surface);
}

void EglGraphics::SwapBuffers(Graphics::SurfaceId surfaceId, const std::vector<Rect<int>>& damagedRegion)
{
  auto search = mSurfaceMap.find(surfaceId);
  DALI_ASSERT_DEBUG(search != mSurfaceMap.end());

  mEglImplementation->SwapBuffers(search->second.surface, damagedRegion);
}

void EglGraphics::Initialize(const Dali::DisplayConnection& displayConnection)
{
  EglInitialize();

  // Sync and context helper require EGL to be initialized first (can't execute in the constructor)
  mGraphicsController.Initialize(*mEglSync.get(), *this);
  InitializeGraphicsAPI(displayConnection);
}

void EglGraphics::Initialize(const Dali::DisplayConnection& displayConnection, bool depth, bool stencil, bool partialRendering, int msaa)
{
  mDepthBufferRequired   = static_cast<Integration::DepthBufferAvailable>(depth);
  mStencilBufferRequired = static_cast<Integration::StencilBufferAvailable>(stencil);
  mPartialUpdateRequired = static_cast<Integration::PartialUpdateAvailable>(partialRendering);
  mMultiSamplingLevel    = msaa;

  EglInitialize();
  InitializeGraphicsAPI(displayConnection);
}

void EglGraphics::InitializeGraphicsAPI(const Dali::DisplayConnection& displayConnection)
{
  // Bad name - it does call "eglInitialize"!!!! @todo Rename me!
  auto display    = displayConnection.GetDisplay();
  auto x11Display = display.Get<::Display*>();
  auto eglDisplay = static_cast<EGLNativeDisplayType>(x11Display);
  mEglImplementation->InitializeGles(eglDisplay);
}

void EglGraphics::EglInitialize()
{
  mEglSync            = Utils::MakeUnique<EglSyncImplementation>();
  mEglImplementation  = Utils::MakeUnique<EglImplementation>(mMultiSamplingLevel, mDepthBufferRequired, mStencilBufferRequired, mPartialUpdateRequired);
  mEglImageExtensions = Utils::MakeUnique<EglImageExtensions>(mEglImplementation.get());

  mEglSync->Initialize(mEglImplementation.get()); // The sync impl needs the EglDisplay
}

Graphics::SurfaceId EglGraphics::CreateSurface(Graphics::SurfaceFactory* surfaceFactory, WindowBase* windowBase, ColorDepth colorDepth, int width, int height)
{
  // Create the OpenGL context for this window
  mEglImplementation->ChooseConfig(true, colorDepth);
  EGLContext context = 0;
  mEglImplementation->CreateWindowContext(context);

  auto window     = windowBase->CreateWindow(width, height);
  auto eglWindow  = reinterpret_cast<EGLNativeWindowType>(window.Get<void*>());
  auto eglSurface = mEglImplementation->CreateSurfaceWindow(eglWindow, colorDepth);

  auto surfaceId         = ++mBaseSurfaceId;
  mSurfaceMap[surfaceId] = EglSurfaceContext{windowBase, eglSurface, context};

  return surfaceId;
}

void EglGraphics::DestroySurface(Graphics::SurfaceId surfaceId)
{
  auto search = mSurfaceMap.find(surfaceId);
  DALI_ASSERT_DEBUG(search != mSurfaceMap.end());

  mEglImplementation->DestroySurface(search->second.surface);
  mEglImplementation->DestroyContext(search->second.context);
  search->second.windowBase->DestroyWindow();
  mSurfaceMap.erase(search);
}

bool EglGraphics::ReplaceSurface(Graphics::SurfaceId surfaceId, int width, int height)
{
  auto search = mSurfaceMap.find(surfaceId);
  DALI_ASSERT_DEBUG(search != mSurfaceMap.end());

  auto& windowBase = search->second.windowBase;

  // Destroy the old graphics window
  windowBase->DestroyWindow();

  // Create the EGL window
  Dali::Any window = windowBase->CreateWindow(width, height);

  EGLNativeWindowType eglWindow = window.Get<EGLNativeWindowType>();
  auto&               context   = search->second.context;
  auto&               surface   = search->second.surface;
  return mEglImplementation->ReplaceSurfaceWindow(eglWindow, surface, context); // Should update the map.
}

void EglGraphics::ConfigureSurface(Dali::Integration::RenderSurfaceInterface* surface)
{
  DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");

  // Try to use OpenGL es 3.0
  // ChooseConfig returns false here when the device only support gles 2.0.
  // Because eglChooseConfig with gles 3.0 setting fails when the device only support gles 2.0 and Our default setting is gles 3.0.
  if(!mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32))
  {
    // Retry to use OpenGL es 2.0
    mEglImplementation->SetGlesVersion(20);

    // Mark gles that we will use gles 2.0 version.
    // After this call, we will not change mGLES version anymore.
    mGLES->SetGlesVersion(20);

    mEglImplementation->ChooseConfig(true, COLOR_DEPTH_32);
  }

  // Check whether surfaceless context is supported
  bool isSurfacelessContextSupported = mEglImplementation->IsSurfacelessContextSupported();
  SetIsSurfacelessContextSupported(isSurfacelessContextSupported);

  Integration::RenderSurfaceInterface* currentSurface = nullptr;
  if(isSurfacelessContextSupported)
  {
    // Create a surfaceless OpenGL context for shared resources
    mEglImplementation->CreateContext();
    ActivateResourceContext();
  }
  else
  {
    currentSurface = surface;
    if(currentSurface)
    {
      ActivateSurfaceContext(currentSurface);
    }
  }

  mGLES->ContextCreated(); // After this call, we can know exact gles version.
  auto glesVersion = mGLES->GetGlesVersion();

  // Set more detail GLES version to egl and graphics controller.
  // Note. usually we don't need EGL client's minor version. So don't need to choose config one more time.
  mEglImplementation->SetGlesVersion(glesVersion);
  mGraphicsController.SetGLESVersion(static_cast<Graphics::GLES::GLESVersion>(glesVersion));
}

void EglGraphics::Shutdown()
{
  if(mEglImplementation)
  {
    // Shutdown controller
    mGraphicsController.Shutdown();

    // Terminate GLES
    mEglImplementation->TerminateGles();
  }
}

void EglGraphics::Destroy()
{
  mGraphicsController.Destroy();
}

Integration::GlAbstraction& EglGraphics::GetGlAbstraction() const
{
  DALI_ASSERT_DEBUG(mGLES && "GLImplementation not created");
  return *mGLES;
}

EglImplementation& EglGraphics::GetEglImplementation() const
{
  DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
  return *mEglImplementation;
}

EglInterface& EglGraphics::GetEglInterface() const
{
  DALI_ASSERT_ALWAYS(mEglImplementation && "EGLImplementation not created");
  EglInterface* eglInterface = mEglImplementation.get();
  return *eglInterface;
}

EglSyncImplementation& EglGraphics::GetSyncImplementation()
{
  DALI_ASSERT_DEBUG(mEglSync && "EglSyncImplementation not created");
  return *mEglSync;
}

EglImageExtensions* EglGraphics::GetImageExtensions()
{
  DALI_ASSERT_DEBUG(mEglImageExtensions && "EglImageExtensions not created");
  return mEglImageExtensions.get();
}

Graphics::Controller& EglGraphics::GetController()
{
  return mGraphicsController;
}

void EglGraphics::CacheConfigurations(ConfigurationManager& configurationManager)
{
  mGLES->SetIsAdvancedBlendEquationSupported(configurationManager.IsAdvancedBlendEquationSupported());
  mGLES->SetIsMultisampledRenderToTextureSupported(configurationManager.IsMultisampledRenderToTextureSupported());
  mGLES->SetShadingLanguageVersion(configurationManager.GetShadingLanguageVersion());
}

void EglGraphics::FrameStart()
{
  mGraphicsController.FrameStart();
}

void EglGraphics::PostRenderDebug()
{
  mGLES->PostRender();
}

void EglGraphics::LogMemoryPools()
{
  std::size_t graphicsCapacity = mGraphicsController.GetCapacity();
  DALI_LOG_RELEASE_INFO(
    "EglGraphics:\n"
    "  GraphicsController Capacity: %lu\n",
    graphicsCapacity);
}

} // namespace Adaptor
} // namespace Internal
} // namespace Dali