Flutter Impeller
metal_screenshotter.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 <CoreImage/CoreImage.h>
10 #define GLFW_INCLUDE_NONE
11 #include "flutter/fml/synchronization/waitable_event.h"
12 #include "third_party/glfw/include/GLFW/glfw3.h"
13 
14 namespace impeller {
15 namespace testing {
16 
18  FML_CHECK(::glfwInit() == GLFW_TRUE);
19  playground_ = PlaygroundImpl::Create(PlaygroundBackend::kMetal, switches);
20 }
21 
22 std::unique_ptr<Screenshot> MetalScreenshotter::MakeScreenshot(
23  AiksContext& aiks_context,
24  const std::shared_ptr<Texture> texture) {
25  @autoreleasepool {
26  std::shared_ptr<Context> context = aiks_context.GetContext();
27  fml::AutoResetWaitableEvent latch;
28  if (auto cmd_buffer = context->CreateCommandBuffer()) {
29  if (context->GetCommandQueue()
30  ->Submit({cmd_buffer},
31  [&latch](CommandBuffer::Status status) {
32  FML_CHECK(status == CommandBuffer::Status::kCompleted);
33  latch.Signal();
34  })
35  .ok()) {
36  latch.Wait();
37  }
38  }
39 
40  id<MTLTexture> metal_texture =
41  std::static_pointer_cast<TextureMTL>(texture)->GetMTLTexture();
42 
43  CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
44  CIImage* ciImage = [[CIImage alloc]
45  initWithMTLTexture:metal_texture
46  options:@{kCIImageColorSpace : (__bridge id)color_space}];
47  CGColorSpaceRelease(color_space);
48  FML_CHECK(ciImage);
49 
50  std::shared_ptr<ContextMTL> context_mtl =
51  std::static_pointer_cast<ContextMTL>(context);
52  CIContext* cicontext =
53  [CIContext contextWithMTLDevice:context_mtl->GetMTLDevice()];
54  FML_CHECK(context);
55 
56  CIImage* flipped = [ciImage
57  imageByApplyingOrientation:kCGImagePropertyOrientationDownMirrored];
58 
59  CGImageRef cgImage = [cicontext createCGImage:flipped
60  fromRect:[flipped extent]];
61 
62  // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
63  return std::unique_ptr<MetalScreenshot>(new MetalScreenshot(cgImage));
64  }
65 }
66 
67 } // namespace testing
68 } // namespace impeller
std::shared_ptr< Context > GetContext() const
Definition: aiks_context.cc:38
static std::unique_ptr< PlaygroundImpl > Create(PlaygroundBackend backend, PlaygroundSwitches switches)
A screenshot that was produced from MetalScreenshotter.
std::unique_ptr< Screenshot > MakeScreenshot(AiksContext &aiks_context, const std::shared_ptr< Texture > texture) override
MetalScreenshotter(const PlaygroundSwitches &switches)