forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_metal_surface_impl.mm
119 lines (96 loc) · 3.89 KB
/
test_metal_surface_impl.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/testing/test_metal_surface_impl.h"
#include <Metal/Metal.h>
#include "flutter/fml/logging.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "third_party/skia/include/core/SkSurface.h"
namespace flutter {
TestMetalSurfaceImpl::TestMetalSurfaceImpl(SkISize surface_size) {
if (surface_size.isEmpty()) {
FML_LOG(ERROR) << "Size of test Metal surface was empty.";
return;
}
auto device = fml::scoped_nsobject{[MTLCreateSystemDefaultDevice() retain]};
if (!device) {
FML_LOG(ERROR) << "Could not acquire Metal device.";
return;
}
auto command_queue = fml::scoped_nsobject{[device.get() newCommandQueue]};
if (!command_queue) {
FML_LOG(ERROR) << "Could not create the default command queue.";
return;
}
auto texture_descriptor = fml::scoped_nsobject{
[[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
width:surface_size.width()
height:surface_size.height()
mipmapped:NO] retain]};
// The most pessimistic option and disables all optimizations but allows tests
// the most flexible access to the surface. They may read and write to the
// surface from shaders or use as a pixel view.
texture_descriptor.get().usage = MTLTextureUsageUnknown;
if (!texture_descriptor) {
FML_LOG(ERROR) << "Invalid texture descriptor.";
return;
}
auto texture =
fml::scoped_nsobject{[device.get() newTextureWithDescriptor:texture_descriptor.get()]};
if (!texture) {
FML_LOG(ERROR) << "Could not create texture from texture descriptor.";
return;
}
auto skia_context = GrContext::MakeMetal(device.get(), command_queue.get());
if (skia_context) {
// Skia wants ownership of the device and queue. If a context was created,
// we now no longer own the argument. Release the arguments only on
// successful creation of the context.
FML_ALLOW_UNUSED_LOCAL(device.release());
FML_ALLOW_UNUSED_LOCAL(command_queue.release());
} else {
FML_LOG(ERROR) << "Could not create the GrContext from the Metal Device "
"and command queue.";
return;
}
GrMtlTextureInfo skia_texture_info;
skia_texture_info.fTexture = sk_cf_obj<const void*>{[texture.get() retain]};
auto backend_render_target = GrBackendRenderTarget{
surface_size.width(), // width
surface_size.height(), // height
1, // sample count
skia_texture_info // texture info
};
auto surface = SkSurface::MakeFromBackendRenderTarget(
skia_context.get(), // context
backend_render_target, // backend render target
kTopLeft_GrSurfaceOrigin, // surface origin
kBGRA_8888_SkColorType, // color type
nullptr, // color space
nullptr, // surface properties
nullptr, // release proc (texture is already ref counted in sk_cf_obj)
nullptr // release context
);
if (!surface) {
FML_LOG(ERROR) << "Could not create Skia surface from a Metal texture.";
return;
}
surface_ = std::move(surface);
context_ = std::move(skia_context);
is_valid_ = true;
}
// |TestMetalSurface|
TestMetalSurfaceImpl::~TestMetalSurfaceImpl() = default;
// |TestMetalSurface|
bool TestMetalSurfaceImpl::IsValid() const {
return is_valid_;
}
// |TestMetalSurface|
sk_sp<GrContext> TestMetalSurfaceImpl::GetGrContext() const {
return IsValid() ? context_ : nullptr;
}
// |TestMetalSurface|
sk_sp<SkSurface> TestMetalSurfaceImpl::GetSurface() const {
return IsValid() ? surface_ : nullptr;
}
} // namespace flutter