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

Commit a45a052

Browse files
committed
[embedder] Refactor GL texture resolution
This moves the texture resolution out of embedder.cc and into embedder external_texture_gl.cc
1 parent 2e10a97 commit a45a052

File tree

4 files changed

+74
-56
lines changed

4 files changed

+74
-56
lines changed

shell/platform/embedder/embedder.cc

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,51 +1146,13 @@ FlutterEngineResult FlutterEngineInitialize(size_t version,
11461146
nullptr) != nullptr) {
11471147
external_texture_callback =
11481148
[ptr = open_gl_config->gl_external_texture_frame_callback, user_data](
1149-
int64_t texture_identifier, GrDirectContext* context,
1150-
const SkISize& size) -> sk_sp<SkImage> {
1151-
FlutterOpenGLTexture texture = {};
1152-
1153-
if (!ptr(user_data, texture_identifier, size.width(), size.height(),
1154-
&texture)) {
1155-
return nullptr;
1156-
}
1157-
1158-
GrGLTextureInfo gr_texture_info = {texture.target, texture.name,
1159-
texture.format};
1160-
1161-
size_t width = size.width();
1162-
size_t height = size.height();
1163-
1164-
if (texture.width != 0 && texture.height != 0) {
1165-
width = texture.width;
1166-
height = texture.height;
1167-
}
1168-
1169-
GrBackendTexture gr_backend_texture(width, height, GrMipMapped::kNo,
1170-
gr_texture_info);
1171-
SkImage::TextureReleaseProc release_proc = texture.destruction_callback;
1172-
auto image = SkImage::MakeFromTexture(
1173-
context, // context
1174-
gr_backend_texture, // texture handle
1175-
kTopLeft_GrSurfaceOrigin, // origin
1176-
kRGBA_8888_SkColorType, // color type
1177-
kPremul_SkAlphaType, // alpha type
1178-
nullptr, // colorspace
1179-
release_proc, // texture release proc
1180-
texture.user_data // texture release context
1181-
);
1182-
1183-
if (!image) {
1184-
// In case Skia rejects the image, call the release proc so that
1185-
// embedders can perform collection of intermediates.
1186-
if (release_proc) {
1187-
release_proc(texture.user_data);
1188-
}
1189-
FML_LOG(ERROR) << "Could not create external texture.";
1149+
int64_t texture_identifier, size_t width,
1150+
size_t height) -> std::unique_ptr<FlutterOpenGLTexture> {
1151+
FlutterOpenGLTexture* texture = new FlutterOpenGLTexture();
1152+
if (!ptr(user_data, texture_identifier, width, height, texture)) {
11901153
return nullptr;
11911154
}
1192-
1193-
return image;
1155+
return std::unique_ptr<FlutterOpenGLTexture>(texture);
11941156
};
11951157
}
11961158
}

shell/platform/embedder/embedder_external_texture_gl.cc

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include "flutter/shell/platform/embedder/embedder_external_texture_gl.h"
66

77
#include "flutter/fml/logging.h"
8+
#include "third_party/skia/include/core/SkImage.h"
9+
#include "third_party/skia/include/core/SkSize.h"
10+
#include "third_party/skia/include/gpu/GrBackendSurface.h"
11+
#include "third_party/skia/include/gpu/GrDirectContext.h"
812

