@@ -30,18 +30,41 @@ struct ExternalTextureGLState {
3030
3131static std::atomic_long nextTextureId = {1 };
3232
33+ static void MarkTbmSurfaceToUse (void * surface) {
34+ #ifndef WEARABLE_PROFILE
35+ FT_ASSERT (surface);
36+ tbm_surface_h tbm_surface = (tbm_surface_h)surface;
37+ tbm_surface_internal_ref (tbm_surface);
38+ #endif
39+ }
40+
41+ static void UnmarkTbmSurfaceToUse (void * surface) {
42+ FT_ASSERT (surface);
43+ tbm_surface_h tbm_surface = (tbm_surface_h)surface;
44+ #ifndef WEARABLE_PROFILE
45+ tbm_surface_internal_unref (tbm_surface);
46+ #else
47+ tbm_surface_destroy (tbm_surface);
48+ #endif
49+ }
50+
3351ExternalTextureGL::ExternalTextureGL ()
3452 : state_(std::make_unique<ExternalTextureGLState>()),
35- texture_tbm_surface_( NULL ),
53+ available_tbm_surface_( nullptr ),
3654 texture_id_(nextTextureId++) {}
3755
3856ExternalTextureGL::~ExternalTextureGL () {
3957 mutex_.lock ();
4058 if (state_->gl_texture != 0 ) {
4159 glDeleteTextures (1 , &state_->gl_texture );
4260 }
61+
62+ // If there is a available_tbm_surface_ that is not populated, remove it
63+ if (available_tbm_surface_) {
64+ UnmarkTbmSurfaceToUse (available_tbm_surface_);
65+ }
66+
4367 state_.release ();
44- DestructionTbmSurface ();
4568 mutex_.unlock ();
4669}
4770
@@ -52,35 +75,42 @@ bool ExternalTextureGL::OnFrameAvailable(tbm_surface_h tbm_surface) {
5275 mutex_.unlock ();
5376 return false ;
5477 }
55- if (texture_tbm_surface_) {
56- FT_LOGD (" texture_tbm_surface_ does not destruction, discard" );
78+
79+ if (available_tbm_surface_) {
80+ FT_LOGD (
81+ " Discard! an available tbm surface that has not yet been used exists" );
5782 mutex_.unlock ();
5883 return false ;
5984 }
85+
6086 tbm_surface_info_s info;
6187 if (tbm_surface_get_info (tbm_surface, &info) != TBM_SURFACE_ERROR_NONE) {
6288 FT_LOGD (" tbm_surface not valid, pass" );
6389 mutex_.unlock ();
6490 return false ;
6591 }
66- texture_tbm_surface_ = tbm_surface;
92+
93+ available_tbm_surface_ = tbm_surface;
94+ MarkTbmSurfaceToUse (available_tbm_surface_);
95+
6796 mutex_.unlock ();
6897 return true ;
6998}
7099
71100bool ExternalTextureGL::PopulateTextureWithIdentifier (
72101 size_t width, size_t height, FlutterOpenGLTexture* opengl_texture) {
73102 mutex_.lock ();
74- if (!texture_tbm_surface_ ) {
75- FT_LOGD (" texture_tbm_surface_ is NULL " );
103+ if (!available_tbm_surface_ ) {
104+ FT_LOGD (" available_tbm_surface_ is null " );
76105 mutex_.unlock ();
77106 return false ;
78107 }
79108 tbm_surface_info_s info;
80- if (tbm_surface_get_info (texture_tbm_surface_ , &info) !=
109+ if (tbm_surface_get_info (available_tbm_surface_ , &info) !=
81110 TBM_SURFACE_ERROR_NONE) {
82- FT_LOGD (" tbm_surface not valid" );
83- DestructionTbmSurface ();
111+ FT_LOGD (" tbm_surface is invalid" );
112+ UnmarkTbmSurfaceToUse (available_tbm_surface_);
113+ available_tbm_surface_ = nullptr ;
84114 mutex_.unlock ();
85115 return false ;
86116 }
@@ -89,7 +119,7 @@ bool ExternalTextureGL::PopulateTextureWithIdentifier(
89119 int attribs[] = {EVAS_GL_IMAGE_PRESERVED, GL_TRUE, 0 };
90120 EvasGLImage egl_src_image = evasglCreateImageForContext (
91121 g_evas_gl, evas_gl_current_context_get (g_evas_gl),
92- EVAS_GL_NATIVE_SURFACE_TIZEN, (void *)(intptr_t )texture_tbm_surface_ ,
122+ EVAS_GL_NATIVE_SURFACE_TIZEN, (void *)(intptr_t )available_tbm_surface_ ,
93123 attribs);
94124 if (!egl_src_image) {
95125 mutex_.unlock ();
@@ -120,7 +150,8 @@ bool ExternalTextureGL::PopulateTextureWithIdentifier(
120150 EGL_NONE};
121151 EGLImageKHR egl_src_image = n_eglCreateImageKHR (
122152 eglGetCurrentDisplay (), EGL_NO_CONTEXT, EGL_NATIVE_SURFACE_TIZEN,
123- (EGLClientBuffer)texture_tbm_surface_, attribs);
153+ (EGLClientBuffer)available_tbm_surface_, attribs);
154+
124155 if (!egl_src_image) {
125156 FT_LOGE (" egl_src_image create fail!!, errorcode == %d" , eglGetError ());
126157 mutex_.unlock ();
@@ -154,31 +185,14 @@ bool ExternalTextureGL::PopulateTextureWithIdentifier(
154185 opengl_texture->target = GL_TEXTURE_EXTERNAL_OES;
155186 opengl_texture->name = state_->gl_texture ;
156187 opengl_texture->format = GL_RGBA8;
157- opengl_texture->destruction_callback = (VoidCallback)DestructionCallback;
158- opengl_texture->user_data = static_cast <void *>(this );
188+ opengl_texture->destruction_callback = (VoidCallback)UnmarkTbmSurfaceToUse;
189+
190+ // Abandon ownership of tmb_surface
191+ opengl_texture->user_data = available_tbm_surface_;
192+ available_tbm_surface_ = nullptr ;
193+
159194 opengl_texture->width = width;
160195 opengl_texture->height = height;
161196 mutex_.unlock ();
162197 return true ;
163198}
164-
165- void ExternalTextureGL::DestructionTbmSurfaceWithLock () {
166- mutex_.lock ();
167- DestructionTbmSurface ();
168- mutex_.unlock ();
169- }
170-
171- void ExternalTextureGL::DestructionTbmSurface () {
172- if (!texture_tbm_surface_) {
173- FT_LOGE (" tbm_surface_h is NULL" );
174- return ;
175- }
176- tbm_surface_destroy (texture_tbm_surface_);
177- texture_tbm_surface_ = NULL ;
178- }
179-
180- void ExternalTextureGL::DestructionCallback (void * user_data) {
181- ExternalTextureGL* externalTextureGL =
182- reinterpret_cast <ExternalTextureGL*>(user_data);
183- externalTextureGL->DestructionTbmSurfaceWithLock ();
184- }
0 commit comments