7#include <Metal/Metal.h>
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"
21static_assert(__has_feature(objc_arc),
"ARC must be enabled.");
25void TestMetalSurfaceImpl::Init(
const TestMetalContext::TextureInfo& texture_info,
27 id<MTLTexture>
texture = (__bridge id<MTLTexture>)texture_info.texture;
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);
34 sk_sp<SkSurface>
surface = SkSurfaces::WrapBackendTexture(
35 test_metal_context_.
GetSkiaContext().get(), backend_texture, kTopLeft_GrSurfaceOrigin, 1,
36 kBGRA_8888_SkColorType,
nullptr,
nullptr);
39 FML_LOG(ERROR) <<
"Could not create Skia surface from a Metal texture.";
43 surface_ = std::move(surface);
44 texture_info_ = texture_info;
51 : test_metal_context_(test_metal_context) {
54 Init(texture_info, surface_size);
59 : test_metal_context_(test_metal_context) {
61 FML_LOG(ERROR) <<
"Size of test Metal surface was empty.";
65 const_cast<TestMetalContext&
>(test_metal_context_).CreateMetalTexture(surface_size);
66 Init(texture_info, surface_size);
69sk_sp<SkImage> TestMetalSurfaceImpl::GetRasterSurfaceSnapshot() {
75 FML_LOG(ERROR) <<
"Aborting snapshot because of on-screen surface "
76 "acquisition failure.";
80 auto device_snapshot = surface_->makeImageSnapshot();
82 if (!device_snapshot) {
83 FML_LOG(ERROR) <<
"Could not create the device snapshot while attempting "
84 "to snapshot the Metal surface.";
88 auto host_snapshot = device_snapshot->makeRasterImage(
nullptr);
91 FML_LOG(ERROR) <<
"Could not create the host snapshot while attempting to "
92 "snapshot the Metal surface.";
103bool TestMetalSurfaceImpl::IsValid()
const {
108sk_sp<GrDirectContext> TestMetalSurfaceImpl::GetGrContext()
const {
109 return IsValid() ? test_metal_context_.
GetSkiaContext() :
nullptr;
113sk_sp<SkSurface> TestMetalSurfaceImpl::GetSurface()
const {
114 return IsValid() ? surface_ :
nullptr;
118TestMetalContext::TextureInfo TestMetalSurfaceImpl::GetTextureInfo() {
119 return IsValid() ? texture_info_ : TestMetalContext::TextureInfo();
sk_sp< GrDirectContext > GetSkiaContext() const
#define FML_LOG(severity)
impeller::ISize32 DlISize
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.