| Flutter Impeller |

Public Member Functions | Static Public Member Functions | List of all members
impeller::SurfaceMTL Class Referencefinal

`#include <surface_mtl.h>`

Inheritance diagram for impeller::SurfaceMTL:

## Public Member Functions
| ~SurfaceMTL () override
id< MTLDrawable > | drawable () const
IRect | coverage () const
void | PresentWithTransaction (bool present_with_transaction)
bool | PreparePresent () const
| Perform the final blit and trigger end of frame workloads. More...
bool | Present () const override
void | SetFrameBoundary (bool frame_boundary)
Public Member Functions inherited from impeller::Surface
| Surface ()
| Surface (const RenderTarget &target_desc)
virtual | ~Surface ()
const ISize & | GetSize () const
bool | IsValid () const
const RenderTarget & | GetRenderTarget () const

## Static Public Member Functions
static id< CAMetalDrawable > | GetMetalDrawableAndValidate (const std::shared_ptr< Context > &context, CAMetalLayer *layer)
| Wraps the current drawable of the given Metal layer to create a surface Impeller can render to. The surface must be created as late as possible and discarded immediately after rendering to it. More...
static std::unique_ptr< SurfaceMTL > | MakeFromMetalLayerDrawable (const std::shared_ptr< Context > &context, id< CAMetalDrawable > drawable, std::optional< IRect > clip_rect=std::nullopt)
static std::unique_ptr< SurfaceMTL > | MakeFromTexture (const std::shared_ptr< Context > &context, id< MTLTexture > texture, std::optional< IRect > clip_rect, id< CAMetalDrawable > drawable=nil)

## Detailed Description

Definition at line 17 of file surface_mtl.h.

## Constructor & Destructor Documentation

## ◆ ~SurfaceMTL()

impeller::SurfaceMTL::~SurfaceMTL | ( |  | ) |
| override default

## Member Function Documentation

## ◆ coverage()

IRect impeller::SurfaceMTL::coverage | ( |  | ) | const

Definition at line 221 of file surface_mtl.mm.

221 {
222 return IRect::MakeSize(resolve_texture_->GetSize());
223 }

References impeller::TRect< T >::MakeSize().

## ◆ drawable()

id<MTLDrawable> impeller::SurfaceMTL::drawable | ( |  | ) | const
| inline

Definition at line 56 of file surface_mtl.h.

56 { return drawable_; }

Referenced by MakeFromMetalLayerDrawable(), MakeFromTexture(), and Present().

## ◆ GetMetalDrawableAndValidate()

id< CAMetalDrawable > impeller::SurfaceMTL::GetMetalDrawableAndValidate | ( | const std::shared_ptr< Context > & | context,
|  | CAMetalLayer * | layer
| ) |  |
| static

Wraps the current drawable of the given Metal layer to create a surface Impeller can render to. The surface must be created as late as possible and discarded immediately after rendering to it.

Parameters

  [in] | context | The context
  [in] | layer | The layer whose current drawable to wrap to create a surface.

Returns
  A pointer to the wrapped surface or null.

Definition at line 25 of file surface_mtl.mm.

27 {
28 TRACE_EVENT0("impeller", "SurfaceMTL::WrapCurrentMetalLayerDrawable");
29
30 if (context == nullptr || !context->IsValid() || layer == nil) {
31 return nullptr;
32 }
33
34 id<CAMetalDrawable> current_drawable = nil;
35 {
36 TRACE_EVENT0("impeller", "WaitForNextDrawable");
37 current_drawable = [layer nextDrawable];
38 }
39
40 if (!current_drawable) {
41 VALIDATION_LOG << "Could not acquire current drawable.";
42 return nullptr;
43 }
44 return current_drawable;
45 }

References VALIDATION_LOG.

## ◆ MakeFromMetalLayerDrawable()

std::unique_ptr< SurfaceMTL > impeller::SurfaceMTL::MakeFromMetalLayerDrawable | ( | const std::shared_ptr< Context > & | context,
|  | id< CAMetalDrawable > | drawable,
|  | std::optional< IRect > | clip_rect = `std::nullopt`
| ) |  |
| static

Definition at line 123 of file surface_mtl.mm.

126 {
127 return SurfaceMTL::MakeFromTexture(context, drawable.texture, clip_rect,
128 drawable);
129 }

References drawable(), and MakeFromTexture().

## ◆ MakeFromTexture()

std::unique_ptr< SurfaceMTL > impeller::SurfaceMTL::MakeFromTexture | ( | const std::shared_ptr< Context > & | context,
|  | id< MTLTexture > | texture,
|  | std::optional< IRect > | clip_rect,
|  | id< CAMetalDrawable > | drawable = `nil`
| ) |  |
| static

Definition at line 131 of file surface_mtl.mm.

135 {
136 bool partial_repaint_blit_required = ShouldPerformPartialRepaint(clip_rect);
137
138 // The returned render target is the texture that Impeller will render the
139 // root pass to. If partial repaint is in use, this may be a new texture which
140 // is smaller than the given MTLTexture.
141 auto render_target =
142 WrapTextureWithRenderTarget(*context->GetResourceAllocator(), texture,
143 partial_repaint_blit_required, clip_rect);
144 if (!render_target) {
145 return nullptr;
146 }
147
148 // If partial repainting, set a "source" texture. The presence of a source
149 // texture and clip rect instructs the surface to blit this texture to the
150 // destination texture.
151 auto source_texture = partial_repaint_blit_required
152 ? render_target->GetRenderTargetTexture()
153 : nullptr;
154
155 // The final "destination" texture is the texture that will be presented. In
156 // this case, it's always the given drawable.
157 std::shared_ptr<Texture> destination_texture;
158 if (partial_repaint_blit_required) {
159 // If blitting for partial repaint, we need to wrap the drawable. Simply
160 // reuse the texture descriptor that was already formed for the new render
161 // target, but override the size with the drawable's size.
162 auto destination_descriptor =
163 render_target->GetRenderTargetTexture()->GetTextureDescriptor();
164 destination_descriptor.size = {static_cast<ISize::Type>(texture.width),
165 static_cast<ISize::Type>(texture.height)};
166 destination_texture = TextureMTL::Wrapper(destination_descriptor, texture);
167 } else {
168 // When not partial repaint blit is needed, the render target texture _is_
169 // the drawable texture.
170 destination_texture = render_target->GetRenderTargetTexture();
171 }
172
173 return std::unique_ptr<SurfaceMTL>(new SurfaceMTL(
174 context, // context
175 *render_target, // target
176 render_target->GetRenderTargetTexture(), // resolve_texture
177 drawable, // drawable
178 source_texture, // source_texture
179 destination_texture, // destination_texture
180 partial_repaint_blit_required, // requires_blit
181 clip_rect // clip_rect
182 ));
183 }

References drawable(), impeller::TextureMTL::Wrapper(), and impeller::WrapTextureWithRenderTarget().

Referenced by MakeFromMetalLayerDrawable().

## ◆ PreparePresent()

bool impeller::SurfaceMTL::PreparePresent | ( |  | ) | const

Perform the final blit and trigger end of frame workloads.

Definition at line 225 of file surface_mtl.mm.

225 {
226 auto context = context_.lock();
227 if (!context) {
228 return false;
229 }
230
231 #ifdef IMPELLER_DEBUG
232 context->GetResourceAllocator()->DebugTraceMemoryStatistics();
233 if (frame_boundary_) {
234 ContextMTL::Cast(context.get())->GetCaptureManager()->FinishCapture();
235 }
236 #endif // IMPELLER_DEBUG
237
238 if (requires_blit_) {
239 if (!(source_texture_ && destination_texture_)) {
240 return false;
241 }
242
243 auto blit_command_buffer = context->CreateCommandBuffer();
244 if (!blit_command_buffer) {
245 return false;
246 }
247 auto blit_pass = blit_command_buffer->CreateBlitPass();
248 if (!clip_rect_.has_value()) {
249 VALIDATION_LOG << "Missing clip rectangle.";
250 return false;
251 }
252 blit_pass->AddCopy(source_texture_, destination_texture_, std::nullopt,
253 clip_rect_->GetOrigin());
254 blit_pass->EncodeCommands(context->GetResourceAllocator());
255 if (!context->GetCommandQueue()->Submit({blit_command_buffer}).ok()) {
256 return false;
257 }
258 }
259 #ifdef IMPELLER_DEBUG
260 ContextMTL::Cast(context.get())->GetGPUTracer()->MarkFrameEnd();
261 #endif // IMPELLER_DEBUG
262 prepared_ = true;
263 return true;
264 }

References impeller::BackendCast< ContextMTL, Context >::Cast(), impeller::ContextMTL::CreateCommandBuffer(), and VALIDATION_LOG.

Referenced by Present().

## ◆ Present()

bool impeller::SurfaceMTL::Present | ( |  | ) | const
| override virtual

Reimplemented from impeller::Surface.

Definition at line 267 of file surface_mtl.mm.

