Flutter Engine
 
Loading...
Searching...
No Matches
embedder_test_context_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 <memory>
8#include <utility>
9
10#include "embedder.h"
11#include "flutter/fml/logging.h"
13
14namespace flutter::testing {
15
17 : EmbedderTestContext(std::move(assets_path)) {
18 metal_context_ = std::make_unique<TestMetalContext>();
22 .device = (__bridge FlutterMetalDeviceHandle)metal_context_->GetMetalDevice(),
23 .present_command_queue =
24 (__bridge FlutterMetalCommandQueueHandle)metal_context_->GetMetalCommandQueue(),
25 .get_next_drawable_callback =
26 [](void* user_data, const FlutterFrameInfo* frame_info) {
27 return reinterpret_cast<EmbedderTestContextMetal*>(user_data)->GetNextDrawable(
28 frame_info);
29 },
30 .present_drawable_callback = [](void* user_data, const FlutterMetalTexture* texture) -> bool {
31 return reinterpret_cast<EmbedderTestContextMetal*>(user_data)->Present(texture->texture_id);
32 },
33 .external_texture_frame_callback = [](void* user_data, int64_t texture_id, size_t width,
34 size_t height,
35 FlutterMetalExternalTexture* texture_out) -> bool {
37 texture_id, width, height, texture_out);
38 },
39 };
40}
41
43
47
49 return present_count_;
50}
51
53 return metal_context_.get();
54}
55
57 return metal_surface_.get();
58}
59
61 present_callback_ = std::move(present_callback);
62}
63
66 [&]() { return metal_surface_->GetRasterSurfaceSnapshot(); });
67 present_count_++;
68 if (present_callback_ != nullptr) {
69 return present_callback_(texture_id);
70 }
71 return metal_context_->Present(texture_id);
72}
73
75 TestExternalTextureCallback external_texture_frame_callback) {
76 external_texture_frame_callback_ = std::move(external_texture_frame_callback);
77}
78
80 size_t w,
81 size_t h,
83 if (external_texture_frame_callback_ != nullptr) {
84 return external_texture_frame_callback_(texture_id, w, h, output);
85 } else {
86 return false;
87 }
88}
89
91 NextDrawableCallback next_drawable_callback) {
92 next_drawable_callback_ = std::move(next_drawable_callback);
93}
94
96 if (next_drawable_callback_ != nullptr) {
97 return next_drawable_callback_(frame_info);
98 }
99
100 auto texture_info = metal_surface_->GetTextureInfo();
103 texture.texture_id = texture_info.texture_id;
104 texture.texture = reinterpret_cast<FlutterMetalTextureHandle>(texture_info.texture);
105 return texture;
106}
107
108void EmbedderTestContextMetal::SetSurface(DlISize surface_size) {
109 FML_CHECK(surface_size_.IsEmpty());
110 surface_size_ = surface_size;
111 metal_surface_ = TestMetalSurface::Create(*metal_context_, surface_size_);
112}
113
114void EmbedderTestContextMetal::SetupCompositor() {
115 FML_CHECK(!compositor_) << "Already set up a compositor in this context.";
116 FML_CHECK(metal_surface_) << "Set up the Metal surface before setting up a compositor.";
118 std::make_unique<EmbedderTestCompositorMetal>(surface_size_, metal_surface_->GetGrContext());
119}
120
121} // namespace flutter::testing
std::unique_ptr< EmbedderTestCompositor > compositor_
void FireRootSurfacePresentCallbackIfPresent(const std::function< sk_sp< SkImage >(void)> &image_callback)
std::function< bool(int64_t texture_id, size_t w, size_t h, FlutterMetalExternalTexture *output)> TestExternalTextureCallback
std::function< FlutterMetalTexture(const FlutterFrameInfo *frame_info)> NextDrawableCallback
std::function< bool(int64_t texture_id)> PresentCallback
void SetNextDrawableCallback(NextDrawableCallback next_drawable_callback)
void SetPresentCallback(PresentCallback present_callback)
void SetExternalTextureCallback(TestExternalTextureCallback external_texture_frame_callback)
FlutterMetalTexture GetNextDrawable(const FlutterFrameInfo *frame_info)
EmbedderTestContextType GetContextType() const override
bool PopulateExternalTexture(int64_t texture_id, size_t w, size_t h, FlutterMetalExternalTexture *output)
Creates a MTLTexture backed SkSurface and context that can be used to render to in unit-tests.
static std::unique_ptr< TestMetalSurface > Create(const TestMetalContext &test_metal_context, DlISize surface_size=DlISize())
const void * FlutterMetalDeviceHandle
Alias for id<MTLDevice>.
Definition embedder.h:800
@ kMetal
Definition embedder.h:85
const void * FlutterMetalCommandQueueHandle
Alias for id<MTLCommandQueue>.
Definition embedder.h:803
const void * FlutterMetalTextureHandle
Alias for id<MTLTexture>.
Definition embedder.h:806
VkDevice device
Definition main.cc:69
#define FML_CHECK(condition)
Definition logging.h:104
FlTexture * texture
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder h
Definition switch_defs.h:54
Definition ref_ptr.h:261
int32_t height
int32_t width
size_t struct_size
The size of this struct. Must be sizeof(FlutterMetalRendererConfig).
Definition embedder.h:889
size_t struct_size
The size of this struct. Must be sizeof(FlutterMetalTexture).
Definition embedder.h:855
FlutterMetalRendererConfig metal
Definition embedder.h:1035
FlutterRendererType type
Definition embedder.h:1031
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition size.h:123
int64_t texture_id