From 484e2d7ba7ba48aef87fc3996dd786de591b0309 Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Wed, 10 Feb 2021 05:45:40 -0800 Subject: [PATCH] [embedder] Refactor GL texture resolution This moves the texture resolution out of embedder.cc and into embedder external_texture_gl.cc --- shell/platform/embedder/embedder.cc | 48 ++------------ .../embedder/embedder_external_texture_gl.cc | 63 +++++++++++++++++-- .../embedder/embedder_external_texture_gl.h | 13 ++-- 3 files changed, 71 insertions(+), 53 deletions(-) diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 00fd7633a2611..b0cc085921c29 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -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 { - 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* texture = new FlutterOpenGLTexture(); + if (!ptr(user_data, texture_identifier, width, height, texture)) { return nullptr; } - - return image; + return std::unique_ptr(texture); }; } } diff --git a/shell/platform/embedder/embedder_external_texture_gl.cc b/shell/platform/embedder/embedder_external_texture_gl.cc index e7839e1ddc703..0b5ed43c78028 100644 --- a/shell/platform/embedder/embedder_external_texture_gl.cc +++ b/shell/platform/embedder/embedder_external_texture_gl.cc @@ -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 { @@ -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; } @@ -40,6 +44,55 @@ void EmbedderExternalTextureGL::Paint(SkCanvas& canvas, } } +sk_sp EmbedderExternalTextureGL::ResolveTexture( + int64_t texture_id, + GrDirectContext* context, + const SkISize& size) { + std::unique_ptr 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() {} diff --git a/shell/platform/embedder/embedder_external_texture_gl.h b/shell/platform/embedder/embedder_external_texture_gl.h index 68d96cf4f3bee..f1373405aec98 100644 --- a/shell/platform/embedder/embedder_external_texture_gl.h +++ b/shell/platform/embedder/embedder_external_texture_gl.h @@ -7,6 +7,7 @@ #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" @@ -14,10 +15,8 @@ namespace flutter { class EmbedderExternalTextureGL : public flutter::Texture { public: - using ExternalTextureCallback = - std::function(int64_t texture_identifier, - GrDirectContext*, - const SkISize&)>; + using ExternalTextureCallback = std::function< + std::unique_ptr(int64_t, size_t, size_t)>; EmbedderExternalTextureGL(int64_t texture_identifier, const ExternalTextureCallback& callback); @@ -25,9 +24,13 @@ class EmbedderExternalTextureGL : public flutter::Texture { ~EmbedderExternalTextureGL(); private: - ExternalTextureCallback external_texture_callback_; + const ExternalTextureCallback& external_texture_callback_; sk_sp last_image_; + sk_sp ResolveTexture(int64_t texture_id, + GrDirectContext* context, + const SkISize& size); + // |flutter::Texture| void Paint(SkCanvas& canvas, const SkRect& bounds,