267 {
268 if (!prepared_) {
269 PreparePresent();
270 }
271 auto context = context_.lock();
272 if (!context) {
273 return false;
274 }
275
276 if (drawable_) {
277 id<MTLCommandBuffer> command_buffer =
278 ContextMTL::Cast(context.get())
279 ->CreateMTLCommandBuffer("Present Waiter Command Buffer");
280
281 id<CAMetalDrawable> metal_drawable =
282 reinterpret_cast<id<CAMetalDrawable>>(drawable_);
283 if ([metal_drawable conformsToProtocol:@protocol(FlutterMetalDrawable)]) {
284 [(id<FlutterMetalDrawable>)metal_drawable
285 flutterPrepareForPresent:command_buffer];
286 }
287
288 // Intel iOS simulators do not seem to give backpressure on Metal drawable
289 // aquisition, which can result in Impeller running head of the GPU
290 // workload by dozens of frames. Slow this process down by blocking
291 // on submit until the last command buffer is at least scheduled.
292 #if defined(FML_OS_IOS_SIMULATOR) && defined(FML_ARCH_CPU_X86_64)
293 constexpr bool alwaysWaitForScheduling = true;
294 #else
295 constexpr bool alwaysWaitForScheduling = false;
296 #endif // defined(FML_OS_IOS_SIMULATOR) && defined(FML_ARCH_CPU_X86_64)
297
298 // If the threads have been merged, or there is a pending frame capture,
299 // then block on cmd buffer scheduling to ensure that the
300 // transaction/capture work correctly.
301 if (present_with_transaction_ || [[NSThread currentThread] isMainThread] ||
302 [[MTLCaptureManager sharedCaptureManager] isCapturing] ||
303 alwaysWaitForScheduling) {
304 TRACE_EVENT0("flutter", "waitUntilScheduled");
305 [command_buffer commit];
306 #if defined(FML_OS_IOS_SIMULATOR) && defined(FML_ARCH_CPU_X86_64)
307 [command_buffer waitUntilCompleted];
308 #else
309 [command_buffer waitUntilScheduled];
310 #endif // defined(FML_OS_IOS_SIMULATOR) && defined(FML_ARCH_CPU_X86_64)
311 [drawable_ present];
312 } else {
313 // The drawable may come from a FlutterMetalLayer, so it can't be
314 // presented through the command buffer.
315 id<CAMetalDrawable> drawable = drawable_;
316 [command_buffer addScheduledHandler:^(id<MTLCommandBuffer> buffer) {
317 [drawable present];
318 }];
319 [command_buffer commit];
320 }
321 }
322
323 return true;
324 }

References impeller::BackendCast< ContextMTL, Context >::Cast(), impeller::ContextMTL::CreateMTLCommandBuffer(), drawable(), and PreparePresent().

## ◆ PresentWithTransaction()

void impeller::SurfaceMTL::PresentWithTransaction | ( | bool | present_with_transaction | ) |
| inline

Mark this surface as presenting with a transaction.

If true, [Present] will block on the scheduling of a command buffer.

Definition at line 64 of file surface_mtl.h.

64 {
65 present_with_transaction_ = present_with_transaction;
66 }

## ◆ SetFrameBoundary()

void impeller::SurfaceMTL::SetFrameBoundary | ( | bool | frame_boundary | ) |
| inline

Definition at line 74 of file surface_mtl.h.

74 {
75 frame_boundary_ = frame_boundary;
76 }

The documentation for this class was generated from the following files:

- impeller/renderer/backend/metal/surface_mtl.h
- impeller/renderer/backend/metal/surface_mtl.mm

impeller::TSize::Type
T Type
Definition: size.h:20
impeller::WrapTextureWithRenderTarget
static std::optional< RenderTarget > WrapTextureWithRenderTarget(Allocator &allocator, id< MTLTexture > texture, bool requires_blit, std::optional< IRect > clip_rect)
Definition: surface_mtl.mm:47
FlutterMetalDrawable-p
Definition: surface_mtl.mm:16
impeller::SurfaceMTL::MakeFromTexture
static std::unique_ptr< SurfaceMTL > MakeFromTexture(const std::shared_ptr< Context > &context, id< MTLTexture > texture, std::optional< IRect > clip_rect, id< CAMetalDrawable > drawable=nil)
Definition: surface_mtl.mm:131
impeller::SurfaceMTL::PreparePresent
bool PreparePresent() const
Perform the final blit and trigger end of frame workloads.
Definition: surface_mtl.mm:225
impeller::SurfaceMTL::drawable
id< MTLDrawable > drawable() const
Definition: surface_mtl.h:56
impeller::TextureMTL::Wrapper
static std::shared_ptr< TextureMTL > Wrapper(TextureDescriptor desc, id< MTLTexture > texture, std::function< void()> deletion_proc=nullptr)
Definition: texture_mtl.mm:41
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::TRect::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
impeller::BackendCast< ContextMTL, Context >::Cast
static ContextMTL & Cast(Context &base)
Definition: backend_cast.h:13
impeller::ContextMTL::CreateMTLCommandBuffer
id< MTLCommandBuffer > CreateMTLCommandBuffer(const std::string &label) const
Definition: context_mtl.mm:373
impeller::ContextMTL::CreateCommandBuffer
std::shared_ptr< CommandBuffer > CreateCommandBuffer() const override
Create a new command buffer. Command buffers can be used to encode graphics, blit,...
Definition: context_mtl.mm:318