Skip to content

Commit 84fea6d

Browse files
authored
[Windows] Introduce egl::Context (#49954)
This introduces a new `egl::Context` type that abstracts a raw `EGLContext`. This also removes the EGL context logic out from `EGLManager`. Part of flutter#141996 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent a426b0d commit 84fea6d

13 files changed

Lines changed: 308 additions & 86 deletions

File tree

‎ci/licenses_golden/licenses_flutter‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7233,6 +7233,10 @@ ORIGIN: ../../../flutter/shell/platform/windows/direct_manipulation.cc + ../../.
72337233
ORIGIN: ../../../flutter/shell/platform/windows/direct_manipulation.h + ../../../flutter/LICENSE
72347234
ORIGIN: ../../../flutter/shell/platform/windows/dpi_utils.cc + ../../../flutter/LICENSE
72357235
ORIGIN: ../../../flutter/shell/platform/windows/dpi_utils.h + ../../../flutter/LICENSE
7236+
ORIGIN: ../../../flutter/shell/platform/windows/egl/context.cc + ../../../flutter/LICENSE
7237+
ORIGIN: ../../../flutter/shell/platform/windows/egl/context.h + ../../../flutter/LICENSE
7238+
ORIGIN: ../../../flutter/shell/platform/windows/egl/egl.cc + ../../../flutter/LICENSE
7239+
ORIGIN: ../../../flutter/shell/platform/windows/egl/egl.h + ../../../flutter/LICENSE
72367240
ORIGIN: ../../../flutter/shell/platform/windows/egl/manager.cc + ../../../flutter/LICENSE
72377241
ORIGIN: ../../../flutter/shell/platform/windows/egl/manager.h + ../../../flutter/LICENSE
72387242
ORIGIN: ../../../flutter/shell/platform/windows/egl/proc_table.cc + ../../../flutter/LICENSE
@@ -10103,6 +10107,10 @@ FILE: ../../../flutter/shell/platform/windows/direct_manipulation.cc
1010310107
FILE: ../../../flutter/shell/platform/windows/direct_manipulation.h
1010410108
FILE: ../../../flutter/shell/platform/windows/dpi_utils.cc
1010510109
FILE: ../../../flutter/shell/platform/windows/dpi_utils.h
10110+
FILE: ../../../flutter/shell/platform/windows/egl/context.cc
10111+
FILE: ../../../flutter/shell/platform/windows/egl/context.h
10112+
FILE: ../../../flutter/shell/platform/windows/egl/egl.cc
10113+
FILE: ../../../flutter/shell/platform/windows/egl/egl.h
1010610114
FILE: ../../../flutter/shell/platform/windows/egl/manager.cc
1010710115
FILE: ../../../flutter/shell/platform/windows/egl/manager.h
1010810116
FILE: ../../../flutter/shell/platform/windows/egl/proc_table.cc

‎shell/platform/windows/BUILD.gn‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ source_set("flutter_windows_source") {
5151
"direct_manipulation.h",
5252
"dpi_utils.cc",
5353
"dpi_utils.h",
54+
"egl/context.cc",
55+
"egl/context.h",
56+
"egl/egl.cc",
57+
"egl/egl.h",
5458
"egl/manager.cc",
5559
"egl/manager.h",
5660
"egl/proc_table.cc",
@@ -202,6 +206,7 @@ executable("flutter_windows_unittests") {
202206
"settings_plugin_unittests.cc",
203207
"system_utils_unittests.cc",
204208
"task_runner_unittests.cc",
209+
"testing/egl/mock_context.h",
205210
"testing/egl/mock_manager.h",
206211
"testing/egl/mock_proc_table.h",
207212
"testing/engine_modifier.h",
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/windows/egl/context.h"
6+
7+
#include "flutter/shell/platform/windows/egl/egl.h"
8+
9+
namespace flutter {
10+
namespace egl {
11+
12+
Context::Context(EGLDisplay display, EGLContext context)
13+
: display_(display), context_(context) {}
14+
15+
Context::~Context() {
16+
if (display_ == EGL_NO_DISPLAY && context_ == EGL_NO_CONTEXT) {
17+
return;
18+
}
19+
20+
if (::eglDestroyContext(display_, context_) != EGL_TRUE) {
21+
WINDOWS_LOG_EGL_ERROR;
22+
}
23+
}
24+
25+
bool Context::IsCurrent() const {
26+
return ::eglGetCurrentContext() == context_;
27+
}
28+
29+
bool Context::MakeCurrent() const {
30+
const auto result =
31+
::eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, context_);
32+
if (result != EGL_TRUE) {
33+
WINDOWS_LOG_EGL_ERROR;
34+
return false;
35+
}
36+
37+
return true;
38+
}
39+
40+
bool Context::ClearCurrent() const {
41+
const auto result = ::eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE,
42+
EGL_NO_CONTEXT);
43+
if (result != EGL_TRUE) {
44+
WINDOWS_LOG_EGL_ERROR;
45+
return false;
46+
}
47+
48+
return true;
49+
}
50+
51+
const EGLContext& Context::GetHandle() const {
52+
return context_;
53+
}
54+
55+
} // namespace egl
56+
} // namespace flutter
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_CONTEXT_H_
6+
#define FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_CONTEXT_H_
7+
8+
#include <EGL/egl.h>
9+
10+
#include "flutter/fml/macros.h"
11+
12+
namespace flutter {
13+
namespace egl {
14+
15+
// An EGL context to interact with OpenGL.
16+
//
17+
// This enables automatic eror logging and mocking.
18+
//
19+
// Flutter Windows uses this to create render and resource contexts.
20+
class Context {
21+
public:
22+
Context(EGLDisplay display, EGLContext context);
23+
~Context();
24+
25+
// Check if this context is currently bound to the thread.
26+
virtual bool IsCurrent() const;
27+
28+
// Bind the context to the thread without any read or draw surfaces.
29+
//
30+
// Returns true on success.
31+
virtual bool MakeCurrent() const;
32+
33+
// Unbind any context and surfaces from the thread.
34+
//
35+
// Returns true on success.
36+
virtual bool ClearCurrent() const;
37+
38+
// Get the raw EGL context.
39+
virtual const EGLContext& GetHandle() const;
40+
41+
private:
42+
EGLDisplay display_ = EGL_NO_DISPLAY;
43+
EGLContext context_ = EGL_NO_CONTEXT;
44+
45+
FML_DISALLOW_COPY_AND_ASSIGN(Context);
46+
};
47+
48+
} // namespace egl
49+
} // namespace flutter
50+
51+
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_CONTEXT_H_

‎shell/platform/windows/egl/egl.cc‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/windows/egl/egl.h"
6+
7+
#include <EGL/egl.h>
8+
9+
#include "flutter/fml/logging.h"
10+
11+
namespace flutter {
12+
namespace egl {
13+
14+
namespace {
15+
16+
const char* EGLErrorToString(EGLint error) {
17+
switch (error) {
18+
case EGL_SUCCESS:
19+
return "Success";
20+
case EGL_NOT_INITIALIZED:
21+
return "Not Initialized";
22+
case EGL_BAD_ACCESS:
23+
return "Bad Access";
24+
case EGL_BAD_ALLOC:
25+
return "Bad Alloc";
26+
case EGL_BAD_ATTRIBUTE:
27+
return "Bad Attribute";
28+
case EGL_BAD_CONTEXT:
29+
return "Bad Context";
30+
case EGL_BAD_CONFIG:
31+
return "Bad Config";
32+
case EGL_BAD_CURRENT_SURFACE:
33+
return "Bad Current Surface";
34+
case EGL_BAD_DISPLAY:
35+
return "Bad Display";
36+
case EGL_BAD_SURFACE:
37+
return "Bad Surface";
38+
case EGL_BAD_MATCH:
39+
return "Bad Match";
40+
case EGL_BAD_PARAMETER:
41+
return "Bad Parameter";
42+
case EGL_BAD_NATIVE_PIXMAP:
43+
return "Bad Native Pixmap";
44+
case EGL_BAD_NATIVE_WINDOW:
45+
return "Bad Native Window";
46+
case EGL_CONTEXT_LOST:
47+
return "Context Lost";
48+
}
49+
FML_UNREACHABLE();
50+
return "Unknown";
51+
}
52+
53+
} // namespace
54+
55+
void LogEGLError(std::string_view message) {
56+
const EGLint error = ::eglGetError();
57+
return FML_LOG(ERROR) << "EGL Error: " << EGLErrorToString(error) << " ("
58+
<< error << ") " << message;
59+
}
60+
61+
void LogEGLError(std::string_view file, int line) {
62+
std::stringstream stream;
63+
stream << "in " << file << ":" << line;
64+
LogEGLError(stream.str());
65+
}
66+
67+
} // namespace egl
68+
} // namespace flutter

‎shell/platform/windows/egl/egl.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_EGL_H_
6+
#define FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_EGL_H_
7+
8+
#include <string_view>
9+
10+
namespace flutter {
11+
namespace egl {
12+
13+
/// Log the last EGL error with an error message.
14+
void LogEGLError(std::string_view message);
15+
16+
/// Log the last EGL error.
17+
void LogEGLError(std::string_view file, int line);
18+
19+
#define WINDOWS_LOG_EGL_ERROR LogEGLError(__FILE__, __LINE__);
20+
21+
} // namespace egl
22+
} // namespace flutter
23+
24+
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_EGL_H_

0 commit comments

Comments
 (0)