From de7301573dea77b3deb31c50b3e5ad564b0a3ebf Mon Sep 17 00:00:00 2001 From: Xiaowei Guan Date: Fri, 14 Jun 2024 10:09:23 +0800 Subject: [PATCH] [Tizen] Remove FlutterEngineRegisterExternalTextureWithType interface Add texture type to FlutterOpenGLTexture --- shell/platform/embedder/BUILD.gn | 2 + shell/platform/embedder/embedder.cc | 26 +-- shell/platform/embedder/embedder.h | 61 ++----- shell/platform/embedder/embedder_engine.cc | 9 - shell/platform/embedder/embedder_engine.h | 2 - .../embedder/embedder_external_texture_gl.cc | 156 ++-------------- .../embedder/embedder_external_texture_gl.h | 69 +------- .../embedder_external_texture_gl_impeller.cc | 166 ++++++++++++++++++ .../embedder_external_texture_gl_impeller.h | 62 +++++++ .../embedder_external_texture_resolver.cc | 42 ++--- .../embedder_external_texture_resolver.h | 7 +- 11 files changed, 285 insertions(+), 317 deletions(-) create mode 100644 shell/platform/embedder/embedder_external_texture_gl_impeller.cc create mode 100644 shell/platform/embedder/embedder_external_texture_gl_impeller.h diff --git a/shell/platform/embedder/BUILD.gn b/shell/platform/embedder/BUILD.gn index ba7d1276f54a7..c097e3a587d19 100644 --- a/shell/platform/embedder/BUILD.gn +++ b/shell/platform/embedder/BUILD.gn @@ -118,6 +118,8 @@ template("embedder_source_set") { sources += [ "embedder_external_texture_gl.cc", "embedder_external_texture_gl.h", + "embedder_external_texture_gl_impeller.cc", + "embedder_external_texture_gl_impeller.h", "embedder_surface_gl.cc", "embedder_surface_gl.h", ] diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 0921b71a04cd0..5acf151c9bb02 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -2029,8 +2029,8 @@ FlutterEngineResult FlutterEngineInitialize(size_t version, } return texture; }; - external_texture_resolver = - std::make_unique(external_texture_callback); + external_texture_resolver = std::make_unique( + external_texture_callback, settings.enable_impeller); } } #endif @@ -2712,26 +2712,6 @@ FlutterEngineResult FlutterEngineRegisterExternalTexture( return kSuccess; } -FlutterEngineResult FlutterEngineRegisterExternalTextureWithType( - FLUTTER_API_SYMBOL(FlutterEngine) engine, - int64_t texture_identifier, - FlutterTextureType type) { - if (engine == nullptr) { - return LOG_EMBEDDER_ERROR(kInvalidArguments, "Engine handle was invalid."); - } - - if (texture_identifier == 0) { - return LOG_EMBEDDER_ERROR(kInvalidArguments, - "Texture identifier was invalid."); - } - if (!reinterpret_cast(engine)->RegisterTexture( - texture_identifier, type)) { - return LOG_EMBEDDER_ERROR(kInternalInconsistency, - "Could not register the specified texture."); - } - return kSuccess; -} - FlutterEngineResult FlutterEngineUnregisterExternalTexture( FLUTTER_API_SYMBOL(FlutterEngine) engine, int64_t texture_identifier) { @@ -3307,8 +3287,6 @@ FlutterEngineResult FlutterEngineGetProcAddresses( SET_PROC(SendPlatformMessageResponse, FlutterEngineSendPlatformMessageResponse); SET_PROC(RegisterExternalTexture, FlutterEngineRegisterExternalTexture); - SET_PROC(RegisterExternalTextureWithType, - FlutterEngineRegisterExternalTextureWithType); SET_PROC(UnregisterExternalTexture, FlutterEngineUnregisterExternalTexture); SET_PROC(MarkExternalTextureFrameAvailable, FlutterEngineMarkExternalTextureFrameAvailable); diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index 6a879452cd139..72d07b8571578 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -362,6 +362,11 @@ typedef enum { kFlutterSoftwarePixelFormatNative32, } FlutterSoftwarePixelFormat; +typedef enum { + kFlutterGLImpellerTexturePixelBuffer, + kFlutterGLImpellerTextureGpuSuface, +} FlutterGLImpellerTextureType; + typedef struct { /// Target texture of the active texture unit (example GL_TEXTURE_2D or /// GL_TEXTURE_RECTANGLE). @@ -370,10 +375,16 @@ typedef struct { uint32_t name; /// The texture format (example GL_RGBA8). uint32_t format; - /// User data to be returned on the invocation of the destruction callback. - void* user_data; + /// The pixel data buffer. + const uint8_t* buffer; + /// The size of buffer. + size_t buffer_size; /// Callback invoked that texture start binding. BoolCallback bind_callback; + /// the type of the texture. + FlutterGLImpellerTextureType impeller_texture_type; + /// User data to be returned on the invocation of the destruction callback. + void* user_data; /// Callback invoked (on an engine managed thread) that asks the embedder to /// collect the texture. VoidCallback destruction_callback; @@ -385,10 +396,6 @@ typedef struct { size_t width; /// Height of the texture. size_t height; - /// The pixel data buffer. - const uint8_t* buffer; - /// The size of buffer. - size_t buffer_size; } FlutterOpenGLTexture; typedef struct { @@ -408,15 +415,6 @@ typedef struct { VoidCallback destruction_callback; } FlutterOpenGLFramebuffer; -// Possible values for the type specified in FlutterDesktopTextureInfo. -// Additional types may be added in the future. -typedef enum { - // A Pixel buffer-based texture. - kFlutterPixelBufferTexture, - // A platform-specific GPU surface-backed texture. - kFlutterGpuSurfaceTexture -} FlutterTextureType; - typedef FlutterTransformation (*TransformationCallback)(void* /* user data */); typedef uint32_t (*UIntCallback)(void* /* user data */); typedef bool (*SoftwareSurfacePresentCallback)(void* /* user data */, @@ -2694,32 +2692,6 @@ FlutterEngineResult FlutterEngineRegisterExternalTexture( FLUTTER_API_SYMBOL(FlutterEngine) engine, int64_t texture_identifier); -//------------------------------------------------------------------------------ -/// @brief Register an external texture with a unique (per engine) -/// identifier. Only rendering backends that support external -/// textures accept external texture registrations. After the -/// external texture is registered, the application can mark that a -/// frame is available by calling -/// `FlutterEngineMarkExternalTextureFrameAvailable`. -/// -/// @see FlutterEngineUnregisterExternalTexture() -/// @see FlutterEngineMarkExternalTextureFrameAvailable() -/// -/// @param[in] engine A running engine instance. -/// @param[in] texture_identifier The identifier of the texture to register -/// with the engine. The embedder may supply new -/// frames to this texture using the same -/// identifier. -/// @param[in] type The type of the texture. -/// -/// @return The result of the call. -/// -FLUTTER_EXPORT -FlutterEngineResult FlutterEngineRegisterExternalTextureWithType( - FLUTTER_API_SYMBOL(FlutterEngine) engine, - int64_t texture_identifier, - FlutterTextureType type); - //------------------------------------------------------------------------------ /// @brief Unregister a previous texture registration. /// @@ -3172,11 +3144,6 @@ typedef FlutterEngineResult (*FlutterEngineSendPlatformMessageResponseFnPtr)( typedef FlutterEngineResult (*FlutterEngineRegisterExternalTextureFnPtr)( FLUTTER_API_SYMBOL(FlutterEngine) engine, int64_t texture_identifier); -typedef FlutterEngineResult ( - *FlutterEngineRegisterExternalTextureWithTypeFnPtr)( - FLUTTER_API_SYMBOL(FlutterEngine) engine, - int64_t texture_identifier, - FlutterTextureType type); typedef FlutterEngineResult (*FlutterEngineUnregisterExternalTextureFnPtr)( FLUTTER_API_SYMBOL(FlutterEngine) engine, int64_t texture_identifier); @@ -3263,8 +3230,6 @@ typedef struct { PlatformMessageReleaseResponseHandle; FlutterEngineSendPlatformMessageResponseFnPtr SendPlatformMessageResponse; FlutterEngineRegisterExternalTextureFnPtr RegisterExternalTexture; - FlutterEngineRegisterExternalTextureWithTypeFnPtr - RegisterExternalTextureWithType; FlutterEngineUnregisterExternalTextureFnPtr UnregisterExternalTexture; FlutterEngineMarkExternalTextureFrameAvailableFnPtr MarkExternalTextureFrameAvailable; diff --git a/shell/platform/embedder/embedder_engine.cc b/shell/platform/embedder/embedder_engine.cc index 16a4e08f17a94..b34da8a287f4e 100644 --- a/shell/platform/embedder/embedder_engine.cc +++ b/shell/platform/embedder/embedder_engine.cc @@ -153,15 +153,6 @@ bool EmbedderEngine::RegisterTexture(int64_t texture) { return true; } -bool EmbedderEngine::RegisterTexture(int64_t texture, FlutterTextureType type) { - if (!IsValid()) { - return false; - } - shell_->GetPlatformView()->RegisterTexture( - external_texture_resolver_->ResolveExternalTexture(texture, type)); - return true; -} - bool EmbedderEngine::UnregisterTexture(int64_t texture) { if (!IsValid()) { return false; diff --git a/shell/platform/embedder/embedder_engine.h b/shell/platform/embedder/embedder_engine.h index 472d108ddf8c8..a587eeb3eaa74 100644 --- a/shell/platform/embedder/embedder_engine.h +++ b/shell/platform/embedder/embedder_engine.h @@ -58,8 +58,6 @@ class EmbedderEngine { bool RegisterTexture(int64_t texture); - bool RegisterTexture(int64_t texture, FlutterTextureType type); - bool UnregisterTexture(int64_t texture); bool MarkTextureFrameAvailable(int64_t texture); diff --git a/shell/platform/embedder/embedder_external_texture_gl.cc b/shell/platform/embedder/embedder_external_texture_gl.cc index 978b8409921e0..bbf30d143d133 100644 --- a/shell/platform/embedder/embedder_external_texture_gl.cc +++ b/shell/platform/embedder/embedder_external_texture_gl.cc @@ -5,11 +5,6 @@ #include "flutter/shell/platform/embedder/embedder_external_texture_gl.h" #include "flutter/fml/logging.h" -#include "flutter/impeller/display_list/dl_image_impeller.h" -#include "flutter/impeller/renderer/backend/gles/context_gles.h" -#include "flutter/impeller/renderer/backend/gles/texture_gles.h" -#include "impeller/aiks/aiks_context.h" -#include "impeller/renderer/backend/gles/gles.h" #include "include/core/SkCanvas.h" #include "include/core/SkPaint.h" #include "third_party/skia/include/core/SkAlphaType.h" @@ -42,7 +37,7 @@ void EmbedderExternalTextureGL::Paint(PaintContext& context, if (last_image_ == nullptr) { last_image_ = ResolveTexture(Id(), // - context, // + context.gr_context, // SkISize::Make(bounds.width(), bounds.height()) // ); } @@ -60,34 +55,12 @@ void EmbedderExternalTextureGL::Paint(PaintContext& context, } } -// |flutter::Texture| -void EmbedderExternalTextureGL::OnGrContextCreated() {} - -// |flutter::Texture| -void EmbedderExternalTextureGL::OnGrContextDestroyed() {} - -// |flutter::Texture| -void EmbedderExternalTextureGL::MarkNewFrameAvailable() { - last_image_ = nullptr; -} - -// |flutter::Texture| -void EmbedderExternalTextureGL::OnTextureUnregistered() {} - -EmbedderExternalTextureSkiaGL::EmbedderExternalTextureSkiaGL( - int64_t texture_identifier, - const ExternalTextureCallback& callback) - : EmbedderExternalTextureGL(texture_identifier, callback) {} - -EmbedderExternalTextureSkiaGL::~EmbedderExternalTextureSkiaGL() = default; - -sk_sp EmbedderExternalTextureSkiaGL::ResolveTexture( +sk_sp EmbedderExternalTextureGL::ResolveTexture( int64_t texture_id, - PaintContext& context, + GrDirectContext* context, const SkISize& size) { - GrDirectContext* gr_context = context.gr_context; - gr_context->flushAndSubmit(); - gr_context->resetContext(kAll_GrBackendState); + context->flushAndSubmit(); + context->resetContext(kAll_GrBackendState); std::unique_ptr texture = external_texture_callback_(texture_id, size.width(), size.height()); @@ -110,7 +83,7 @@ sk_sp EmbedderExternalTextureSkiaGL::ResolveTexture( width, height, skgpu::Mipmapped::kNo, gr_texture_info); SkImages::TextureReleaseProc release_proc = texture->destruction_callback; auto image = - SkImages::BorrowTextureFrom(gr_context, // context + SkImages::BorrowTextureFrom(context, // context gr_backend_texture, // texture handle kTopLeft_GrSurfaceOrigin, // origin kRGBA_8888_SkColorType, // color type @@ -126,6 +99,7 @@ sk_sp EmbedderExternalTextureSkiaGL::ResolveTexture( if (release_proc) { release_proc(texture->user_data); } + FML_LOG(ERROR) << "Could not create external texture->"; return nullptr; } @@ -133,116 +107,18 @@ sk_sp EmbedderExternalTextureSkiaGL::ResolveTexture( return DlImage::Make(std::move(image)); } -EmbedderExternalTextureGLImpellerPixelBuffer:: - EmbedderExternalTextureGLImpellerPixelBuffer( - int64_t texture_identifier, - const ExternalTextureCallback& callback) - : EmbedderExternalTextureGL(texture_identifier, callback) {} - -sk_sp EmbedderExternalTextureGLImpellerPixelBuffer::ResolveTexture( - int64_t texture_id, - PaintContext& context, - const SkISize& size) { - std::unique_ptr texture = - external_texture_callback_(texture_id, size.width(), size.height()); - - if (!texture) { - return nullptr; - } - - size_t width = size.width(); - size_t height = size.height(); +// |flutter::Texture| +void EmbedderExternalTextureGL::OnGrContextCreated() {} - if (texture->width != 0 && texture->height != 0) { - width = texture->width; - height = texture->height; - } +// |flutter::Texture| +void EmbedderExternalTextureGL::OnGrContextDestroyed() {} - impeller::TextureDescriptor desc; - desc.type = impeller::TextureType::kTexture2D; - impeller::AiksContext* aiks_context = context.aiks_context; - const auto& gl_context = - impeller::ContextGLES::Cast(*aiks_context->GetContext()); - desc.storage_mode = impeller::StorageMode::kDevicePrivate; - desc.format = impeller::PixelFormat::kR8G8B8A8UNormInt; - desc.size = {static_cast(width), static_cast(height)}; - desc.mip_count = 1; - auto textureGLES = - std::make_shared(gl_context.GetReactor(), desc); - if (!textureGLES->SetContents(texture->buffer, texture->buffer_size)) { - if (texture->destruction_callback) { - texture->destruction_callback(texture->user_data); - } - return nullptr; - } - if (texture->destruction_callback) { - texture->destruction_callback(texture->user_data); - } - return impeller::DlImageImpeller::Make(textureGLES); +// |flutter::Texture| +void EmbedderExternalTextureGL::MarkNewFrameAvailable() { + last_image_ = nullptr; } -EmbedderExternalTextureGLImpellerPixelBuffer:: - ~EmbedderExternalTextureGLImpellerPixelBuffer() = default; - -EmbedderExternalTextureGLImpellerSurface:: - EmbedderExternalTextureGLImpellerSurface( - int64_t texture_identifier, - const ExternalTextureCallback& callback) - : EmbedderExternalTextureGL(texture_identifier, callback) {} - -sk_sp EmbedderExternalTextureGLImpellerSurface::ResolveTexture( - int64_t texture_id, - PaintContext& context, - const SkISize& size) { - std::unique_ptr texture = - external_texture_callback_(texture_id, size.width(), size.height()); - - if (!texture) { - return nullptr; - } - size_t width = size.width(); - size_t height = size.height(); - - if (texture->width != 0 && texture->height != 0) { - width = texture->width; - height = texture->height; - } - - impeller::TextureDescriptor desc; - desc.type = impeller::TextureType::kTextureExternalOES; - impeller::AiksContext* aiks_context = context.aiks_context; - const auto& gl_context = - impeller::ContextGLES::Cast(*aiks_context->GetContext()); - desc.storage_mode = impeller::StorageMode::kDevicePrivate; - desc.format = impeller::PixelFormat::kR8G8B8A8UNormInt; - desc.size = {static_cast(width), static_cast(height)}; - desc.mip_count = 1; - auto textureGLES = std::make_shared( - gl_context.GetReactor(), desc, - impeller::TextureGLES::IsWrapped::kWrapped); - textureGLES->SetCoordinateSystem( - impeller::TextureCoordinateSystem::kUploadFromHost); - if (!textureGLES->Bind()) { - if (texture->destruction_callback) { - texture->destruction_callback(texture->user_data); - } - return nullptr; - } - - if (!texture->bind_callback(texture->user_data)) { - if (texture->destruction_callback) { - texture->destruction_callback(texture->user_data); - } - return nullptr; - } - - if (texture->destruction_callback) { - texture->destruction_callback(texture->user_data); - } - - return impeller::DlImageImpeller::Make(textureGLES); -} +// |flutter::Texture| +void EmbedderExternalTextureGL::OnTextureUnregistered() {} -EmbedderExternalTextureGLImpellerSurface:: - ~EmbedderExternalTextureGLImpellerSurface() = default; } // namespace flutter diff --git a/shell/platform/embedder/embedder_external_texture_gl.h b/shell/platform/embedder/embedder_external_texture_gl.h index 15457c4c0b00b..b01991d0cf1df 100644 --- a/shell/platform/embedder/embedder_external_texture_gl.h +++ b/shell/platform/embedder/embedder_external_texture_gl.h @@ -22,6 +22,14 @@ class EmbedderExternalTextureGL : public flutter::Texture { ~EmbedderExternalTextureGL(); + private: + 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(PaintContext& context, const SkRect& bounds, @@ -40,70 +48,9 @@ class EmbedderExternalTextureGL : public flutter::Texture { // |flutter::Texture| void OnTextureUnregistered() override; - protected: - virtual sk_sp ResolveTexture(int64_t texture_id, - PaintContext& context, - const SkISize& size) = 0; - const ExternalTextureCallback& external_texture_callback_; - sk_sp last_image_; - - private: FML_DISALLOW_COPY_AND_ASSIGN(EmbedderExternalTextureGL); }; -class EmbedderExternalTextureSkiaGL - : public flutter::EmbedderExternalTextureGL { - public: - EmbedderExternalTextureSkiaGL(int64_t texture_identifier, - const ExternalTextureCallback& callback); - - ~EmbedderExternalTextureSkiaGL(); - - protected: - sk_sp ResolveTexture(int64_t texture_id, - PaintContext& context, - const SkISize& size) override; - - private: - FML_DISALLOW_COPY_AND_ASSIGN(EmbedderExternalTextureSkiaGL); -}; - -class EmbedderExternalTextureGLImpellerPixelBuffer - : public flutter::EmbedderExternalTextureGL { - public: - EmbedderExternalTextureGLImpellerPixelBuffer( - int64_t texture_identifier, - const ExternalTextureCallback& callback); - - ~EmbedderExternalTextureGLImpellerPixelBuffer(); - - protected: - sk_sp ResolveTexture(int64_t texture_id, - PaintContext& context, - const SkISize& size) override; - - private: - FML_DISALLOW_COPY_AND_ASSIGN(EmbedderExternalTextureGLImpellerPixelBuffer); -}; - -class EmbedderExternalTextureGLImpellerSurface - : public flutter::EmbedderExternalTextureGL { - public: - EmbedderExternalTextureGLImpellerSurface( - int64_t texture_identifier, - const ExternalTextureCallback& callback); - - ~EmbedderExternalTextureGLImpellerSurface(); - - protected: - sk_sp ResolveTexture(int64_t texture_id, - PaintContext& context, - const SkISize& size) override; - - private: - FML_DISALLOW_COPY_AND_ASSIGN(EmbedderExternalTextureGLImpellerSurface); -}; - } // namespace flutter #endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_EXTERNAL_TEXTURE_GL_H_ diff --git a/shell/platform/embedder/embedder_external_texture_gl_impeller.cc b/shell/platform/embedder/embedder_external_texture_gl_impeller.cc new file mode 100644 index 0000000000000..adbc8cc895706 --- /dev/null +++ b/shell/platform/embedder/embedder_external_texture_gl_impeller.cc @@ -0,0 +1,166 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/embedder/embedder_external_texture_gl_impeller.h" + +#include "flutter/fml/logging.h" +#include "flutter/impeller/display_list/dl_image_impeller.h" +#include "flutter/impeller/renderer/backend/gles/context_gles.h" +#include "flutter/impeller/renderer/backend/gles/texture_gles.h" +#include "impeller/aiks/aiks_context.h" +#include "impeller/renderer/backend/gles/gles.h" +#include "include/core/SkCanvas.h" +#include "include/core/SkPaint.h" + +namespace flutter { + +EmbedderExternalTextureGLImpeller::EmbedderExternalTextureGLImpeller( + int64_t texture_identifier, + const ExternalTextureCallback& callback) + : Texture(texture_identifier), external_texture_callback_(callback) { + FML_DCHECK(external_texture_callback_); +} + +EmbedderExternalTextureGLImpeller::~EmbedderExternalTextureGLImpeller() = + default; + +// |flutter::Texture| +void EmbedderExternalTextureGLImpeller::Paint(PaintContext& context, + const SkRect& bounds, + bool freeze, + const DlImageSampling sampling) { + if (last_image_ == nullptr) { + last_image_ = + ResolveTexture(Id(), // + context, // + SkISize::Make(bounds.width(), bounds.height()) // + ); + } + + DlCanvas* canvas = context.canvas; + const DlPaint* paint = context.paint; + + if (last_image_) { + SkRect image_bounds = SkRect::Make(last_image_->bounds()); + if (bounds != image_bounds) { + canvas->DrawImageRect(last_image_, image_bounds, bounds, sampling, paint); + } else { + canvas->DrawImage(last_image_, {bounds.x(), bounds.y()}, sampling, paint); + } + } +} + +// |flutter::Texture| +void EmbedderExternalTextureGLImpeller::OnGrContextCreated() {} + +// |flutter::Texture| +void EmbedderExternalTextureGLImpeller::OnGrContextDestroyed() {} + +// |flutter::Texture| +void EmbedderExternalTextureGLImpeller::MarkNewFrameAvailable() { + last_image_ = nullptr; +} + +// |flutter::Texture| +void EmbedderExternalTextureGLImpeller::OnTextureUnregistered() {} + +sk_sp EmbedderExternalTextureGLImpeller::ResolveTexture( + int64_t texture_id, + PaintContext& context, + const SkISize& size) { + std::unique_ptr texture = + external_texture_callback_(texture_id, size.width(), size.height()); + if (!texture) { + return nullptr; + } + if (texture->impeller_texture_type == + FlutterGLImpellerTextureType::kFlutterGLImpellerTexturePixelBuffer) { + return ResolvePixelBufferTexture(texture.get(), context, size); + } else { + return ResolveGpuSurfaceTexture(texture.get(), context, size); + } +} + +sk_sp EmbedderExternalTextureGLImpeller::ResolvePixelBufferTexture( + FlutterOpenGLTexture* texture, + PaintContext& context, + const SkISize& size) { + size_t width = size.width(); + size_t height = size.height(); + if (texture->width != 0 && texture->height != 0) { + width = texture->width; + height = texture->height; + } + impeller::TextureDescriptor desc; + desc.type = impeller::TextureType::kTexture2D; + + impeller::AiksContext* aiks_context = context.aiks_context; + const auto& gl_context = + impeller::ContextGLES::Cast(*aiks_context->GetContext()); + desc.storage_mode = impeller::StorageMode::kDevicePrivate; + desc.format = impeller::PixelFormat::kR8G8B8A8UNormInt; + desc.size = {static_cast(width), static_cast(height)}; + desc.mip_count = 1; + + auto textureGLES = + std::make_shared(gl_context.GetReactor(), desc); + if (!textureGLES->SetContents(texture->buffer, texture->buffer_size)) { + if (texture->destruction_callback) { + texture->destruction_callback(texture->user_data); + } + return nullptr; + } + if (texture->destruction_callback) { + texture->destruction_callback(texture->user_data); + } + return impeller::DlImageImpeller::Make(textureGLES); +} + +sk_sp EmbedderExternalTextureGLImpeller::ResolveGpuSurfaceTexture( + FlutterOpenGLTexture* texture, + PaintContext& context, + const SkISize& size) { + size_t width = size.width(); + size_t height = size.height(); + if (texture->width != 0 && texture->height != 0) { + width = texture->width; + height = texture->height; + } + impeller::TextureDescriptor desc; + desc.type = impeller::TextureType::kTextureExternalOES; + impeller::AiksContext* aiks_context = context.aiks_context; + const auto& gl_context = + impeller::ContextGLES::Cast(*aiks_context->GetContext()); + desc.storage_mode = impeller::StorageMode::kDevicePrivate; + desc.format = impeller::PixelFormat::kR8G8B8A8UNormInt; + desc.size = {static_cast(texture->width), + static_cast(texture->height)}; + desc.mip_count = 1; + auto textureGLES = std::make_shared( + gl_context.GetReactor(), desc, + impeller::TextureGLES::IsWrapped::kWrapped); + textureGLES->SetCoordinateSystem( + impeller::TextureCoordinateSystem::kUploadFromHost); + if (!textureGLES->Bind()) { + if (texture->destruction_callback) { + texture->destruction_callback(texture->user_data); + } + return nullptr; + } + + if (!texture->bind_callback(texture->user_data)) { + if (texture->destruction_callback) { + texture->destruction_callback(texture->user_data); + } + return nullptr; + } + + if (texture->destruction_callback) { + texture->destruction_callback(texture->user_data); + } + + return impeller::DlImageImpeller::Make(textureGLES); +} + +} // namespace flutter diff --git a/shell/platform/embedder/embedder_external_texture_gl_impeller.h b/shell/platform/embedder/embedder_external_texture_gl_impeller.h new file mode 100644 index 0000000000000..c80028513e9e2 --- /dev/null +++ b/shell/platform/embedder/embedder_external_texture_gl_impeller.h @@ -0,0 +1,62 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_EXTERNAL_TEXTURE_GL_IMPELLER_H_ +#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_EXTERNAL_TEXTURE_GL_IMPELLER_H_ + +#include "flutter/common/graphics/texture.h" +#include "flutter/fml/macros.h" +#include "flutter/shell/platform/embedder/embedder.h" +#include "third_party/skia/include/core/SkSize.h" + +namespace flutter { + +class EmbedderExternalTextureGLImpeller : public flutter::Texture { + public: + using ExternalTextureCallback = std::function< + std::unique_ptr(int64_t, size_t, size_t)>; + + EmbedderExternalTextureGLImpeller(int64_t texture_identifier, + const ExternalTextureCallback& callback); + + ~EmbedderExternalTextureGLImpeller(); + + private: + const ExternalTextureCallback& external_texture_callback_; + sk_sp last_image_; + + sk_sp ResolveTexture(int64_t texture_id, + PaintContext& context, + const SkISize& size); + sk_sp ResolveGpuSurfaceTexture(FlutterOpenGLTexture* texture, + PaintContext& context, + const SkISize& size); + sk_sp ResolvePixelBufferTexture(FlutterOpenGLTexture* texture, + PaintContext& context, + const SkISize& size); + + // |flutter::Texture| + void Paint(PaintContext& context, + const SkRect& bounds, + bool freeze, + const DlImageSampling sampling) override; + + // |flutter::Texture| + void OnGrContextCreated() override; + + // |flutter::Texture| + void OnGrContextDestroyed() override; + + // |flutter::Texture| + void MarkNewFrameAvailable() override; + + // |flutter::Texture| + void OnTextureUnregistered() override; + + FML_DISALLOW_COPY_AND_ASSIGN(EmbedderExternalTextureGLImpeller); +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_EXTERNAL_TEXTURE_GL_IMPELLER_H_ diff --git a/shell/platform/embedder/embedder_external_texture_resolver.cc b/shell/platform/embedder/embedder_external_texture_resolver.cc index d4fbff9eb7ca0..dca494c9ed0c0 100644 --- a/shell/platform/embedder/embedder_external_texture_resolver.cc +++ b/shell/platform/embedder/embedder_external_texture_resolver.cc @@ -4,6 +4,10 @@ #include "flutter/shell/platform/embedder/embedder_external_texture_resolver.h" +#ifdef SHELL_ENABLE_GL +#include "flutter/shell/platform/embedder/embedder_external_texture_gl_impeller.h" +#endif + #include #include @@ -11,8 +15,9 @@ namespace flutter { #ifdef SHELL_ENABLE_GL EmbedderExternalTextureResolver::EmbedderExternalTextureResolver( - EmbedderExternalTextureGL::ExternalTextureCallback gl_callback) - : gl_callback_(std::move(gl_callback)) {} + EmbedderExternalTextureGL::ExternalTextureCallback gl_callback, + bool enable_impeller) + : gl_callback_(std::move(gl_callback)), enable_impeller_(enable_impeller) {} #endif #ifdef SHELL_ENABLE_METAL @@ -22,17 +27,15 @@ EmbedderExternalTextureResolver::EmbedderExternalTextureResolver( #endif std::unique_ptr -EmbedderExternalTextureResolver::ResolveExternalTexture( - int64_t texture_id, - FlutterTextureType type) { +EmbedderExternalTextureResolver::ResolveExternalTexture(int64_t texture_id) { #ifdef SHELL_ENABLE_GL if (gl_callback_) { - if (type == FlutterTextureType::kFlutterGpuSurfaceTexture) { - return std::make_unique( - texture_id, gl_callback_); + if (enable_impeller_) { + return std::make_unique(texture_id, + gl_callback_); } else { - return std::make_unique( - texture_id, gl_callback_); + return std::make_unique(texture_id, + gl_callback_); } } #endif @@ -47,25 +50,6 @@ EmbedderExternalTextureResolver::ResolveExternalTexture( return nullptr; } -std::unique_ptr -EmbedderExternalTextureResolver::ResolveExternalTexture(int64_t texture_id) { -#ifdef SHELL_ENABLE_GL - if (gl_callback_) { - return std::make_unique(texture_id, - gl_callback_); - } -#endif - -#ifdef SHELL_ENABLE_METAL - if (metal_callback_) { - return std::make_unique(texture_id, - metal_callback_); - } -#endif - - return nullptr; -} - bool EmbedderExternalTextureResolver::SupportsExternalTextures() { #ifdef SHELL_ENABLE_GL if (gl_callback_) { diff --git a/shell/platform/embedder/embedder_external_texture_resolver.h b/shell/platform/embedder/embedder_external_texture_resolver.h index bc77a6885b9fa..da421e2b837a1 100644 --- a/shell/platform/embedder/embedder_external_texture_resolver.h +++ b/shell/platform/embedder/embedder_external_texture_resolver.h @@ -8,7 +8,6 @@ #include #include "flutter/common/graphics/texture.h" -#include "flutter/shell/platform/embedder/embedder.h" #ifdef SHELL_ENABLE_GL #include "flutter/shell/platform/embedder/embedder_external_texture_gl.h" @@ -27,7 +26,8 @@ class EmbedderExternalTextureResolver { #ifdef SHELL_ENABLE_GL explicit EmbedderExternalTextureResolver( - EmbedderExternalTextureGL::ExternalTextureCallback gl_callback); + EmbedderExternalTextureGL::ExternalTextureCallback gl_callback, + bool enable_impeller); #endif #ifdef SHELL_ENABLE_METAL @@ -36,8 +36,6 @@ class EmbedderExternalTextureResolver { #endif std::unique_ptr ResolveExternalTexture(int64_t texture_id); - std::unique_ptr ResolveExternalTexture(int64_t texture_id, - FlutterTextureType type); bool SupportsExternalTextures(); @@ -49,6 +47,7 @@ class EmbedderExternalTextureResolver { #ifdef SHELL_ENABLE_METAL EmbedderExternalTextureMetal::ExternalTextureCallback metal_callback_; #endif + bool enable_impeller_ = false; FML_DISALLOW_COPY_AND_ASSIGN(EmbedderExternalTextureResolver); };