Flutter Engine
The 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
5#include "flutter/shell/platform/android/surface_texture_external_texture.h"
6
7#include <GLES/glext.h>
8
9#include <utility>
10
11#include "flutter/display_list/effects/dl_color_source.h"
21
22namespace flutter {
23
25 int64_t id,
26 const fml::jni::ScopedJavaGlobalRef<jobject>& surface_texture,
27 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade)
28 : Texture(id),
29 jni_facade_(jni_facade),
30 surface_texture_(surface_texture),
31 transform_(SkMatrix::I()) {}
32
34
38
42
44 const SkRect& bounds,
45 bool freeze,
46 const DlImageSampling sampling) {
48 return;
49 }
50 const bool should_process_frame =
51 !freeze || ShouldUpdate() || dl_image_ == nullptr;
52 if (should_process_frame) {
53 ProcessFrame(context, bounds);
54 }
56
57 if (dl_image_) {
58 DlAutoCanvasRestore autoRestore(context.canvas, true);
59
60 // The incoming texture is vertically flipped, so we flip it
61 // back. OpenGL's coordinate system has Positive Y equivalent to up, while
62 // Skia's coordinate system has Negative Y equvalent to up.
63 context.canvas->Translate(bounds.x(), bounds.y() + bounds.height());
64 context.canvas->Scale(bounds.width(), -bounds.height());
65
66 if (!transform_.isIdentity()) {
68 DlTileMode::kClamp, sampling, &transform_);
69
70 DlPaint paintWithShader;
71 if (context.paint) {
72 paintWithShader = *context.paint;
73 }
74 paintWithShader.setColorSource(&source);
75 context.canvas->DrawRect(SkRect::MakeWH(1, 1), paintWithShader);
76 } else {
77 context.canvas->DrawImage(dl_image_, {0, 0}, sampling, context.paint);
78 }
79 } else {
80 FML_LOG(WARNING)
81 << "No DlImage available for SurfaceTextureExternalTexture to paint.";
82 }
83}
84
91
93
99
101 jni_facade_->SurfaceTextureAttachToGLContext(
104}
105
110
112 jni_facade_->SurfaceTextureUpdateTexImage(
114
115 jni_facade_->SurfaceTextureGetTransformMatrix(
117
118 // Android's SurfaceTexture transform matrix works on texture coordinate
119 // lookups in the range 0.0-1.0, while Skia's Shader transform matrix works on
120 // the image itself, as if it were inscribed inside a clip rect.
121 // An Android transform that scales lookup by 0.5 (displaying 50% of the
122 // texture) is the same as a Skia transform by 2.0 (scaling 50% of the image
123 // outside of the virtual "clip rect"), so we invert the incoming matrix.
124 SkMatrix inverted;
125 if (!transform_.invert(&inverted)) {
127 << "Invalid (not invertable) SurfaceTexture transformation matrix";
128 }
129 transform_ = inverted;
130}
131
132} // namespace flutter
bool invert(SkMatrix *inverse) const
Definition SkMatrix.h:1206
bool isIdentity() const
Definition SkMatrix.h:223
virtual void DrawRect(const SkRect &rect, const DlPaint &paint)=0
virtual void Scale(SkScalar sx, SkScalar sy)=0
virtual void Translate(SkScalar tx, SkScalar ty)=0
virtual void DrawImage(const sk_sp< DlImage > &image, const SkPoint point, DlImageSampling sampling, const DlPaint *paint=nullptr)=0
DlPaint & setColorSource(std::shared_ptr< const DlColorSource > source)
Definition dl_paint.h:132
SurfaceTextureExternalTexture(int64_t id, const fml::jni::ScopedJavaGlobalRef< jobject > &surface_texture, const std::shared_ptr< PlatformViewAndroidJNI > &jni_facade)
void Paint(PaintContext &context, const SkRect &bounds, bool freeze, const DlImageSampling sampling) override
fml::jni::ScopedJavaGlobalRef< jobject > surface_texture_
std::shared_ptr< PlatformViewAndroidJNI > jni_facade_
virtual void ProcessFrame(PaintContext &context, const SkRect &bounds)=0
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310
SkBitmap source
Definition examples.cpp:28
#define FATAL(error)
#define FML_LOG(severity)
Definition logging.h:82
#define FML_CHECK(condition)
Definition logging.h:85
Definition SkMD5.cpp:134
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
const uintptr_t id