Flutter Engine
 
Loading...
Searching...
No Matches
surface_texture_external_texture.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <GLES/glext.h>
8
9#include <utility>
10
12#include "third_party/skia/include/core/SkAlphaType.h"
13#include "third_party/skia/include/core/SkColorSpace.h"
14#include "third_party/skia/include/core/SkColorType.h"
15#include "third_party/skia/include/core/SkImage.h"
16
17namespace flutter {
18
20 int64_t id,
21 const fml::jni::ScopedJavaGlobalRef<jobject>& surface_texture,
22 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade)
23 : Texture(id),
24 jni_facade_(jni_facade),
25 surface_texture_(surface_texture),
26 transform_(SkMatrix::I()) {}
27
29
30void SurfaceTextureExternalTexture::OnGrContextCreated() {
32}
33
34void SurfaceTextureExternalTexture::MarkNewFrameAvailable() {
35 // NOOP.
36}
37
38void SurfaceTextureExternalTexture::Paint(PaintContext& context,
39 const DlRect& bounds,
40 bool freeze,
41 const DlImageSampling sampling) {
43 return;
44 }
45 const bool should_process_frame =
46 !freeze || ShouldUpdate() || dl_image_ == nullptr;
47 if (should_process_frame) {
48 ProcessFrame(context, ToSkRect(bounds));
49 }
50 // If process frame failed, this may not be in attached state.
52 return;
53 }
54
55 if (!dl_image_) {
56 FML_LOG(WARNING)
57 << "No DlImage available for SurfaceTextureExternalTexture to paint.";
58 return;
59 }
60
61 DrawFrame(context, ToSkRect(bounds), sampling);
62}
63
65 PaintContext& context,
66 const SkRect& bounds,
67 const DlImageSampling sampling) const {
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}
107
108void SurfaceTextureExternalTexture::OnGrContextDestroyed() {
110 Detach();
111 }
113}
114
115void SurfaceTextureExternalTexture::OnTextureUnregistered() {}
116
118 jni_facade_->SurfaceTextureDetachFromGLContext(
120 dl_image_.reset();
121}
122
124 jni_facade_->SurfaceTextureAttachToGLContext(
127}
128
133
135 jni_facade_->SurfaceTextureUpdateTexImage(
137 transform_ = jni_facade_->SurfaceTextureGetTransformMatrix(
139}
140
142 return transform_;
143}
144
145} // namespace flutter
virtual void DrawRect(const DlRect &rect, const DlPaint &paint)=0
virtual void DrawImage(const sk_sp< DlImage > &image, const DlPoint &point, DlImageSampling sampling, const DlPaint *paint=nullptr)=0
virtual void Translate(DlScalar tx, DlScalar ty)=0
virtual void Scale(DlScalar sx, DlScalar sy)=0
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
SurfaceTextureExternalTexture(int64_t id, const fml::jni::ScopedJavaGlobalRef< jobject > &surface_texture, const std::shared_ptr< PlatformViewAndroidJNI > &jni_facade)
virtual void DrawFrame(PaintContext &context, const SkRect &bounds, const DlImageSampling sampling) const
fml::jni::ScopedJavaGlobalRef< jobject > surface_texture_
void Attach(int gl_tex_id)
Attaches the given OpenGL texture handle to the surface texture via a bind operation.
std::shared_ptr< PlatformViewAndroidJNI > jni_facade_
virtual void ProcessFrame(PaintContext &context, const SkRect &bounds)=0
Subclasses override this method to bind the OpenGL texture resource represented by this surface textu...
const SkM44 & GetCurrentUVTransformation() const
Get the transformation that should be applied to the UV texture coordinates when sampling from this t...
virtual void Detach()
Provides an opportunity for the subclasses to sever the connection between the OpenGL texture resourc...
void Update()
Update the surface texture contents and transformation matrix.
#define FML_LOG(severity)
Definition logging.h:101
impeller::Rect DlRect
DlMatrix ToDlMatrix(const SkMatrix &matrix)
const SkRect & ToSkRect(const DlRect &rect)
static constexpr TRect MakeWH(Type width, Type height)
Definition rect.h:140
const uintptr_t id