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