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 "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<Screenshot> MetalScreenshotter::MakeScreenshot(
23  AiksContext& aiks_context,
24  const Picture& picture,
25  const ISize& size,
26  bool scale_content) {
27  Vector2 content_scale =
28  scale_content ? playground_->GetContentScale() : Vector2{1, 1};
29  std::shared_ptr<Image> image = picture.ToImage(
30  aiks_context,
31  ISize(size.width * content_scale.x, size.height * content_scale.y));
32  std::shared_ptr<Texture> texture = image->GetTexture();
33  id<MTLTexture> metal_texture =
34  std::static_pointer_cast<TextureMTL>(texture)->GetMTLTexture();
35 
36  if (metal_texture.pixelFormat != MTLPixelFormatBGRA8Unorm) {
37  return {};
38  }
39 
40  CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
41  CIImage* ciImage = [[CIImage alloc]
42  initWithMTLTexture:metal_texture
43  options:@{kCIImageColorSpace : (__bridge id)color_space}];
44  CGColorSpaceRelease(color_space);
45  FML_CHECK(ciImage);
46 
47  std::shared_ptr<Context> context = playground_->GetContext();
48  std::shared_ptr<ContextMTL> context_mtl =
49  std::static_pointer_cast<ContextMTL>(context);
50  CIContext* cicontext =
51  [CIContext contextWithMTLDevice:context_mtl->GetMTLDevice()];
52  FML_CHECK(context);
53 
54  CIImage* flipped = [ciImage
55  imageByApplyingOrientation:kCGImagePropertyOrientationDownMirrored];
56 
57  CGImageRef cgImage = [cicontext createCGImage:flipped
58  fromRect:[ciImage extent]];
59 
60  return std::unique_ptr<MetalScreenshot>(new MetalScreenshot(cgImage));
61 }
62 
63 } // namespace testing
64 } // namespace impeller
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::AiksContext
Definition: aiks_context.h:20
impeller::testing::MetalScreenshot
A screenshot that was produced from MetalScreenshotter.
Definition: metal_screenshot.h:20
context_mtl.h
impeller::PlaygroundBackend::kMetal
@ kMetal
impeller::testing::MetalScreenshotter::MetalScreenshotter
MetalScreenshotter()
Definition: metal_screenshotter.mm:16
impeller::testing::MetalScreenshotter::MakeScreenshot
std::unique_ptr< Screenshot > MakeScreenshot(AiksContext &aiks_context, const Picture &picture, const ISize &size={300, 300}, bool scale_content=true) override
Definition: metal_screenshotter.mm:22
impeller::Picture
Definition: picture.h:20
impeller::TSize< int64_t >
impeller::TSize::width
Type width
Definition: size.h:22
impeller::PlaygroundImpl::Create
static std::unique_ptr< PlaygroundImpl > Create(PlaygroundBackend backend, PlaygroundSwitches switches)
Definition: playground_impl.cc:25
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::ISize
TSize< int64_t > ISize
Definition: size.h:138
impeller::Picture::ToImage
std::shared_ptr< Image > ToImage(AiksContext &context, ISize size) const
Definition: picture.cc:31
impeller::PlaygroundSwitches
Definition: switches.h:16
impeller::TPoint< Scalar >
texture_mtl.h
impeller::TSize::height
Type height
Definition: size.h:23
metal_screenshotter.h
impeller
Definition: aiks_blur_unittests.cc:20