Flutter Engine
 
Loading...
Searching...
No Matches
flutter::SurfaceTextureExternalTexture Class Referenceabstract

Instances of external textures peered to android.graphics.SurfaceTexture. More...

#include <surface_texture_external_texture.h>

Inheritance diagram for flutter::SurfaceTextureExternalTexture:
flutter::Texture flutter::ContextListener flutter::SurfaceTextureExternalTextureGLImpeller flutter::SurfaceTextureExternalTextureGLSkia flutter::SurfaceTextureExternalTextureVKImpeller

Public Member Functions

 SurfaceTextureExternalTexture (int64_t id, const fml::jni::ScopedJavaGlobalRef< jobject > &surface_texture, const std::shared_ptr< PlatformViewAndroidJNI > &jni_facade)
 
 ~SurfaceTextureExternalTexture () override
 
- Public Member Functions inherited from flutter::Texture
 Texture (int64_t id)
 
virtual ~Texture ()
 
int64_t Id ()
 
- Public Member Functions inherited from flutter::ContextListener
 ContextListener ()
 
 ~ContextListener ()
 

Protected Types

enum class  AttachmentState {
  kUninitialized ,
  kAttached ,
  kDetached
}
 Specifies how this instance is bound to the underlying surface texture. More...
 

Protected Member Functions

virtual void ProcessFrame (PaintContext &context, const SkRect &bounds)=0
 Subclasses override this method to bind the OpenGL texture resource represented by this surface texture to the package specific texture (SkImage, impeller::Texture, etc...).
 
virtual void DrawFrame (PaintContext &context, const SkRect &bounds, const DlImageSampling sampling) const
 
const SkM44 & GetCurrentUVTransformation () const
 Get the transformation that should be applied to the UV texture coordinates when sampling from this texture.
 
virtual void Detach ()
 Provides an opportunity for the subclasses to sever the connection between the OpenGL texture resource represented by this surface texture and the underlying package handle (SkImage, impeller::Texture, etc...).
 
void Attach (int gl_tex_id)
 Attaches the given OpenGL texture handle to the surface texture via a bind operation.
 
bool ShouldUpdate ()
 
void Update ()
 Update the surface texture contents and transformation matrix.
 

Protected Attributes

std::shared_ptr< PlatformViewAndroidJNIjni_facade_
 
fml::jni::ScopedJavaGlobalRef< jobject > surface_texture_
 
AttachmentState state_ = AttachmentState::kUninitialized
 
sk_sp< flutter::DlImagedl_image_
 

Detailed Description

Instances of external textures peered to android.graphics.SurfaceTexture.

SurfaceTextures are used on older versions of Android (API < 29). On newer versions, the Android Hardware Buffer backend flutter::ImageExternalTexture instances are used instead.

Due to the way surface textures are designed, it is not possible to have a Vulkan renderer interoperate with such textures. Consequently, both Skia and Impeller only have OpenGL implementations for these kinds of textures.

This is an abstract base class. Minimally, subclasses override the pure virtual ProcessFrame method to bind the package specific texture implementation to the surface texture.

Definition at line 33 of file surface_texture_external_texture.h.

Member Enumeration Documentation

◆ AttachmentState

Specifies how this instance is bound to the underlying surface texture.

Enumerator
kUninitialized 
kAttached 
kDetached 

Definition at line 107 of file surface_texture_external_texture.h.

Constructor & Destructor Documentation

◆ SurfaceTextureExternalTexture()

flutter::SurfaceTextureExternalTexture::SurfaceTextureExternalTexture ( int64_t  id,
const fml::jni::ScopedJavaGlobalRef< jobject > &  surface_texture,
const std::shared_ptr< PlatformViewAndroidJNI > &  jni_facade 
)

Definition at line 19 of file surface_texture_external_texture.cc.

23 : Texture(id),
24 jni_facade_(jni_facade),
25 surface_texture_(surface_texture),
26 transform_(SkMatrix::I()) {}
fml::jni::ScopedJavaGlobalRef< jobject > surface_texture_
std::shared_ptr< PlatformViewAndroidJNI > jni_facade_
Texture(int64_t id)
Definition texture.cc:13

◆ ~SurfaceTextureExternalTexture()

flutter::SurfaceTextureExternalTexture::~SurfaceTextureExternalTexture ( )
override

Definition at line 28 of file surface_texture_external_texture.cc.

28{}

Member Function Documentation

◆ Attach()

void flutter::SurfaceTextureExternalTexture::Attach ( int  gl_tex_id)
protected

Attaches the given OpenGL texture handle to the surface texture via a bind operation.

@important It is the responsibility of the subclass to ensure that a context is current when this call is made. Subclass can do this by overriding this method, making the context current in the implementation and calling the base class method.

Parameters
[in]gl_tex_idThe gl tex identifier

Definition at line 123 of file surface_texture_external_texture.cc.

References jni_facade_, kAttached, state_, and surface_texture_.

◆ Detach()

void flutter::SurfaceTextureExternalTexture::Detach ( )
protectedvirtual

Provides an opportunity for the subclasses to sever the connection between the OpenGL texture resource represented by this surface texture and the underlying package handle (SkImage, impeller::Texture, etc...).

@important It is the responsibility of the subclass to ensure that a context is current when this call is made. Subclass can do this by overriding this method, making the context current in the implementation and calling the base class method.

Definition at line 117 of file surface_texture_external_texture.cc.

