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

Commit 81537b8

Browse files
author
auto-submit[bot]
committed
Revert "[Android] Cache GPU resources using HardwareBuffer's id as key (#50028)"
This reverts commit bb9a6d1.
1 parent 8a4aa8e commit 81537b8

14 files changed

Lines changed: 74 additions & 317 deletions

ci/licenses_golden/excluded_files

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@
282282
../../../flutter/shell/platform/android/external_view_embedder/surface_pool_unittests.cc
283283
../../../flutter/shell/platform/android/flutter_shell_native_unittests.cc
284284
../../../flutter/shell/platform/android/gradle.properties
285-
../../../flutter/shell/platform/android/image_lru_unittests.cc
286285
../../../flutter/shell/platform/android/jni/jni_mock_unittest.cc
287286
../../../flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate_unittests.cc
288287
../../../flutter/shell/platform/android/platform_view_android_unittests.cc

ci/licenses_golden/licenses_flutter

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6348,8 +6348,6 @@ ORIGIN: ../../../flutter/shell/platform/android/image_external_texture.h + ../..
63486348
ORIGIN: ../../../flutter/shell/platform/android/image_external_texture_gl.cc + ../../../flutter/LICENSE
63496349
ORIGIN: ../../../flutter/shell/platform/android/image_external_texture_gl.h + ../../../flutter/LICENSE
63506350
ORIGIN: ../../../flutter/shell/platform/android/image_external_texture_vk.h + ../../../flutter/LICENSE
6351-
ORIGIN: ../../../flutter/shell/platform/android/image_lru.cc + ../../../flutter/LICENSE
6352-
ORIGIN: ../../../flutter/shell/platform/android/image_lru.h + ../../../flutter/LICENSE
63536351
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/BuildConfig.java + ../../../flutter/LICENSE
63546352
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/FlutterInjector.java + ../../../flutter/LICENSE
63556353
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/Log.java + ../../../flutter/LICENSE
@@ -9207,8 +9205,6 @@ FILE: ../../../flutter/shell/platform/android/image_external_texture_gl.cc
92079205
FILE: ../../../flutter/shell/platform/android/image_external_texture_gl.h
92089206
FILE: ../../../flutter/shell/platform/android/image_external_texture_vk.cc
92099207
FILE: ../../../flutter/shell/platform/android/image_external_texture_vk.h
9210-
FILE: ../../../flutter/shell/platform/android/image_lru.cc
9211-
FILE: ../../../flutter/shell/platform/android/image_lru.h
92129208
FILE: ../../../flutter/shell/platform/android/io/flutter/BuildConfig.java
92139209
FILE: ../../../flutter/shell/platform/android/io/flutter/FlutterInjector.java
92149210
FILE: ../../../flutter/shell/platform/android/io/flutter/Log.java

shell/platform/android/BUILD.gn

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ executable("flutter_shell_native_unittests") {
4747
"android_shell_holder_unittests.cc",
4848
"apk_asset_provider_unittests.cc",
4949
"flutter_shell_native_unittests.cc",
50-
"image_lru_unittests.cc",
5150
"platform_view_android_unittests.cc",
5251
]
5352
public_configs = [ "//flutter:config" ]
@@ -112,8 +111,6 @@ source_set("flutter_shell_native_src") {
112111
"image_external_texture_gl.h",
113112
"image_external_texture_vk.cc",
114113
"image_external_texture_vk.h",
115-
"image_lru.cc",
116-
"image_lru.h",
117114
"library_loader.cc",
118115
"ndk_helpers.cc",
119116
"ndk_helpers.h",

shell/platform/android/image_external_texture.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ void ImageExternalTexture::Paint(PaintContext& context,
2525
if (state_ == AttachmentState::kDetached) {
2626
return;
2727
}
28-
latest_bounds_ = bounds;
2928
Attach(context);
3029
const bool should_process_frame = !freeze;
3130
if (should_process_frame) {
@@ -62,7 +61,6 @@ void ImageExternalTexture::OnGrContextCreated() {
6261
void ImageExternalTexture::OnGrContextDestroyed() {
6362
if (state_ == AttachmentState::kAttached) {
6463
dl_image_.reset();
65-
image_lru_.Clear();
6664
Detach();
6765
}
6866
state_ = AttachmentState::kDetached;

shell/platform/android/image_external_texture.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include "flutter/common/graphics/texture.h"
99
#include "flutter/fml/logging.h"
10-
#include "flutter/shell/platform/android/image_lru.h"
1110
#include "flutter/shell/platform/android/jni/platform_view_android_jni.h"
1211
#include "flutter/shell/platform/android/platform_view_android_jni_impl.h"
1312

@@ -59,13 +58,11 @@ class ImageExternalTexture : public flutter::Texture {
5958

6059
fml::jni::ScopedJavaGlobalRef<jobject> image_texture_entry_;
6160
std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
62-
SkRect latest_bounds_ = SkRect::MakeEmpty();
63-
fml::jni::ScopedJavaGlobalRef<jobject> latest_android_image_;
6461

6562
enum class AttachmentState { kUninitialized, kAttached, kDetached };
6663
AttachmentState state_ = AttachmentState::kUninitialized;
64+
6765
sk_sp<flutter::DlImage> dl_image_;
68-
ImageLRU image_lru_ = ImageLRU();
6966

7067
FML_DISALLOW_COPY_AND_ASSIGN(ImageExternalTexture);
7168
};

shell/platform/android/image_external_texture_gl.cc

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88
#include <android/sensor.h>
99

1010
#include "flutter/common/graphics/texture.h"
11+
#include "flutter/display_list/effects/dl_color_source.h"
12+
#include "flutter/flow/layers/layer.h"
1113
#include "flutter/impeller/core/formats.h"
1214
#include "flutter/impeller/display_list/dl_image_impeller.h"
15+
#include "flutter/impeller/renderer/backend/gles/texture_gles.h"
1316
#include "flutter/impeller/toolkit/egl/image.h"
1417
#include "flutter/impeller/toolkit/gles/texture.h"
1518
#include "flutter/shell/platform/android/ndk_helpers.h"
1619
#include "third_party/skia/include/core/SkAlphaType.h"
20+
#include "third_party/skia/include/core/SkColorSpace.h"
1721
#include "third_party/skia/include/core/SkColorType.h"
22+
#include "third_party/skia/include/core/SkImage.h"
23+
#include "third_party/skia/include/gpu/GrBackendSurface.h"
24+
#include "third_party/skia/include/gpu/GrDirectContext.h"
1825
#include "third_party/skia/include/gpu/ganesh/SkImageGanesh.h"
1926
#include "third_party/skia/include/gpu/ganesh/gl/GrGLBackendSurface.h"
2027
#include "third_party/skia/include/gpu/gl/GrGLTypes.h"
@@ -29,61 +36,40 @@ ImageExternalTextureGL::ImageExternalTextureGL(
2936

3037
void ImageExternalTextureGL::Attach(PaintContext& context) {
3138
if (state_ == AttachmentState::kUninitialized) {
32-
if (!latest_android_image_.is_null() && !latest_bounds_.isEmpty()) {
33-
// After detach the cache of textures will have been cleared. If
34-
// there is an android image we must populate it now so that the
35-
// first frame isn't blank.
36-
JavaLocalRef hardware_buffer = HardwareBufferFor(latest_android_image_);
37-
UpdateImage(hardware_buffer, context);
39+
if (!android_image_.is_null()) {
40+
JavaLocalRef hardware_buffer = HardwareBufferFor(android_image_);
41+
AHardwareBuffer* hardware_buffer_ahw =
42+
AHardwareBufferFor(hardware_buffer);
43+
egl_image_ = CreateEGLImage(hardware_buffer_ahw);
3844
CloseHardwareBuffer(hardware_buffer);
3945
}
4046
state_ = AttachmentState::kAttached;
4147
}
4248
}
4349

44-
void ImageExternalTextureGL::UpdateImage(JavaLocalRef& hardware_buffer,
45-
PaintContext& context) {
46-
AHardwareBuffer* latest_hardware_buffer = AHardwareBufferFor(hardware_buffer);
47-
HardwareBufferKey key =
48-
flutter::NDKHelpers::AHardwareBuffer_getId(latest_hardware_buffer);
49-
auto existing_image = image_lru_.FindImage(key);
50-
if (existing_image != nullptr) {
51-
dl_image_ = existing_image;
52-
return;
53-
}
54-
55-
auto egl_image = CreateEGLImage(latest_hardware_buffer);
56-
if (!egl_image.is_valid()) {
57-
return;
58-
}
59-
60-
dl_image_ = CreateDlImage(context, latest_bounds_, key, std::move(egl_image));
61-
gl_entries_.erase(image_lru_.AddImage(dl_image_, key));
50+
void ImageExternalTextureGL::Detach() {
51+
egl_image_.reset();
6252
}
6353

64-
void ImageExternalTextureGL::ProcessFrame(PaintContext& context,
65-
const SkRect& bounds) {
54+
bool ImageExternalTextureGL::MaybeSwapImages() {
6655
JavaLocalRef image = AcquireLatestImage();
6756
if (image.is_null()) {
68-
return;
57+
return false;
6958
}
70-
JavaLocalRef hardware_buffer = HardwareBufferFor(image);
71-
UpdateImage(hardware_buffer, context);
72-
CloseHardwareBuffer(hardware_buffer);
73-
7459
// NOTE: In the following code it is important that old_android_image is
7560
// not closed until after the update of egl_image_ otherwise the image might
7661
// be closed before the old EGLImage referencing it has been deleted. After
7762
// an image is closed the underlying HardwareBuffer may be recycled and used
7863
// for a future frame.
79-
JavaLocalRef old_android_image(latest_android_image_);
80-
latest_android_image_.Reset(image);
64+
JavaLocalRef old_android_image(android_image_);
65+
android_image_.Reset(image);
66+
JavaLocalRef hardware_buffer = HardwareBufferFor(image);
67+
egl_image_ = CreateEGLImage(AHardwareBufferFor(hardware_buffer));
68+
CloseHardwareBuffer(hardware_buffer);
69+
// IMPORTANT: We only close the old image after egl_image_ stops referencing
70+
// it.
8171
CloseImage(old_android_image);
82-
}
83-
84-
void ImageExternalTextureGL::Detach() {
85-
image_lru_.Clear();
86-
gl_entries_.clear();
72+
return true;
8773
}
8874

8975
impeller::UniqueEGLImageKHR ImageExternalTextureGL::CreateEGLImage(
@@ -124,11 +110,26 @@ void ImageExternalTextureGLSkia::Attach(PaintContext& context) {
124110
// After this call state_ will be AttachmentState::kAttached and egl_image_
125111
// will have been created if we still have an Image associated with us.
126112
ImageExternalTextureGL::Attach(context);
113+
GLuint texture_name;
114+
glGenTextures(1, &texture_name);
115+
texture_.reset(impeller::GLTexture{texture_name});
127116
}
128117
}
129118

130119
void ImageExternalTextureGLSkia::Detach() {
131120
ImageExternalTextureGL::Detach();
121+
texture_.reset();
122+
}
123+
124+
void ImageExternalTextureGLSkia::ProcessFrame(PaintContext& context,
125+
const SkRect& bounds) {
126+
const bool swapped = MaybeSwapImages();
127+
if (!swapped && !egl_image_.is_valid()) {
128+
// Nothing to do.
129+
return;
130+
}
131+
BindImageToTexture(egl_image_, texture_.get().texture_name);
132+
dl_image_ = CreateDlImage(context, bounds);
132133
}
133134

134135
void ImageExternalTextureGLSkia::BindImageToTexture(
@@ -144,22 +145,11 @@ void ImageExternalTextureGLSkia::BindImageToTexture(
144145

145146
sk_sp<flutter::DlImage> ImageExternalTextureGLSkia::CreateDlImage(
146147
PaintContext& context,
147-
const SkRect& bounds,
148-
HardwareBufferKey id,
149-
impeller::UniqueEGLImageKHR&& egl_image) {
150-
GLuint texture_name;
151-
glGenTextures(1, &texture_name);
152-
auto gl_texture = impeller::GLTexture{texture_name};
153-
impeller::UniqueGLTexture unique_texture;
154-
unique_texture.reset(gl_texture);
155-
156-
BindImageToTexture(egl_image, unique_texture.get().texture_name);
157-
GrGLTextureInfo textureInfo = {
158-
GL_TEXTURE_EXTERNAL_OES, unique_texture.get().texture_name, GL_RGBA8_OES};
148+
const SkRect& bounds) {
149+
GrGLTextureInfo textureInfo = {GL_TEXTURE_EXTERNAL_OES,
150+
texture_.get().texture_name, GL_RGBA8_OES};
159151
auto backendTexture =
160152
GrBackendTextures::MakeGL(1, 1, skgpu::Mipmapped::kNo, textureInfo);
161-
gl_entries_[id] = GlEntry{.egl_image = std::move(egl_image),
162-
.texture = std::move(unique_texture)};
163153
return DlImage::Make(SkImages::BorrowTextureFrom(
164154
context.gr_context, backendTexture, kTopLeft_GrSurfaceOrigin,
165155
kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr));
@@ -181,11 +171,19 @@ void ImageExternalTextureGLImpeller::Attach(PaintContext& context) {
181171
}
182172
}
183173

174+
void ImageExternalTextureGLImpeller::ProcessFrame(PaintContext& context,
175+
const SkRect& bounds) {
176+
const bool swapped = MaybeSwapImages();
177+
if (!swapped && !egl_image_.is_valid()) {
178+
// Nothing to do.
179+
return;
180+
}
181+
dl_image_ = CreateDlImage(context, bounds);
182+
}
183+
184184
sk_sp<flutter::DlImage> ImageExternalTextureGLImpeller::CreateDlImage(
185185
PaintContext& context,
186-
const SkRect& bounds,
187-
HardwareBufferKey id,
188-
impeller::UniqueEGLImageKHR&& egl_image) {
186+
const SkRect& bounds) {
189187
impeller::TextureDescriptor desc;
190188
desc.type = impeller::TextureType::kTextureExternalOES;
191189
desc.storage_mode = impeller::StorageMode::kDevicePrivate;
@@ -203,10 +201,7 @@ sk_sp<flutter::DlImage> ImageExternalTextureGLImpeller::CreateDlImage(
203201
}
204202
// Associate the hardware buffer image with the texture.
205203
glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES,
206-
(GLeglImageOES)egl_image.get().image);
207-
gl_entries_[id] = GlEntry{
208-
.egl_image = std::move(egl_image),
209-
};
204+
(GLeglImageOES)egl_image_.get().image);
210205
return impeller::DlImageImpeller::Make(texture);
211206
}
212207

shell/platform/android/image_external_texture_gl.h

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#ifndef FLUTTER_SHELL_PLATFORM_ANDROID_IMAGE_EXTERNAL_TEXTURE_GL_H_
66
#define FLUTTER_SHELL_PLATFORM_ANDROID_IMAGE_EXTERNAL_TEXTURE_GL_H_
77

8-
#include <unordered_map>
9-
108
#include "flutter/fml/platform/android/scoped_java_ref.h"
119
#include "flutter/shell/platform/android/image_external_texture.h"
1210

@@ -32,26 +30,14 @@ class ImageExternalTextureGL : public ImageExternalTexture {
3230
protected:
3331
void Attach(PaintContext& context) override;
3432
void Detach() override;
35-
void ProcessFrame(PaintContext& context, const SkRect& bounds) override;
36-
void UpdateImage(JavaLocalRef& hardware_buffer, PaintContext& context);
37-
38-
virtual sk_sp<flutter::DlImage> CreateDlImage(
39-
PaintContext& context,
40-
const SkRect& bounds,
41-
HardwareBufferKey id,
42-
impeller::UniqueEGLImageKHR&& egl_image) = 0;
4333

34+
// Returns true if a new image was acquired and android_image_ and egl_image_
35+
// were updated.
36+
bool MaybeSwapImages();
4437
impeller::UniqueEGLImageKHR CreateEGLImage(AHardwareBuffer* buffer);
4538

46-
struct GlEntry {
47-
impeller::UniqueEGLImageKHR egl_image;
48-
impeller::UniqueGLTexture texture;
49-
};
50-
51-
// Each GL entry is keyed off of the currently active
52-
// hardware buffers and evicted when the hardware buffer
53-
// is removed from the LRU cache.
54-
std::unordered_map<HardwareBufferKey, GlEntry> gl_entries_;
39+
fml::jni::ScopedJavaGlobalRef<jobject> android_image_;
40+
impeller::UniqueEGLImageKHR egl_image_;
5541

5642
FML_DISALLOW_COPY_AND_ASSIGN(ImageExternalTextureGL);
5743
};
@@ -67,14 +53,13 @@ class ImageExternalTextureGLSkia : public ImageExternalTextureGL {
6753
private:
6854
void Attach(PaintContext& context) override;
6955
void Detach() override;
56+
void ProcessFrame(PaintContext& context, const SkRect& bounds) override;
7057

7158
void BindImageToTexture(const impeller::UniqueEGLImageKHR& image, GLuint tex);
59+
sk_sp<flutter::DlImage> CreateDlImage(PaintContext& context,
60+
const SkRect& bounds);
7261

73-
sk_sp<flutter::DlImage> CreateDlImage(
74-
PaintContext& context,
75-
const SkRect& bounds,
76-
HardwareBufferKey id,
77-
impeller::UniqueEGLImageKHR&& egl_image) override;
62+
impeller::UniqueGLTexture texture_;
7863

7964
FML_DISALLOW_COPY_AND_ASSIGN(ImageExternalTextureGLSkia);
8065
};
@@ -90,13 +75,11 @@ class ImageExternalTextureGLImpeller : public ImageExternalTextureGL {
9075

9176
private:
9277
void Attach(PaintContext& context) override;
78+
void ProcessFrame(PaintContext& context, const SkRect& bounds) override;
9379
void Detach() override;
9480

95-
sk_sp<flutter::DlImage> CreateDlImage(
96-
PaintContext& context,
97-
const SkRect& bounds,
98-
HardwareBufferKey id,
99-
impeller::UniqueEGLImageKHR&& egl_image) override;
81+
sk_sp<flutter::DlImage> CreateDlImage(PaintContext& context,
82+
const SkRect& bounds);
10083

10184
const std::shared_ptr<impeller::ContextGLES> impeller_context_;
10285

shell/platform/android/image_external_texture_vk.cc

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
#include "flutter/shell/platform/android/image_external_texture_vk.h"
3-
#include <cstdint>
43

54
#include "flutter/impeller/core/formats.h"
65
#include "flutter/impeller/core/texture_descriptor.h"
@@ -38,26 +37,14 @@ void ImageExternalTextureVK::ProcessFrame(PaintContext& context,
3837
if (image.is_null()) {
3938
return;
4039
}
41-
JavaLocalRef old_android_image(latest_android_image_);
42-
latest_android_image_.Reset(image);
43-
JavaLocalRef hardware_buffer = HardwareBufferFor(latest_android_image_);
40+
JavaLocalRef old_android_image(android_image_);
41+
android_image_.Reset(image);
42+
JavaLocalRef hardware_buffer = HardwareBufferFor(android_image_);
4443
AHardwareBuffer* latest_hardware_buffer = AHardwareBufferFor(hardware_buffer);
4544

4645
AHardwareBuffer_Desc hb_desc = {};
4746
flutter::NDKHelpers::AHardwareBuffer_describe(latest_hardware_buffer,
4847
&hb_desc);
49-
HardwareBufferKey key =
50-
flutter::NDKHelpers::AHardwareBuffer_getId(latest_hardware_buffer);
51-
auto existing_image = image_lru_.FindImage(key);
52-
if (existing_image != nullptr) {
53-
dl_image_ = existing_image;
54-
55-
CloseHardwareBuffer(hardware_buffer);
56-
// IMPORTANT: We have just received a new frame to display so close the
57-
// previous Java Image so that it is recycled and used for a future frame.
58-
CloseImage(old_android_image);
59-
return;
60-
}
6148

6249
impeller::TextureDescriptor desc;
6350
desc.storage_mode = impeller::StorageMode::kDevicePrivate;
@@ -101,10 +88,9 @@ void ImageExternalTextureVK::ProcessFrame(PaintContext& context,
10188
}
10289

10390
dl_image_ = impeller::DlImageImpeller::Make(texture);
104-
image_lru_.AddImage(dl_image_, key);
10591
CloseHardwareBuffer(hardware_buffer);
106-
// IMPORTANT: We have just received a new frame to display so close the
107-
// previous Java Image so that it is recycled and used for a future frame.
92+
// IMPORTANT: We only close the old image after texture stops referencing
93+
// it.
10894
CloseImage(old_android_image);
10995
}
11096

0 commit comments

Comments
 (0)