Flutter Engine
 
Loading...
Searching...
No Matches
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
6
15
16#include "include/core/SkPaint.h"
17#include "third_party/skia/include/core/SkAlphaType.h"
18#include "third_party/skia/include/core/SkColorSpace.h"
19#include "third_party/skia/include/core/SkColorType.h"
20#include "third_party/skia/include/core/SkImage.h"
21#include "third_party/skia/include/core/SkSize.h"
22#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
23#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
24#include "third_party/skia/include/gpu/ganesh/SkImageGanesh.h"
25#include "third_party/skia/include/gpu/ganesh/gl/GrGLBackendSurface.h"
26#include "third_party/skia/include/gpu/ganesh/gl/GrGLTypes.h"
27
28namespace flutter {
29
31 int64_t texture_identifier,
33 : Texture(texture_identifier), external_texture_callback_(callback) {
34 FML_DCHECK(external_texture_callback_);
35}
36
38
39// |flutter::Texture|
40void EmbedderExternalTextureGL::Paint(PaintContext& context,
41 const DlRect& bounds,
42 bool freeze,
43 const DlImageSampling sampling) {
44 if (last_image_ == nullptr) {
45 last_image_ =
46 ResolveTexture(Id(), //
47 context.gr_context, //
48 context.aiks_context, //
49 SkISize::Make(bounds.GetWidth(), bounds.GetHeight()) //
50 );
51 }
52
53 DlCanvas* canvas = context.canvas;
54 const DlPaint* paint = context.paint;
55
56 if (last_image_) {
57 DlRect image_bounds = DlRect::Make(last_image_->GetBounds());
58 if (bounds != image_bounds) {
59 canvas->DrawImageRect(last_image_, image_bounds, bounds, sampling, paint);
60 } else {
61 canvas->DrawImage(last_image_, bounds.GetOrigin(), sampling, paint);
62 }
63 }
64}
65
66sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTexture(
67 int64_t texture_id,
68 GrDirectContext* context,
69 impeller::AiksContext* aiks_context,
70 const SkISize& size) {
71 if (!!aiks_context) {
72 return ResolveTextureImpeller(texture_id, aiks_context, size);
73 } else {
74 return ResolveTextureSkia(texture_id, context, size);
75 }
76}
77
78sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTextureSkia(
79 int64_t texture_id,
80 GrDirectContext* context,
81 const SkISize& size) {
82 context->flushAndSubmit();
83 context->resetContext(kAll_GrBackendState);
84 std::unique_ptr<FlutterOpenGLTexture> texture =
85 external_texture_callback_(texture_id, size.width(), size.height());
86
87 if (!texture) {
88 return nullptr;
89 }
90
91 GrGLTextureInfo gr_texture_info = {texture->target, texture->name,
92 texture->format};
93
94 size_t width = size.width();
95 size_t height = size.height();
96
97 if (texture->width != 0 && texture->height != 0) {
98 width = texture->width;
99 height = texture->height;
100 }
101
102 auto gr_backend_texture = GrBackendTextures::MakeGL(
103 width, height, skgpu::Mipmapped::kNo, gr_texture_info);
104 SkImages::TextureReleaseProc release_proc = texture->destruction_callback;
105 auto image =
106 SkImages::BorrowTextureFrom(context, // context
107 gr_backend_texture, // texture handle
108 kTopLeft_GrSurfaceOrigin, // origin
109 kRGBA_8888_SkColorType, // color type
110 kPremul_SkAlphaType, // alpha type
111 nullptr, // colorspace
112 release_proc, // texture release proc
113 texture->user_data // texture release context
114 );
115
116 if (!image) {
117 // In case Skia rejects the image, call the release proc so that
118 // embedders can perform collection of intermediates.
119 if (release_proc) {
120 release_proc(texture->user_data);
121 }
122 FML_LOG(ERROR) << "Could not create external texture->";
123 return nullptr;
124 }
125
126 // This image should not escape local use by EmbedderExternalTextureGL
127 return DlImage::Make(std::move(image));
128}
129
130sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTextureImpeller(
131 int64_t texture_id,
132 impeller::AiksContext* aiks_context,
133 const SkISize& size) {
134 std::unique_ptr<FlutterOpenGLTexture> texture =
135 external_texture_callback_(texture_id, size.width(), size.height());
136
137 if (!texture) {
138 return nullptr;
139 }
140
142 desc.size = impeller::ISize(texture->width, texture->height);
143
144 impeller::ContextGLES& context =
145 impeller::ContextGLES::Cast(*aiks_context->GetContext());
146 impeller::HandleGLES handle = context.GetReactor()->CreateHandle(
148 std::shared_ptr<impeller::TextureGLES> image =
149 impeller::TextureGLES::WrapTexture(context.GetReactor(), desc, handle);
150
151 if (!image) {
152 // In case Skia rejects the image, call the release proc so that
153 // embedders can perform collection of intermediates.
154 if (texture->destruction_callback) {
155 texture->destruction_callback(texture->user_data);
156 }
157 FML_LOG(ERROR) << "Could not create external texture";
158 return nullptr;
159 }
160 if (texture->destruction_callback &&
161 !context.GetReactor()->RegisterCleanupCallback(
162 handle,
163 [callback = texture->destruction_callback,
164 user_data = texture->user_data]() { callback(user_data); })) {
165 FML_LOG(ERROR) << "Could not register destruction callback";
166 return nullptr;
167 }
168
170}
171
172// |flutter::Texture|
173void EmbedderExternalTextureGL::OnGrContextCreated() {}
174
175// |flutter::Texture|
176void EmbedderExternalTextureGL::OnGrContextDestroyed() {}
177
178// |flutter::Texture|
179void EmbedderExternalTextureGL::MarkNewFrameAvailable() {
180 last_image_ = nullptr;
181}
182
183// |flutter::Texture|
184void EmbedderExternalTextureGL::OnTextureUnregistered() {}
185
186} // namespace flutter
static sk_sp< DlImage > Make(const SkImage *image)
Definition dl_image.cc:11
EmbedderExternalTextureGL(int64_t texture_identifier, const ExternalTextureCallback &callback)
std::function< std::unique_ptr< FlutterOpenGLTexture >(int64_t, size_t, size_t)> ExternalTextureCallback
int64_t Id()
Definition texture.h:61
std::shared_ptr< Context > GetContext() const
static ContextGLES & Cast(Context &base)
const std::shared_ptr< ReactorGLES > & GetReactor() const
static sk_sp< DlImageImpeller > Make(std::shared_ptr< Texture > texture, OwningContext owning_context=OwningContext::kIO)
Represents a handle to an underlying OpenGL object. Unlike OpenGL object handles, these handles can b...
Definition handle_gles.h:37
static std::shared_ptr< TextureGLES > WrapTexture(std::shared_ptr< ReactorGLES > reactor, TextureDescriptor desc, HandleGLES external_handle)
Create a texture by wrapping an external OpenGL texture handle. Ownership of the texture handle is as...
FlutterVulkanImage * image
FlutterDesktopBinaryReply callback
#define FML_LOG(severity)
Definition logging.h:101
#define FML_DCHECK(condition)
Definition logging.h:122
FlTexture * texture
impeller::Rect DlRect
it will be possible to load the file into Perfetto s trace viewer use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
ISize64 ISize
Definition size.h:162
int32_t height
int32_t width
impeller::AiksContext * aiks_context
Definition layer.h:108
DlCanvas * canvas
Definition layer.h:92
GrDirectContext * gr_context
Definition layer.h:99
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition rect.h:347
static constexpr std::enable_if_t< std::is_floating_point_v< FT >, TRect > Make(const TRect< U > &rect)
Definition rect.h:157
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition rect.h:341
constexpr TPoint< Type > GetOrigin() const
Returns the upper left corner of the rectangle as specified by the left/top or x/y values when it was...
Definition rect.h:320
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
int64_t texture_id