Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
embedder_external_texture_metal.mm
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
7#include "flow/layers/layer.h"
11#include "third_party/skia/include/core/SkImage.h"
12#include "third_party/skia/include/core/SkSize.h"
13#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
14#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
15
16namespace flutter {
17
18static bool ValidNumTextures(int expected, int actual) {
19 if (expected == actual) {
20 return true;
21 } else {
22 FML_LOG(ERROR) << "Invalid number of textures, expected: " << expected << ", got: " << actual;
23 return false;
24 }
25}
26
29 : Texture(texture_identifier), external_texture_callback_(callback) {
30 FML_DCHECK(external_texture_callback_);
31}
32
34
35// |flutter::Texture|
36void EmbedderExternalTextureMetal::Paint(PaintContext& context,
37 const DlRect& bounds,
38 bool freeze,
39 const DlImageSampling sampling) {
40 if (last_image_ == nullptr) {
41 last_image_ = ResolveTexture(Id(), context.gr_context, context.aiks_context,
42 SkISize::Make(bounds.GetWidth(), bounds.GetHeight()));
43 }
44
45 DlCanvas* canvas = context.canvas;
46 const DlPaint* paint = context.paint;
47
48 if (last_image_) {
49 DlRect image_bounds = DlRect::Make(last_image_->GetBounds());
50 if (bounds != image_bounds) {
51 canvas->DrawImageRect(last_image_, image_bounds, bounds, sampling, paint);
52 } else {
53 canvas->DrawImage(last_image_, DlPoint(bounds.GetX(), bounds.GetY()), sampling, paint);
54 }
55 }
56}
57
58sk_sp<DlImage> EmbedderExternalTextureMetal::ResolveTexture(int64_t texture_id,
59 GrDirectContext* context,
60 impeller::AiksContext* aiks_context,
61 const SkISize& size) {
62 std::unique_ptr<FlutterMetalExternalTexture> texture =
63 external_texture_callback_(texture_id, size.width(), size.height());
64
65 if (!texture) {
66 return nullptr;
67 }
68 if (aiks_context) {
69 switch (texture->pixel_format) {
71 if (ValidNumTextures(1, texture->num_textures)) {
72 id<MTLTexture> rgbaTex = (__bridge id<MTLTexture>)texture->textures[0];
73 return [FlutterDarwinExternalTextureImpellerImageWrapper wrapRGBATexture:rgbaTex
74 aiksContext:aiks_context];
75 }
76 break;
77 }
79 if (ValidNumTextures(2, texture->num_textures)) {
80 id<MTLTexture> yTex = (__bridge id<MTLTexture>)texture->textures[0];
81 id<MTLTexture> uvTex = (__bridge id<MTLTexture>)texture->textures[1];
82 impeller::YUVColorSpace colorSpace =
83 texture->yuv_color_space ==
87 return [FlutterDarwinExternalTextureImpellerImageWrapper wrapYUVATexture:yTex
88 UVTex:uvTex
89 YUVColorSpace:colorSpace
90 aiksContext:aiks_context];
91 }
92 break;
93 }
94 }
95 return nullptr;
96 }
97
98 sk_sp<SkImage> image;
99
100 switch (texture->pixel_format) {
102 if (ValidNumTextures(1, texture->num_textures)) {
103 id<MTLTexture> rgbaTex = (__bridge id<MTLTexture>)texture->textures[0];
104 image = [FlutterDarwinExternalTextureSkImageWrapper wrapRGBATexture:rgbaTex
105 grContext:context
106 width:rgbaTex.width
107 height:rgbaTex.height];
108 }
109 break;
110 }
112 if (ValidNumTextures(2, texture->num_textures)) {
113 id<MTLTexture> yTex = (__bridge id<MTLTexture>)texture->textures[0];
114 id<MTLTexture> uvTex = (__bridge id<MTLTexture>)texture->textures[1];
115 SkYUVColorSpace colorSpace =
117 ? kRec601_Limited_SkYUVColorSpace
118 : kJPEG_Full_SkYUVColorSpace;
120 UVTex:uvTex
121 YUVColorSpace:colorSpace
122 grContext:context
123 width:size.width()
124 height:size.height()];
125 }
126 break;
127 }
128 }
129
130 if (!image) {
131 FML_LOG(ERROR) << "Could not create external texture: " << texture_id;
132 }
133
134 // This image should not escape local use by EmbedderExternalTextureMetal
135 return DlImageSkia::Make(std::move(image));
136}
137
138// |flutter::Texture|
139void EmbedderExternalTextureMetal::OnGrContextCreated() {}
140
141// |flutter::Texture|
142void EmbedderExternalTextureMetal::OnGrContextDestroyed() {}
143
144// |flutter::Texture|
145void EmbedderExternalTextureMetal::MarkNewFrameAvailable() {
146 last_image_ = nullptr;
147}
148
149// |flutter::Texture|
150void EmbedderExternalTextureMetal::OnTextureUnregistered() {}
151
152} // namespace flutter
static sk_sp< DlImage > Make(const SkImage *image)
EmbedderExternalTextureMetal(int64_t texture_identifier, const ExternalTextureCallback &callback)
std::function< std::unique_ptr< FlutterMetalExternalTexture >(int64_t, size_t, size_t)> ExternalTextureCallback
int64_t Id()
Definition texture.h:61
@ kBT601LimitedRange
Definition embedder.h:824
@ kRGBA
Definition embedder.h:818
@ kYUVA
Definition embedder.h:817
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
static bool ValidNumTextures(int expected, int actual)
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
impeller::Point DlPoint
YUVColorSpace
Definition color.h:54
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 GetY() const
Returns the Y coordinate of the upper left corner, equivalent to |GetOrigin().y|.
Definition rect.h:371
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 GetX() const
Returns the X coordinate of the upper left corner, equivalent to |GetOrigin().x|.
Definition rect.h:367
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition rect.h:375
int64_t texture_id