Flutter Engine
 
Loading...
Searching...
No Matches
test_metal_context.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 <Metal/Metal.h>
8#include <iostream>
9
10#include "flutter/fml/logging.h"
11#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
12#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlBackendContext.h"
13#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlDirectContext.h"
14
15static_assert(__has_feature(objc_arc), "ARC must be enabled.");
16
17namespace flutter::testing {
18
20 id<MTLDevice> device = MTLCreateSystemDefaultDevice();
21 if (!device) {
22 FML_LOG(ERROR) << "Could not acquire Metal device.";
23 return;
24 }
25
26 id<MTLCommandQueue> command_queue = [device newCommandQueue];
27 if (!command_queue) {
28 FML_LOG(ERROR) << "Could not create the default command queue.";
29 return;
30 }
31
32 [command_queue setLabel:@"Flutter Test Queue"];
33
34 GrMtlBackendContext backendContext = {};
35 // Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later
36 // when the GrDirectContext is collected.
37 backendContext.fDevice.retain((__bridge GrMTLHandle)device);
38 backendContext.fQueue.retain((__bridge GrMTLHandle)command_queue);
39 skia_context_ = GrDirectContexts::MakeMetal(backendContext);
40 if (!skia_context_) {
41 FML_LOG(ERROR) << "Could not create the GrDirectContext from the Metal Device "
42 "and command queue.";
43 }
44 device_ = device;
45 command_queue_ = command_queue;
46}
47
49 std::scoped_lock lock(textures_mutex_);
50 textures_.clear();
51}
52
53id<MTLDevice> TestMetalContext::GetMetalDevice() const {
54 return device_;
55}
56
57id<MTLCommandQueue> TestMetalContext::GetMetalCommandQueue() const {
58 return command_queue_;
59}
60
61sk_sp<GrDirectContext> TestMetalContext::GetSkiaContext() const {
62 return skia_context_;
63}
64
66 std::scoped_lock lock(textures_mutex_);
67 MTLTextureDescriptor* texture_descriptor =
68 [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
69 width:size.width
70 height:size.height
71 mipmapped:NO];
72
73 // The most pessimistic option and disables all optimizations but allows tests
74 // the most flexible access to the surface. They may read and write to the
75 // surface from shaders or use as a pixel view.
76 texture_descriptor.usage = MTLTextureUsageUnknown;
77
78 if (!texture_descriptor) {
79 FML_CHECK(false) << "Invalid texture descriptor.";
80 return {.texture_id = -1, .texture = nullptr};
81 }
82
83 if (!device_) {
84 FML_CHECK(false) << "Invalid Metal device.";
85 return {.texture_id = -1, .texture = nullptr};
86 }
87
88 id<MTLTexture> texture = [device_ newTextureWithDescriptor:texture_descriptor];
89 if (!texture) {
90 FML_CHECK(false) << "Could not create texture from texture descriptor.";
91 return {.texture_id = -1, .texture = nullptr};
92 }
93
94 const int64_t texture_id = texture_id_ctr_++;
95 sk_cfp<void*> texture_ptr;
96 texture_ptr.retain((__bridge void*)texture);
97 textures_[texture_id] = texture_ptr;
98
99 return {
100 .texture_id = texture_id,
101 .texture = (__bridge void*)texture,
102 };
103}
104
105// Don't remove the texture from the map here.
107 std::scoped_lock lock(textures_mutex_);
108 if (textures_.find(texture_id) == textures_.end()) {
109 return false;
110 } else {
111 return true;
112 }
113}
114
116 std::scoped_lock lock(textures_mutex_);
117 if (textures_.find(texture_id) == textures_.end()) {
118 FML_CHECK(false) << "Invalid texture id: " << texture_id;
119 return {.texture_id = -1, .texture = nullptr};
120 } else {
121 return {
122 .texture_id = texture_id,
123 .texture = textures_[texture_id].get(),
124 };
125 }
126}
127
128} // namespace flutter::testing
id< MTLCommandQueue > GetMetalCommandQueue() const
TextureInfo GetTextureInfo(int64_t texture_id)
TextureInfo CreateMetalTexture(const DlISize &size)
Returns texture_id = -1 when texture creation fails.
sk_sp< GrDirectContext > GetSkiaContext() const
VkDevice device
Definition main.cc:69
#define FML_LOG(severity)
Definition logging.h:101
#define FML_CHECK(condition)
Definition logging.h:104
FlTexture * texture
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
int32_t height
int32_t width
int64_t texture_id