Flutter Engine
embedder_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/embedder/embedder_external_texture_gl.h"
6 
7 #include "flutter/fml/logging.h"
8 #include "third_party/skia/include/core/SkImage.h"
9 #include "third_party/skia/include/core/SkSize.h"
10 #include "third_party/skia/include/gpu/GrBackendSurface.h"
11 #include "third_party/skia/include/gpu/GrDirectContext.h"
12 
13 namespace flutter {
14 
16  int64_t texture_identifier,
18  : Texture(texture_identifier), external_texture_callback_(callback) {
19  FML_DCHECK(external_texture_callback_);
20 }
21 
23 
24 // |flutter::Texture|
25 void EmbedderExternalTextureGL::Paint(SkCanvas& canvas,
26  const SkRect& bounds,
27  bool freeze,
28  GrDirectContext* context,
29  const SkSamplingOptions& sampling) {
30  if (last_image_ == nullptr) {
31  last_image_ =
32  ResolveTexture(Id(), //
33  context, //
34  SkISize::Make(bounds.width(), bounds.height()) //
35  );
36  }
37 
38  if (last_image_) {
39  if (bounds != SkRect::Make(last_image_->bounds())) {
40  canvas.drawImageRect(last_image_, bounds, sampling);
41  } else {
42  canvas.drawImage(last_image_, bounds.x(), bounds.y(), sampling, nullptr);
43  }
44  }
45 }
46 
47 sk_sp<SkImage> EmbedderExternalTextureGL::ResolveTexture(
48  int64_t texture_id,
49  GrDirectContext* context,
50  const SkISize& size) {
51  context->flushAndSubmit();
52  context->resetContext(kAll_GrBackendState);
53  std::unique_ptr<FlutterOpenGLTexture> texture =
54  external_texture_callback_(texture_id, size.width(), size.height());
55 
56  if (!texture) {
57  return nullptr;
58  }
59 
60  GrGLTextureInfo gr_texture_info = {texture->target, texture->name,
61  texture->format};
62 
63  size_t width = size.width();
64  size_t height = size.height();
65 
66  if (texture->width != 0 && texture->height != 0) {
67  width = texture->width;
68  height = texture->height;
69  }
70 
71  GrBackendTexture gr_backend_texture(width, height, GrMipMapped::kNo,
72  gr_texture_info);
73  SkImage::TextureReleaseProc release_proc = texture->destruction_callback;
74  auto image =
75  SkImage::MakeFromTexture(context, // context
76  gr_backend_texture, // texture handle
77  kTopLeft_GrSurfaceOrigin, // origin
78  kRGBA_8888_SkColorType, // color type
79  kPremul_SkAlphaType, // alpha type
80  nullptr, // colorspace
81  release_proc, // texture release proc
82  texture->user_data // texture release context
83  );
84 
85  if (!image) {
86  // In case Skia rejects the image, call the release proc so that
87  // embedders can perform collection of intermediates.
88  if (release_proc) {
89  release_proc(texture->user_data);
90  }
91  FML_LOG(ERROR) << "Could not create external texture->";
92  return nullptr;
93  }
94 
95  return image;
96 }
97 
98 // |flutter::Texture|
99 void EmbedderExternalTextureGL::OnGrContextCreated() {}
100 
101 // |flutter::Texture|
102 void EmbedderExternalTextureGL::OnGrContextDestroyed() {}
103 
104 // |flutter::Texture|
105 void EmbedderExternalTextureGL::MarkNewFrameAvailable() {
106  last_image_ = nullptr;
107 }
108 
109 // |flutter::Texture|
110 void EmbedderExternalTextureGL::OnTextureUnregistered() {}
111 
112 } // namespace flutter
int64_t Id()
Definition: texture.h:43
G_BEGIN_DECLS FlTexture * texture
#define FML_DCHECK(condition)
Definition: logging.h:86
int64_t texture_id
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
#define FML_LOG(severity)
Definition: logging.h:65
FlKeyEvent FlKeyResponderAsyncCallback callback
EmbedderExternalTextureGL(int64_t texture_identifier, const ExternalTextureCallback &callback)
int32_t width
int32_t height
std::function< std::unique_ptr< FlutterOpenGLTexture >(int64_t, size_t, size_t)> ExternalTextureCallback