Flutter Engine
android_external_texture_gl.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/android_external_texture_gl.h"
6 
7 #include <GLES/glext.h>
8 
9 #include "third_party/skia/include/gpu/GrBackendSurface.h"
10 #include "third_party/skia/include/gpu/GrDirectContext.h"
11 
12 namespace flutter {
13 
15  int64_t id,
16  const fml::jni::JavaObjectWeakGlobalRef& surface_texture,
17  std::shared_ptr<PlatformViewAndroidJNI> jni_facade)
18  : Texture(id),
19  jni_facade_(jni_facade),
20  surface_texture_(surface_texture),
21  transform(SkMatrix::I()) {}
22 
24  if (state_ == AttachmentState::attached) {
25  glDeleteTextures(1, &texture_name_);
26  }
27 }
28 
30  state_ = AttachmentState::uninitialized;
31 }
32 
34  new_frame_ready_ = true;
35 }
36 
37 void AndroidExternalTextureGL::Paint(SkCanvas& canvas,
38  const SkRect& bounds,
39  bool freeze,
40  GrDirectContext* context,
41  SkFilterQuality filter_quality) {
42  if (state_ == AttachmentState::detached) {
43  return;
44  }
45  if (state_ == AttachmentState::uninitialized) {
46  glGenTextures(1, &texture_name_);
47  Attach(static_cast<jint>(texture_name_));
48  state_ = AttachmentState::attached;
49  }
50  if (!freeze && new_frame_ready_) {
51  Update();
52  new_frame_ready_ = false;
53  }
54  GrGLTextureInfo textureInfo = {GL_TEXTURE_EXTERNAL_OES, texture_name_,
55  GL_RGBA8_OES};
56  GrBackendTexture backendTexture(1, 1, GrMipMapped::kNo, textureInfo);
57  sk_sp<SkImage> image = SkImage::MakeFromTexture(
58  context, backendTexture, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
59  kPremul_SkAlphaType, nullptr);
60  if (image) {
61  SkAutoCanvasRestore autoRestore(&canvas, true);
62  canvas.translate(bounds.x(), bounds.y());
63  canvas.scale(bounds.width(), bounds.height());
64  if (!transform.isIdentity()) {
65  SkMatrix transformAroundCenter(transform);
66 
67  transformAroundCenter.preTranslate(-0.5, -0.5);
68  transformAroundCenter.postScale(1, -1);
69  transformAroundCenter.postTranslate(0.5, 0.5);
70  canvas.concat(transformAroundCenter);
71  }
72  SkPaint paint;
73  paint.setFilterQuality(filter_quality);
74  canvas.drawImage(image, 0, 0, &paint);
75  }
76 }
77 
78 void AndroidExternalTextureGL::UpdateTransform() {
79  jni_facade_->SurfaceTextureGetTransformMatrix(surface_texture_, transform);
80 }
81 
83  if (state_ == AttachmentState::attached) {
84  Detach();
85  glDeleteTextures(1, &texture_name_);
86  }
87  state_ = AttachmentState::detached;
88 }
89 
90 void AndroidExternalTextureGL::Attach(jint textureName) {
91  jni_facade_->SurfaceTextureAttachToGLContext(surface_texture_, textureName);
92 }
93 
94 void AndroidExternalTextureGL::Update() {
95  jni_facade_->SurfaceTextureUpdateTexImage(surface_texture_);
96  UpdateTransform();
97 }
98 
99 void AndroidExternalTextureGL::Detach() {
100  jni_facade_->SurfaceTextureDetachFromGLContext(surface_texture_);
101 }
102 
104 
105 } // namespace flutter
void Paint(SkCanvas &canvas, const SkRect &bounds, bool freeze, GrDirectContext *context, SkFilterQuality filter_quality) override
AndroidExternalTextureGL(int64_t id, const fml::jni::JavaObjectWeakGlobalRef &surface_texture, std::shared_ptr< PlatformViewAndroidJNI > jni_facade)