Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
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
16
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/SkPaint.h"
22#include "third_party/skia/include/core/SkSize.h"
23#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
24#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
25#include "third_party/skia/include/gpu/ganesh/SkImageGanesh.h"
26#include "third_party/skia/include/gpu/ganesh/gl/GrGLBackendSurface.h"
27#include "third_party/skia/include/gpu/ganesh/gl/GrGLTypes.h"
28
29namespace flutter {
30
32 int64_t texture_identifier,
34 : Texture(texture_identifier), external_texture_callback_(callback) {
35 FML_DCHECK(external_texture_callback_);
36}
37
39
40// |flutter::Texture|
41void EmbedderExternalTextureGL::Paint(PaintContext& context,
42 const DlRect& bounds,
43 bool freeze,
44 const DlImageSampling sampling) {
45 if (last_image_ == nullptr) {
46 last_image_ =
47 ResolveTexture(Id(), //
48 context.gr_context, //
49 context.aiks_context, //
50 SkISize::Make(bounds.GetWidth(), bounds.GetHeight()) //
51 );
52 }
53
54 DlCanvas* canvas = context.canvas;
55 const DlPaint* paint = context.paint;
56
57 if (last_image_) {
58 DlRect image_bounds = DlRect::Make(last_image_->GetBounds());
59 if (bounds != image_bounds) {
60 canvas->DrawImageRect(last_image_, image_bounds, bounds, sampling, paint);
61 } else {
62 canvas->DrawImage(last_image_, bounds.GetOrigin(), sampling, paint);
63 }
64 }
65}
66
67sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTexture(
68 int64_t texture_id,
69 GrDirectContext* context,
70 impeller::AiksContext* aiks_context,
71 const SkISize& size) {
72 if (!!aiks_context) {
73 return ResolveTextureImpeller(texture_id, aiks_context, size);
74 } else {
75 return ResolveTextureSkia(texture_id, context, size);
76 }
77}
78
79sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTextureSkia(
80 int64_t texture_id,
81 GrDirectContext* context,
82 const SkISize& size) {
83 context->flushAndSubmit();
84 context->resetContext(kAll_GrBackendState);
85 std::unique_ptr<FlutterOpenGLTexture> texture =
86 external_texture_callback_(texture_id, size.width(), size.height());
87
88 if (!texture) {
89 return nullptr;
90 }
91
92 GrGLTextureInfo gr_texture_info = {texture->target, texture->name,
93 texture->format};
94
95 size_t width = size.width();
96 size_t height = size.height();
97
98 if (texture->width != 0 && texture->height != 0) {
99 width = texture->width;
100 height = texture->height;
101 }
102
103 auto gr_backend_texture = GrBackendTextures::MakeGL(
104 width, height, skgpu::Mipmapped::kNo, gr_texture_info);
105 SkImages::TextureReleaseProc release_proc = texture->destruction_callback;
106 auto image =
107 SkImages::BorrowTextureFrom(context, // context
108 gr_backend_texture, // texture handle
109 kTopLeft_GrSurfaceOrigin, // origin
110 kRGBA_8888_SkColorType, // color type
111 kPremul_SkAlphaType, // alpha type
112 nullptr, // colorspace
113 release_proc, // texture release proc
114 texture->user_data // texture release context
115 );
116
117 if (!image) {
118 // In case Skia rejects the image, call the release proc so that
119 // embedders can perform collection of intermediates.
120 if (release_proc) {
121 release_proc(texture->user_data);
122 }
123 FML_LOG(ERROR) << "Could not create external texture->";
124 return nullptr;
125 }
126
127 // This image should not escape local use by EmbedderExternalTextureGL
128 return DlImageSkia::Make(std::move(image));
129}
130
131sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTextureImpeller(
132 int64_t texture_id,
133 impeller::AiksContext* aiks_context,
134 const SkISize& size) {
135 std::unique_ptr<FlutterOpenGLTexture> texture =
136 external_texture_callback_(texture_id, size.width(), size.height());
137
138 if (!texture) {
139 return nullptr;
140 }
141
142 // Call the destruction callback if an error occurs.
143 fml::ScopedCleanupClosure scoped_cleanup([&texture]() {
144 if (texture->destruction_callback) {
145 texture->destruction_callback(texture->user_data);
146 }
147 });
148
149 if (texture->format != GL_RGBA8) {
150 FML_LOG(ERROR) << "Only support GL_RGBA8 format now";
151 return nullptr;
152 }
153
155 desc.size = impeller::ISize(texture->width, texture->height);
157
158 impeller::ContextGLES& context =
159 impeller::ContextGLES::Cast(*aiks_context->GetContext());
160 impeller::HandleGLES handle = context.GetReactor()->CreateHandle(
162 std::shared_ptr<impeller::TextureGLES> image =
163 impeller::TextureGLES::WrapTexture(context.GetReactor(), desc, handle);
164
165 if (!image) {
166 FML_LOG(ERROR) << "Could not create external texture";
167 return nullptr;
168 }
169
170 VoidCallback destruction_callback = texture->destruction_callback;
171 if (!destruction_callback) {
172 // Set a no-op cleanup callback if the texture does not provide a callback.
173 // The presence of a cleanup callback indicates that the embedder controls
174 // the GL texture's lifetime and Impeller should not delete it.
175 destruction_callback = [](void*) {};
176 }
177 auto cleanup_callback = [callback = destruction_callback,
178 user_data = texture->user_data]() {
180 };
181 if (!context.GetReactor()->RegisterCleanupCallback(handle,
182 cleanup_callback)) {
183 FML_LOG(ERROR) << "Could not register destruction callback";
184 return nullptr;
185 }
186
187 image->SetCoordinateSystem(
189
190 scoped_cleanup.Release();
191
193}
194
195// |flutter::Texture|
196void EmbedderExternalTextureGL::OnGrContextCreated() {}
197
198// |flutter::Texture|
199void EmbedderExternalTextureGL::OnGrContextDestroyed() {}
200
201// |flutter::Texture|
202void EmbedderExternalTextureGL::MarkNewFrameAvailable() {
203 last_image_ = nullptr;
204}
205
206// |flutter::Texture|
207void EmbedderExternalTextureGL::OnTextureUnregistered() {}
208
209} // namespace flutter
static sk_sp< DlImage > Make(const SkImage *image)
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
Wraps a closure that is invoked in the destructor unless released by the caller.
Definition closure.h:32
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:42
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...
void(* VoidCallback)(void *)
Definition embedder.h:416
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:381
static constexpr std::enable_if_t< std::is_floating_point_v< FT >, TRect > Make(const TRect< U > &rect)
Definition rect.h:181
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition rect.h:375
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:354
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
int64_t texture_id