Flutter Impeller
metal_screenshoter.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 "third_party/glfw/include/GLFW/glfw3.h"
12 
13 namespace impeller {
14 namespace testing {
15 
17  FML_CHECK(::glfwInit() == GLFW_TRUE);
18  playground_ =
20 }
21 
22 std::unique_ptr<MetalScreenshot> MetalScreenshoter::MakeScreenshot(
23  AiksContext& aiks_context,
24  const Picture& picture,
25  const ISize& size) {
26  Vector2 content_scale = playground_->GetContentScale();
27  std::shared_ptr<Image> image = picture.ToImage(
28  aiks_context,
29  ISize(size.width * content_scale.x, size.height * content_scale.y));
30  std::shared_ptr<Texture> texture = image->GetTexture();
31  id<MTLTexture> metal_texture =
32  std::static_pointer_cast<TextureMTL>(texture)->GetMTLTexture();
33 
34  if (metal_texture.pixelFormat != MTLPixelFormatBGRA8Unorm) {
35  return {};
36  }
37 
38  CIImage* ciImage = [[CIImage alloc] initWithMTLTexture:metal_texture
39  options:@{}];
40  FML_CHECK(ciImage);
41 
42  std::shared_ptr<Context> context = playground_->GetContext();
43  std::shared_ptr<ContextMTL> context_mtl =
44  std::static_pointer_cast<ContextMTL>(context);
45  CIContext* cicontext =
46  [CIContext contextWithMTLDevice:context_mtl->GetMTLDevice()];
47  FML_CHECK(context);
48 
49  CIImage* flipped = [ciImage
50  imageByApplyingOrientation:kCGImagePropertyOrientationDownMirrored];
51 
52  CGImageRef cgImage = [cicontext createCGImage:flipped
53  fromRect:[ciImage extent]];
54 
55  return std::unique_ptr<MetalScreenshot>(new MetalScreenshot(cgImage));
56 }
57 
58 } // namespace testing
59 } // namespace impeller
impeller::TPoint::y
Type y
Definition: point.h:24
impeller::AiksContext
Definition: aiks_context.h:20
impeller::testing::MetalScreenshot
A screenshot that was produced from MetalScreenshoter.
Definition: metal_screenshot.h:19
context_mtl.h
impeller::PlaygroundBackend::kMetal
@ kMetal
metal_screenshoter.h
impeller::Picture
Definition: picture.h:19
impeller::TSize< int64_t >
impeller::TSize::width
Type width
Definition: size.h:21
impeller::PlaygroundImpl::Create
static std::unique_ptr< PlaygroundImpl > Create(PlaygroundBackend backend, PlaygroundSwitches switches)
Definition: playground_impl.cc:24
impeller::TPoint::x
Type x
Definition: point.h:23
impeller::ISize
TSize< int64_t > ISize
Definition: size.h:136
impeller::Picture::ToImage
std::shared_ptr< Image > ToImage(AiksContext &context, ISize size) const
Definition: picture.cc:31
impeller::PlaygroundSwitches
Definition: switches.h:15
impeller::TPoint< Scalar >
texture_mtl.h
impeller::TSize::height
Type height
Definition: size.h:22
impeller::testing::MetalScreenshoter::MetalScreenshoter
MetalScreenshoter()
Definition: metal_screenshoter.mm:16
impeller
Definition: aiks_context.cc:10
impeller::testing::MetalScreenshoter::MakeScreenshot
std::unique_ptr< MetalScreenshot > MakeScreenshot(AiksContext &aiks_context, const Picture &picture, const ISize &size={300, 300})
Definition: metal_screenshoter.mm:22