6
6
7
7
#include < EGL/egl.h>
8
8
#include < EGL/eglext.h>
9
-
10
9
#include < iostream>
11
10
12
- namespace flutter {
11
+ # include " flutter/shell/platform/embedder/embedder_struct_macros.h "
13
12
14
- struct ExternalTextureD3dState {
15
- GLuint gl_texture = 0 ;
16
- EGLSurface egl_surface = EGL_NO_SURFACE;
17
- void * last_surface_handle = nullptr ;
18
- };
13
+ namespace flutter {
19
14
20
15
ExternalTextureD3d::ExternalTextureD3d (
16
+ FlutterDesktopGpuSurfaceType type,
21
17
const FlutterDesktopGpuSurfaceTextureCallback texture_callback,
22
18
void * user_data,
23
19
const AngleSurfaceManager* surface_manager,
24
20
const GlProcs& gl_procs)
25
- : state_(std::make_unique<ExternalTextureD3dState>() ),
21
+ : type_(type ),
26
22
texture_callback_ (texture_callback),
27
23
user_data_(user_data),
28
24
surface_manager_(surface_manager),
29
25
gl_(gl_procs) {}
30
26
31
27
ExternalTextureD3d::~ExternalTextureD3d () {
32
- if (state_->egl_surface != EGL_NO_SURFACE) {
33
- eglDestroySurface (surface_manager_->egl_display (), state_->egl_surface );
34
- }
28
+ ReleaseImage ();
35
29
36
- if (state_-> gl_texture != 0 ) {
37
- gl_.glDeleteTextures (1 , &state_-> gl_texture );
30
+ if (gl_texture_ != 0 ) {
31
+ gl_.glDeleteTextures (1 , &gl_texture_ );
38
32
}
39
33
}
40
34
@@ -50,66 +44,81 @@ bool ExternalTextureD3d::PopulateTexture(size_t width,
50
44
51
45
// Populate the texture object used by the engine.
52
46
opengl_texture->target = GL_TEXTURE_2D;
53
- opengl_texture->name = state_-> gl_texture ;
47
+ opengl_texture->name = gl_texture_ ;
54
48
opengl_texture->format = GL_RGBA8;
55
49
opengl_texture->destruction_callback = nullptr ;
56
50
opengl_texture->user_data = nullptr ;
57
- opengl_texture->width = descriptor-> visible_width ;
58
- opengl_texture->height = descriptor-> visible_height ;
51
+ opengl_texture->width = SAFE_ACCESS ( descriptor, visible_width, 0 ) ;
52
+ opengl_texture->height = SAFE_ACCESS ( descriptor, visible_height, 0 ) ;
59
53
60
54
return true ;
61
55
}
62
56
57
+ void ExternalTextureD3d::ReleaseImage () {
58
+ if (egl_surface_ != EGL_NO_SURFACE) {
59
+ eglReleaseTexImage (surface_manager_->egl_display (), egl_surface_,
60
+ EGL_BACK_BUFFER);
61
+ eglDestroySurface (surface_manager_->egl_display (), egl_surface_);
62
+ egl_surface_ = EGL_NO_SURFACE;
63
+ }
64
+ }
65
+
63
66
bool ExternalTextureD3d::CreateOrUpdateTexture (
64
67
const FlutterDesktopGpuSurfaceDescriptor* descriptor) {
65
- if (descriptor == nullptr || descriptor->handle == nullptr ) {
68
+ if (descriptor == nullptr ||
69
+ SAFE_ACCESS (descriptor, handle, nullptr ) == nullptr ) {
70
+ ReleaseImage ();
66
71
return false ;
67
72
}
68
73
69
- ExternalTextureD3dState* state = state_.get ();
70
- if (state->gl_texture == 0 ) {
71
- gl_.glGenTextures (1 , &state_->gl_texture );
74
+ if (gl_texture_ == 0 ) {
75
+ gl_.glGenTextures (1 , &gl_texture_);
72
76
73
- gl_.glBindTexture (GL_TEXTURE_2D, state_-> gl_texture );
77
+ gl_.glBindTexture (GL_TEXTURE_2D, gl_texture_ );
74
78
75
79
gl_.glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
76
80
gl_.glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
77
81
78
82
gl_.glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
79
83
gl_.glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
80
84
} else {
81
- gl_.glBindTexture (GL_TEXTURE_2D, state-> gl_texture );
85
+ gl_.glBindTexture (GL_TEXTURE_2D, gl_texture_ );
82
86
}
83
87
84
- if (descriptor-> handle != state-> last_surface_handle ) {
85
- if (state-> egl_surface != EGL_NO_SURFACE ) {
86
- eglReleaseTexImage (surface_manager_-> egl_display (), state-> egl_surface ,
87
- EGL_BACK_BUFFER);
88
- eglDestroySurface (surface_manager_-> egl_display (), state-> egl_surface );
89
- }
90
-
91
- EGLint attributes[] = {EGL_WIDTH ,
92
- static_cast <EGLint>(descriptor-> width ),
93
- EGL_HEIGHT ,
94
- static_cast <EGLint>(descriptor-> height ) ,
95
- EGL_TEXTURE_TARGET ,
96
- EGL_TEXTURE_2D,
97
- EGL_TEXTURE_FORMAT,
98
- EGL_TEXTURE_RGBA, // always EGL_TEXTURE_RGBA
99
- EGL_NONE};
100
-
101
- state-> last_surface_handle = descriptor-> handle ;
102
- state-> egl_surface = surface_manager_-> CreateSurfaceFromHandle (
103
- EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE, descriptor-> handle , attributes);
104
-
105
- if (state-> egl_surface == EGL_NO_SURFACE ||
106
- eglBindTexImage (surface_manager_->egl_display (), state-> egl_surface ,
88
+ auto handle = SAFE_ACCESS (descriptor, handle, nullptr );
89
+ if (handle != last_surface_handle_ ) {
90
+ ReleaseImage ();
91
+
92
+ EGLint attributes[] = {
93
+ EGL_WIDTH,
94
+ static_cast <EGLint>( SAFE_ACCESS (descriptor, width, 0 )),
95
+ EGL_HEIGHT ,
96
+ static_cast <EGLint>(SAFE_ACCESS ( descriptor, height, 0 ) ),
97
+ EGL_TEXTURE_TARGET ,
98
+ EGL_TEXTURE_2D ,
99
+ EGL_TEXTURE_FORMAT ,
100
+ EGL_TEXTURE_RGBA, // always EGL_TEXTURE_RGBA
101
+ EGL_NONE};
102
+
103
+ egl_surface_ = surface_manager_-> CreateSurfaceFromHandle (
104
+ (type_ == kFlutterDesktopGpuSurfaceTypeD3d11Texture2D )
105
+ ? EGL_D3D_TEXTURE_ANGLE
106
+ : EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE,
107
+ handle, attributes);
108
+
109
+ if (egl_surface_ == EGL_NO_SURFACE ||
110
+ eglBindTexImage (surface_manager_->egl_display (), egl_surface_ ,
107
111
EGL_BACK_BUFFER) == EGL_FALSE) {
108
- std::cerr << " Binding DXGI surface failed." << std::endl;
112
+ std::cerr << " Binding D3D surface failed." << std::endl;
109
113
}
114
+ last_surface_handle_ = handle;
110
115
}
111
116
112
- return state->egl_surface != EGL_NO_SURFACE;
117
+ auto release_callback = SAFE_ACCESS (descriptor, release_callback, nullptr );
118
+ if (release_callback) {
119
+ release_callback (SAFE_ACCESS (descriptor, release_context, nullptr ));
120
+ }
121
+ return egl_surface_ != EGL_NO_SURFACE;
113
122
}
114
123
115
124
} // namespace flutter
0 commit comments