Flutter Engine
 
Loading...
Searching...
No Matches
test_metal_surface_impl.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
11#include "third_party/skia/include/core/SkColorSpace.h"
12#include "third_party/skia/include/core/SkImage.h"
13#include "third_party/skia/include/core/SkRefCnt.h"
14#include "third_party/skia/include/core/SkSurface.h"
15#include "third_party/skia/include/gpu/GpuTypes.h"
16#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
17#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
18#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlBackendSurface.h"
19#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlTypes.h"
20
21static_assert(__has_feature(objc_arc), "ARC must be enabled.");
22
23namespace flutter::testing {
24
25void TestMetalSurfaceImpl::Init(const TestMetalContext::TextureInfo& texture_info,
26 const DlISize& surface_size) {
27 id<MTLTexture> texture = (__bridge id<MTLTexture>)texture_info.texture;
28
29 GrMtlTextureInfo skia_texture_info;
30 skia_texture_info.fTexture.retain((__bridge GrMTLHandle)texture);
31 GrBackendTexture backend_texture = GrBackendTextures::MakeMtl(
32 surface_size.width, surface_size.height, skgpu::Mipmapped::kNo, skia_texture_info);
33
34 sk_sp<SkSurface> surface = SkSurfaces::WrapBackendTexture(
35 test_metal_context_.GetSkiaContext().get(), backend_texture, kTopLeft_GrSurfaceOrigin, 1,
36 kBGRA_8888_SkColorType, nullptr, nullptr);
37
38 if (!surface) {
39 FML_LOG(ERROR) << "Could not create Skia surface from a Metal texture.";
40 return;
41 }
42
43 surface_ = std::move(surface);
44 texture_info_ = texture_info;
45 is_valid_ = true;
46}
47
49 int64_t texture_id,
50 const DlISize& surface_size)
51 : test_metal_context_(test_metal_context) {
53 const_cast<TestMetalContext&>(test_metal_context_).GetTextureInfo(texture_id);
54 Init(texture_info, surface_size);
55}
56
58 const DlISize& surface_size)
59 : test_metal_context_(test_metal_context) {
60 if (surface_size.IsEmpty()) {
61 FML_LOG(ERROR) << "Size of test Metal surface was empty.";
62 return;
63 }
65 const_cast<TestMetalContext&>(test_metal_context_).CreateMetalTexture(surface_size);
66 Init(texture_info, surface_size);
67}
68
69sk_sp<SkImage> TestMetalSurfaceImpl::GetRasterSurfaceSnapshot() {
70 if (!IsValid()) {
71 return nullptr;
72 }
73
74 if (!surface_) {
75 FML_LOG(ERROR) << "Aborting snapshot because of on-screen surface "
76 "acquisition failure.";
77 return nullptr;
78 }
79
80 auto device_snapshot = surface_->makeImageSnapshot();
81
82 if (!device_snapshot) {
83 FML_LOG(ERROR) << "Could not create the device snapshot while attempting "
84 "to snapshot the Metal surface.";
85 return nullptr;
86 }
87
88 auto host_snapshot = device_snapshot->makeRasterImage(nullptr);
89
90 if (!host_snapshot) {
91 FML_LOG(ERROR) << "Could not create the host snapshot while attempting to "
92 "snapshot the Metal surface.";
93 return nullptr;
94 }
95
96 return host_snapshot;
97}
98
99// |TestMetalSurface|
101
102// |TestMetalSurface|
103bool TestMetalSurfaceImpl::IsValid() const {
104 return is_valid_;
105}
106
107// |TestMetalSurface|
108sk_sp<GrDirectContext> TestMetalSurfaceImpl::GetGrContext() const {
109 return IsValid() ? test_metal_context_.GetSkiaContext() : nullptr;
110}
111
112// |TestMetalSurface|
113sk_sp<SkSurface> TestMetalSurfaceImpl::GetSurface() const {
114 return IsValid() ? surface_ : nullptr;
115}
116
117// |TestMetalSurface|
118TestMetalContext::TextureInfo TestMetalSurfaceImpl::GetTextureInfo() {
119 return IsValid() ? texture_info_ : TestMetalContext::TextureInfo();
120}
121
122} // namespace flutter::testing
sk_sp< GrDirectContext > GetSkiaContext() const
TestMetalSurfaceImpl(const TestMetalContext &test_metal_context, const DlISize &surface_size)
VkSurfaceKHR surface
Definition main.cc:65
#define FML_LOG(severity)
Definition logging.h:101
FlTexture * texture
impeller::ISize32 DlISize
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