Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2848b11

Browse files
jnschulzeNiklas Schulze
authored and
Niklas Schulze
committed
Windows: Texture Registrar: Destroy textures on raster thread
1 parent 436f18b commit 2848b11

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
lines changed

shell/platform/windows/flutter_windows_engine.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,27 @@ bool FlutterWindowsEngine::MarkExternalTextureFrameAvailable(
509509
engine_, texture_id) == kSuccess);
510510
}
511511

512+
bool FlutterWindowsEngine::PostRasterThreadTask(
513+
std::function<void()> callback) {
514+
struct Captures {
515+
std::function<void()> callback;
516+
};
517+
auto captures = std::make_unique<Captures>();
518+
captures->callback = callback;
519+
if (embedder_api_.PostRenderThreadTask(
520+
engine_,
521+
[](void* opaque) {
522+
auto captures = reinterpret_cast<Captures*>(opaque);
523+
captures->callback();
524+
delete captures;
525+
},
526+
captures.get()) == kSuccess) {
527+
captures.release();
528+
return true;
529+
}
530+
return false;
531+
}
532+
512533
bool FlutterWindowsEngine::DispatchSemanticsAction(
513534
uint64_t target,
514535
FlutterSemanticsAction action,

shell/platform/windows/flutter_windows_engine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ class FlutterWindowsEngine {
170170
// given |texture_id|.
171171
bool MarkExternalTextureFrameAvailable(int64_t texture_id);
172172

173+
// Posts the given callback onto the raster thread.
174+
bool PostRasterThreadTask(std::function<void()> callback);
175+
173176
// Invoke on the embedder's vsync callback to schedule a frame.
174177
void OnVsync(intptr_t baton);
175178

shell/platform/windows/flutter_windows_engine_unittests.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,5 +374,21 @@ TEST(FlutterWindowsEngine, AddPluginRegistrarDestructionCallback) {
374374
EXPECT_EQ(result2, 2);
375375
}
376376

377+
TEST(FlutterWindowsEngine, PostRasterThreadTask) {
378+
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
379+
EngineModifier modifier(engine.get());
380+
381+
modifier.embedder_api().PostRenderThreadTask = MOCK_ENGINE_PROC(
382+
PostRenderThreadTask, ([](auto engine, auto callback, auto context) {
383+
callback(context);
384+
return kSuccess;
385+
}));
386+
387+
bool called = false;
388+
engine->PostRasterThreadTask([&called]() { called = true; });
389+
390+
EXPECT_TRUE(called);
391+
}
392+
377393
} // namespace testing
378394
} // namespace flutter

shell/platform/windows/flutter_windows_texture_registrar.cc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "flutter/shell/platform/windows/flutter_windows_texture_registrar.h"
66

7+
#include <future>
78
#include <iostream>
89
#include <mutex>
910

@@ -78,19 +79,22 @@ int64_t FlutterWindowsTextureRegistrar::EmplaceTexture(
7879
}
7980

8081
bool FlutterWindowsTextureRegistrar::UnregisterTexture(int64_t texture_id) {
81-
{
82+
engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() {
83+
engine->UnregisterExternalTexture(texture_id);
84+
});
85+
86+
std::promise<bool> promise;
87+
engine_->PostRasterThreadTask([this, &promise, texture_id]() {
8288
std::lock_guard<std::mutex> lock(map_mutex_);
8389
auto it = textures_.find(texture_id);
84-
if (it == textures_.end()) {
85-
return false;
90+
if (it != textures_.end()) {
91+
textures_.erase(it);
92+
promise.set_value(true);
93+
} else {
94+
promise.set_value(false);
8695
}
87-
textures_.erase(it);
88-
}
89-
90-
engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() {
91-
engine->UnregisterExternalTexture(texture_id);
9296
});
93-
return true;
97+
return promise.get_future().get();
9498
}
9599

96100
bool FlutterWindowsTextureRegistrar::MarkTextureFrameAvailable(

shell/platform/windows/flutter_windows_texture_registrar_unittests.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ TEST(FlutterWindowsTextureRegistrarTest, RegisterUnregisterTexture) {
113113
return kSuccess;
114114
}));
115115

116+
modifier.embedder_api().PostRenderThreadTask =
117+
MOCK_ENGINE_PROC(PostRenderThreadTask,
118+
[](auto engine, auto callback, void* callback_data) {
119+
callback(callback_data);
120+
return kSuccess;
121+
});
122+
116123
auto texture_id = registrar.RegisterTexture(&texture_info);
117124
EXPECT_TRUE(register_called);
118125
EXPECT_NE(texture_id, -1);

0 commit comments

Comments
 (0)