913
namespace flutter {
1014

@@ -23,11 +27,11 @@ void EmbedderExternalTextureGL::Paint(SkCanvas& canvas,
2327
bool freeze,
2428
GrDirectContext* context,
2529
const SkSamplingOptions& sampling) {
26-
if (auto image = external_texture_callback_(
27-
Id(), //
28-
context, //
29-
SkISize::Make(bounds.width(), bounds.height()) //
30-
)) {
30+
if (auto image =
31+
ResolveTexture(Id(), //
32+
context, //
33+
SkISize::Make(bounds.width(), bounds.height()) //
34+
)) {
3135
last_image_ = image;
3236
}
3337

@@ -40,6 +44,55 @@ void EmbedderExternalTextureGL::Paint(SkCanvas& canvas,
4044
}
4145
}
4246

47+
sk_sp<SkImage> EmbedderExternalTextureGL::ResolveTexture(
48+
int64_t texture_id,
49+
GrDirectContext* context,
50+
const SkISize& size) {
51+
std::unique_ptr<FlutterOpenGLTexture> texture =
52+
external_texture_callback_(texture_id, size.width(), size.height());
53+
54+
if (!texture) {
55+
return nullptr;
56+
}
57+
58+
GrGLTextureInfo gr_texture_info = {texture->target, texture->name,
59+
texture->format};
60+
61+
size_t width = size.width();
62+
size_t height = size.height();
63+
64+
if (texture->width != 0 && texture->height != 0) {
65+
width = texture->width;
66+
height = texture->height;
67+
}
68+
69+
GrBackendTexture gr_backend_texture(width, height, GrMipMapped::kNo,
70+
gr_texture_info);
71+
SkImage::TextureReleaseProc release_proc = texture->destruction_callback;
72+
auto image =
73+
SkImage::MakeFromTexture(context, // context
74+
gr_backend_texture, // texture handle
75+
kTopLeft_GrSurfaceOrigin, // origin
76+
kRGBA_8888_SkColorType, // color type
77+
kPremul_SkAlphaType, // alpha type
78+
nullptr, // colorspace
79+
release_proc, // texture release proc
80+
texture->user_data // texture release context
81+
);
82+
83+
if (!image) {
84+
// In case Skia rejects the image, call the release proc so that
85+
// embedders can perform collection of intermediates.
86+
if (release_proc) {
87+
release_proc(texture->user_data);
88+
}
89+
FML_LOG(ERROR) << "Could not create external texture->";
90+
return nullptr;
91+
}
92+
93+
return image;
94+
}
95+
4396
// |flutter::Texture|
4497
void EmbedderExternalTextureGL::OnGrContextCreated() {}
4598

shell/platform/embedder/embedder_external_texture_gl.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,30 @@
77

88
#include "flutter/common/graphics/texture.h"
99
#include "flutter/fml/macros.h"
10+
#include "flutter/shell/platform/embedder/embedder.h"
1011
#include "third_party/skia/include/core/SkImage.h"
1112
#include "third_party/skia/include/core/SkSize.h"
1213

1314
namespace flutter {
1415

1516
class EmbedderExternalTextureGL : public flutter::Texture {
1617
public:
17-
using ExternalTextureCallback =
18-
std::function<sk_sp<SkImage>(int64_t texture_identifier,
19-
GrDirectContext*,
20-
const SkISize&)>;
18+
using ExternalTextureCallback = std::function<
19+
std::unique_ptr<FlutterOpenGLTexture>(int64_t, size_t, size_t)>;
2120

2221
EmbedderExternalTextureGL(int64_t texture_identifier,
2322
const ExternalTextureCallback& callback);
2423

2524
~EmbedderExternalTextureGL();
2625

2726
private:
28-
ExternalTextureCallback external_texture_callback_;
27+
const ExternalTextureCallback& external_texture_callback_;
2928
sk_sp<SkImage> last_image_;
3029

30+
sk_sp<SkImage> ResolveTexture(int64_t texture_id,
31+
GrDirectContext* context,
32+
const SkISize& size);
33+
3134
// |flutter::Texture|
3235
void Paint(SkCanvas& canvas,
3336
const SkRect& bounds,

shell/platform/embedder/platform_view_embedder.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class PlatformViewEmbedder final : public PlatformView {
5050

5151
// Create a platform view that sets up a software rasterizer.
5252
PlatformViewEmbedder(
53-
PlatformView::Delegate& delegate,
53+
const PlatformView::Delegate& delegate,
5454
flutter::TaskRunners task_runners,
5555
EmbedderSurfaceSoftware::SoftwareDispatchTable software_dispatch_table,
5656
PlatformDispatchTable platform_dispatch_table,
@@ -59,7 +59,7 @@ class PlatformViewEmbedder final : public PlatformView {
5959
#ifdef SHELL_ENABLE_GL
6060
// Creates a platform view that sets up an OpenGL rasterizer.
6161
PlatformViewEmbedder(
62-
PlatformView::Delegate& delegate,
62+
const PlatformView::Delegate& delegate,
6363
flutter::TaskRunners task_runners,
6464
EmbedderSurfaceGL::GLDispatchTable gl_dispatch_table,
6565
bool fbo_reset_after_present,
@@ -70,7 +70,7 @@ class PlatformViewEmbedder final : public PlatformView {
7070
#ifdef SHELL_ENABLE_METAL
7171
// Creates a platform view that sets up an metal rasterizer.
7272
PlatformViewEmbedder(
73-
PlatformView::Delegate& delegate,
73+
const PlatformView::Delegate& delegate,
7474
flutter::TaskRunners task_runners,
7575
std::unique_ptr<EmbedderSurfaceMetal> embedder_surface,
7676
PlatformDispatchTable platform_dispatch_table,

0 commit comments

Comments
 (0)