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

Commit a2f2b8f

Browse files
committed
Add avoid_cache field to EmbedderRenderTarget to allow RenderTargets to bypass the cache
1 parent 868324d commit a2f2b8f

9 files changed

+106
-14
lines changed

shell/platform/embedder/embedder.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <memory>
1111
#include <set>
1212
#include <string>
13+
#include <utility>
1314
#include <vector>
1415

1516
#include "flutter/fml/build_config.h"
@@ -472,9 +473,11 @@ CreateEmbedderRenderTarget(const FlutterCompositor* compositor,
472473
auto c_create_callback = compositor->create_backing_store_callback;
473474
auto c_collect_callback = compositor->collect_backing_store_callback;
474475

476+
bool avoid_cache = false;
475477
{
476478
TRACE_EVENT0("flutter", "FlutterCompositorCreateBackingStore");
477-
if (!c_create_callback(&config, &backing_store, compositor->user_data)) {
479+
if (!c_create_callback(&config, &backing_store, compositor->user_data,
480+
&avoid_cache)) {
478481
FML_LOG(ERROR) << "Could not create the embedder backing store.";
479482
return nullptr;
480483
}
@@ -526,7 +529,8 @@ CreateEmbedderRenderTarget(const FlutterCompositor* compositor,
526529
}
527530

528531
return std::make_unique<flutter::EmbedderRenderTarget>(
529-
backing_store, std::move(render_surface), collect_callback.Release());
532+
backing_store, std::move(render_surface), collect_callback.Release(),
533+
avoid_cache);
530534
}
531535

532536
static std::pair<std::unique_ptr<flutter::EmbedderExternalViewEmbedder>,

shell/platform/embedder/embedder.h

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

55
#ifndef FLUTTER_EMBEDDER_H_
66
#define FLUTTER_EMBEDDER_H_
7-
87
#include <stdbool.h>
98
#include <stddef.h>
109
#include <stdint.h>
@@ -905,7 +904,8 @@ typedef struct {
905904
typedef bool (*FlutterBackingStoreCreateCallback)(
906905
const FlutterBackingStoreConfig* config,
907906
FlutterBackingStore* backing_store_out,
908-
void* user_data);
907+
void* user_data,
908+
bool* avoid_cache);
909909

910910
typedef bool (*FlutterBackingStoreCollectCallback)(
911911
const FlutterBackingStore* renderer,

shell/platform/embedder/embedder_external_view_embedder.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,10 @@ void EmbedderExternalViewEmbedder::SubmitFrame(
263263
// Hold all rendered layers in the render target cache for one frame to
264264
// see if they may be reused next frame.
265265
for (auto& render_target : matched_render_targets) {
266-
render_target_cache_.CacheRenderTarget(render_target.first,
267-
std::move(render_target.second));
266+
if (!render_target.second->GetAvoidCache()) {
267+
render_target_cache_.CacheRenderTarget(render_target.first,
268+
std::move(render_target.second));
269+
}
268270
}
269271

270272
frame->Submit();

shell/platform/embedder/embedder_render_target.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ namespace flutter {
1010

1111
EmbedderRenderTarget::EmbedderRenderTarget(FlutterBackingStore backing_store,
1212
sk_sp<SkSurface> render_surface,
13-
fml::closure on_release)
13+
fml::closure on_release,
14+
bool avoid_cache)
1415
: backing_store_(backing_store),
1516
render_surface_(std::move(render_surface)),
16-
on_release_(on_release) {
17+
on_release_(on_release),
18+
avoid_cache_(avoid_cache) {
1719
// TODO(38468): The optimization to elide backing store updates between frames
1820
// has not been implemented yet.
1921
backing_store_.did_update = true;
@@ -34,4 +36,8 @@ sk_sp<SkSurface> EmbedderRenderTarget::GetRenderSurface() const {
3436
return render_surface_;
3537
}
3638

39+
bool EmbedderRenderTarget::GetAvoidCache() {
40+
return avoid_cache_;
41+
}
42+
3743
} // namespace flutter

shell/platform/embedder/embedder_render_target.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class EmbedderRenderTarget {
3636
///
3737
EmbedderRenderTarget(FlutterBackingStore backing_store,
3838
sk_sp<SkSurface> render_surface,
39-
fml::closure on_release);
39+
fml::closure on_release,
40+
bool avoid_cache);
4041

4142
//----------------------------------------------------------------------------
4243
/// @brief Destroys this instance of the render target and invokes the
@@ -65,10 +66,18 @@ class EmbedderRenderTarget {
6566
///
6667
const FlutterBackingStore* GetBackingStore() const;
6768

69+
//----------------------------------------------------------------------------
70+
/// @brief The render target may want to avoid being cached.
71+
///
72+
/// @return A bool representing if the embedder render target should
73+
/// avoid the cache.
74+
bool GetAvoidCache();
75+
6876
private:
6977
FlutterBackingStore backing_store_;
7078
sk_sp<SkSurface> render_surface_;
7179
fml::closure on_release_;
80+
bool avoid_cache_;
7281

7382
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderRenderTarget);
7483
};

shell/platform/embedder/tests/embedder_config_builder.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,10 @@ void EmbedderConfigBuilder::SetCompositor() {
257257
compositor_.create_backing_store_callback =
258258
[](const FlutterBackingStoreConfig* config, //
259259
FlutterBackingStore* backing_store_out, //
260-
void* user_data //
261-
) {
260+
void* user_data, //
261+
bool* avoid_cache) {
262262
return reinterpret_cast<EmbedderTestCompositor*>(user_data)
263-
->CreateBackingStore(config, backing_store_out);
263+
->CreateBackingStore(config, backing_store_out, avoid_cache);
264264
};
265265
compositor_.collect_backing_store_callback =
266266
[](const FlutterBackingStore* backing_store, //

shell/platform/embedder/tests/embedder_test_compositor.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ static void InvokeAllCallbacks(const std::vector<fml::closure>& callbacks) {
2929

3030
bool EmbedderTestCompositor::CreateBackingStore(
3131
const FlutterBackingStoreConfig* config,
32-
FlutterBackingStore* backing_store_out) {
32+
FlutterBackingStore* backing_store_out,
33+
bool* avoid_cache) {
3334
bool success = backingstore_producer_->Create(config, backing_store_out);
3435
if (success) {
3536
backing_stores_created_++;
3637
InvokeAllCallbacks(on_create_render_target_callbacks_);
3738
}
39+
if (avoid_cache_callback_) {
40+
avoid_cache_callback_(avoid_cache);
41+
}
3842
return success;
3943
}
4044

@@ -104,6 +108,11 @@ void EmbedderTestCompositor::SetPlatformViewRendererCallback(
104108
platform_view_renderer_callback_ = callback;
105109
}
106110

111+
void EmbedderTestCompositor::SetUpdateAvoidCacheCallback(
112+
const UpdateAvoidCacheCallback& avoid_cache_callback) {
113+
avoid_cache_callback_ = avoid_cache_callback;
114+
}
115+
107116
size_t EmbedderTestCompositor::GetPendingBackingStoresCount() const {
108117
FML_CHECK(backing_stores_created_ >= backing_stores_collected_);
109118
return backing_stores_created_ - backing_stores_collected_;

shell/platform/embedder/tests/embedder_test_compositor.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class EmbedderTestCompositor {
2424
using PresentCallback =
2525
std::function<void(const FlutterLayer** layers, size_t layers_count)>;
2626

27+
using UpdateAvoidCacheCallback = std::function<void(bool* avoid_cache)>;
28+
2729
EmbedderTestCompositor(SkISize surface_size, sk_sp<GrDirectContext> context);
2830

2931
virtual ~EmbedderTestCompositor();
@@ -32,7 +34,8 @@ class EmbedderTestCompositor {
3234
std::unique_ptr<EmbedderTestBackingStoreProducer> backingstore_producer);
3335

3436
bool CreateBackingStore(const FlutterBackingStoreConfig* config,
35-
FlutterBackingStore* backing_store_out);
37+
FlutterBackingStore* backing_store_out,
38+
bool* avoid_cache);
3639

3740
bool CollectBackingStore(const FlutterBackingStore* backing_store);
3841

@@ -53,6 +56,15 @@ class EmbedderTestCompositor {
5356
void SetPresentCallback(const PresentCallback& present_callback,
5457
bool one_shot);
5558

59+
//----------------------------------------------------------------------------
60+
/// @brief Allows tests to install a callback to update the
61+
/// avoid_cache_callback_ bool.
62+
///
63+
/// @param[in] avoid_cache_callback The callback to update the
64+
/// avoid_cache_callback_.
65+
void SetUpdateAvoidCacheCallback(
66+
const UpdateAvoidCacheCallback& avoid_cache_callback);
67+
5668
using NextSceneCallback = std::function<void(sk_sp<SkImage> image)>;
5769
void SetNextSceneCallback(const NextSceneCallback& next_scene_callback);
5870

@@ -85,6 +97,7 @@ class EmbedderTestCompositor {
8597
bool present_callback_is_one_shot_ = false;
8698
PresentCallback present_callback_;
8799
NextSceneCallback next_scene_callback_;
100+
UpdateAvoidCacheCallback avoid_cache_callback_;
88101
sk_sp<SkImage> last_composition_;
89102
size_t backing_stores_created_ = 0;
90103
size_t backing_stores_collected_ = 0;

shell/platform/embedder/tests/embedder_unittests_gl.cc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2950,6 +2950,55 @@ TEST_F(EmbedderTest, CompositorRenderTargetsAreRecycled) {
29502950
ASSERT_EQ(context.GetCompositor().GetBackingStoresCollectedCount(), 10u);
29512951
}
29522952

2953+
//------------------------------------------------------------------------------
2954+
/// The RenderTargetCache being disabled should result in the render targets
2955+
/// always being discarded.
2956+
///
2957+
TEST_F(EmbedderTest, RenderTargetCacheDisabled) {
2958+
auto& context = GetEmbedderContext(ContextType::kOpenGLContext);
2959+
2960+
EmbedderConfigBuilder builder(context);
2961+
builder.SetOpenGLRendererConfig(SkISize::Make(300, 200));
2962+
builder.SetCompositor();
2963+
builder.SetDartEntrypoint("render_targets_are_recycled");
2964+
builder.SetRenderTargetType(
2965+
EmbedderTestBackingStoreProducer::RenderTargetType::kOpenGLTexture);
2966+
2967+
fml::CountDownLatch latch(2);
2968+
2969+
context.AddNativeCallback("SignalNativeTest",
2970+
CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
2971+
latch.CountDown();
2972+
}));
2973+
2974+
context.GetCompositor().SetNextPresentCallback(
2975+
[&](const FlutterLayer** layers, size_t layers_count) {
2976+
ASSERT_EQ(layers_count, 20u);
2977+
latch.CountDown();
2978+
});
2979+
2980+
context.GetCompositor().SetUpdateAvoidCacheCallback(
2981+
[](bool* avoid_cache) { *avoid_cache = true; });
2982+
2983+
auto engine = builder.LaunchEngine();
2984+
ASSERT_TRUE(engine.is_valid());
2985+
2986+
FlutterWindowMetricsEvent event = {};
2987+
event.struct_size = sizeof(event);
2988+
event.width = 300;
2989+
event.height = 200;
2990+
event.pixel_ratio = 1.0;
2991+
ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event),
2992+
kSuccess);
2993+
2994+
latch.Wait();
2995+
// Instead of being recycled, the 10 render targets should be created for each
2996+
// of the 7 frames.
2997+
ASSERT_EQ(context.GetCompositor().GetPendingBackingStoresCount(), 0u);
2998+
ASSERT_EQ(context.GetCompositor().GetBackingStoresCreatedCount(), 70u);
2999+
ASSERT_EQ(context.GetCompositor().GetBackingStoresCollectedCount(), 70u);
3000+
}
3001+
29533002
TEST_F(EmbedderTest, CompositorRenderTargetsAreInStableOrder) {
29543003
auto& context = GetEmbedderContext(ContextType::kOpenGLContext);
29553004

0 commit comments

Comments
 (0)