117 {
118 jni_facade_->SurfaceTextureDetachFromGLContext(
120 dl_image_.reset();
121}

References dl_image_, jni_facade_, and surface_texture_.

◆ DrawFrame()

void flutter::SurfaceTextureExternalTexture::DrawFrame ( PaintContext context,
const SkRect &  bounds,
const DlImageSampling  sampling 
) const
protectedvirtual

Definition at line 64 of file surface_texture_external_texture.cc.

67 {
69
70 // Android's SurfaceTexture transform matrix works on texture coordinate
71 // lookups in the range 0.0-1.0, while Skia's Shader transform matrix works on
72 // the image itself, as if it were inscribed inside a clip rect.
73 // An Android transform that scales lookup by 0.5 (displaying 50% of the
74 // texture) is the same as a Skia transform by 2.0 (scaling 50% of the image
75 // outside of the virtual "clip rect"), so we invert the incoming matrix.
76
77 if (transform.IsIdentity()) {
78 context.canvas->DrawImage(dl_image_, DlPoint{0, 0}, sampling,
79 context.paint);
80 return;
81 }
82
83 if (!transform.IsInvertible()) {
84 FML_LOG(FATAL)
85 << "Invalid (not invertable) SurfaceTexture transformation matrix";
86 }
87 transform = transform.Invert();
88
89 DlAutoCanvasRestore autoRestore(context.canvas, true);
90
91 // The incoming texture is vertically flipped, so we flip it
92 // back. OpenGL's coordinate system has Positive Y equivalent to up, while
93 // Skia's coordinate system has Negative Y equvalent to up.
94 context.canvas->Translate(bounds.x(), bounds.y() + bounds.height());
95 context.canvas->Scale(bounds.width(), -bounds.height());
96
97 auto source = DlColorSource::MakeImage(
99
100 DlPaint paintWithShader;
101 if (context.paint) {
102 paintWithShader = *context.paint;
103 }
104 paintWithShader.setColorSource(source);
105 context.canvas->DrawRect(DlRect::MakeWH(1, 1), paintWithShader);
106}
static std::shared_ptr< DlColorSource > MakeImage(const sk_sp< const DlImage > &image, DlTileMode horizontal_tile_mode, DlTileMode vertical_tile_mode, DlImageSampling sampling=DlImageSampling::kLinear, const DlMatrix *matrix=nullptr)
DlPaint & setColorSource(std::nullptr_t source)
Definition dl_paint.h:131
const SkM44 & GetCurrentUVTransformation() const
Get the transformation that should be applied to the UV texture coordinates when sampling from this t...
#define FML_LOG(severity)
Definition logging.h:101
DlMatrix ToDlMatrix(const SkMatrix &matrix)
impeller::Point DlPoint
flutter::DlPaint DlPaint
static constexpr TRect MakeWH(Type width, Type height)
Definition rect.h:140

References flutter::Texture::PaintContext::canvas, dl_image_, flutter::DlCanvas::DrawImage(), flutter::DlCanvas::DrawRect(), FML_LOG, GetCurrentUVTransformation(), flutter::kClamp, flutter::DlColorSource::MakeImage(), impeller::TRect< Scalar >::MakeWH(), flutter::Texture::PaintContext::paint, flutter::DlCanvas::Scale(), flutter::DlPaint::setColorSource(), flutter::ToDlMatrix(), transform, and flutter::DlCanvas::Translate().

◆ GetCurrentUVTransformation()

const SkM44 & flutter::SurfaceTextureExternalTexture::GetCurrentUVTransformation ( ) const
protected

Get the transformation that should be applied to the UV texture coordinates when sampling from this texture.

Returns
The current uv transformation.

Definition at line 141 of file surface_texture_external_texture.cc.

141 {
142 return transform_;
143}

Referenced by DrawFrame().

◆ ProcessFrame()

virtual void flutter::SurfaceTextureExternalTexture::ProcessFrame ( PaintContext context,
const SkRect &  bounds 
)
protectedpure virtual

Subclasses override this method to bind the OpenGL texture resource represented by this surface texture to the package specific texture (SkImage, impeller::Texture, etc...).

@important The state of texture should be AttachmentState::kAttached after a call to this method. That is the responsibility of the subclass.

Parameters
contextThe context.
[in]boundsThe bounds of the texture.

◆ ShouldUpdate()

bool flutter::SurfaceTextureExternalTexture::ShouldUpdate ( )
protected

Definition at line 129 of file surface_texture_external_texture.cc.

129 {
130 return jni_facade_->SurfaceTextureShouldUpdate(
132}

References jni_facade_, and surface_texture_.

◆ Update()

void flutter::SurfaceTextureExternalTexture::Update ( )
protected

Update the surface texture contents and transformation matrix.

Definition at line 134 of file surface_texture_external_texture.cc.

134 {
135 jni_facade_->SurfaceTextureUpdateTexImage(
137 transform_ = jni_facade_->SurfaceTextureGetTransformMatrix(
139}

References jni_facade_, and surface_texture_.

Member Data Documentation

◆ dl_image_

sk_sp<flutter::DlImage> flutter::SurfaceTextureExternalTexture::dl_image_
protected

Definition at line 112 of file surface_texture_external_texture.h.

Referenced by Detach(), and DrawFrame().

◆ jni_facade_

std::shared_ptr<PlatformViewAndroidJNI> flutter::SurfaceTextureExternalTexture::jni_facade_
protected

Definition at line 109 of file surface_texture_external_texture.h.

Referenced by Attach(), Detach(), ShouldUpdate(), and Update().

◆ state_

AttachmentState flutter::SurfaceTextureExternalTexture::state_ = AttachmentState::kUninitialized
protected

Definition at line 111 of file surface_texture_external_texture.h.

Referenced by Attach().

◆ surface_texture_

fml::jni::ScopedJavaGlobalRef<jobject> flutter::SurfaceTextureExternalTexture::surface_texture_
protected

Definition at line 110 of file surface_texture_external_texture.h.

Referenced by Attach(), Detach(), ShouldUpdate(), and Update().


The documentation for this class was generated from the following files: