Flutter Engine
flutter::TestMetalSurfaceImpl Class Reference

#include <test_metal_surface_impl.h>

Inheritance diagram for flutter::TestMetalSurfaceImpl:
flutter::TestMetalSurface

Public Member Functions

 TestMetalSurfaceImpl (SkISize surface_size)
 
 ~TestMetalSurfaceImpl () override
 
- Public Member Functions inherited from flutter::TestMetalSurface
virtual ~TestMetalSurface ()
 

Additional Inherited Members

- Static Public Member Functions inherited from flutter::TestMetalSurface
static bool PlatformSupportsMetal ()
 
static std::unique_ptr< TestMetalSurfaceCreate (SkISize surface_size=SkISize::MakeEmpty())
 
- Protected Member Functions inherited from flutter::TestMetalSurface
 TestMetalSurface ()
 

Detailed Description

Definition at line 13 of file test_metal_surface_impl.h.

Constructor & Destructor Documentation

◆ TestMetalSurfaceImpl()

flutter::TestMetalSurfaceImpl::TestMetalSurfaceImpl ( SkISize  surface_size)

Definition at line 15 of file test_metal_surface_impl.mm.

References FML_ALLOW_UNUSED_LOCAL, FML_LOG, fml::scoped_nsprotocol< NST >::get(), height, width, and ~TestMetalSurfaceImpl().

15  {
16  if (surface_size.isEmpty()) {
17  FML_LOG(ERROR) << "Size of test Metal surface was empty.";
18  return;
19  }
20 
21  auto device = fml::scoped_nsobject{[MTLCreateSystemDefaultDevice() retain]};
22  if (!device) {
23  FML_LOG(ERROR) << "Could not acquire Metal device.";
24  return;
25  }
26 
27  auto command_queue = fml::scoped_nsobject{[device.get() newCommandQueue]};
28  if (!command_queue) {
29  FML_LOG(ERROR) << "Could not create the default command queue.";
30  return;
31  }
32 
33  auto texture_descriptor = fml::scoped_nsobject{
34  [[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
35  width:surface_size.width()
36  height:surface_size.height()
37  mipmapped:NO] retain]};
38 
39  // The most pessimistic option and disables all optimizations but allows tests
40  // the most flexible access to the surface. They may read and write to the
41  // surface from shaders or use as a pixel view.
42  texture_descriptor.get().usage = MTLTextureUsageUnknown;
43 
44  if (!texture_descriptor) {
45  FML_LOG(ERROR) << "Invalid texture descriptor.";
46  return;
47  }
48 
49  auto texture =
50  fml::scoped_nsobject{[device.get() newTextureWithDescriptor:texture_descriptor.get()]};
51 
52  if (!texture) {
53  FML_LOG(ERROR) << "Could not create texture from texture descriptor.";
54  return;
55  }
56 
57  auto skia_context = GrDirectContext::MakeMetal(device.get(), command_queue.get());
58 
59  if (skia_context) {
60  // Skia wants ownership of the device and queue. If a context was created,
61  // we now no longer own the argument. Release the arguments only on
62  // successful creation of the context.
63  FML_ALLOW_UNUSED_LOCAL(device.release());
64  FML_ALLOW_UNUSED_LOCAL(command_queue.release());
65  } else {
66  FML_LOG(ERROR) << "Could not create the GrDirectContext from the Metal Device "
67  "and command queue.";
68  return;
69  }
70 
71  GrMtlTextureInfo skia_texture_info;
72  skia_texture_info.fTexture = sk_cf_obj<const void*>{[texture.get() retain]};
73 
74  auto backend_render_target = GrBackendRenderTarget{
75  surface_size.width(), // width
76  surface_size.height(), // height
77  1, // sample count
78  skia_texture_info // texture info
79  };
80 
81  auto surface = SkSurface::MakeFromBackendRenderTarget(
82  skia_context.get(), // context
83  backend_render_target, // backend render target
84  kTopLeft_GrSurfaceOrigin, // surface origin
85  kBGRA_8888_SkColorType, // color type
86  nullptr, // color space
87  nullptr, // surface properties
88  nullptr, // release proc (texture is already ref counted in sk_cf_obj)
89  nullptr // release context
90  );
91 
92  if (!surface) {
93  FML_LOG(ERROR) << "Could not create Skia surface from a Metal texture.";
94  return;
95  }
96 
97  surface_ = std::move(surface);
98  context_ = std::move(skia_context);
99 
100  is_valid_ = true;
101 }
#define FML_ALLOW_UNUSED_LOCAL(x)
#define FML_LOG(severity)
Definition: logging.h:65
int32_t height
int32_t width
if(event->type==GDK_BUTTON_PRESS)
Definition: fl_view.cc:79

◆ ~TestMetalSurfaceImpl()

flutter::TestMetalSurfaceImpl::~TestMetalSurfaceImpl ( )
overridedefault

Referenced by TestMetalSurfaceImpl().


The documentation for this class was generated from the following files: