Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 5 additions & 43 deletions shell/platform/embedder/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1146,51 +1146,13 @@ FlutterEngineResult FlutterEngineInitialize(size_t version,
nullptr) != nullptr) {
external_texture_callback =
[ptr = open_gl_config->gl_external_texture_frame_callback, user_data](
int64_t texture_identifier, GrDirectContext* context,
const SkISize& size) -> sk_sp<SkImage> {
FlutterOpenGLTexture texture = {};

if (!ptr(user_data, texture_identifier, size.width(), size.height(),
&texture)) {
return nullptr;
}

GrGLTextureInfo gr_texture_info = {texture.target, texture.name,
texture.format};

size_t width = size.width();
size_t height = size.height();

if (texture.width != 0 && texture.height != 0) {
width = texture.width;
height = texture.height;
}

GrBackendTexture gr_backend_texture(width, height, GrMipMapped::kNo,
gr_texture_info);
SkImage::TextureReleaseProc release_proc = texture.destruction_callback;
auto image = SkImage::MakeFromTexture(
context, // context
gr_backend_texture, // texture handle
kTopLeft_GrSurfaceOrigin, // origin
kRGBA_8888_SkColorType, // color type
kPremul_SkAlphaType, // alpha type
nullptr, // colorspace
release_proc, // texture release proc
texture.user_data // texture release context
);

if (!image) {
// In case Skia rejects the image, call the release proc so that
// embedders can perform collection of intermediates.
if (release_proc) {
release_proc(texture.user_data);
}
FML_LOG(ERROR) << "Could not create external texture.";
int64_t texture_identifier, size_t width,
size_t height) -> std::unique_ptr<FlutterOpenGLTexture> {
FlutterOpenGLTexture* texture = new FlutterOpenGLTexture();
if (!ptr(user_data, texture_identifier, width, height, texture)) {
return nullptr;
}

return image;
return std::unique_ptr<FlutterOpenGLTexture>(texture);
};
}
}
Expand Down
63 changes: 58 additions & 5 deletions shell/platform/embedder/embedder_external_texture_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "flutter/shell/platform/embedder/embedder_external_texture_gl.h"

#include "flutter/fml/logging.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSize.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "third_party/skia/include/gpu/GrDirectContext.h"

namespace flutter {

Expand All @@ -23,11 +27,11 @@ void EmbedderExternalTextureGL::Paint(SkCanvas& canvas,
bool freeze,
GrDirectContext* context,
const SkSamplingOptions& sampling) {
if (auto image = external_texture_callback_(
Id(), //
context, //
SkISize::Make(bounds.width(), bounds.height()) //
)) {
if (auto image =
ResolveTexture(Id(), //
context, //
SkISize::Make(bounds.width(), bounds.height()) //
)) {
last_image_ = image;
}

Expand All @@ -40,6 +44,55 @@ void EmbedderExternalTextureGL::Paint(SkCanvas& canvas,
}
}

sk_sp<SkImage> EmbedderExternalTextureGL::ResolveTexture(
int64_t texture_id,
GrDirectContext* context,
const SkISize& size) {
std::unique_ptr<FlutterOpenGLTexture> texture =
external_texture_callback_(texture_id, size.width(), size.height());

if (!texture) {
return nullptr;
}

GrGLTextureInfo gr_texture_info = {texture->target, texture->name,
texture->format};

size_t width = size.width();
size_t height = size.height();

if (texture->width != 0 && texture->height != 0) {
width = texture->width;
height = texture->height;
}

GrBackendTexture gr_backend_texture(width, height, GrMipMapped::kNo,
gr_texture_info);
SkImage::TextureReleaseProc release_proc = texture->destruction_callback;
auto image =
SkImage::MakeFromTexture(context, // context
gr_backend_texture, // texture handle
kTopLeft_GrSurfaceOrigin, // origin
kRGBA_8888_SkColorType, // color type
kPremul_SkAlphaType, // alpha type
nullptr, // colorspace
release_proc, // texture release proc
texture->user_data // texture release context
);

if (!image) {
// In case Skia rejects the image, call the release proc so that
// embedders can perform collection of intermediates.
if (release_proc) {
release_proc(texture->user_data);
}
FML_LOG(ERROR) << "Could not create external texture->";
return nullptr;
}

return image;
}

// |flutter::Texture|
void EmbedderExternalTextureGL::OnGrContextCreated() {}

Expand Down
13 changes: 8 additions & 5 deletions shell/platform/embedder/embedder_external_texture_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,30 @@

#include "flutter/common/graphics/texture.h"
#include "flutter/fml/macros.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSize.h"

namespace flutter {

class EmbedderExternalTextureGL : public flutter::Texture {
public:
using ExternalTextureCallback =
std::function<sk_sp<SkImage>(int64_t texture_identifier,
GrDirectContext*,
const SkISize&)>;
using ExternalTextureCallback = std::function<
std::unique_ptr<FlutterOpenGLTexture>(int64_t, size_t, size_t)>;

EmbedderExternalTextureGL(int64_t texture_identifier,
const ExternalTextureCallback& callback);

~EmbedderExternalTextureGL();

private:
ExternalTextureCallback external_texture_callback_;
const ExternalTextureCallback& external_texture_callback_;
sk_sp<SkImage> last_image_;

sk_sp<SkImage> ResolveTexture(int64_t texture_id,
GrDirectContext* context,
const SkISize& size);

// |flutter::Texture|
void Paint(SkCanvas& canvas,
const SkRect& bounds,
Expand Down